KickJava   Java API By Example, From Geeks To Geeks.

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


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: IntraVmCopyMonitor.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.core.ivm;
46
47 import org.openejb.util.FastThreadLocal;
48
49 /**
50  * This class is used to demarcate intra-VM copy operations so
51  * that intra-VM artifacts such as IntraVmHandle, IntraVmMetaData,
52  * and BaseEjbProxyHandlers (EjbHomeProxyHandler and EjbObjectProxyHandler)
53  * can know when they should replace themselves during serialization
54  * with an IntraVmArtifact or a application server specific artifact.
55  * <P>
56  * Basically, we mark all local serialization operations the same
57  * way you would mark a transaction.
58  * <p>
59  * <h2><b>LOCAL to LOCAL SERIALIZATION</b></h2> <p>
60  *
61  * <i>Definition:</i><p>
62  * This is a full serialization/deserialization takes place in
63  * the local vm inside the marked scope of the IntraVM server.
64  * <p>
65  * <i>Circumstances:</i><p>
66  * When an IntraVM implementation of a javax.ejb.* interface is
67  * serialized in the scope of a local IntraVM serialization.
68  * <p>
69  * These serializations happen when objects are passed as
70  * parameters or return values from one client/ejb to another
71  * client/ejb running inside the same VM.
72  * <p>
73  * <i>Action:</i><p>
74  * Temporarily cache the instance in memory during
75  * serialization, retrieve again during deserialization.
76  * <p>
77  * <i>Example Scenario:</i><p>
78  * BEFORE SERIALIZATION<br>
79  * <br>1. Call IntraVmCopyMonitor.preCopyOperation().
80  * <br>2. Method parameters are sent to ObjectOutputStream.
81  *
82  * <p>SERIALIZATION<br>
83  * <br>3. ObjectOutputStream encounters an IntraVmMetaData instance
84  * in the object graph and calls its writeReplace method.
85  * <br>4. The IntraVmMetaData instance determines it is being
86  * serialized in the scope of an IntraVM serialization by
87  * calling IntraVmCopyMonitor.isIntraVmCopyOperation().
88  * <br>5. The IntraVmMetaData instance creates an IntraVmArtifact
89  * that caches it in a static hashtable keyed on a
90  * combination of the thread id and instance hashCode.
91  * <br>6. The IntraVmMetaData instance returns the IntraVmArtifact
92  * instance from the writeReplace method.
93  * <br>7. The ObjectOutputStream serializes the IntraVmArtifact
94  * instance in place of the IntraVmMetaData instance.
95  * <P> DESERIALIZATION<br>
96  * <br>8. ObjectInputStream encounters and deserializes an
97  * IntraVmArtifact instance and calls its readResolve method.
98  * <br>9. The IntraVmArtifact instance uses the key it created in
99  * step 5 to retrieve the IntraVmMetaData instance from the
100  * static hashtable.
101  * <br>10. The IntraVmArtifact instance returns the IntraVmMetaData
102  * instance from the readResolve method.
103  * <br>11. ObjectInputStream places the IntraVmMetaData instance in
104  * the object graph in place of the IntraVmArtifact
105  * instance.
106  * <P>AFTER<br>
107  * <br>12. Method parameters are now de-referenced as mandated by the
108  * spec and can be passed into the bean's method.
109  * <br>13. IntraVmCopyMonitor.postCopyOperation() is called, ending
110  * the local IntraVm serialization scope.
111  * <p>
112  *
113  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
114  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
115  * @version $Revision: 1921 $ $Date: 2005-06-19 15:40:34 -0700 (Sun, 19 Jun 2005) $
116  */

117 public class IntraVmCopyMonitor {
118     
119     private static FastThreadLocal threadStorage = new FastThreadLocal();
120     
121     /**
122      * Set to true if the current operation is an
123      * IntraVM copy.
124      */

125     boolean intraVmCopyOperation = false;
126     
127     /**
128      * Set to true if the current operation is the
129      * passivation of a stateful session bean.
130      */

131     boolean statefulPassivationOperation = false;
132     
133     /**
134      */

135     IntraVmCopyMonitor(){}
136         
137     public static boolean exists(){
138         return (threadStorage.get()!=null);
139     }
140
141     /**
142      * Not to be used lightly. This class is more performant if
143      * every thread has a ThreadContext set only use this method
144      * if the thread is never going to re-access the container.
145      */

146     public static void release( ){
147         threadStorage.set(null);
148     }
149
150
151     /**
152      * Returns the IntraVmCopyMonitor singleton.
153      *
154      * If the IntraVmCopyMonitor has not already been
155      * create, it is instantiated.
156      *
157      * @return IntraVmCopyMonitor
158      */

159     static IntraVmCopyMonitor getMonitor( ){
160         IntraVmCopyMonitor monitor = (IntraVmCopyMonitor)threadStorage.get();
161         if(monitor==null){
162             monitor = new IntraVmCopyMonitor();
163             threadStorage.set(monitor);
164         }
165         return monitor;
166     }
167
168     /**
169      * Notifies the monitor for this thread just before a
170      * copy operation is to take place.
171      *
172      * This happens when one bean access another bean and
173      * arguments or return values are copied.
174      */

175     public static void preCopyOperation(){
176         IntraVmCopyMonitor monitor = getMonitor();
177         monitor.intraVmCopyOperation = true;
178     }
179     /**
180      * Notifies the monitor for this thread just after a
181      * copy operation has taken place.
182      *
183      * This happens when one bean access another bean and
184      * arguments or return values are copied.
185      */

186     public static void postCopyOperation(){
187         IntraVmCopyMonitor monitor = getMonitor();
188         monitor.intraVmCopyOperation = false;
189     }
190     /**
191      * Notifies the monitor for this thread just before a
192      * stateful session bean is passified.
193      *
194      * This happens when a stateful session bean is
195      * passified and all its member variables are serialized.
196      */

197     public static void prePassivationOperation(){
198         IntraVmCopyMonitor monitor = getMonitor();
199         monitor.statefulPassivationOperation = true;
200     }
201     /**
202      * Notifies the monitor for this thread just after a
203      * stateful session bean is passified.
204      *
205      * This happens when a stateful session bean is
206      * passified and all its member variables are serialized.
207      */

208     public static void postPassivationOperation(){
209         IntraVmCopyMonitor monitor = getMonitor();
210         monitor.statefulPassivationOperation = false;
211     }
212     /**
213      * Returns true if the current operation is an
214      * IntraVM copy.
215      *
216      * @return boolean
217      */

218     public static boolean isIntraVmCopyOperation(){
219         IntraVmCopyMonitor monitor = getMonitor();
220         if(monitor.intraVmCopyOperation)
221             return true;
222         else
223             return false;
224     }
225     /**
226      * Returns true if the current operation is the
227      * passivation of a stateful session bean.
228      *
229      * @return boolean
230      */

231     public static boolean isStatefulPassivationOperation(){
232         IntraVmCopyMonitor monitor = getMonitor();
233         if(monitor.statefulPassivationOperation)
234             return true;
235         else
236             return false;
237     }
238 }
239
Popular Tags