Sunday, October 14, 2007

Rename/Move Files Using java.io.File API

1. Rename a File in Place

Rename a file in the original place is trivial by using renameTo() method in java.io.File class. The following code snapshot will change a file name from “oldName.pdf” to “newName.pdf”:

String oldName = “oldName.pdf”;
String newName = “newName.pdf”;

File f = new File(oldName);
f.renameTo(newName);

2. Rename/Move a File to an Existing Directory

It is also easy to rename a file, and move the file to an existing directory. The following code will change the file name from “oldName.pdf” to “newName.pdf”, and also move the file from “C:\oldDir” to “C:\newDirsubDir” provide “C:\newDir\subDir” exists:

String oldDir = “C:\\oldDir\\”;
String oldName = “oldName.pdf”;

String newDir = “C:\\oldDir\subDir\\”;
String newName = “newName.pdf”;

File f = new File (oldDir, oldName);

f.renameTo(newDir + newName);


The last line of code will rename and also move the file to newDir , C:\newDir\subDir.

3. Rename/Move a File to an Directory that does not exist

The piece of code above won’t work if C:\newDir\subDir\ does not exist. However, we still can achieve our goal by a little more work. Here is how we do:

String oldDir = “C:\\oldDir\\”;
String oldName = “oldName.pdf”;

String newDir = “C:\\oldDir\\subDir\\”;
File pDir = new File(newDir);
pDir.mkroots();

String newName = “newName.pdf”;

File f = new File (oldDir, oldName);
f.renameTo(newDir + newName);

The last two lines of code in red will create all directories along the path if they do not exist, and then rename and move the file to the new directory.

Wednesday, August 22, 2007

Having Fun with Trac: Tracd + Digest Authentication


Tracd
is a lightweight standalone Trac web server, which was added to Trac framework since version 0.7. Running Trac on Tracd is very simple, while needs a bit more work with authentication. Tracd supports both Basic and Digest authentication. The default is to use Digest authentication. In certain situations, I found out that Digest authentication is an easy and quick way to provide access controls to Trac web application. Here are the steps to create Digest authentication for tracd web server:


1. Creating a Password File


We used the htdigest tool which Apache provides to create a digest password file. The following command creates a digest password file users.mytest with realm name mytest and also adds user tom to the file:


htdigest -c tractest/users.mytest mytest tom


If file users.mytest exits, then we should remove option –c in the command above. We will be promoted for a password to enter for each user that we create.


2. Creating Trac Permissions


We also need to add Trac permissions for each user created in step 1. Permission privileges are managed using the trac-admin tool. For example, the following command will grant all privileges to user tom to project tractest:


trac-admin tractest/ permission add tom TRAC_ADMIN


3. Using Digest Authentication


The following command will force Tracd to trace users' activities with project tractest using Digest authentication:

tracd -p 1234 -a tractest, tractest/users.mytest, mytest tractest

When launching Trac from http://localhost:1234/tractest/login, a log-in form pops up and asks for user's name and password. Tracd also allows sharing a password file among multiple projects on one instance of Trac.

Reference:


Tracd : http://trac.edgewall.org/wiki/TracStandalone
Trac Permission: http://trac.edgewall.org/wiki/TracPermissions




Tuesday, August 7, 2007

Comparison of C++ and Java (VIII): Vtable vs. Method Invocation Table and Binary Compatibility (1)

Both Java and C++ have mechanisms to support dynamic polymorphism via run-time method binding, C++ uses vtable to achieve this goal while Java is by method invocation table. The difference of these two mechanisms leads to the different behaviours regarding to binary compatibility.

A vtable exmple

in C++, a vtable contains the addresses of the object's dynamically bound methods. Method calls are performed by fetching the method's address from the object's vtable. The vtable is the same for all objects belonging to the same class, and is therefore typically shared between them.

Considering the following example. Class A defines one virtual function methodA which just prints out a silly message:


A.h

class A{

public:
A(){}
virtual void methodB();
};

A.cc

void A::methodA(){
cout<<"print from method A "<<endl;
};

The vtable of A looks like:

vtable layout of class A:

A::_ZTV1A: 3u entries
0 0u
8 (int (*)(...))(&_ZTI1A)
16 A::methodA

The offset of A::methodA is 16.

Here is a test class to reference class A:

test.cc

int main (int argc, char *argv[]){
A* a = new A();
a->methodA();
}


We compile and link program by the following three steps:

>>g++ -c A.cc
>>g++ -c test.cc
>>g++ A.o test.o –o test

and run test program
>>./test

and get output:
>>Print from method A

In the example above, method call a->methodA fetches methodA from a address with offset 16.

Adding a virtual method breaks binary compatibility in C++

To show this, we add a virtual function methodB to class A right BEFORE methodA :

A.h

class A{
public:
A(){}
virtual void methodB(); // a new added function
virtual void methodA();
};

A.cc

void A::methodA(){
cout<<"print from method A "<<endl;
}

