KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > pooled > interfaces > PooledMarshalledValue


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.invocation.pooled.interfaces;
23
24 import java.io.ObjectInputStream JavaDoc;
25 import java.io.ObjectOutputStream JavaDoc;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.ObjectInput JavaDoc;
31 import java.io.ObjectOutput JavaDoc;
32 import java.util.Arrays JavaDoc;
33
34 /**
35  * A simple replacement for the RMI MarshalledObject that uses the thread
36  * context class loader for resolving classes and proxies. This currently does
37  * not support class annotations and dynamic class loading.
38  *
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 37459 $
41  */

42 public class PooledMarshalledValue
43    implements java.io.Externalizable JavaDoc
44 {
45    /** Serial Version Identifier. */
46    private static final long serialVersionUID = -1265347772036749711L;
47
48    /**
49     * The serialized form of the value. If <code>serializedForm</code> is
50     * <code>null</code> then the object marshalled was a <code>null</code>
51     * reference.
52     */

53    private byte[] serializedForm;
54    
55    /**
56     * The RMI MarshalledObject hash of the serializedForm array
57     */

58    private int hashCode;
59
60    /**
61     * Exposed for externalization.
62     */

63    public PooledMarshalledValue()
64    {
65       super();
66    }
67
68    public PooledMarshalledValue(Object JavaDoc obj) throws IOException JavaDoc
69    {
70       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
71       ObjectOutputStream JavaDoc mvos = new OptimizedObjectOutputStream(baos);
72       mvos.writeObject(obj);
73       mvos.flush();
74       serializedForm = baos.toByteArray();
75       mvos.close();
76       // Use the java.rmi.MarshalledObject hash code calculation
77
int hash = 0;
78       for (int i = 0; i < serializedForm.length; i++)
79       {
80          hash = 31 * hash + serializedForm[i];
81       }
82       
83       hashCode = hash;
84    }
85
86    public Object JavaDoc get() throws IOException JavaDoc, ClassNotFoundException JavaDoc
87    {
88       if (serializedForm == null)
89          return null;
90
91       ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(serializedForm);
92       ObjectInputStream JavaDoc mvis = new OptimizedObjectInputStream(bais);
93       Object JavaDoc retValue = mvis.readObject();
94       mvis.close();
95       return retValue;
96    }
97
98    public byte[] toByteArray()
99    {
100       return serializedForm;
101    }
102
103    public int size()
104    {
105       int size = serializedForm != null ? serializedForm.length : 0;
106       return size;
107    }
108
109    /**
110     * Return a hash code for the serialized form of the value.
111     *
112     * @return the serialized form value hash.
113     */

114    public int hashCode()
115    {
116       return hashCode;
117    }
118
119    public boolean equals(Object JavaDoc obj)
120    {
121       if( this == obj )
122          return true;
123
124       boolean equals = false;
125       if( obj instanceof PooledMarshalledValue )
126       {
127          PooledMarshalledValue mv = (PooledMarshalledValue) obj;
128          if( serializedForm == mv.serializedForm )
129          {
130             equals = true;
131          }
132          else
133          {
134             equals = Arrays.equals(serializedForm, mv.serializedForm);
135          }
136       }
137       return equals;
138    }
139    
140    /**
141     * The object implements the readExternal method to restore its
142     * contents by calling the methods of DataInput for primitive
143     * types and readObject for objects, strings and arrays. The
144     * readExternal method must read the values in the same sequence
145     * and with the same types as were written by writeExternal.
146     *
147     * @param in the stream to read data from in order to restore the object
148     *
149     * @throws IOException if I/O errors occur
150     * @throws ClassNotFoundException If the class for an object being
151     * restored cannot be found.
152     */

153    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
154    {
155       int length = in.readInt();
156       serializedForm = null;
157       if( length > 0 )
158       {
159          serializedForm = new byte[length];
160          in.readFully(serializedForm);
161       }
162       hashCode = in.readInt();
163    }
164
165    /**
166     * The object implements the writeExternal method to save its contents
167     * by calling the methods of DataOutput for its primitive values or
168     * calling the writeObject method of ObjectOutput for objects, strings,
169     * and arrays.
170     *
171     * @serialData Overriding methods should use this tag to describe
172     * the data layout of this Externalizable object.
173     * List the sequence of element types and, if possible,
174     * relate the element to a public/protected field and/or
175     * method of this Externalizable class.
176     *
177     * @param out the stream to write the object to
178     *
179     * @throws IOException Includes any I/O exceptions that may occur
180     */

181    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
182    {
183       int length = serializedForm != null ? serializedForm.length : 0;
184       out.writeInt(length);
185       if( length > 0 )
186       {
187          out.write(serializedForm);
188       }
189       out.writeInt(hashCode);
190    }
191 }
192
Popular Tags