1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.security.Identity ; 29 import java.security.Principal ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Properties ; 33 34 import javax.ejb.EJBContext ; 35 import javax.ejb.EJBHome ; 36 import javax.ejb.EJBLocalHome ; 37 import javax.ejb.EnterpriseBean ; 38 import javax.ejb.TimerService ; 39 import javax.resource.spi.work.WorkManager ; 40 import javax.transaction.Status ; 41 import javax.transaction.SystemException ; 42 import javax.transaction.UserTransaction ; 43 44 import org.objectweb.transaction.jta.TransactionManager; 45 46 import org.objectweb.jonas_lib.deployment.api.SecurityRoleRefDesc; 47 48 import org.objectweb.util.monolog.api.BasicLevel; 49 50 56 public abstract class JContext implements EJBContext { 57 58 protected EnterpriseBean instance; 59 protected final JFactory bf; 60 protected final TransactionManager tm; 61 private final JHome home; 62 private final JLocalHome localhome; 63 private final JContainer cont; 64 65 68 private PermissionManager permissionManager = null; 69 70 71 75 int instanceState = 0; 76 77 81 86 protected JContext(JFactory bf, EnterpriseBean i) { 87 this.bf = bf; 88 this.instance = i; 89 if (i == null) { 90 TraceEjb.logger.log(BasicLevel.ERROR, "null EnterpriseBean!"); 91 } 92 this.home = bf.getHome(); 93 this.localhome = bf.getLocalHome(); 94 this.cont = bf.getContainer(); 95 this.tm = bf.getTransactionManager(); 96 this.permissionManager = cont.getPermissionManager(); 97 98 } 99 100 103 public void setState(int newState) { 104 instanceState = newState; 105 if (TraceEjb.isDebugContext()) { 106 TraceEjb.context.log(BasicLevel.DEBUG, "" + instanceState); 107 } 108 } 109 110 114 public int getState() { 115 if (TraceEjb.isDebugContext()) { 116 TraceEjb.context.log(BasicLevel.DEBUG, "" + instanceState); 117 } 118 return instanceState; 119 } 120 121 125 128 public WorkManager getWorkManager() { 129 return bf.getWorkManager(); 130 } 131 132 136 141 public Identity getCallerIdentity() { 142 throw new RuntimeException ("getCallerIdentity() method deprecated. use instead getCallerPrincipal()"); 143 } 144 145 146 152 public Principal getCallerPrincipal() throws IllegalStateException { 153 154 if (getState() == 0) { 155 throw new IllegalStateException ("the instance is not allowed to call this method"); 156 } 157 boolean inRunAs = false; 158 if (bf.dd.getRunAsRole() != null) { 159 inRunAs = true; 160 } 161 162 Principal principal = cont.getPrincipalFactory().getCallerPrincipal(inRunAs); 163 if (principal == null) { 164 throw new IllegalStateException ("no security context exists"); 165 } 166 return principal; 167 168 } 169 170 175 public EJBHome getEJBHome() throws IllegalStateException { 176 return home; 177 } 178 179 184 public EJBLocalHome getEJBLocalHome() throws IllegalStateException { 185 if (!bf.dd.hasDefinedLocalInterface()) { 186 TraceEjb.logger.log(BasicLevel.ERROR, "No Local Interface declared for this bean"); 187 throw new IllegalStateException ("No Local Interface declared for this bean"); 188 } 189 return localhome; 190 } 191 192 201 public Properties getEnvironment() { 202 if (TraceEjb.isDebugIc()) { 203 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 204 } 205 return bf.getEjb10Environment(); 206 } 207 208 213 public boolean getRollbackOnly() throws IllegalStateException { 214 if (TraceEjb.isDebugIc()) { 215 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 216 } 217 218 if (getState() == 0) { 219 throw new IllegalStateException ("the instance is not allowed to call this method"); 220 } 221 222 try { 223 switch (tm.getStatus()) { 224 case Status.STATUS_MARKED_ROLLBACK: 225 case Status.STATUS_ROLLING_BACK: 226 return true; 227 case Status.STATUS_ACTIVE: 228 case Status.STATUS_COMMITTING: 229 case Status.STATUS_PREPARED: 230 case Status.STATUS_PREPARING: 231 return false; 232 case Status.STATUS_ROLLEDBACK: 233 throw new IllegalStateException ("Transaction already rolled back"); 234 case Status.STATUS_COMMITTED: 235 throw new IllegalStateException ("Transaction already committed"); 236 case Status.STATUS_NO_TRANSACTION: 237 case Status.STATUS_UNKNOWN: 238 throw new IllegalStateException ("Cannot getRollbackOnly outside transaction"); 239 } 240 } catch (SystemException e) { 241 TraceEjb.logger.log(BasicLevel.ERROR, "cannot get transaction status:", e); 242 throw new IllegalStateException ("Cannot get transaction status"); 243 } 244 return true; 245 } 246 247 253 public abstract TimerService getTimerService() throws IllegalStateException ; 254 255 263 public UserTransaction getUserTransaction() throws IllegalStateException { 264 265 if (TraceEjb.isDebugIc()) { 266 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 267 } 268 269 if (!bf.isTxBeanManaged()) { 270 throw new IllegalStateException ("This bean is not allowed to use UserTransaction interface"); 271 } 272 if (getState() == 0) { 273 throw new IllegalStateException ("the instance is not allowed to call this method"); 274 } 275 return (UserTransaction ) tm; 276 } 277 278 284 public boolean isCallerInRole(Identity role) { 285 throw new RuntimeException ("isCallerInRole(Identity) method deprecated. use instead isCallerInRole(String)"); 286 } 287 288 297 public boolean isCallerInRole(String roleName) throws IllegalStateException { 298 if (TraceEjb.isDebugSecurity()) { 299 TraceEjb.security.log(BasicLevel.DEBUG, ""); 300 } 301 302 if (getState() == 0) { 303 throw new IllegalStateException ("the instance is not allowed to call this method"); 304 } 305 306 309 List list = bf.dd.getSecurityRoleRefDescList(); 311 312 if (list == null) { 313 TraceEjb.logger.log(BasicLevel.WARN, "EJB 2.1 spec, Chapter 21 : 21.2.5.2 : No security-role-ref list. Invalid usage of isCallerInRole without security-role-ref elements."); 314 return false; 315 } 316 boolean foundItem = false; 317 Iterator it = bf.dd.getSecurityRoleRefDescList().iterator(); 318 String tmpRoleName = null; 319 SecurityRoleRefDesc sRoleRefDesc = null; 320 while (!foundItem && it.hasNext()) { 321 sRoleRefDesc = (SecurityRoleRefDesc) it.next(); 322 tmpRoleName = sRoleRefDesc.getRoleName(); 323 if (tmpRoleName.equals(roleName)) { 324 foundItem = true; 325 } 326 } 327 328 if (!foundItem) { 329 if (TraceEjb.isDebugSecurity()) { 330 TraceEjb.security.log(BasicLevel.DEBUG, "No security-role-ref with role name '" + roleName 331 + "' was found in the deployment descriptor of bean '" 332 + bf.getEJBName() + "."); 333 } 334 return false; 335 } 336 337 boolean inRunAs = false; 338 if (bf.dd.getRunAsRole() != null) { 339 inRunAs = true; 340 } 341 boolean inRole = permissionManager.isCallerInRole(bf.getEJBName(), roleName, inRunAs); 343 344 if (TraceEjb.isDebugSecurity()) { 345 TraceEjb.security.log(BasicLevel.DEBUG, "isCallerInRole: " + inRole); 346 } 347 return inRole; 348 349 } 350 351 356 public void setRollbackOnly() throws IllegalStateException { 357 if (TraceEjb.isDebugIc()) { 358 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 359 } 360 361 getRollbackOnly(); 365 366 try { 367 tm.setRollbackOnly(); 368 } catch (IllegalStateException e) { 369 TraceEjb.logger.log(BasicLevel.ERROR, "current thread not associated with transaction"); 370 throw e; 371 } catch (SystemException e) { 372 TraceEjb.logger.log(BasicLevel.ERROR, "setRollbackOnly unexpected exception:", e); 373 } 374 } 375 376 } 377 | Popular Tags |