Matt,

I would guess that serializing a class and then trying to deserialize it without access to the original class would cause issues.

I wrote a quick and dirty program that creates a simple class loader to manually load a class and then serializes the class to a file and then deserializes the class from the file without access to the loading class loader and it (as expected) throws an exception. However, the exception that I am seeing is CNFE, not StreamCorruptedException...but that could be due to how you are deserializing...

Here is the program I used:

public class Main
{
   public static void main(String[] args)
   {
       {
           ObjectOutputStream oos = null;
           try
           {
               ClassLoader loader = new SimpleClassLoader();
               Class clazz = loader.loadClass("test.Hello");
               System.out.println("Loaded: " + clazz);
oos = new ObjectOutputStream(new FileOutputStream("test.ser"));
               oos.writeObject(clazz);
               oos.close();
               System.out.println("Serialized class");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.ser"));
               clazz = (Class) ois.readObject();
               ois.close();
               System.out.println("Deserialized class");
           }
           catch (IOException ex)
           {
               ex.printStackTrace();
           }
           catch (ClassNotFoundException ex)
           {
               ex.printStackTrace();
           }
           finally
           {
               try
               {
                   oos.close();
               }
               catch (IOException ex)
               {
                   ex.printStackTrace();
               }
           }
       }
   }

   public static class SimpleClassLoader extends ClassLoader
   {
       protected Class findClass(String name) throws ClassNotFoundException
       {
           if (name.equals("test.Hello"))
           {
               try
               {
FileInputStream fis = new FileInputStream("Hello.class"); ByteArrayOutputStream baos = new ByteArrayOutputStream();
                   byte[] buf = new byte[1024];
for (int len = fis.read(buf); len >= 0; len = fis.read(buf))
                   {
                       baos.write(buf, 0, len);
                   }
                   buf = baos.toByteArray();
                   baos.close();
                   fis.close();
                   return defineClass(name, buf, 0, buf.length);
               }
               catch (Exception ex)
               {
                   ex.printStackTrace();
                   throw new ClassNotFoundException(name);
               }
           }
           else
           {
               return super.findClass(name);
           }
} }
}

My test.Hello class implemented Runnable. So, if you are serializing the class, then yes you could have an issue...

-> richard

Matt Clark wrote:
We have an app where we are using an embedded felix instance to deploy
plugin functionality.  Our bundles contain implementations of a
publically knnown interface, and also contain a properties file with the
class names to load.  When the bundle is loaded, we look at the
properties file, load the Class from the classfile, and pass around the
reference to that Class (just the Class, not an instance of it) in our
application.

We're seeing a StreamCorruptedException in our application, and after
some serious debugging cannot figure out where it's coming from.  My
theory is that we accidentally serialized the object holding on to the
osgi-loaded class into a disk-based cache, and the exception is
occurring because the ObjectInputStream can't load that class with the
web container's classloader when it tries to deserialize it.

Question: Does it make sense that ObjectInputStream would have trouble
deserializing an instance of java.lang.Class?  I'm not sure if
deserializing the Class object actually requires that the class be
loaded from a classloader or not.  If it did require classloading, then
I could understand the exception, since the servlet container's
classloader wouldn't know about the class.

Thanks in advance for any help...



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to