KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > ivm > IntraVmArtifact


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: IntraVmArtifact.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.core.ivm;
46
47 import java.io.Externalizable JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.InvalidObjectException JavaDoc;
50 import java.io.ObjectInput JavaDoc;
51 import java.io.ObjectOutput JavaDoc;
52 import java.io.ObjectStreamException JavaDoc;
53 import java.util.ArrayList JavaDoc;
54 import java.util.List JavaDoc;
55
56 import org.openejb.util.FastThreadLocal;
57
58 /**
59  * This class represents all artifacts of the IntraVM in a stream.
60  * <p>
61  * Classes create this object in the writeReplace method.
62  * <p>
63  * When the object is serialized, the writeReplace method is invoked
64  * and this artifact is written to the stream instead. The original
65  * object instance is placed in a HashMap and not serialized.
66  * <p>
67  * During deserialization, it is this object that is deserialized.
68  * This class implements the readResolve method of the serialization API.
69  * In the readResolve method, the original object instance is retrieved
70  * from the HashMap and returned instead.
71  * <p>
72  * <P>
73  * Basically, we mark all local serialization operations the same
74  * way you would mark a transaction.
75  * <p>
76  * <h2><b>LOCAL to LOCAL SERIALIZATION</b></h2> <p>
77  *
78  * <i>Definition:</i><p>
79  * This is a full serialization/deserialization takes place in
80  * the local vm inside the marked scope of the IntraVM server.
81  * <p>
82  * <i>Circumstances:</i><p>
83  * When an IntraVM implementation of a javax.ejb.* interface is
84  * serialized in the scope of a local IntraVM serialization.
85  * <p>
86  * These serializations happen when objects are passed as
87  * parameters or return values from one client/ejb to another
88  * client/ejb running inside the same VM.
89  * <p>
90  * <i>Action:</i><p>
91  * Temporarily cache the instance in memory during
92  * serialization, retrieve again during deserialization.
93  * <p>
94  * <i>Example Scenario:</i><p>
95  * BEFORE SERIALIZATION<br>
96  * <br>1. Call IntraVmCopyMonitor.preCopyOperation().
97  * <br>2. Method parameters are sent to ObjectOutputStream.
98  *
99  * <p>SERIALIZATION<br>
100  * <br>3. ObjectOutputStream encounters an IntraVmMetaData instance
101  * in the object graph and calls its writeReplace method.
102  * <br>4. The IntraVmMetaData instance determines it is being
103  * serialized in the scope of an IntraVM serialization by
104  * calling IntraVmCopyMonitor.isIntraVmCopyOperation().
105  * <br>5. The IntraVmMetaData instance creates an IntraVmArtifact
106  * that caches it in a static hashtable keyed on a
107  * combination of the thread id and instance hashCode.
108  * <br>6. The IntraVmMetaData instance returns the IntraVmArtifact
109  * instance from the writeReplace method.
110  * <br>7. The ObjectOutputStream serializes the IntraVmArtifact
111  * instance in place of the IntraVmMetaData instance.
112  * <P> DESERIALIZATION<br>
113  * <br>8. ObjectInputStream encounters and deserializes an
114  * IntraVmArtifact instance and calls its readResolve method.
115  * <br>9. The IntraVmArtifact instance uses the key it created in
116  * step 5 to retrieve the IntraVmMetaData instance from the
117  * static hashtable.
118  * <br>10. The IntraVmArtifact instance returns the IntraVmMetaData
119  * instance from the readResolve method.
120  * <br>11. ObjectInputStream places the IntraVmMetaData instance in
121  * the object graph in place of the IntraVmArtifact
122  * instance.
123  * <P>AFTER<br>
124  * <br>12. Method parameters are now de-referenced as mandated by the
125  * spec and can be passed into the bean's method.
126  * <br>13. IntraVmCopyMonitor.postCopyOperation() is called, ending
127  * the local IntraVm serialization scope.
128  * <p>
129  *
130  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
131  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
132  */

133 public class IntraVmArtifact implements Externalizable JavaDoc {
134
135     /**
136      * A handle created using information about the object
137      * instance for which this IntraVMArtifact was created.
138      */

139     private int instanceHandle;
140     
141     /**
142      * Holds a list of threads. Each thread gets a HashMap to store
143      * instances artifacts of the intra-vm. The instances are not serialized,
144      * instead, a key for the object is serialized to the stream.
145      *
146      * At deserialization, the key is used to get the original object
147      * instance from the List
148      */

149     private static FastThreadLocal thread = new FastThreadLocal();
150     
151     /**
152      * Error detailing that the List for this Thread can not be found.
153      */

154     private static final String JavaDoc NO_MAP_ERROR = "There is no HashMap stored in the thread. This object may have been moved outside the Virtual Machine.";
155     
156     /**
157      * Error detailing that the object instance can not be found in the thread's List.
158      */

159     private static final String JavaDoc NO_ARTIFACT_ERROR = "The artifact this object represents could not be found.";
160
161     /**
162      * Used to creat an IntraVmArtifact object that can represent
163      * the true intra-vm artifact in a stream.
164      *
165      * @param obj The object instance this class should represent in the stream.
166      */

167     public IntraVmArtifact(Object JavaDoc obj) {
168         // the prev implementation used a hash map and removed the handle in the readResolve method
169
// which would prevent marshaling of objects with the same hashcode in one request.
170
List JavaDoc list = (List JavaDoc)thread.get();
171         if (list == null) {
172             list = new ArrayList JavaDoc();
173             thread.set(list);
174         }
175         instanceHandle = list.size();
176         list.add(obj);
177             }
178
179     /**
180      * This class is Externalizable and this public, no-arg, constructor is required.
181      *
182      * This constructor should only be used by the deserializing stream.
183      */

184     public IntraVmArtifact() {
185     }
186
187     /**
188      * Writes the instanceHandle to the stream.
189      *
190      * @param out
191      * @exception IOException
192      */

193     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc{
194         out.write(instanceHandle);
195     }
196
197     /**
198      * Reads the instanceHandle from the stream
199      *
200      * @param in
201      * @exception IOException
202      */

203     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc{
204         instanceHandle = in.read();
205     }
206
207     /**
208      * During deserialization, it is this object that is deserialized.
209      * This class implements the readResolve method of the serialization API.
210      * In the readResolve method, the original object instance is retrieved
211      * from the List and returned instead.
212      *
213      * @return Object
214      * @exception ObjectStreamException
215      */

216     private Object JavaDoc readResolve() throws ObjectStreamException JavaDoc{
217         List JavaDoc list = (List JavaDoc) thread.get();
218         if (list == null) throw new InvalidObjectException JavaDoc(NO_MAP_ERROR);
219         Object JavaDoc artifact = list.get(instanceHandle);
220         if (artifact == null) throw new InvalidObjectException JavaDoc(NO_ARTIFACT_ERROR+instanceHandle);
221         if(list.size()==instanceHandle+1) {
222             list.clear();
223         }
224         return artifact;
225     }
226
227 }
228
Popular Tags