Sunday, December 1, 2013

'JavaScript and JSON Essentials' : The second book I reviewed is now published in PacktPub


Few months ago i received an invitation from Packt Publishing to review the book 'Instant GSON Starter' which is a comprehensive guide on getting start with Google GSON library. I have previously worked with gson in the last few years during Google Summer of Code programme when i was an undergrad student therefore it was a great experience for me reviewing a tech book for the first time, as the technical reviewer.


The book 'JavaScript and JSON Essentials' is the second book which i got the opportunity to review recently after starting my career as a Software Engineer. As it is described in the book, 'JavaScript and JSON Essentials' is a step-by-step guide that will introduce you to JSON and help you understand how the lightweight JSON data format can be used in different ways either to store data locally or to transfer data over the Internet. This book will teach you how to use JSON effectively with JavaScript. The book is written in a manner that it provides a detailed introduction into each and every section as well as it provides examples in a tutorial-based approach. Therefore even for a novice it would be easy to learn and follow the examples given in the book in order to get familiarized with JavaScript and JSON.

The book is written by Sai Srinivas Sriparasa. It is currently published in the PacktPub catalogue and can be purchased via the link here.

Sunday, August 11, 2013

[Webinar] How to build a custom stack with WSO2 Carbon

I have conducted a webinar on the topic mentioned above by few weeks ago, dated Wednesday 24th July 2013. The presenters were my self and Ananda Manoj and the webinar was mainly focused on discussing how to combine and use the various features  of the WSO2 Carbon platform to suit an organization's middleware needs.

WSO2 Carbon middleware stack consists of more than a 175 OSGi-based components. Even though WSO2 Carbon ships some of the well defined component configurations as a set of products,  it is also possible for organizations to come up with their own product configurations through a mix and match of available components with WSO2 Carbon. Our webinar, which was the final one of the 'Carbon Webinar Series' was conducted in explaining how to create a customized stack with WSO2 Carbon with including a step by step guide and a live demonstration on how to create a custom component.

First let's know the key points that were highlighted in webinar.

•  The feature/component concept in the WSO2 Carbon platform.
•  Installing features using the Feature Manager.
•  How to create your custom products for cluster wide deployment.
•  Configuring logs/adding configs etc.

In the next paragraphs i am going to brief on what exactly is discussed from each point here.

•  The feature/component concept in the WSO2 Carbon platform
Component is simple a OSGi bundle. This is what we develop. Carbon component should follow the rules define in carbon framework. Component has a core runtime which will provide a clean SOA management interface. There are two type of components Back-end component and Front-end component. All bussiness logic goes to back end component while front-end component have all client side code.  A component can use core Carbon services like Registry Service, UserManager Service etc... via OSGi services registry.

In Carbon world feature is an installable unit. This is what we install on Carbon. A feature has one or more logically related Carbon components. We can write an aggregate feature which has two or more features with it. This features can be installed into Carbon base products using feature Manager service, provided by Carbon Framework.
   
•  Installing features using the Feature Manager
As mentioned in above there is a feature manager service which provides nice and easy way to install features on Carbon based products. This feature manager supports all feature provisioning works in runtime like install/uninstall features etc. Once you installed the new features to your Carbon based product you must restart the system to active them. Using Feature Manager you can install features only in runtime but if you need to install your custom features in build time you can do that using Carbon p2 maven plugin. It is just needed to add that plugin to your pom.xml file with few configurations.   

•  How to create your custom products for cluster wide deployment
In productions you may need to scale up and down your system or avoid single point of failure using two or more instance in runtime. One way to do this is cluster your runtime instances and expose it as a single service. For that your product should have clustering capabilites with it, Carbon kernel provides clustering support for all products which are build on top of that. It means if you build your custom products on top of Carbon you will get the culstering capability for your product. Another barrier is volume, reality is even you start your product with low number of requests per sec, with the growth of the business you may need handle large number or request per sec. One way to handle this is use caching. With caching you can store state less data used in requests other than using back end database. Where do we need to store and retrive the same data per reqeust? As you know a database call is very expensive. Therefore caching is the best solution for that kind of situation. Carbon kernel it self comes up with Caching implentation, not only local caching it provide distribute caching too. Like wise you will get advanced features like depsync, GostDeployment capabilities too.

•  Configuring logs/adding configs etc.
Carbon Framework is a customizable framework as it provides number of configuration files to customize the runtime behavior. All configuration file are placed under <CARBON_HOME>/repository/conf directory. Following are the set of important configuration files.

  • axis2.xml  - Use for configure MessageBuilders, MessageFormatters , Clustering etc ...
  • carbon.xml - Use for configure Ports, KeyStores , DeploymentSynchronizer , JNDI etc ...
  • catalina-server.xml - Use for configure TransportListners and TransportSenders, Tomcat valves
  • registry.xml - All registry related configurations.
  • log4j.properties - All log related properties
  • master-datasources.xml - All datasource related configurations.

This webinar was mainly targetted the audience who are interested in,
• Customizing existing WSO2 Carbon servers with additional components.
• Scripting feature installations for automated deployment in a clustered environment.
• Configuring Carbon servers for optimal performance.
 If you belong to any of these categories, it will be worth your time watching it from the link below.

To watch the recording or read the slides used in webinar, goto the official link from here.....
To read the presentation in SlideShare, visit here ...
To download Student Manager sample click here ...

Tuesday, July 16, 2013

Java ClassLoader Example with URLClassLoader


Here i am going to explain how to use java URLClassLoader to load classes in runtime. First i will explain how to run the code without class loaders. Then i will explain how to run the same code using URLClassLoader.

Let's try to convert java object to JSON using google gson 3rd part library.

1. Without ClassLoaders

