KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > util > MarshalledValue


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.aop.util;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectInput JavaDoc;
28 import java.io.ObjectOutput JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.Arrays JavaDoc;
31
32 /**
33  * A simple replacement for the RMI MarshalledObject that uses the thread
34  * context class loader for resolving classes and proxies. This currently does
35  * not support class annotations and dynamic class loading.
36  *
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 37406 $
39  */

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

51    private byte[] serializedForm = null;
52    
53    /**
54     * The RMI MarshalledObject hash of the serializedForm array
55     */

56    private int hashCode;
57    private boolean isHashComputed = false;
58    
59    private ByteArrayOutputStream JavaDoc baos = null;
60
61    /**
62     * Exposed for externalization.
63     */

64    public MarshalledValue()
65    {
66       super();
67    }
68
69    public MarshalledValue(Object JavaDoc obj) throws IOException JavaDoc
70    {
71       baos = new ByteArrayOutputStream JavaDoc();
72       MarshalledValueOutputStream mvos = new MarshalledValueOutputStream(baos);
73       mvos.writeObject(obj);
74       mvos.flush();
75
76       serializedForm = baos.toByteArray();
77       
78       isHashComputed = false;
79    }
80
81    public Object JavaDoc get() throws IOException JavaDoc, ClassNotFoundException JavaDoc
82    {
83       if (serializedForm == null)
84          return null;
85
86       ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(serializedForm);
87       MarshalledValueInputStream mvis = new MarshalledValueInputStream(bais);
88       return mvis.readObject();
89    }
90
91    public byte[] toByteArray()
92    {
93       return serializedForm;
94    }
95
96    public int size()
97    {
98       int size = serializedForm != null ? serializedForm.length : 0;
99       return size;
100    }
101
102    /**
103     * Return a hash code for the serialized form of the value.
104     *
105     * @return the serialized form value hash.
106     */

107    public int hashCode()
108    {
109       // lazy computing of hash: we don't need it most of the time
110
//
111
if (!isHashComputed)
112       {
113          int hash = 0;
114          for (int i = 0; i < serializedForm.length; i++)
115          {
116             hash = 31 * hash + serializedForm[i];
117          }
118          
119          hashCode = hash;
120       }
121
122       return hashCode;
123    }
124
125    public boolean equals(Object JavaDoc obj)
126    {
127       if( this == obj )
128          return true;
129
130       boolean equals = false;
131       if( obj instanceof MarshalledValue )
132       {
133          MarshalledValue mv = (MarshalledValue) obj;
134          if( serializedForm == mv.serializedForm )
135          {
136             equals = true;
137          }
138          else
139          {
140             equals = Arrays.equals(serializedForm, mv.serializedForm);
141          }
142       }
143       return equals;
144    }
145    
146    /**
147     * The object implements the readExternal method to restore its
148     * contents by calling the methods of DataInput for primitive
149     * types and readObject for objects, strings and arrays. The
150     * readExternal method must read the values in the same sequence
151     * and with the same types as were written by writeExternal.
152     *
153     * @param in the stream to read data from in order to restore the object
154     *
155     * @throws java.io.IOException if I/O errors occur
156     * @throws java.lang.ClassNotFoundException If the class for an object being
157     * restored cannot be found.
158     */

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

187    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
188    {
189       out.writeInt(baos.size());
190       baos.writeTo((OutputStream JavaDoc)out);
191    }
192 }
193
Popular Tags