Java ByteCode Manipulation JavaAssist

These days in J2EE we hear a lot about AOP and Weaving of Aspects and Injection of Code at different points in code. All this wouldn’t have been without a concept of bytecode manipulation of Java Class File while loading.

This has it roots in ClassLoading and Class file. Main benefit of java is that compile code is converted to bytecode and not any machine code which makes it easy to manipulate bytecode before it can be converted to Machine readable code

And Before Loading any class if we can change the byte code it would be same as saying ByteCode Manipulation at Runtime/LoadTime

We have lots of libraries in Java which do this BCEL / ASM / JavaAssist

Of all of them the one which is most developer friendly is JavaAssist

Before you Talk about ByteCode manipulation we should also know one ore concept that lets us to Add hook before class is loaded by JVM. Java provides concept of Agents which can be hooked with Java Command to Get callback from JVM while Class is loaded.

To Implement an agent we have to Implement ClassFileTransformer
when ever JVM loads any class it gives callback to premain method of the Agent.

Using all the concepts above here is how ByteCode manipulation is done at runtime

By hooking our agents in JVM and by manipulating Java Byte code when Loaded using Javaassist we can do our custom modifications

Attached is sample code with ant build file
Rename to zip

Any Queries mail at ashwin@linkwithweb.com
ByteCodeManipulation

Load/Read XML Configuration Files Using Apache Digester

There would no developer in this world who haven’t heard or used XML in his daily usage. When it really comes to developing a new product , Creating a Soap message or Communicating over ESB we always have to think about XML. Here i’m providing an efficient approach other then SAX and DOM that can easily setup ypur project configuration in Minutes by creating configuration objects from xml

Apache Digestor is One such utility which was create for a purpose to make XML to Dataobject Conversion very easy and straight forward

Digestor as the name suggests consumes XML and Created Javaobjects which represent the Configuration

Code that can do this is pretty simple and straight forward

/**
*
*/
package com.linkwithweb.configuration.utilities;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;

import com.linkwithweb.configuration.dataobject.ReplayMagix;

/**
* @author ashwin kumar
*
*/
public class ReplayMagixDigestor {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ReplayMagixDigestor xmlDigest = new ReplayMagixDigestor();
xmlDigest.digest();
}

public void digest() {
try {
// Create Digester using rules defined in academyRules.xml
Digester digester = DigesterLoader.createDigester(this.getClass()
.getClassLoader().getResource("replayRules.xml"));

// Parse academy.xml using the Digester to get an instance of
// Academy
ReplayMagix a = (ReplayMagix) digester.parse(this.getClass()
.getClassLoader().getResourceAsStream("replay-magix.xml"));

System.out.println(a.getClasspath().getPathElements().size());

System.out.println(a.getClasspath().getPathElements().get(0)
.getLocation());

System.out.println(a.getDebugPoints().get(0).getPoint());
} catch (Exception e) {
e.printStackTrace();
}
}

}

As you see in the code there is a XML which i want to load into configuration object and there is another xml which defines how i should load .

My Sample XML Definition is (replay-magix.xml)

<?xml version=”1.0″?>
<replaymagix name=”TEST”>
<classpath>
<pathelement location=”lib/” />
<pathelement path=”lib2/test.jar” />
</classpath>

<debug-point>
<classname></classname>
<interfacename></interfacename>
<parentname></parentname>
<methodname></methodname>
<point>before/after</point>
</debug-point>
</replaymagix>

And Here is my Rule which defines how to Load it (replayRules.xml)

<?xml version=”1.0″?>
<digester-rules>
<pattern value=”replaymagix”>
<object-create-rule
classname=”com.linkwithweb.configuration.dataobject.ReplayMagix” />
<set-properties-rule />
<pattern value=”classpath”>
<object-create-rule
classname=”com.linkwithweb.configuration.dataobject.Classpath” />

<pattern value=”pathelement”>
<object-create-rule
classname=”com.linkwithweb.configuration.dataobject.PathElement” />
<set-properties-rule />
<set-next-rule methodname=”addPathElement” />
</pattern>

<set-next-rule methodname=”setClasspath” />
</pattern>

<pattern value=”debug-point”>
<object-create-rule
classname=”com.linkwithweb.configuration.dataobject.DebugPoint” />
<bean-property-setter-rule pattern=”classname” />
<bean-property-setter-rule pattern=”interfacename” />
<bean-property-setter-rule pattern=”parentname” />
<bean-property-setter-rule pattern=”methodname” />
<bean-property-setter-rule pattern=”point” />

<set-next-rule methodname=”addDebugPoint” />
</pattern>
</pattern>
</digester-rules>

If you need working and mavenized code contact ashwin@linkwithweb.com

GWT Spring & Hibernate Mavenized Crud Sample

Attached is the Mavenized Project which has sample CRUD operations

Tested Configuration

Using CATALINA_BASE: “C:\setup\Apache\apache-tomcat-6.0.26”
Using CATALINA_HOME: “C:\setup\Apache\apache-tomcat-6.0.26”
Using CATALINA_TMPDIR: “C:\setup\Apache\apache-tomcat-6.0.26\temp”
Using JRE_HOME: “C:\setup\java\jdk1.5.0_21”

Maven 2.2.1
GWT 1.7.1

Download this link extract using winrar
MavenizedSample

It has InstallMaven.txt which has command to install some thirdparty libraries
It has InstallDB.txt which has commoand to import Oracle Dump

Once the above configuration is done then Run
mvn clean package

This shall create war file as well as package in target folder. copy that deploy in tomcat.
For any questions
mail me at ashwin@linkwithweb.com

Spring Poolable Database Connection

<bean id=”cpmsDS” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>
<property name=”driverClassName”><value>your.driver.classname</value></property>
<property name=”url”><value>yourDatabaseUrl</value></property>
<property name=”username”><value>yourUsername</value></property>
<property name=”password”><value>yourPassword</value></property>
</bean>

<bean id=”pool” class=”org.apache.commons.pool.impl.GenericObjectPool”>
<property name=”minEvictableIdleTimeMillis”><value>300000</value></property>
<property name=”timeBetweenEvictionRunsMillis”><value>60000</value></property>
</bean>

<bean id=”dsConnectionFactory” class=”org.apache.commons.dbcp.DataSourceConnectionFactory”>
<constructor-arg><ref bean=”cpmsDS”/></constructor-arg>
</bean>

<bean id=”poolableConnectionFactory” class=”org.apache.commons.dbcp.PoolableConnectionFactory”>
<constructor-arg index=”0″><ref bean=”dsConnectionFactory”/></constructor-arg>
<constructor-arg index=”1″><ref bean=”pool”/></constructor-arg>
<constructor-arg index=”2″><null/></constructor-arg>
<constructor-arg index=”3″><null/></constructor-arg>
<constructor-arg index=”4″><value>false</value></constructor-arg>
<constructor-arg index=”5″><value>true</value></constructor-arg>
</bean>

<bean id=”pooledDS” class=”org.apache.commons.dbcp.PoolingDataSource” depends-on=”poolableConnectionFactory”>
<constructor-arg><ref bean=”pool”/></constructor-arg>
</bean>