1 45 package org.openejb.core; 46 47 import javax.ejb.EJBHome ; 48 import javax.ejb.EJBLocalHome ; 49 import javax.ejb.EJBLocalObject ; 50 import javax.ejb.TimerService ; 51 import javax.transaction.Status ; 52 53 import org.openejb.OpenEJB; 54 import org.openejb.RpcContainer; 55 import org.openejb.core.ivm.EjbObjectProxyHandler; 56 import org.openejb.util.proxy.ProxyManager; 57 58 62 public abstract class CoreContext implements java.io.Serializable { 63 64 68 public final static byte SECURITY_METHOD = (byte)1; 69 70 public final static byte USER_TRANSACTION_METHOD = (byte)2; 71 72 public final static byte ROLLBACK_METHOD = (byte)3; 73 74 public final static byte EJBOBJECT_METHOD = (byte)4; 75 76 public final static byte EJBHOME_METHOD = (byte)5; 77 81 82 CoreUserTransaction userTransaction; 83 84 85 86 public CoreContext() { 87 userTransaction = new CoreUserTransaction(OpenEJB.getTransactionManager()); 88 } 89 90 91 public abstract void checkBeanState(byte methodCategory) throws IllegalStateException ; 92 93 94 public java.security.Principal getCallerPrincipal() { 95 checkBeanState(SECURITY_METHOD); 96 Object securityIdentity = ThreadContext.getThreadContext().getSecurityIdentity(); 97 return(java.security.Principal )OpenEJB.getSecurityService().translateTo(securityIdentity, java.security.Principal .class); 98 } 99 100 public boolean isCallerInRole(java.lang.String roleName) { 101 checkBeanState(SECURITY_METHOD); 102 ThreadContext threadContext = ThreadContext.getThreadContext(); 103 org.openejb.core.DeploymentInfo di = (org.openejb.core.DeploymentInfo)threadContext.getDeploymentInfo(); 104 String physicalRoles [] = di.getPhysicalRole(roleName); 105 Object caller = threadContext.getSecurityIdentity(); 106 return OpenEJB.getSecurityService().isCallerAuthorized(caller,physicalRoles); 107 } 108 109 public EJBHome getEJBHome() { 110 checkBeanState(EJBHOME_METHOD); 111 112 ThreadContext threadContext = ThreadContext.getThreadContext(); 113 org.openejb.core.DeploymentInfo di = (org.openejb.core.DeploymentInfo)threadContext.getDeploymentInfo(); 114 115 return di.getEJBHome(); 116 } 117 118 public javax.ejb.EJBObject getEJBObject() { 119 checkBeanState(EJBOBJECT_METHOD); 120 121 123 ThreadContext threadContext = ThreadContext.getThreadContext(); 124 org.openejb.DeploymentInfo di = threadContext.getDeploymentInfo(); 125 126 EjbObjectProxyHandler handler = newEjbObjectHandler((RpcContainer)di.getContainer(), threadContext.getPrimaryKey(), di.getDeploymentID()); 127 Object newProxy = null; 128 try { 129 Class [] interfaces = new Class []{ di.getRemoteInterface(), org.openejb.core.ivm.IntraVmProxy.class }; 130 newProxy = ProxyManager.newProxyInstance( interfaces , handler ); 131 } catch ( IllegalAccessException iae ) { 132 throw new RuntimeException ("Could not create IVM proxy for "+di.getRemoteInterface()+" interface"); 133 } 134 return(javax.ejb.EJBObject )newProxy; 135 } 136 137 public EJBLocalObject getEJBLocalObject() { 138 ThreadContext threadContext = ThreadContext.getThreadContext(); 139 org.openejb.DeploymentInfo di = threadContext.getDeploymentInfo(); 140 141 EjbObjectProxyHandler handler = newEjbObjectHandler((RpcContainer)di.getContainer(), threadContext.getPrimaryKey(), di.getDeploymentID()); 142 handler.setLocal(true); 143 Object newProxy = null; 144 try { 145 Class [] interfaces = new Class []{ di.getLocalInterface(), org.openejb.core.ivm.IntraVmProxy.class }; 146 newProxy = ProxyManager.newProxyInstance( interfaces , handler ); 147 } catch ( IllegalAccessException iae ) { 148 throw new RuntimeException ("Could not create IVM proxy for "+di.getLocalInterface()+" interface"); 149 } 150 return(EJBLocalObject )newProxy; 151 } 152 153 public EJBLocalHome getEJBLocalHome() { 154 ThreadContext threadContext = ThreadContext.getThreadContext(); 155 org.openejb.core.DeploymentInfo di = (org.openejb.core.DeploymentInfo)threadContext.getDeploymentInfo(); 156 157 return di.getEJBLocalHome(); 158 } 159 public TimerService getTimerService() { 160 return null; } 162 163 public Object getPrimaryKey( ) { 164 168 checkBeanState(EJBOBJECT_METHOD); 169 170 ThreadContext threadContext = ThreadContext.getThreadContext(); 172 return threadContext.getPrimaryKey(); 173 } 174 175 public boolean getRollbackOnly() { 176 177 ThreadContext threadContext = ThreadContext.getThreadContext(); 178 org.openejb.DeploymentInfo di = threadContext.getDeploymentInfo(); 179 if ( di.isBeanManagedTransaction() ) 180 throw new IllegalStateException ("bean-managed transaction beans can not access the getRollbackOnly( ) method"); 181 182 checkBeanState(ROLLBACK_METHOD); 183 try { 184 int status = OpenEJB.getTransactionManager().getStatus(); 185 if ( status == Status.STATUS_MARKED_ROLLBACK || status == Status.STATUS_ROLLEDBACK ) 186 return true; 187 else if ( status == Status.STATUS_NO_TRANSACTION ) throw new IllegalStateException ("No current transaction"); 189 else 190 return false; 191 } catch ( javax.transaction.SystemException se ) { 192 throw new RuntimeException ("Transaction service has thrown a SystemException"); 193 } 194 } 195 196 public void setRollbackOnly() { 197 ThreadContext threadContext = ThreadContext.getThreadContext(); 198 org.openejb.DeploymentInfo di = threadContext.getDeploymentInfo(); 199 if ( di.isBeanManagedTransaction() ) 200 throw new IllegalStateException ("bean-managed transaction beans can not access the setRollbackOnly( ) method"); 201 202 checkBeanState(ROLLBACK_METHOD); 203 204 try { 205 OpenEJB.getTransactionManager().setRollbackOnly(); 206 } catch ( javax.transaction.SystemException se ) { 207 throw new RuntimeException ("Transaction service has thrown a SystemException"); 208 } 209 210 } 211 212 public javax.transaction.UserTransaction getUserTransaction() { 213 214 ThreadContext threadContext = ThreadContext.getThreadContext(); 215 org.openejb.DeploymentInfo di = threadContext.getDeploymentInfo(); 216 if ( di.isBeanManagedTransaction() ) { 217 checkBeanState(USER_TRANSACTION_METHOD); 218 return userTransaction; 219 } else 220 throw new java.lang.IllegalStateException ("container-managed transaction beans can not access the UserTransaction"); 221 } 222 223 224 225 226 227 public boolean isCallerInRole(java.security.Identity role) { 228 throw new java.lang.UnsupportedOperationException (); 229 } 230 231 public java.security.Identity getCallerIdentity() { 232 throw new java.lang.UnsupportedOperationException (); 233 } 234 235 public java.util.Properties getEnvironment() { 236 throw new java.lang.UnsupportedOperationException (); 237 } 238 239 protected abstract EjbObjectProxyHandler newEjbObjectHandler(RpcContainer container, Object pk, Object depID); 240 } | Popular Tags |