void A::methodB(){
cout<<"print from method B "<<endl;
}

Then we recompile class A BUT NOT class test, and link them again:

>>g++ -c A.cc
>>g++ A.o test.o –o test

Then we run test program:
>>./test

and get out put:
>>Print from method B

Why not " Print from method A" as I would expect? Let's take a look at the vtable of the revised class A:

vtable of revised class A

A::_ZTV1A: 4u entries
0 0u
8 (int (*)(...))(&_ZTI1A)
16 A::methodB
24 A::methodA

Note the address of methodA is now with offset 24, while the address of 16 is occupied by methodB. If we execute test program without recompilation, the address with offset 16 is still referenced and thus methodB gets called.

This problem shown above is known as "constant recompilation problem" and is usually considered as a side effect of C++: because C++ compiler references methods or variables by numeric offset at compilation time, sometimes we add a new method or a new instance variable to a class, any and all classes that reference that class will require a recompilation, or they break.







Monday, July 30, 2007

Comparisons of C++ and Java (VII): An Example of Desirable Data Hiding

In many of my readings, data hidings are said undesirable and are not encouraged. However, I at some circumstance, data hidings can make things simpler. For example, in my work, I need to implement an application with classes relationship looks like:


A
/ \
C B
/ | \
B1 B2 B3


Class A is the base class, class B and C are subclasses of A, and class B1, B2 and B3 are subclasses of B. Base class A is defined as:

A.h

class A{
public:
A(){}
static int id;
virtual void doSomething();
virtual int getId(){return ++id;}
};

A.cc

int A::id=0;
void A::doSomething(){
for (int k = 0; k<10; k++)
cout<<"id is : "<<getId()<<endl;
}



Here method doSomething simply print out ids. We want class B and C have their own id sequences, and all class B's share the same id sequences. That is both B and C classes' ids should start from 1. Class B1, B2, and B3 will share the same id sequence. To achieve this, We can design class C and B as following:

B.h

