Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / October 2003

Tip: Looking for answers? Try searching our database.

Custom Class Loaders

Thread view: 
Dave Rudolf - 19 Oct 2003 18:08 GMT
Hi all,

I'm playing around with class loaders, and I seem to be having some
problems. Suppose that I write my own class loader, like so:

///////////////////////////////////////////

import java.io.*;

public class StreamClassLoader extends ClassLoader
{
   public StreamClassLoader()
   {
   }

   private static byte[] readData( final InputStream inStream,
                                   final int length )
       throws IOException
   {
       final byte[] data = new byte[ length ];
       int readSoFar = 0;
       while( readSoFar < length )
       {
           readSoFar += inStream.read( data, readSoFar, length -
readSoFar );
       }
       return data;
   }

   private Class loadClass( final String expectedClassName,
                            final byte[] classData )
   {
       return defineClass( classData, 0, classData.length );
   }

   public Class loadClass( final String expectedClassName, final
InputStream inStream, final int length )
   {
       return loadClass( expectedClassName, readData( inStream, length ) );
   }
}

///////////////////////////////////////////

Now, if I want to use this class loader, I can't just use Class.forName(
String ), as it will just use the default loader. Instead, I have to do
something like:

       StreamClassLoader loader = new StreamClassLoader();
       Class someClassType = loader.loadClass( "some.ClassType" );

Okay, I can live with that. But the problem is that if I am reading objects
off of an ObjectInputStream, it uses the default class loader.
Interestingly, an ObjectOutputStream does
not need the class loader to write an object of this type. I assume that it
just uses the object's class directly.

So my question is: how do I link the custom class loader to an
ObjectInputStream? It would also be nice get Class.forName(...) to work
also, so that other parts of the code would not have to know about the
custom loader.

Thanks,

Dave.
Matthias Ernst - 20 Oct 2003 10:28 GMT
> So my question is: how do I link the custom class loader to an
> ObjectInputStream? It would also be nice get Class.forName(...) to work
> also, so that other parts of the code would not have to know about the
> custom loader.

It is good practice for any class that finds classes by name, such as
factories, object deserializer, ... to use the current thread's {@link
Thread#getContextClassLoader context classloader}. I think
ObjectInputStream complies to that, so the solution would be:

ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(streamLoader);
try {
 return ois.readObject();
} finally {
 Thread.currentThread().setContextClassLoader(old);
}

Matthias
Signature

Matthias Ernst
Software Engineer

CoreMedia - Smart Content Technology



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.