Main Class
package my.sample.classloading;

import com.google.gson.Gson;

public class Main {
public static void main(String[] args) {
Gson gson = new Gson();
String jsonString = gson.toJson(new Person());
System.out.println(jsonString);
 }
}

Person class
package my.sample.classloading;

public class Person {
private String name="Foo Bar";
private int age= 25;
} 

If you try to run this sample code without providing gson-<version>.jar  at runtime you will end up getting following exception.

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
    at my.sample.classloading.Main.main(Main.java:27)
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
 
But if you add gson-<version>.jar to your classpath you will not get above exception instead you will get expected out put.

Let's see how we can handle this with class loading.

2. With ClassLoaders

Main Class
package org.wso2.carbon.classloading;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, MalformedURLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {


        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{new URL("file:///home/shameera/Desktop/gson-2.2.4.jar")});
        Class gsonClass = urlClassLoader.loadClass("com.google.gson.Gson");
        Constructor constructor = gsonClass.getConstructor();
        Object gsonObj = constructor.newInstance();
        Method method = gsonClass.getMethod("toJson",Object.class);
        Object returnObj =  method.invoke(gsonObj, new Person());
        String jsonString = (String)returnObj;
        System.out.println(jsonString);
    }
}

 With above sample it loads gson-2.2.4.jar file at runtime using Java URLClassLoader and convert a Person object to json string. As you can see there is no any import for Gson class instead we load and use it in runtime.

Install a JAR to local maven repository

When dependency jars are not available in maven central repository you have to install it to your local repository or you can provide it using system path. Using following command you can send your dependency jar to your local m2 repository.

mvn install:install-file -Dfile=<path/to/your/dependecy/jar> -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar


Eg:Let's get google gson library as an example, This is already available in maven repository but here we are going to manually install it to our local m2 repo. Assume i have downloaded gson-2.2.4.jar to my desktop

         <dependency>
             <groupId>com.google.code.gson</groupId>
             <artifactId>gson</artifactId>
             <version>2.2.4</version>
         </dependency>

mvn install:install-file -Dfile=/home/shameera/Desktop/gson-2.2.4.jar -DgroupId=com.google.code.gson -DartifactId=gson -Dversion=2.2.4 -Dpackaging=jar

You will see similar output like below.

[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) @ standalone-pom ---
[INFO] Installing /home/shameera/Desktop/gson-2.2.4.jar to /home/shameera/.m2/repository/com/google/code/gson/gson/2.2.4/gson-2.2.4.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------


As same as you can install your own jars to local maven repo and refer it in different project.





Sunday, May 5, 2013

Elected as Apache Axis2 Committer and PMC

It has been a long time, when i started to contribute to Apache Axis2 . As my first contribution to Apache Axis2, I have implemented Axis2 JDK 5 Enum support,. Here is my blog post which explain how to consume that feature. From that onwards I continued my contribution for Apache Axis2 as not only in the Axis2 core , I also contributed to Apache Sandesha2 which is an Axis2 module that provides reliable messaging capability.

I did my GSOC 2012 with Apache Axis2 in improving the JSON support of Apahce Axis2. As an outcome of this my mentor Amila Suriaarachchi  and myself could able to introduce a new approach to convert native a JSON message to XML bidirectional transformation mechanism without any data loss, and it supports namespace too.

Considering the contribution i have done so far in both coding and user mailing list, I have been elected to be an Apache Axis2 Committer and PMC. This is a great achievement for me and I am very much happy about it.


Wednesday, March 20, 2013

Fix machine hostname return null as it value

If your getting null value as your host name, check that both /etc/hosts and /etc/hostname files has the same name to the host, If not put the same name to both file as hostname, Here[1] you can find how to do that.

How to change machine name in Ubuntu 12.04 LTS

Go to the terminal
Type "sudo su" , because you need log into root mode to modify following files.
Now type "gedit /etc/hostname"
Replace new name with old name
Save and close the file

Now type "gedit /etc/hosts"
Replace your new name with old name
    eg : 127.0.1.1 <old name>    -->   127.0.1.1 <new name>
Save and close the file

Now you need to restart the machine, type "reboot" in terminal to restart via terminal.
Now you have successfully changed machine name. 

Monday, February 18, 2013

Install jar to local repo which are not available in maven repository

If you find a jar which is not in any maven repository. But you need it to build you maven project you can first install that jar to you local repo using mvn install:install-file .

e.g: java-gmail-imap.1.4.4-gm-ext-0.6.jar is not in any maven repository therefore i need to install it on my local repository, run following command in terminal

mvn install:install-file -Dfile=/path/to/java-gmail-imap.1.4.4-gm-ext-0.6.jar -DgroupId=com.google.code -DartifactId=java-gmail-imap -Dversion=1.4.4-gm-ext-0.6 -Dpackaging=jar

you will see

[INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) @ org.example --- [INFO] Installing /path/to/java-gmail-imap-1.4.4-gm-ext-0.5.jar to /home/user/.m2/repository/com/google/code/java-gmail-imap/1.4.4-gm-ext-0.6/java-gmail-imap-1.4.4-gm-ext-0.6.jar [INFO] Installing /tmp/mvninstall4760815746928565678.pom to /home/user/.m2/repository/com/google/code/java-gmail-imap/1.4.4-gm-ext-0.6/java-gmail-imap-1.4.4-gm-ext-0.6.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------

Now my java-gmail-imap.1.4.4-gm-ext-0.6.jar is in my local repo, and will add this as a dependency to my project

<dependency>
<groupId>com.google.code</groupId> <artifactId>java-gmail-imap</artifactId> <version>1.4.4-gm-ext-0.6</version>
</dependency>


that's all now you can build your project :)


Sample Text

Website counter

Categories