class B:public A{
public:
B(){}
static int id;
virtual int getId()
{
return ++id;
}

B.cc
int B::id=0;


Since data member id is defined again in both class C and B, A/id is hidden and inaccessible from both classes. Each class holds their own data id which starts from 0;

For class B's subclasses, we want them to share the same id sequence, so we DO NOT want to hide the id from class B. class B1, B2 and B3 are defined as following:

B1.h

class B1:public B{
public:
B1(){}
};

B2.h

class B2:public B{
public:
B2(){}
};

B3.h

class B3:public B{
public:
B3(){}
};


And the following test code generate some results that is what we expect:


C* c = new C();
B1* b1 = new B1();
B2* b2 = new B2();
B3* b3 = new B3();

cout<<"In class C, ids are: "<<endl;
c->doSomething();
cout<<"In class B1, ids are:"<<endl;
b1->doSomething();
cout<<"In class B2, ids are: "<<endl;
b2->doSomething();


The result looks like:


In class C, ids are:
id is : 1
id is : 2
id is : 3
id is : 4
id is : 5
id is : 6
id is : 7
id is : 8
id is : 9
id is : 10
In class B1, ids are:
id is : 1
id is : 2
id is : 3
id is : 4
id is : 5
id is : 6
id is : 7
id is : 8
id is : 9
id is : 10
In class B2, ids are:
id is : 11
id is : 12
id is : 13
id is : 14
id is : 15
id is : 16
id is : 17
id is : 18
id is : 19
id is : 20
In class B3, ids are:
id is : 21
id is : 22
id is : 23
id is : 24
id is : 25
id is : 26
id is : 27
id is : 28
id is : 29
id is : 30


There may be many other ways to achieve the same goal, but I found this solution is so simple and easy to understand and implement.


In Java, we can do the exactly same thing except a little change in the syntax. I am not going to bother to write the code.


In both Java and C++, the mechanism of data hiding from inheritance is called implicitly data hiding. In C#, it is said we can do explicitly data hiding by using "new" modifier. I think it is a cool feature in the language because I can tell the compiler I am doing data hiding on purpose.

Saturday, July 7, 2007

Comparisons of C++ and Java (VI): Constant Modifier

Constant modifier is const in C++ and final in Java. However, const and final are not equivalent at any situations (In C++, we can also define a constant by using the #define preprocessor directive, but I am not going to discuss it here)


1. For primitive data type,
const and final have the same meaning, that is the variable can only be assigned once and its value cannot be changed. For instance:


const int i = 10;

in C++ is equivalent to


final int i = 10;

in Java. It illegal to modify the variable after its initialization in both cases.


2. However,
for non-primitive data type (reference data type in Java, structured data types and address types in C++), const and final are different.

Let take a look at some examples. In Java, this piece of code is perfectly legal:


final int[] ary = {1, 2, 3, 4, 5};
ary[4] = 6; //legal

However in C++, this piece of code:


const int ary[] = {1, 2, 3, 4, 5};
ary[4] = 6; // illegal. compilation-time error

will generate compilation-time error:

Error: assignment of read-only location.


3. Why const and final are different regarding to non-primitive datatype?

For mathematics, a constant is a value that never changes, thereby remaining a fixed value. So in C++, a constant is a REAL constant. const is infectious. A const modifier ensures that the object is immutable (It is called Const Correctness in C++). When const keyword is involved when used with pointers or references, we can't modify what the pointer points to or the reference refers to.

While, there is no such const correctness in Java. A final modifier just tells the compiler that the reference to the object cannot be re-assigned, but nothing about its content. A final modifier can never change the immutability of an object. A constant in Java is NOT a real constant. const is a reserved keyword in Java. We may expect the implementation of Const Correctness in Java in the future.


Wednesday, June 27, 2007

Comparisons of C++ and Java (V): Immutability of Strings

Strings are not the same in C++ and Java regarding to immutability. In Java, String objects are immutable, where C++ string type is mutable.

Let’s take a look at some examples:

A C++ Example

string str1=”It is Friday, ”;
string str2=”I am completely released”;
str1.append(str2);

cout<<str1<<endl;

The program will print: It is Friday, I am completely released

A Java Example

String str1 = “It is Friday,”;
String str2 = “I am completely released!”;
str1.concat(str2);

System.out.println(str1);

The program will print: It is Friday,

Immutability of strings in C++ and Java

In C++, a string type is mutable. As shown in example 1, str1.append(str2) appends str2 to the end of str1, and str1 becomes “It is Friday, I am completely released”. In addition to append(), similar functions like substr() and assign() etc. in std::string class DO alter a C++ string.

By contrast, in Java, String objects designed as immutable. As shown in example 2, str1.concat() creates a new String object, the old string str1 remains unchanged. It is also true for some other methods such as substring(), toLowerCase() and toUpperCase(). In other words, any operation performed on one String reference will never have any effect on the content of the String object.

Reduce Object Creation in Java

Because of its mutability and the object-creation overhead of a String object, performance could be a problem. In fact, Java provides several mechanisms to reduce String objects creation:

  • String literal Pool: Java compiler optimizes handling of String literals. Only one String object is shared be all strings that have same character sequence. Such strings are said to be interned, meaning that they share a unique String object. The String class maintains a private pool called String intern pool, where such strings are interned. Java automatically interns String literals.
  • Interning of Strings: we can also explicitly make a String object interned. Intern() method from String class can help achieve interning of a String . When the intern() method is invoked on a String, JVM first checks the String literal pool. If a String with the same content is already in the pool, a reference to the String in the pool is returned. Otherwise, the String is added to the pool and a reference to it is returned. The result is that after interning, all Strings with the same content will point to the same object.
Can we intern all Strings? The answer is may not. As the intern string pool grows large, the cost to find a String in the pool could become more expensive than creating a new object.
  • StringBuffer and StringBuilder serves as mutable strings: as a sort of complementary, Java provides StringBuffer as a sort of mutable string. Content of a StringBuffer object can be modified. It is more efficient to use a StringBuffer instead of operations that result in the creation of many intermediate String object. Introduced in J2SE 5.0, Stringbuilder class provides an API compatible with StringBuilder but is unsynchronized.


Wednesday, May 30, 2007

Comparisons of C++ and Java (IV): Accessibility of Indirect Base Classes

Firstly, let’s take a look at two examples in C++ and Java respectively:

Example 1 (In Java):

A.java
public class A {
A (){}
void printMsg(){
System.out.println("print from class A");
}
}

B.java
public class B extends A {
B (){}
void printMsg(){
System.out.println("print from class B ");
}
}

C.java
public class C extends B {
C (){}
void printMsg(){
// direct base class B is accessible but now indirect base class A
super.printMsg();
System.out.println("print from class C");
}
}

Test code snippet

C c = new C();
c.printMsg();

The inheritance path looks like:

A (indirect base)
|
B (direct base)
|
C

The test Results are:
print from class B
print from class C

In class C, method "printMsg()" in direct base class B is accessible by calling:

super.printMsg()

However, the method "printMsg()" in indirect base class A is not accessible. What about in C++? Let’s take a look at an exactly same example but in C++.

Example 2 (In C++): same as Example 1 but written in C++

class A:

class A{
public:
A(){}
void printMsg( ){
cout&lt;&lt;" print from class A "&lt;&lt;endl;
}
};

class B:

class B : public A{
public:
B(){}
void printMsg(){
cout&lt;&lt;" print from class B "&lt;&lt;endl;
}
};

class C:

class C:public B{
public:
C(){}
void printMsg(){
A::printMsg();//indirect base is accessible too
B::printMsg();
cout&lt;&lt;" print from class C "&lt;&lt;endl;
}
};

Test code snippet:

C* c = new Polygon::C();
c->printMsg();

The test code will print:
print from class A
print from class B
print from class C

As we have noticed, the indirect base class A is accessible in class C which is different than in Java as shown in Example 1.

Why C++ and Java are different at this point?

As my understanding it is because in C++, multiple-inheritance is allowed. A derived class may inherit methods with same finger print from different base classes as shown below:

class A
class A {
public A();
pulbic printMsg();
}

class B1
class B1 {
public B1()
pulbic printMsg();
}

class B2
class B2 {
public B2();
pulbic printMsg();
}

class C
class C : public B1,B2{
public C();
pulbic printMsg();
}


The inheritance path is shown as:
A (indirect base)
/ \
B1 B2 (direct base)
\ /
C

Class C has two direct base classes B1 and B2. This arises the ambiguity because both have a method called “printMsg()”. Image what if the printMsg() in class C would like to inherit both "printMsg()" from class B1 and class B2? Fortunately, C++ provides techniques to eliminates the ambiguities by explict qualification: B1::printMsg() or B2::printMsg() explicitly tells compiler which version of "printMsg()" is been used. However, the solution also leads the de-encapsulation of indirect base classes A in the inheritance tree.

Unlike C++, in Java, strict inheritance is enforced and all classes are single-rooted by the class Object. Multiple implementation inheritance is not allowed, thus the confusion in the examples above does not exist. Everything in the super class is inherited by the subclass. Thus, there is no need to access the indirect base class upward in the inheritance tree.

Monday, May 21, 2007

Comparisons of C++ and Java (III): Method Overriding

Method overriding is not quite the same in C++ as in Java. Here are some examples show the differences:

Method overriding in Java

base.java

public class base {
base (){}
void printMsg(){
System.out.println("print from base class");
}
}

Derived.java

public class derived extends base {
derived (){}
void printMsg(){
System.out.println("print from derived class");
}
}

test code snippet:


public static void main(String args[]){
base b = new derived();
b.printMsg();
}

What do we get:

>> print from derived class

Non-virtual method overriding in C++

base.h

class base{
public:
base(){}
void printMsg();
};

base.cc
void base::printMsg(){
cout&lt;&lt;" print from base"&lt;&lt;endl;
}

derived.h
class derived : public base {
public:
derived(){};
void printMsg();
};

derived.cc

void derived::printMsg(){
cout&lt;&lt;" print from derived class"&lt;&lt;endl;
}

test code snippet:

base* d = new derived();
d->printMsg();

And what do I get:

>>print from base class

which is not what I expect? But how does it happen? The reason is still because of the binding mechanisms in C++ and Java.

As we know there are two binding mechanisms in C++: static binding and dynamic binding. Dynamic binding is implemented through virtual functions; the actual code for the functions is not attached or bound until execution time.

On the other hand, static binding occurs when the functions code is “bound” at compile time. This means that when a non-virtual function is called, the compiler determines at compile time which version of the function to call. When overriding a non-virtual method in a derived class, if we call the method via base class reference or pointer type, the method in the base class is called. In our example above, the printMsg() in the base class is called and print out "print from base class".

As we have discussed before, in Java all method are treated as virtual, so dynamic binding applied all the time. And when we override a method in a derived class, we are able to call the method via a base class reference.

So what is the solution in C++ if I want to call the overridden methods in a derived class via a base class reference?

The answer is to make the method in base class virtual, as shown below:

base.h

class base{
public:
base(){}
virtual void printMsg();
};

Because the function printMsg() is virtual, dynamic binding applies. When we call the derived method via a base class reference, we get what we want:

>> print from derived class

Sunday, May 20, 2007

Comparisons of C++ and Java (II): Static Variables

I used a lot of static variables in Java programming and I don't feel any tricks except you have to know why you make a variable static. I thought it would be the same to use static variable in C++. However, it is not true.

In C++, static data member must be defined outside class definition

In Java, we declare a static variable in class scope and can access it anywhere inside the class. In C++, I thought it must be the same way. Actually it is not. Here is the example. I declare a variable static in head file (test.h) and try to use it in the program file (test.cc) as shown in the sample code:

test.h

class test {
private:
static int i;
public:
test();
}

test.cc
test::test(){
i = 0;
}

At compilation, I get an error says “undefined reference to `test::i”. After I looked up some books and also did some Google search, I finally figured out that a static variable must be defined in the program file. So test.cc should look like:

test.cc

int test::i; //We can also initialize i here, eg. int test::i = 0.
test::test(){
i = 0;
}

Now compiler is happy. But why? The reason is that C++ keeps two files for a class: class declaration (header file) and class definition (program) file. The header file contains the class declaration and does not necessary reserve storage space associated with the identifier, while the program file contains method definitions and reserve storage space for each given identifier.

So when we declare a static data member within a class definition, it is NOT defined. In other words, we are not allocate storage for it. Instead, we must provide a global definition for it elsewhere outside class definition because it does not belong to any specific instance of the class.

What about in Java? Java has a simplified matter by only have class and variable definitions. There are no sole declarations in Java, so class definition and variable definitions are also class declarations and variable declarations. In other words, in Java, all classes, data members and variables within methods are defined, therefore storage spaces are reserved for primitive data types.

In C++, a local variable can be static

In C++, a local variable can be static. For example:

test::test(){

i = 0;

static int count = 0;

}

is perfectly legal in C++. A local static variable has the same life time as a global static variable, but its scope is local to the function in which it is declared. It sounds a good idea to me.

In Java, a static variable can only be declared inside a class scope. Compiler will complain if a local variable is declared as static inside a method scope, Even inner classed cannot have static declarations.








Thursday, May 17, 2007

Comparisons of C++ and Java (I): Static Binding and Dynamic Binding in C++

After years of Java programming, I have to pick up C++ for my work. Sometimes, it is hard to switch from Java thinking. Here is one of the example: Static binding and Dynamic binding in C++.

Let me start with 3 examples.

Example 1 (In Java):

base class:

public class base{
public base(){}
public void printMsg(){
System.out.println("I am printMsg() from base class");
}

derived class

public class derived extends base{
public derived(){}
public void printMsg(){
System.out.println("I am printMsg() from derived class")
}
)

A test class:

public class test {
public static void main(String[] args){
base myClass = new derived();
myClass.printMsg();
}
}

What do get? Here we go:

>>I am printMsg() from derived class

It is so obvious to a Java programmer. OK, let’s look at C++ version of the example above.

Example 2 (in C++): this example looks exactly same as example 1 but in C++.

base class:

class base{
public:
base(){};
void printMsg( cout<<”hi, printMsg() from base”<<endl;);
};
...

derived class:


class derived : public base {
public:
derived();
void printMsg(cout<<”hi, printMsg() from derived class”<<endl);
};
...
test class snippet

....
base* myClass = new Polygon::derived();
myClass->printMsg();
...

What do we get? There we go:

>>hi, printMsg() from base

and not “hi, printMsg() from derived class” as I expected.

How can I access derived class version of printMsg() in C++? There is a way out: by using pure virtual methods. In base.cc we change declaration of void printMsg() as:

Example 3 (in C++): This example is same as example 2 except in the base class. The method printMsg() is made pure virtual:


class base{
public:
base(){};
void printMsg( cout<<”hi, printMsg() from base”<<endl;);
virtual void printMsg() = 0;
};

Then compile and run, I get what I want:

>>hi, printMsg() from derived class

The derived class version of printMsg() get called.

What is going on here?

It is because Java and C++ provide different binding mechanisms to achieve run-time polymorphism.

In C++ there are two kinds of data binding: static binding and dynamic binding. Static binding is by means of nonpuer virtual functions while dynamic binding is by virtual functions.
At run time, the implementation is chosen differently. In static binding, decision is made by the static type of pointer or reference, while in dynamic binding, decision is made by the actual type of object being pointed to.

This can explain the results from example 2 and 3. In example 2, base class contains no virtual printMsg() function, so it is static binding. In example 3, method printMsg() is a virtual function, so dynamic binding applies, therefore the derived class version of printMsg() is picked up.

What happens in Java? In Java, every method is treated as pure virtual method and dynamic binding applies always.

Extra note on pure virtual method in C++:

  1. When using pure virtual methods in C++, never try to call a pure virtual method from a constructor or destructor. Compiler will complain “error: abstract virtual `virtual method' called from constructor” at compilation time.
  2. Making destructor virtual is a best practice since it would make sure the destructor in derived class get called too as desired.

Tuesday, March 20, 2007

Pack Input Jar Files Dynamically

When host a web site which allows users to download reusable Java class files which are compressed into different jar files, to reduce the download time for the client and the band with usage for the server, it would be nice to provide users an option to be able to download highly compressed packed files. As shown in the snippet of a sample download page, users will select any jar files and choose download them in a highly dense format - pack200.



Pack Compressing Classes

The JSR 200 (Network Transfer Format for JavaTM Archives) specifies a "dense download" format to compress input jar files in pack format. JSR 200 is supported with Java 5. The run-time API to pack and unpack the data and files is abstract class: java.util.jar.pack200.

Pack200 is a abstract class and has one private constructor so it cannot be initialized directly. It provide two static methods to obtain new instance of the Packer engine:


Packer packer = Pack200.newPacker();


and
an instance of the Unpacker engine:


Unpacker unpacker = Pack200.newUnpacker();


Pack Jar Files at Runtime

The following example shows how to convert jar files into a pack format at run-time step by step:

1. Obtain an instance of Packer engine:


Packer packer = Pack200.newPacker();


2. Set up output file stream for output packed stream:


FileOutputStream fos = new FileOutputStream("/tmp/test.pack");


3. Read the input jar into a Jar File input stream:


JarFile jarFile = new JarFile("/path/to/myfile.jar");


4. Call the packer to archive jar file:


packer.pack(jarFile, fos);


5. Close input file streams:


jarFile.close();


If there are more than one input jar files, repeat step 3, 4 and 5.

6. Close output stream:


fos.close();


7. Set content type as "pack200-gzip". This indicates to the server that the client application desires a version of the file encoded with Pack200 and further compressed with gzip:

Unpack Paked Stream

The unpacker engine is used by deployment applications to transform the byte-stream back to JAR format.

The follow steps show how to unpack a packed files:

1. Create an instance of Unpacker engine:


Unpacker unpacker = Pack200.newUnpacker();


2. Read in input packed file and set up output file:


File infile = new File("/tmp/test.pack");

FileOutputStream fostream = new FileOutputStream("/tmp/test.jar");
JarOutputStream jostream = new JarOutputStream(fostream);


3. Call unpacker


unpacker.unpack(infile, jostream);


4. Close file stream:


jostream.close();


References:
  1. JDKTM 5.0 Documentation

  2. Network Transfer Format JSR 200 Specification



Generate Documents With Gridsphere Portlet -- A Bug in build.xml

I wanted to create document for my portlet calledtestportlet created in GridsphereframeworkWhen I run


ant docs


I got errors:


BUILDFAILED
/path/to/gridsphere-2.1.5/projects/testportlet/build.xml:256: No sou rce filesand no packages have been specified.



In the build.xml file the"ant docs" is defined as:

....
<target name="docs" depends="javadocs"description="Create projectdocumentation"/> <!--===================================================================--> <!-- Creates allthe APIdocumentation --> <!--===================================================================--> <targetname="javadocs" depends="setenv" description="CreateJavadocs"> <echo>CreatingJavadocs</echo> <delete quiet="true"dir="${build.javadoc}"/> <mkdirdir="${build.javadoc}"/> <javadocsourcepath="src" classpathref="classpath" destdir="${build.javadoc}" author="true" version="true" splitindex="true" use="true" maxmemory="180m" windowtitle="${project.title}" doctitle="${project.api}"> <!--bottom="Copyright &#169; 2002,2003 GridLab Project. All RightsReserved."> --> </javadoc> </target>
...

I am using Java 1.5 and Apach Ant 1.6. Look like it is the issue of Java 1.5,the sourcepath is ignored even though Irun "javadoc" command from a separatepackage independent of Gridsphere framework. I modified a little bit of thebuild.xml file and now it is workingwell:

1. First remove attribute sourcepath="src"
2. Add "<fileset dir="src" />" to <javadoc> element.

The modified file should look like:

.... <target name="docs" depends="javadocs"description="Create projectdocumentation"/> <!--===================================================================--> <!-- Creates allthe API documentation --> <!--===================================================================--> <targetname="javadocs" depends="setenv" description="CreateJavadocs"> <echo>CreatingJavadocs</echo> <delete quiet="true"dir="${build.javadoc}"/> <mkdirdir="${build.javadoc}"/> <javadocsourcepath="src" classpathref="classpath" destdir="${build.javadoc}" author="true" version="true" splitindex="true" use="true" maxmemory="180m" windowtitle="${project.title}" doctitle="${project.api}"> <filesetdir="src" /> <!--bottom="Copyright &#169; 2002,2003 GridLab Project. All RightsReserved."> --> </javadoc> </target> ...


Save file and run "ant docs" or"ant javadocs", the document files aregenerated in/path/to/gridsphere-2.1.5/projects/testportlet/build/docs/javadocs/.

Commentary:

Some one had mentioned that the combination of ant 1.6 and Java 1.5 wouldigmore attribute "sourcepath" in thebuild file, but I think it is the problem of Java 1.5. Do Gridspherepeople notice that? Anyway, it is still very nice to be able to generatedocuments for portlets from within Gridsphere framework environment if we do alittle bit extra work.

Thursday, March 8, 2007

A Web Form Based PDFs Merger

Task: Provide a web-based application allows users to merger multiple files. Application is running in Grails framework.


Analysis: This problem can be split into three sub-tasks and be solved in three steps: upload multiple pdf files, retrieve submitted files and merger multiple pdfs.

Step1: Upload Multiple PDFs Files


Upload and process multiple files from a Web Form is not a trivial task because file input element allows uploading only one file at a time. Inspired by StickBlog's excellent post: Upload multiple files with a single file element. I decided to use Javascript instead of Applet to achieve this goal. I modified StickBlog's code to accommodate my needs. The user interface pdfmerger.gsp has the following element:


<g:form action="merge" method="post" enctype="multipart/form-data">

<input id='myfile' type='file' name='' onChange="addElement()"></input>
<input type="submit" value="Submit">

<br>Files list (Please note maximun number of uploaded files is 5):
<!-- This is where the output will appear -->
<div id="filesList"></div>
</g:form>


The event onChange of file input is captured. Each time a file is selected, the Javascript function addElement in script.js is invoked


var new_row = document.createElement('div' );


and a new <div> element is created and three elements: a text input box, a button and a file input box are appended to it. The text input box is just for display the file name. You can use other element for this purpose too:


var new_row_input =document.createElement( 'input' );
new_row_input.type = 'text';
new_row_input.name = "ins_" + (childs.length + 1)
new_row_input.value = element.value;


The button is used to delete a corresponding uploaded file if it is clicked on:


var new_row_button =document.createElement( 'input' );

new_row_button.type = 'button';
new_row_button.onclick = function (){
...
...
}


The file input box stored the uploaded files and will be submitted to server, and we like to make it invisible:


var new_row_file_input =document.createElement( 'input' );
new_row_file_input.setAttribute ('name','file_' + count);
new_row_file_input.setAttribute ('id','file_' + count);
new_row_file_input.value = element.value;
new_row_file_input.style.opacity = 0;


Finally, the newly created <div> element is appended to <div>with id"file_list " in pdfmerger.gsp:


new_row.appendChild(new_row_input);
new_row.appendChild( new_row_button );
new_row.appendChild(new_row_file_input);

target_list.appendChild (new_row);


The complete Javascript can be found here.


Step 2: Retrieve Submitted Files

Submitted files are retrieved and processed on server side in acontroller called PdfmergerController.java.In Grails, retrieving files is very easy by using build in Spring file system.We want to retrieve all submitted files and stored into an ArrayList for furtherprocessing.


for (i in 0.. max_num-1){
def file_name = "file_" + i;
def f = request.getFile(file_name);

if(f!=null && !(f.isEmpty())){
//println "file content type " + f.getContentType()
FileInputStream ins = f.getInputStream()
pdfs.add(ins);
}
}


Step 3: Merger Multiple PDFs

Once we have all submitted files in the list pdfs, we are ready to mergerthe PDFs. I just adopted the source code of method "concatPDFs" from Abhi'spost. It works so well with my system. The source code ofPdfmergerController.java can be found here.We added a beforeInterceptor to validatefiles content type before request is processed further.

Commentary

1. If server side processing is done ina J2ee environment, a third party library is needed since Java Servlet and Jspdo not have building mechanism to handle web form based file uploading. I hadApache Jakarta CommonsFileUpload package. It is an open source and can be downloaded from Apache Jakarta CommonsFileUpload project. Another package Apache Jakarta Commons IOproject is also needed internally by FileUpload package.

2. In step 3, we do file content typecheck on server side. However, it is also a good idea to validate content typeat client side when file is uploaded.

3. I just test the sample applicationwith IE and Firefox and no other else.

References

1. Uploadmultiple files with a single file element(StickBlog)
2. MergePDF files with iText (Abhi on Java)
3. Grails onlinetutorial
4. Tutorial: iText ByExample
5. ApacheJakarta Commons FileUpload project
6.
Apache Jakarta Commons IOproject





Sunday, February 18, 2007

Ajax Sandbox (1): Calling Remote Web Services
Play with Json and Java Json API


Json (JavaScript Oject Notation)i s a very interesting format for exchangingdata across domains. Comparing with XML,Jsont is easy to parse with JavaScript,and thus helps to establish a easy way to call remote web services from a Ajaxapplication.

Here is an example of providing web services in Json format by utilizing JavaJson API. There are three core components in this example: a POJO class (Book.java) which stores the data model; a Java servlet (bookWS.java ) which provides web services in Json presentation; A html page (bookWSTest.html) which shows how to call a remote web services in JavaScript using. The working environment is Netbeans 5.5 RC2 as IDE, glassfish v2 as application server.

Data Source

The data source is a POJO class (Book.java),which represents a data table "book" in the database:

id*
title
author
price
1
Java EE 5 Tutorial
John Smith
45.6
2
Ajax For Java Developer
Rayn Todd
29.0

Create a Web Services

The second component is a servlet (bookWS.java) which returns a list of books stored in the database. The result is in Jsonformat and is ready for a client to consume.

Before we go to further, let us take a look at the desired Json format in thisexample:

The following example shows the JSON representation of a object that describe onbook:


List A
:
{"title":"Java EE 5 Tutorial","price":45.6,"author":"John Smith","id":1}

And the returned list of books look like:


List B
:
{"ResultSet":
{"books":
[{"title":"Java EE 5 Tutorial","price":45.6,"author":"John Smith","id":1},
{"title":"Ajax For Java Developer","price":29,"author":"Rayn Todd","id":2}]
}
}

Now let's look at how we can utilize Java Json API to create web service in Json format.

The entity class in accessible via resource injection. A JPQL select query isused to select data from database:


String sql ="select object(o) fromBook as o"; //select all book indb

Query q =em.createQuery(sql); //executequery
ArrayList bookList = newArrayList(q.getResultList()); //store all books in alist
resultSet =getResults(bookList);


A private method is created to convert the list of books into a Json object by utilizing Java Json API:


privateJSONObject getResults(ArrayList bookList){

JSONArray items = new JSONArray();
JSONObject resultSet = newJSONObject();

JSONObject books = new JSONObject();

try {
for (int i = 0; i <bookList.size(); i++){
Book o =(Book)bookList.get(i);

JSONObjectbook_obj = new JSONObject();

book_obj.put("id", o.getId());
book_obj.put("title", o.getTitle());
book_obj.put("author", o.getAuthor());
book_obj.put("price", o.getPrice());
items.put(book_obj);
}

books.put("books", items);
resultSet.put("ResultSet", books);

}catch (JSONException e){
System.out.println("Error: Could not create Book JSONObject");
}
return resultSet;
}


The resulting Json object is then serialized and returned to client:


out.println(callback + "(" +resultSEt.toString() + ")");


"callback" is the name of callback function that can be defined by users' inputand retrieved by:


String callback =request.getParameter("callback");


Don't forget to set up response content type as:


response.setContentType("text/javascript;charset=UTF-8");


We have done all the work in Java servlet. When the servelt receives a request like: http://www.myServer.com/beanTest/bookWS?callback=resultHandler

The returned results should look like:

resultHandler(
	{
"ResultSet":
{
"books":
[
{"title":"Java EE 5 Tutorial","price":45.6,"author":"John Smith","id":1},
{"title":"Ajax For Java Developer","price":29,"author":"Rayn Todd","id":2}
]
}
}
)

Let us go to next section to see how the results is accessible in JavaScript.

Consume Web Services

We use dynamic scripting tag to make a remote web service call. The returned Json object is accessible in the call back function. Here is a sample html page (bookWSTest.html):


<html>

<head>
<title>AjaxRemote Web Services CallTest</title>
</head>
<script>

functionloadPage(){
var url ="http://www.myServer.com/beanTest/booksWS?callback=resultsHandler";
varheadTag =document.getElementsByTagName("head").item(0);
varscriptTag =document.createElement("script");
scriptTag.src = url;
headTag.appendChild( scriptTag );
}

function resultsHandler( resultsObj){
var result =document.getElementById("result");
result.innerHTML= " total number of books is : "+resultsObj.ResultSet.books.length;
}
</script>

<bodyonload="loadPage()">
<divid="result"></div>

</body>

</html>


When page loads, function loadPage() gets executed and write a script tag forthe web services. Inside callback function resultsHandler, the returned Json object is accessible. In this example, the total number of books in db is printed out.



References:

  1. Introducing Json
  2. Json
  3. JSON and the Dynamic Script Tag: Easy, XML-less Web Services for JavaScript
  4. Ajax for Java developers: Java object serialization for Ajax
  5. JSON in Java











Friday, February 9, 2007

Grails -- Using Embedded Derby Database

Apache Derby is a relational database implemented in Java. It is light-weighted and can be easily embed it in any Java-based solution. Here is a summary of using Derby Embedded JDBC driver within Grails framework. I used grails-0.3.1 for the time being.

Section 1: Install Software:

1.Install Apache Derby (v10.1.3.1)

Follow the tutorial from http://db.apache.org/derby/papers/DerbyTut/index.html to download and install software. (Note: ij tool is great to run SQL query from command line).

2.Install Grails (v 3.0.1)

The installation instruction can be found here. Set up environmental variables as described in the tutorial.

Section 2: Configure Grails for Using Embedded Derby Database

Inside $GRAILS_HOME, create sample application.

1. Create new Grails application by run command grails create-app, set up corresponding application name as myTest. Run command “grails create-domain-class”, set up domain name as “Book”.

2. Copy derby.jar and derbytools.jar from $DERBY_INSTALL to myTest/lib.

3. Configure data sources. In myTest/grails-app/conf, there are three data source files:

  • DevelopmentDataSource.groovy
  • ProductionDataSource.groovy
  • TestDataSource.groovy
We need change the settings to let application talk to Derby instead of default Hypersonic database. A sample setting in ProductionDataSource.groovy would look like:


class ProductionDataSource {
boolean pooling = true
String dbCreate = "update" // one of 'create', 'create-drop','update'

##note: this setting point to a embedded db in /opt/db-derby-10.2.1.6-bin.
##The file could be anywhere that the application can reach
String url = "jdbc:derby:/opt/db-derby-10.2.1.6-bin/derbyDB"

String driverClassName = "org.apache.derby.jdbc.EmbeddedDriver”

String username = ""
String password = ""
}


4. We need another file in myTest/grails-app/hibernate/ called hibernate-dialects.properties which looks like:


DerbyDialect=Apache Derby


This file is required especially for Derby DB but not for Postgresql as I know.

Section 3: Run Application:

1. Then we are ready to run the application, execute the command:

grails run-app.

If everything goes right, we should be able to launch the application from http://localhost:8080/myTest.

2. To test the application by create some new records.

3. To verify whether the records are in Embedded Derby database, user Derby ij tool to run SQL query against our database prodDB. Please note, Derby won't allowed access from multiple applications for embedded model, so we have to shut down our application in order to run ij tool.

References
  1. Apache Derby Tutorial (http://db.apache.org/derby/papers/DerbyTut/index.html)
  2. Apache Derby Downloads (http://db.apache.org/derby/derby_downloads.html)
  3. Grails Installation Instructions http://grails.codehaus.org/Installation