Tuesday, August 2, 2016

Java Reflection

Java Reflection is used to examine or modify the run time behavior of class at run time. Java Reflection is quite powerful and useful. Reflection enables us to:
  • Examine an object's class at runtime
  • Construct an object for a class at runtime
  • Examine a class's field and method at runtime
  • Invoke any method of an object at runtime
The most common usage of Java reflection is Junit annotations. Its looks through methods and if @Test annotation appears JUnit will execute those methods as test methods.   java.lang and java.lang.reflect packages are provides the classes for reflection. Using java.lang.Class class we can get metadata of a class at run time. To start, as first step we have to obtain the java.lang.Class object of the class that  need to inspect. We can get it in three ways. 
  1. Using .class -  If know the class name at compile time can use.
    • Class classObject= ReflectionSample.class
  2. Using forName() - static method of Class class. To use this, we need to provide fully qualified class name (Ex: com.javaReflection.ReflectionSample). It loads a class of a given name, using the current class loader.  It throws a ClassNotFoundException if the class cannot be found on the classpath at runtime. 
    • Class classObject = Class.forName(“ReflectionSample”); // ReflectionSample=class name
  3. Using getClass() – it’s a method of object class, It returns the instance of Class class.               
    • ReflectionSample sample= new ReflectionSample                              Class classObject= sample.getClass();
Using the class object, we created we can get general information about the class. 
  • To get the name 
    • String name= classObject.getSimpleName(); //it will return the class name 
    • String name= classObject.getCanonicalName(); //return the canonical name of the underlying class as defined by the Java Language Specification 
    • String name= classObject.getName(); //returns fully qualified class name (including package name)
  • To get the constructors
    • Constructor [] constructors = classObject.getConstructors(); // return public, protected, private constructors.
    • Constructor [] constructors = classObject.getDeclaredConstructors(); //return public constructors 
  • To get the methods
    • Method [] methods = classObject.getMethods(); // return public and inherited from supperclass 
    • Method [] methods = classObject.getDeclaredMethods(); // return public, protected, and private methods, but excludes inherited methods.
  • To get the class fields 
    • Field [] fields= classObject.getfields(); // return public and inherited from supperclass 
    • Field [] fields= classObject.getDeclaredfields(); // return public, protected, and private methods, but excludes inherited Fields.
  • To get annotations 
    • Annotation [] annotations= classObject.getAnnotations(); //return public and inherited from supperclass 
    • Annotation [] annotations= classObject.getDeclaredAnnotations(); // return public, protected, and private methods, but excludes inherited Fields.
  • To get class interfaces
    • Class [] interfaces = classObject.getInterfaces(); // return the class object interface implemented by the class, If a superclass of the class implements an interface, but the class doesn't specifically state that it also implements that interface, that interface will not be returned in the array. Even if the class in practice implements that interface, because the superclass does.To get a complete list of the interfaces implemented by a given class you will have to consult both the class and its superclasses recursively. if classObject is an interface object it will return extended once also.
  • To get supper class 
    • Class superclassObj= classObject.getSuperclass(); //it’s a Class object like any other, so can continue doing class reflection on that too.
  • To get package Info
    • Package package= classObject.getPackage(); // returns the package info if need name of package using getName() can return.  
  • To get class modifiers
    • int modifiers= classObject.getModifiers(); // returns the int representation of the class modifiers, can use java.lang.reflect.toString() to get it in the string format or can use java.lang.reflect.Modifier to check 

Wednesday, January 28, 2015

MySQL as relational storage using WSO2 Storage Server

WSO2 Storage Server, Relational Storage Service (RSS) feature allows creating and managing the Relational Database Management System (RDBMS). By default storage server support H2 database but it can be extent. To setup MySQL as relational storage need to do following configurations

1.      Download and install MySQL using the following command:
sudo apt-get install mysql-server mysql-client
2.      Access the  MySQL shell using following command :
mysql –u root  -p
3.      create a database call test as RSS meta repository using following command :
create database test;
4.      Select the script <SS_HOME>/dbscripts /rss-manager/mysql/wso2_rss_mysql.sql and by running the script create the RSS metadata repository database structure.
5.      Edit the default datasource configuration. The master-datasources.xml file, in the <SS_HOME>/repository/conf/datasources directory. 
<datasource>
            <name>MetadataRepo</name>
            <jndiConfig>
                        <name>MetadataRepoDS</name>
            </jndiConfig>
            <definition type="RDBMS">
            <configuration>
                <dataSourceClassName>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</dataSourceClassName>
            <dataSourceProps>
                <property name="URL">jdbc:mysql://localhost:3306/test</property>
                <property name="user">root</property>
                <property name="password">root</property>
            </dataSourceProps>
            </configuration>
            </definition>
