Ramblings from a Bihari!

Just another WordPress.com weblog

did u know u can find the location of the .class file from the instance??

In general there is no guarantee that u can always get the location(Basically URL) from where ur instance or object is loaded but luck u can’t completely discard.. ūüôā

The following class which i am putting tries to find out the location of teh instance from where it is loaded. All you need to do is call the getClass method on your concerned instance and pass the output to this method.

package com.deepak.classloader;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;

public class FindClassLoaction {

public static URL getClassLocation (final Class clazz)
URL clazzLocation = null;
final String clazzFileName = clazz.getName().replace (‘.’, ‘/’).concat (“.class”);

final ProtectionDomain pd = clazz.getProtectionDomain ();
if (pd != null)
final CodeSource codeSourceOfClazz = pd.getCodeSource();
if (codeSourceOfClazz != null) clazzLocation = codeSourceOfClazz.getLocation ();

if (clazzLocation != null)
if (“file”.equals (clazzLocation.getProtocol ()))
if (clazzLocation.toExternalForm ().endsWith (“.jar”) ||
clazzLocation.toExternalForm ().endsWith (“.zip”))
clazzLocation = new URL (“jar:”.concat (clazzLocation.toExternalForm ())
.concat(“!/”).concat (clazzFileName));
else if (new File (clazzLocation.getFile ()).isDirectory ())
clazzLocation = new URL (clazzLocation, clazzFileName);
catch (MalformedURLException ignore) {}

if (clazzLocation == null)
final ClassLoader clsLoader = clazz.getClassLoader ();

clazzLocation = clsLoader != null ?
clsLoader.getResource (clazzFileName) :
ClassLoader.getSystemResource (clazzFileName);

return clazzLocation;


The luck factor here is that u can’t guarantee the fact that every class is loaded with some ProtectionDomain i.e. It depends on the classloader whether it is creating the ProtectionDomain before loading the class….:(

also the CodeSource can be null depending on the classloader implementation. U can almost be sure that protection domain and code source will be populated for the classloders which are instance of URLClassLoader. So better you can put a check on the claasloader whether its an instance of URLClassLoader to be on safe side.

Sun’s implementation seems to allow finding the class file as resource ..



April 24, 2008 Posted by | techie stuff | 6 Comments

Cases for Shortcomings of Single Request-Response model

My personal experience about implementing a single request response model had been very discouraging. Consider a situation in which u want two remotely hosted instances of database to be synchronized.  The synchronization essentially means  changes at either side to be reflected  at the  other side  whenever  synchronization request  is made.  The changes here essentially means inserts, updates, deletes at either side and not the DDL changes to be reflected.

I have a solution which does this synchronization using transfer of changes at either side as XML data packets and applying the changes after successfully receiving the changed XML data packets from the other side. The solution I implemented initially was a single request response model.

This simply means that

Step 1. I will fetch the database changes at the requester side from the last stable synchronized state and convert the changes resultset to java.sql.rowset.WebRowSet kind of XML format and transfer the changes to the receiver end with the request.

Step 2. The receiver side will receive the changes and try to parse the XML data packets and apply the changes on the database.

Step 3. After committing the changes received from the requester end I will fetch the changes at the receiver end and will prepare the XML data packets out of it.

Step 4. The changes at the receiver end will reach and received at the requester end the same commit will happen.

At the end I ll have the two ends in a synchronized state.

The problem with this approach is that this approach is highly unreliable as it is tightly coupled with network. Any network failure will lead to again creating the synchronization request. Which is not necessary as it is always easier to cache the changes at each step(After fetching the changes, After receiving the changes at either end). Also in the single request response model I feel that acknowledgment of  a particular atomic activity  is not exchanged which will highly increase the reliability or at least we can have a save point based approach. what say ??

April 21, 2008 Posted by | techie stuff | Leave a comment