KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > marshal > serializable > SerializableUnMarshaller


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.marshal.serializable;
8
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import java.util.Map JavaDoc;
13 import org.jboss.remoting.loading.ObjectInputStreamWithClassLoader;
14 import org.jboss.remoting.marshal.UnMarshaller;
15
16 /**
17  * Will perform the deserialization of objects off the wire.
18  *
19  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
20  */

21 public class SerializableUnMarshaller implements UnMarshaller
22 {
23    static final long serialVersionUID = -1554017376768780738L;
24
25    public final static String JavaDoc DATATYPE = "serializable";
26
27    protected ClassLoader JavaDoc customClassLoader = null;
28
29    /**
30     * Reads the data from the input stream and converts to an Object.
31     * <p/>
32     * If the inputStream passed is an ObjectInputStream (which would prefer it not be), it will just
33     * use it as given. If the input stream is not an instance of ObjectInputStream, will wrap it with a
34     * custom ObjectInputStream (ObjectInputStreamWithClassLoader) and use it. The ObjectInputStreamWithClassLoader
35     * will use the custom class loader set in order to ensure that any classes not found within the local classloader
36     * can be loaded from the server and used within the client VM. If the inpustream is of type ObjectInputStreamWithClassLoader,
37     * then will just set the classloader to the custom classloader and proceed.<p>
38     *
39     * @param inputStream
40     * @param metadata
41     * @return
42     * @throws IOException
43     * @throws ClassNotFoundException
44     */

45    public Object JavaDoc read(InputStream JavaDoc inputStream, Map JavaDoc metadata) throws IOException JavaDoc, ClassNotFoundException JavaDoc
46    {
47       ObjectInputStream JavaDoc objInputStream = null;
48       Object JavaDoc obj = null;
49       if(inputStream instanceof ObjectInputStreamWithClassLoader)
50       {
51          if(((ObjectInputStreamWithClassLoader) inputStream).getClassLoader() == null)
52          {
53             ((ObjectInputStreamWithClassLoader) inputStream).setClassLoader(customClassLoader);
54          }
55          objInputStream = (ObjectInputStream JavaDoc) inputStream;
56       }
57       else if(inputStream instanceof ObjectInputStream JavaDoc)
58       {
59          objInputStream = (ObjectInputStream JavaDoc) inputStream;
60       }
61       else
62       {
63          if(customClassLoader != null)
64          {
65             objInputStream = new ObjectInputStreamWithClassLoader(inputStream, customClassLoader);
66          }
67          else
68          {
69             objInputStream = new ObjectInputStream JavaDoc(inputStream);
70          }
71       }
72
73       obj = objInputStream.readObject();
74
75       try
76       {
77          objInputStream.readObject(); // for stupid ObjectInputStream reset
78
}
79       catch(Exception JavaDoc e)
80       {
81          /**
82           * Putting try catch around this because if using servlet sever invoker, the previous
83           * call is not needed, so will throw EOFException and can ignore.
84           */

85       }
86       return obj;
87    }
88
89    /**
90     * Sets the classloader to be used when deserializing objects off the wire. This will ONLY be used in the
91     * when the input stream passed to the read() method is NOT an instance of ObjectInputStream.<p>
92     *
93     * @param classloader
94     */

95    public void setClassLoader(ClassLoader JavaDoc classloader)
96    {
97       this.customClassLoader = classloader;
98    }
99
100    public UnMarshaller cloneUnMarshaller()
101          throws CloneNotSupportedException JavaDoc
102    {
103       SerializableUnMarshaller unmarshaller = new SerializableUnMarshaller();
104       unmarshaller.setClassLoader(this.customClassLoader);
105       return unmarshaller;
106    }
107
108 }
Popular Tags