Monday, July 25, 2011

Add a new servlet to an OSGI bundle



Servlets are useful while handling requests coming from web browsers and setting responses according to the requests and after process them.
In WSO2 carbon environment,OSGI UI bundles are exposed to web and sometimes there might be cases where we need to use a servlet and process on it according to web browser requests.Following steps will help you in such cases when you need to add a servlet for an UI bundle.

Pre-requesties-
  • Any product server from WSO2 Carbon products [1]
  • IntelliJ IDEA 10.0
  • Maven 2.1.0 or higher
  • JDK 1.6 or higher

1.First create a servlet class by extending HttpServlet abstract class inside an OSGI bundle.You can create a new UI-bundle by creating a new maven project and add this servlet class inside it..You can refer the WSO2 dashboard UI bundle code structure from here[2].Else For a new bundle you need to import 'javax.servlet' package from bundle pom.xml as we did for wso2 dashboard UI bundle pom.xml[3].


2.Then you need to add a runtime reference to 'servlet.context.service' and made available it to the bundle by the Service Component Runtime as below.

You can get an idea on this,by refering how we have done for WSO2 dashboard UI bundle as in DashboardUiServiceComponent.java class [4].

3.Then you need to register that created servlet to the OSGI bundle by adding following entry to component.xml file which is in resources/META-INF/component.xml.



In above is used to execute the servlet by mapping this entry value with incoming request URLs to the server. You can have a look on how we have registered GadgetServlet in WSO2 dashboard UI bundle by referring[5].

4.Add the functionalities that you need to process in the servlet to the overridden doGet() method or doPost() methods of the servlet.
4.Build the bundle you added the servlet.Then to test the servlet,if you have created a new bundle(maven project) build it and copy it to the location WSO2_Product_server_home/repository/components/dropins.Then re-start the server and access the following URL.

http(s)://hostName:port/pattern>

Here equal to the value you have added earlier for the entry in component.xml file.

For your easier I have uploaded a sample OSGI UI bundle that has registered one servlet in it.You can download it from here.This UI bundle named as HelloWorld and a servlet called 'HelloWorldServlet' has registered in it by following above steps.Download it,unzip it and have a look on the code of it.Then build it and add the generated .jar file in target folder to the location server_home/repository/components/dropins,and start your WSO2 product server.Try whether you can access the following URL.

http(s)://hostName:port/hello

When accessing above url you'll see 'helloWorld' word will print on web browser.
Such that the HelloworldServlet has successfully registered in UI bundle.



Sunday, July 24, 2011

Love Green...!!!

After staying a week in a dusty brown colored,busy town with full of vehicles noise as in Colombo,what do u prefer in your weekend? Get away from there right?

Yaa,this weekend I was able to go through green colored village and have a wonderful time on there.

Walk through paddy fields,listen to wind.Watch multi-colored birds,Wash hands from water canals.


This facilitates me to come back Colombo on starting of next week with a fresh mind.But what about future?With current human actions,is it guaranteed future kids will get such moments in their lives.I think we have to think this for a while.Money is not all about life.We are part of the nature.We have to protect Nature in sake of survival of ourselves.Hence please don't sell nature for a bundle of money and watch it's end.

The most valuable lession teach by Nature is ''Simplicity".If you obey this lession you will never hurt nature. :):):)






Friday, July 15, 2011

WSO2 ESB learning part1-Usage of a class mediator in a makeFault mediator

Class Mediator-The class mediator creates an instance of a custom specified class and sets it as a mediator. The class must implement the org.apache.synapse.api.Mediator interface.For more info

makeFault mediator-The fault mediator transforms the current message into a SOAP fault message.but does NOT send it. The mediator needs to be invoked to send a fault message created this way.For more info

Sample makeFault mediator

Below is the sample fault mediator that we are going to use in ESB sequence.


Creating a new class mediator

There is a good article on how to create a new class mediator which can be found here.Hence I'm not going to repeat those steps.But for your easier,i'll give the overview of a class mediator.Any class mediator must have to extend 'AbstractMediator' abstract class and hence such a mediator need to implement the method 'mediate()'.The purpose of creating the class mediator here,is to set the property value 'ERROR_CODE' which is going to use as the fault code value in above makeFault mediator.How we have done it using the class mediator is shown as below.
package org.wso2.carbon.mediator;


import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import javax.xml.namespace.QName;

public class SynapseMessageContextMediator extends AbstractMediator{
public boolean mediate(MessageContext messageContext) {
QName qname=new QName("http://www.w3.org/2003/05/soap-envelope","soap12Env:Receiver");
messageContext.setProperty("ERROR_CODE",qname);
return true;

}
}
Since ESB supports SOAP 1.2,based on SOAP 1.2 specification,the fault code value should contains the same prefix of the SOAP envelope namespace.Hence we have set 'ERROR_CODE' property value as a QName,which has a namespace and a prefix.

How to make usage of created class mediator into the makeFault mediator.

To do this we have to add above both class mediator and fault mediator into synapse.xml file.Then add a log mediator exactly after the makeFault mediator.Then once we have accessed http://localhost/8280 we can see the SOAP fault[which follows SOAP 1.2 specification] which is corresponding to the above created makeFault mediator in ESB server log as below.



You can see the above soap envelope fault code value has set to 'soap12Env:Receiver' by using the class mediator we have created.






JMX configurable Support from WSO2 Carbon

When a user start-up any WSO2 product and while having a look on the log displays while server starting he/she could see a URL called JMX Service URL as in [1] displays on the log.This is the URL use to connect with JConsole to monitor memory consumption,thread utility,manage MBeans and more.

As above each of the products from WSO2 family,provides in-built support from Carbon platform to manage and monitor of its resources such as applications,services,JVM using JMX [Java Management Extensions] support.

You can see each JMX service url contains host names and port values.In early Carbon released versions [before 3.2.0 release],these values couldn't be change according to user's preference.Those values directly taken from system properties own by running Carbon server.But there are some use-cases where users need to create JMX URLs according to their preference.For example when apache proxy server configured infront of ESB and user need to connect to JConsole by using apache proxy host name and port values.

Hence from Carbon release 3.2.0 onwards,we have introduce a new config file called jmx.xml [in carbon_home/repository/conf/advanced/jmx.xml] and from that file a user can set the property values for JMX URL as below.




To enable configuring property values from this jmx.xml file,first the property value 'StartRMIServer' should be set to 'true'.

Note that the two JMX port values are included in carbon.xml [in carbon_home/repository/conf/carbon.xml] file too.And the same port values included in jmx.xml file are overridden by the values included in carbon.xml file.
Once user configured JMX RMI property values as he/she required he/she can re-start the server and watch the log displays while server starting.We say these property values as RMI values because we use these property values to transmit client requests[carbon server request] to remote JMX MBean Server [JConsole] .Finally copy the custom JMX Service URL displayed in server log and paste into JConsole connections section called 'Remote Process' as below.




Then you'll able to connect with server you want and monitor resources consumption of it.

[1] service:jmx:rmi://localhost:11111/jndi/rmi://localhost:9999/jmxrmi

For further information,refer following links.