Creating a Carbon Component using WSO2 Carbon Component Archetype

A Carbon Component is an OSGi bundle which has a dependency on Carbon Kernel.
Using WSO2 Carbon Component Archetype which has been published to maven central, you can create a simple Carbon Component with one command.

mvn archetype:generate -DarchetypeGroupId=org.wso2.carbon -DarchetypeArtifactId=org.wso2.carbon.archetypes.component -DarchetypeVersion=5.0.0  -DgroupId=org.sample -DartifactId=org.sample.project -Dversion=1.0.0 -Dpackage=org.sample.project

Above command will create a Carbon Component with the following structure.

├── pom.xml
└── src
    └── main
        └── java
            └── org
                └── sample
                    └── project
                        └── internal

This Carbon Component consumes an OSGi service which is exposed from Carbon Core and also registers an OSGi service of its own.

If you do not pass the parameters for the project then the default values will be applied.

You can find the source for this archetype here.

Creating a generic OSGi bundle using WSO2 Carbon Bundle Archetype

You can create a generic OSGi bundle using one command with WSO2 Carbon Bundle Archetype.
Even though the name is WSO2 Carbon Bundle Archetype which has been published to maven central, this is a generic OSGi bundle.

Execute the following command. The groupID of the archetype is org.wso2.carbon,
The artifactID of the archetype is org.wso2.carbon.archetypes.bundle  and the version of the archetype is  5.0.0

mvn archetype:generate -DarchetypeGroupId=org.wso2.carbon -DarchetypeArtifactId=org.wso2.carbon.archetypes.bundle -DarchetypeVersion=5.0.0  -DgroupId=org.sample -DartifactId=org.sample.project -Dversion=1.0.0 -Dpackage=org.sample.project

 This will create a project with the following structure.

├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── org
    │           └── sample
    │               └── project
    │                   ├──
    │                   └── internal
    │                       └──
    └── test
        └── java
            └── org
                └── sample
                    └── project

If you do not pass the parameters for the project then the default values will be applied.

The source for the archetype can be found here.

Unmarshalling an XML file with JAXB

This is a simple example for unmarshalling an XML file.

This is the XML file we are going to unmarshall
This is the schema of the above XML file
Now let's create
Here for each complex type I have created an inner class.
And for the element "item-name" which has the maxOccurs=unbounded, I have used a List.
Another thing is @XmlAccessorType(XmlAccessType.FIELD) is needed only if the annotated fields inside the class have public setters.  If you don't have public setters for the annotated fields inside the class you don't need to annotate the class.

Now let's write the code for unmarshalling.

Google Summer of Code 2015

This year also I'm participating Google Summer of Code with Eclipse foundation.

My project is Implementing translation between Proj4 and Well-Known-Text in Geotrellis which is a high performance geoprocessing engine and programming toolkit.

Project description in Google Melange can be found here.

Writing a simple web service and a client with Axis2

1. The service

Here a simple POJO service will be created. For simplicity the service will have only two operations.

1. Save an order which consists of an index(String) and a price(Double)
The order will be saved to an xml file.

2. Retrieve the price given the index.

First create a Java Project.

Now inside package I create the Order POJO.

Inside the package orderprocessing.service I create OrderProcessingService Class.

In order to save orders to an XML file I create
To retrieve orders from the XML file I create
Create an XML file orders.xml somewhere and change the "savePath", and "retrievePath" attributes inside the above two classes according to that.

Now create a folder META-INF at the src folder level and create services.xml inside META-INF.

Here, If you do not specify the message receiver, then the default message receiver which is RawXMLINOutMessageReceiver will perform the operation[1]

And since the service implementation class is in Java, all the public methods in that service will be exposed. No need to specifically include them in services.xml[1]

This is the folder structure of my service.

Now export the project as a jar. In eclipse this can be done by, right click on the project->export
Save it to somewhere and change it's extension to .aar (eg: if your jar is OrderProcessingService.jar rename it as OrderProcessingService.aar ) and save it to axis2-1.6.2/repository/services
(I am using axis2-1.6.2)

Start the axis2server. (In linux run inside axis2-1.6.2/bin)

Now if you go to http://localhost:8080/axis2/services/ it should be shown as below.

And you can access the wsdl at, http://localhost:8080/axis2/services/OrderProcessingService?wsdl

2. The client

Create another Java project.

Now in order to use the service we need to create the stub.

Go to axis2-1.6.2/bin in the terminal and run,
Now create another package orderprocessing.client inside your client project and create

This is my folder structure of the client

Run the client and see if it works.

Making SBT(Simple Build Tool) use JDK 7 having both JDK 7 and 8 installed

I was following a scala tutorial and I was using SBT as the build tool. This SBT version required JDK 7 but I had environmental variables set for JDK8 even though I had both installed on my machine.

To make SBT use JDK7 without changing JAVA_HOME environmental variable, this is what I did.

First created a new environmental variable JAVA_HOME7(any name which is not already being used) with the path to JDK7 as the value.

Then replaced JAVA_HOME in the sbt.bat file(which is inside the sbt folder inside program files folder) with JAVA_HOME7.

MATLAB - Applying a function for each element in an array using arrayfun

I'm not a big matlab fan. For a project I needed to apply a function for each element in an array. For that arrayfun can be used.

Here is a simple example.

Say you have 3 arrays as follows.

A = [1,2,3,4]
B = [5,6,7,8]
C = [9,10,11,12]

And you need to calculate the value of a function myf, for A[i], B[i] and C[i] (i=1,2,3,4) which returns A[i] x B[i] + C[i]

You can do it as follows.

First define your function considering a single element.

function output=myf(a,b,c)
  output=a*b + c;


A = [1,2,3,4]
B = [5,6,7,8]
C = [9,10,11,12]

D = arrayfun(@myf,A,B,C)

Remember to have "@" before myf. It denotes a handle in matlab. If you forget it, matlab will try to evaluate the function instead of considering its handle, which may result in "Not enough input arguments" error.