Wednesday, January 28, 2009

Using Jar Class Loader (JCL) to reload classes from a JAR

I have been playing recently with JCL to load classes on demand from a jar file.

The immediate use of this is to build a plugin engine. A jar file might contain some code that can change without the need to restart the application using it.

Suppose you are told there is a specific functionality that should be changing regularly without affecting your server uptime. If you are behind a simple server container using WAR deployments you could use this method to ensure no WAR redeployment is needed when your functionality is in fact improved/changed.

I have put a running sample on SVN (JDK 5 needed, if you need to support a previous version just download jcl and compile it yourself).

To see it in action just run run.sh (using linux this time). If you are running Windows just get the *.sh files and provide similar *.bat files. Yes I know, Ant is cross platform and I should have used it for this post. Next time I will use Ant and even Maven ...

You will get full traces of what is happening every two minutes. Change constant 'version' in PluginImpl.java and run build-plugin.sh to see how the trace message is updated with the new version showing you can change your java class implementation at any time and your application does not need to be restarted.

I should remark here that when used from an application server like JBoss which uses its own class loader it is necessary to add the interface package to the newly created class loader (use jcl#add() for it). However if the plugin class refers directly to some classes in the current class loader jcl#add()-ing individual packages would be overkilling. You need then to use a custom loader like:
package mypackage;

import org.apache.log4j.Logger;

import xeus.jcl.loader.Loader;

class MyLoader extends Loader {
private Logger logger = Logger.getLogger(EISLoader.class);

public MyLoader() {
order = 4;
}

public Class load(String className, boolean resolveIt) {
Class result;
try {
result = this.getClass().getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
return null;
}

if (logger.isTraceEnabled())
logger.trace("Returning class " + className + " loaded with parent classloader");

return result;
}

}
Then later use that loader as in:
MyLoader loader=new MyLoader();
loader.setOrder(4);
jcl.addLoader(loader);

I also found problems with log4j since JBoss uses its own and it might be old you might get:
java.lang.NoSuchMethodError: org.apache.log4j.Logger.isTraceEnabled()
At least in the case of JBoss 4.0.4 I managed to solve the problem using log4j-1.2.15.jar renamed as $JBOSS_HOME/lib/log4j-boot.jar and $JBOSS_HOME/server/$configuration-dir/lib/log4j.jar.

We can also go by ourselves and try to get this done without any special library using UrlClassLoader however I have done some tests and this solution appears to work in Windows but not in Linux. Once the plugin class is loaded the first time any further replacement will not be noticed in my Ubuntu Intrepid 2.6.27-9-generic Kernel.

Friday, January 16, 2009

Deploying applications to IPhone

While developing my first application for the IPhone I had fun. I really enjoy Objective-C messaging style and its dynamic capabilities. The simulator was really cool provided my previous experience with other device simulators.

However I was starting to get really desperate after finding too much problems when trying to deploy (using the official and legal way) the application to the real device.

I found then an excellent post that saved me perhaps several more hours of troubleshooting.

Later I was asked to deploy the application to several other phones. This time the existence of old and new profiles, several Xcode windows opened and lack of project info updates made me lose some more time so I am documenting here what I found as a solution:

. Delete all (related to the application to be deployed) files from ""~/Library/MobileDevice/Provisioning Profiles"".
. Close Xcode and ITunes. IPhone must be disconnected from PC.
. Open Xcode
. Drag the developer and the ad hoc profiles to the Xcode dock icon
. In Xcode select Menu|Window|Organizer and expand "Provisioning Profiles" on the left pane. You must have there two profiles (dev and ad hoc)
. On the IPhone select Settings|General|Profiles and delete any previous installed profiles (at least those related to the application you are trying to deploy)
. Connect the IPhone to the PC and check the profiles (dev and add hoc) are installed using Settings|General|Profiles
. From Xcode open the project and select Menu|Build|Clean All Targets
. Click on the project root folder on the left pane and select Project|Edit Project Settings
. Navigate to Build tab and locate "Code Signing" section. click on the value for "Code Signing Entity/Any IPhone OS Device" and select the value under the Dev profile.
. Build and Go

Followers