1 25 26 package org.objectweb.easybeans.container; 27 28 29 import static javax.ejb.TransactionManagementType.BEAN ; 30 import static javax.ejb.TransactionManagementType.CONTAINER ; 31 32 import java.io.Serializable ; 33 import java.lang.reflect.InvocationTargetException ; 34 import java.lang.reflect.Method ; 35 import java.security.Identity ; 36 import java.security.Principal ; 37 import java.util.Collection ; 38 import java.util.Date ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Properties ; 42 43 import javax.ejb.EJBContext ; 44 import javax.ejb.EJBException ; 45 import javax.ejb.EJBHome ; 46 import javax.ejb.EJBLocalHome ; 47 import javax.ejb.Timer ; 48 import javax.ejb.TimerService ; 49 import javax.ejb.TransactionManagementType ; 50 import javax.naming.InitialContext ; 51 import javax.naming.NamingException ; 52 import javax.transaction.Status ; 53 import javax.transaction.SystemException ; 54 import javax.transaction.TransactionManager ; 55 import javax.transaction.UserTransaction ; 56 57 import org.objectweb.easybeans.api.Factory; 58 import org.objectweb.easybeans.api.bean.EasyBeansBean; 59 import org.objectweb.easybeans.api.container.EZBEJBContext; 60 import org.objectweb.easybeans.log.JLog; 61 import org.objectweb.easybeans.log.JLogFactory; 62 import org.objectweb.easybeans.security.propagation.context.SecurityCurrent; 63 import org.objectweb.easybeans.transaction.JTransactionManager; 64 65 71 public class EasyBeansEJBContext<BeanType extends EasyBeansBean> implements EZBEJBContext<BeanType>, EJBContext { 72 73 76 private JLog logger = JLogFactory.getLog(EasyBeansEJBContext.class); 77 78 81 private static final String JAVA_COMP_ENV = "java:comp/env/"; 82 83 86 private TransactionManager transactionManager = null; 87 88 91 private TransactionManagementType transactionManagementType = null; 92 93 96 private boolean runAsBean = false; 97 98 101 private Factory easyBeansFactory = null; 102 103 107 public EasyBeansEJBContext(final Factory easyBeansFactory) { 108 this.easyBeansFactory = easyBeansFactory; 109 this.transactionManagementType = easyBeansFactory.getBeanInfo().getTransactionManagementType(); 110 this.runAsBean = easyBeansFactory.getBeanInfo().getSecurityInfo().getRunAsRole() != null; 111 112 this.transactionManager = JTransactionManager.getTransactionManager(); 113 } 114 115 121 public EJBHome getEJBHome() throws IllegalStateException { 122 throw new IllegalStateException ("No Home"); 123 } 124 125 131 public EJBLocalHome getEJBLocalHome() throws IllegalStateException { 132 throw new IllegalStateException ("No Local Home"); 133 } 134 135 142 @Deprecated 143 public Properties getEnvironment() { 144 throw new UnsupportedOperationException (); 145 } 146 147 154 @Deprecated 155 public Identity getCallerIdentity() { 156 throw new UnsupportedOperationException (); 157 } 158 159 164 public Principal getCallerPrincipal() { 165 return SecurityCurrent.getCurrent().getSecurityContext().getCallerPrincipal(runAsBean); 166 } 167 168 175 @Deprecated 176 public boolean isCallerInRole(final Identity role) { 177 throw new UnsupportedOperationException (); 178 } 179 180 186 public boolean isCallerInRole(final String roleName) { 187 188 190 boolean foundItem= false; 191 List <String > declaredRoles = easyBeansFactory.getBeanInfo().getSecurityInfo().getDeclaredRoles(); 192 if (declaredRoles == null || !declaredRoles.contains(roleName)) { 193 logger.debug("No security-role with role name ''{0}'' was declared for the bean ''{1}''", roleName, easyBeansFactory 194 .getBeanInfo().getName()); 195 return false; 196 } 197 198 boolean inRole = getBean().getEasyBeansFactory().getContainer().getPermissionManager().isCallerInRole(easyBeansFactory 200 .getBeanInfo().getName(), roleName, runAsBean); 201 202 return inRole; 203 } 204 205 218 public UserTransaction getUserTransaction() throws IllegalStateException { 219 if (transactionManagementType == CONTAINER) { 220 throw new IllegalStateException ("This bean is not allowed to use getUserTransaction() " 221 + " method as it is in ContainerManagedTransaction"); 222 } 223 return (UserTransaction ) transactionManager; 224 } 225 226 235 public void setRollbackOnly() throws IllegalStateException { 236 if (transactionManagementType == BEAN) { 237 throw new IllegalStateException ("This bean is not allowed to use setRollbackOnly() " 238 + " method as it is in BeanManagedTransaction"); 239 } 240 241 try { 243 if (transactionManager.getTransaction() == null) { 244 throw new IllegalStateException ("Cannot use setRollbackOnly() outside transaction"); 245 } 246 } catch (SystemException e) { 247 throw new IllegalStateException ("Cannot get transaction on transaction manager", e); 248 } 249 250 251 try { 252 transactionManager.setRollbackOnly(); 253 } catch (SystemException e) { 254 throw new RuntimeException ("setRollbackOnly() raised an unexpected exception:", e); 255 } 256 } 257 258 270 public boolean getRollbackOnly() throws IllegalStateException { 271 if (transactionManagementType == BEAN) { 272 throw new IllegalStateException ("This bean is not allowed to use getRollbackOnly() " 273 + " method as it is in BeanManagedTransaction"); 274 } 275 try { 276 switch (transactionManager.getStatus()) { 277 case Status.STATUS_MARKED_ROLLBACK: 278 case Status.STATUS_ROLLING_BACK: 279 return true; 280 case Status.STATUS_ACTIVE: 281 case Status.STATUS_COMMITTING: 282 case Status.STATUS_PREPARED: 283 case Status.STATUS_PREPARING: 284 return false; 285 case Status.STATUS_ROLLEDBACK: 286 throw new IllegalStateException ("Transaction already rolled back"); 287 case Status.STATUS_COMMITTED: 288 throw new IllegalStateException ("Transaction already committed"); 289 case Status.STATUS_NO_TRANSACTION: 290 case Status.STATUS_UNKNOWN: 291 throw new IllegalStateException ("Cannot getRollbackOnly outside transaction"); 292 default: 293 throw new IllegalStateException ("Invalid status"); 294 } 295 } catch (SystemException e) { 296 throw new IllegalStateException ("Cannot get transaction status", e); 297 } 298 } 299 300 307 public TimerService getTimerService() throws IllegalStateException { 308 return new DummyTimerService(); 309 } 310 311 314 @Override 315 public String toString() { 316 StringBuilder sb = new StringBuilder (); 317 sb.append(this.getClass().getName().substring(this.getClass().getPackage().getName().length() + 1)); 319 return sb.toString(); 320 } 321 322 327 public Object lookup(final String name) { 328 try { 330 return new InitialContext ().lookup(JAVA_COMP_ENV + name); 331 } catch (NamingException ne) { 332 try { 334 return new InitialContext ().lookup(name); 335 } catch (NamingException e) { 336 throw new IllegalArgumentException ("Lookup on '" + name + "' was not found"); 337 } 338 } 339 } 340 341 342 346 public BeanType getBean() { 347 return null; 348 } 349 350 354 class DummyTimerService implements TimerService { 355 356 380 public Timer createTimer(final Date initialDuration, final long intervalDuration, final Serializable info) 381 throws IllegalArgumentException , IllegalStateException , EJBException { 382 throw new IllegalStateException ("Not yet implemented"); 383 } 384 385 399 public Timer createTimer(final Date expiration, final Serializable info) throws IllegalArgumentException , 400 IllegalStateException , EJBException { 401 throw new IllegalStateException ("Not yet implemented"); 402 } 403 404 428 public Timer createTimer(final long initialDuration, final long intervalDuration, final Serializable info) 429 throws IllegalArgumentException , IllegalStateException , EJBException { 430 throw new IllegalStateException ("Not yet implemented"); 431 } 432 433 447 public Timer createTimer(final long duration, final Serializable info) throws IllegalArgumentException , 448 IllegalStateException , EJBException { 449 throw new IllegalStateException ("Not yet implemented"); 450 } 451 452 461 public Collection getTimers() throws IllegalStateException , EJBException { 462 throw new IllegalStateException ("Not yet implemented"); 463 } 464 465 } 466 } 467 | Popular Tags |