KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > ThreadContext


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: ThreadContext.java 1976 2005-07-06 02:04:04Z dblevins $
44  */

45
46
47 package org.openejb.core;
48
49
50 import org.openejb.util.FastThreadLocal;
51 import org.openejb.OpenEJB;
52
53 /**
54  * TODO: Add comment
55  */

56 public class ThreadContext implements Cloneable JavaDoc {
57
58     /**
59      * TODO: Add comment
60      */

61     protected static final FastThreadLocal threadStorage = new FastThreadLocal();
62     /**
63      * TODO: Add comment
64      */

65     protected static Class JavaDoc implClass = ThreadContext.class;
66
67     /**
68      * TODO: Add comment
69      */

70     protected boolean valid = false;
71     /**
72      * TODO: Add comment
73      */

74     protected DeploymentInfo deploymentInfo;
75     /**
76      * TODO: Add comment
77      */

78     protected Object JavaDoc primaryKey;
79     /**
80      * TODO: Add comment
81      */

82     protected byte currentOperation;
83     /**
84      * TODO: Add comment
85      */

86     protected Object JavaDoc securityIdentity;
87     /**
88      * Unspecified is any object that a customer container may want to
89      * attach to the current thread context. (e.g. CastorCMP11_EntityContainer
90      * attaches a JDO Database object to the thread.
91      */

92     protected Object JavaDoc unspecified;
93
94     static{
95         String JavaDoc className = System.getProperty(EnvProps.THREAD_CONTEXT_IMPL);
96
97         if ( className == null ) {
98             className = System.getProperty(EnvProps.THREAD_CONTEXT_IMPL);
99         }
100
101         if ( className !=null ) {
102             try {
103                 ClassLoader JavaDoc cl = OpenEJB.getContextClassLoader();
104                 implClass = Class.forName(className, true, cl);
105             } catch ( Exception JavaDoc e ) {
106                 System.out.println("Can not load ThreadContext class. org.openejb.core.threadcontext_class = "+className);
107                 e.printStackTrace();
108                 implClass = null;
109             }
110         }
111     }
112
113     protected static ThreadContext newThreadContext() {
114         try {
115             return(ThreadContext)implClass.newInstance();
116         } catch ( Exception JavaDoc e ) {
117             // this error is so serious that the system shouldn't even be running if
118
// you can't get a ThreadContext implemented.
119
e.printStackTrace();
120             throw new RuntimeException JavaDoc("ThreadContext implemenation class could not be instantiated. Class type = "+implClass+" exception message = "+e.getMessage());
121         }
122     }
123
124     public static boolean isValid() {
125         ThreadContext tc = (ThreadContext)threadStorage.get();
126         if ( tc!=null )
127             return tc.valid;
128         else
129             return false;
130     }
131
132     protected void makeInvalid() {
133         valid = false;
134         deploymentInfo = null;
135         primaryKey = null;
136         currentOperation = (byte)0;
137         securityIdentity = null;
138         unspecified = null;
139     }
140
141     public static void invalidate() {
142         ThreadContext tc = (ThreadContext)threadStorage.get();
143         if ( tc!=null )
144             tc.makeInvalid();
145     }
146
147     public static void setThreadContext(ThreadContext tc) {
148         if ( tc==null ) {
149             tc = (ThreadContext)threadStorage.get();
150             if ( tc!=null )tc.makeInvalid();
151         } else {
152             threadStorage.set(tc);
153         }
154     }
155
156     public static ThreadContext getThreadContext( ) {
157         ThreadContext tc = (ThreadContext)threadStorage.get();
158         if ( tc==null ) {
159             tc = ThreadContext.newThreadContext();
160             threadStorage.set(tc);
161         }
162         return tc;
163     }
164
165     public byte getCurrentOperation( ) {
166         return currentOperation;
167     }
168
169     public Object JavaDoc getPrimaryKey( ) {
170         return primaryKey;
171     }
172
173     public DeploymentInfo getDeploymentInfo() {
174         return deploymentInfo;
175     }
176
177     public Object JavaDoc getSecurityIdentity( ) {
178         return securityIdentity;
179     }
180
181     public Object JavaDoc getUnspecified() {
182         return unspecified;
183     }
184
185     public void set(DeploymentInfo di, Object JavaDoc primKey, Object JavaDoc securityIdentity) {
186         setDeploymentInfo(di);
187         setPrimaryKey(primKey);
188         setSecurityIdentity(securityIdentity);
189         valid = true;
190     }
191
192     public void setCurrentOperation(byte op) {
193         currentOperation = op;
194         valid = true;
195     }
196
197     public void setPrimaryKey(Object JavaDoc primKey) {
198         primaryKey = primKey;
199         valid = true;
200     }
201
202     public void setSecurityIdentity(Object JavaDoc identity) {
203         securityIdentity = identity;
204         valid = true;
205     }
206
207     public void setDeploymentInfo(DeploymentInfo info) {
208         deploymentInfo = info;
209     }
210
211     public void setUnspecified(Object JavaDoc obj) {
212         unspecified = obj;
213     }
214
215     public boolean valid() {
216         return valid;
217     }
218
219     public java.lang.Object JavaDoc clone() throws java.lang.CloneNotSupportedException JavaDoc {
220         return super.clone();
221     }
222 }
223
Popular Tags