1 45 46 47 package org.openejb.core; 48 49 50 import org.openejb.util.FastThreadLocal; 51 import org.openejb.OpenEJB; 52 53 56 public class ThreadContext implements Cloneable { 57 58 61 protected static final FastThreadLocal threadStorage = new FastThreadLocal(); 62 65 protected static Class implClass = ThreadContext.class; 66 67 70 protected boolean valid = false; 71 74 protected DeploymentInfo deploymentInfo; 75 78 protected Object primaryKey; 79 82 protected byte currentOperation; 83 86 protected Object securityIdentity; 87 92 protected Object unspecified; 93 94 static{ 95 String 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 cl = OpenEJB.getContextClassLoader(); 104 implClass = Class.forName(className, true, cl); 105 } catch ( Exception 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 e ) { 117 e.printStackTrace(); 120 throw new RuntimeException ("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 getPrimaryKey( ) { 170 return primaryKey; 171 } 172 173 public DeploymentInfo getDeploymentInfo() { 174 return deploymentInfo; 175 } 176 177 public Object getSecurityIdentity( ) { 178 return securityIdentity; 179 } 180 181 public Object getUnspecified() { 182 return unspecified; 183 } 184 185 public void set(DeploymentInfo di, Object primKey, Object 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 primKey) { 198 primaryKey = primKey; 199 valid = true; 200 } 201 202 public void setSecurityIdentity(Object 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 obj) { 212 unspecified = obj; 213 } 214 215 public boolean valid() { 216 return valid; 217 } 218 219 public java.lang.Object clone() throws java.lang.CloneNotSupportedException { 220 return super.clone(); 221 } 222 } 223 | Popular Tags |