</datasource>
6.      To do the RSS meta data specific configurations open the rss-config.xml file stored in the <SS_HOME>/repository/conf/etc/ directory and do the following changes:
<Provider>MYSQL</Provider>
<Environments>
<Environment>
<Name>DEFAULT</Name>
<RSSInstances>
<RSSInstance>
<Name>WSO2RSS1</Name>
<InstanceType>SYSTEM</InstanceType>
<DbmsType>MYSQL</DbmsType>
<ServerCategory>LOCAL</ServerCategory>
<DataSourceConfiguration>
<Definition>
<Url>jdbc:mysql://localhost:3306</Url>
<Username>RootUser</Username>
<Password>RootPassword</Password>
<DriverClassName>com.mysql.jdbc.Driver</DriverClassName>
</Definition>
</DataSourceConfiguration>
</RSSInstance>
</RSSInstances>
</Environment>
</Environments>

Download the MySQL Java connector JAR file and add to the <SS_HOME>/repository/components/lib/ directory

Thursday, January 22, 2015

WSO2 Storage Server

WSO2 Storage Server is conveniently provides a multi-tenanted structured and unstructured data storage provisioning and managing capabilities for development purpose. The storage server provides rapid provisioning and secure access to storage system such as relational, NoSQL Columnar, and Hadoop Distributed File System (HDFS) repositories using a consistent management process. The storage server management console allows users to create database, add users and provisioning access with minimum database administration skills.

The storage server can be downloaded from http://wso2.com/products/storage-server/  or can build from the source code. The source code for carbon-storage management can be downloaded from gitHub using following link https://github.com/wso2-dev/carbon-storage-management.git and the product storage server can be downloaded from gitHub using following address https://github.com/wso2-dev/product-ss.git

To build the product
To build the storage server need to have Maven build tool and JDK1.6 /1.7 installed. When building the storage server needs to follow the order because there are dependencies on carbon- storage management for product storage server, following is the order:  
  1. Build the carbon- storage management
  2. Build the product storage server
“maven clean install” is the command used to build the product from source code. “mvn clean install -Dmaven.test.skip=true” is the command used to build the product from source code without running any of the unit tests.

Running the product
  1. Extract the downloaded zip or extract the wso2ss-SNAPSHOT.zip inside the ../product-ss/modules/distribution/target directory.
  2. Go to the bin directory in the extracted folder
  3. On Windows run  “./wso2server.bat”, on Linux/Solaris run “./wso2server.sh”
  4. Once the server started point the browser to the URL https://localhost:9443/carbon
  5. To login to the management console use “admin”, “admin” as the username and password
Relational Storage Service
The storage server allows users to create and manage Relational Database Management System (RDMS) through the Relational Store Service (RSS). By default RSS support for H2 but it is extensible RSS support for MySQL, SQL SERVER or Oracle also. RSS facilitates to
  •  Create database  
  • Create privilege templates 
  • Create database user 
  • Create RSS instances

Column storage service
By column storage service storage server facilitate users to create and manage the Cassandra cluster using through a simplified management console UI. Column storage service facilitates to: 
  • Creating Keyspaces
  • Creating Column Families   
  • Explore Cassandra Cluster 
  • Cassandra Authentication and Authorization

Hadoop Distributed File System
Hadoop Distributed File System (HDFS) facilitates users to create and manage a distributed file system with authentication and authorization.


Wednesday, December 11, 2013

Sunday, December 8, 2013

Abraham Lincoln's Letter to his Son's Teacher


My son will have to learn I know that all men are not just, all men are not true. But teach him also that forever scoundrel there is a hero; that for every selfish politician, there is a dedicated leader. Teach him that for every enemy there is a friend.

It will take time, I know; but teach him, if you can, that a dollar earned is far more valuable than five found.

Teach him to learn to lose and also to enjoy winning.

Steer him away from envy, if you can.

Teach him the secret of quite laughter. Let him learn early that the bullies are the easiest to tick.

Teach him, if you can the wonder of books... but also give him quiet time to ponder over the eternal mystery of birds in the sky, bees in the sun, and flowers on a green hill –side.

In school teach him it is far more honorable to fail than to cheat.

Teach him to have faith in his own ideas, even if everyone tells him they are wrong.

Teach him to be gentle with gentle people and tough with the tough.

Try to give my son the strength not to follow the crowd when everyone is getting on the bandwagon.

Teach him to listen to all men but teach him also to filter all he hears on a screen of truth and take only the good that comes through.

Teach him, if you can, how to laugh when he is sad. Teach him there is no shame in tears. Teach him to scoff at cynics and to beware of too much sweetness.

Teach him to sell his brawn and brain to the highest bidders; but never to put a price tag on his heart and soul.

Teach him to close his ears to a howling mob… and to stand and fight if he thinks he’s right.

Treat him gently; but do not cuddle him because only the test of fire makes fine steel.

Let him have the courage to be impatient, let him have the patience to be brave. Teach him always to have sublime faith in himself because then he will always have sublime faith in mankind.

This is a big order; but sees what you can do. He is such a fine little fellow, my son.


Abraham Lincoln.