Building Axis2/C on Mac OS X Mountain Lion 10.8.4

This should work on other Mountain Lion versions as well, but I have not tested it. First of all, let’s install a set of dependencies required for the building process.

The easiest way (at least for me) was to install them through macports.

Note: You need to install Xcode and the Xcode command lilne tools for macports to work. Xcode is a (very) large download. So, it will take some time to install. But, an advantage is many tools ask you to install Xcode since it brings in a lot of useful dependencies useful for dev. provided by Apple itself. So, you’ll need it anyway if you’re planning to do some C development ūüôā

After installing macports, you need to install automake, autoconf, libtool, libxml2 and pkgconfig. Type in the following command:


sudo port install automake autoconf libtool libxml2 pkgconfig

Now, we are set to build the source. Check it out using the following command:


svn co https://svn.apache.org/repos/asf/axis/axis2/c/core/trunk axis2-c-trunk

This will checkout the source code the a directory named axis2-c-trunk. Now, we need to apply this patch that fixes a constants issue. Download it and put it inside the directory axis2-c-trunk. Now execute the following commands to patch the source:


cd axis2-c-trunk

patch -p0 < unavailable_constants_macosx_fix.patch

Now, you are ready to build the source. Just execute the build.sh with the following command:


./build.sh

That should create a successful build in the axis2-c-trunk/deploy directory. If it didn’t you probably would need this patch as well. Patch your source the same way as above, and try running the build script again:

patch -p0 < mac_os_build_fix.patch
./build.sh

 

Handling Exceptions with Axis2 Web Services (Code-First Approach)

If you are writing a web service using Java and deploying it in Axis2, you need to follow the steps below to propagate the exception properly to the client side.

Let’s say you have an exception you want to throw with a respective error message. Your method would look something like;

 public String foo throws MyException {
    if (true) {
       throw new MyException("This is my exception");
    }
 }

If you codegen your axis2 client stub you will see that the exception will be thrown when using the client. But if you catch that exception and print out the message, you will notice that the message is lost.

try {
   String s = client.foo();
} catch (MyExceptionException e) { // the codegen exception thrown
   System.out.println("The error that occurred is : " +  e.getMessage());
}

This will output something like;

The error that occurred is : MyException

To preserve the exception message, we need to include it as an attribute just like we would do in a bean class. Here’s an example of the MyException class;

public class MyException extends Exception {
    private String message;

    public MyException(String s, Throwable throwable) {
        super(s, throwable);
        this.message = s;
    }

    public MyException(String s) {
        super(s);
        this.message = s;
    }

    @Override
    public String getMessage() {
        return this.message;
    }

}

This can of course be extended to send any number of arbitrary attributes with the exception, given that they are web service friendly.

Now, you need to codegen your client and modify the client code as follows:

try {
   String s = client.foo();
} catch (MyExceptionException e) {
   System.out.println("The error that occurred is : " +  e.getFaultMessage().getMyException().getMessage());
}

When you run the code, the output will be as follows:

The error that occurred is : This is my exception