1 22 package org.jboss.ejb3; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.lang.reflect.InvocationHandler ; 29 import java.security.Identity ; 30 import java.security.Principal ; 31 import java.util.HashSet ; 32 import java.util.Properties ; 33 import javax.ejb.EJBException ; 34 import javax.ejb.EJBHome ; 35 import javax.ejb.EJBLocalHome ; 36 import javax.ejb.EJBLocalObject ; 37 import javax.ejb.EJBObject ; 38 import javax.ejb.MessageDrivenContext ; 39 import javax.ejb.SessionContext ; 40 import javax.ejb.TimerService ; 41 import javax.ejb.TransactionManagementType ; 42 import javax.naming.InitialContext ; 43 import javax.naming.NamingException ; 44 import javax.transaction.Status ; 45 import javax.transaction.SystemException ; 46 import javax.transaction.TransactionManager ; 47 import javax.transaction.UserTransaction ; 48 import javax.xml.rpc.handler.MessageContext ; 49 import org.jboss.annotation.security.SecurityDomain; 50 import org.jboss.aop.Advisor; 51 import org.jboss.ejb3.tx.TxUtil; 52 import org.jboss.ejb3.tx.UserTransactionImpl; 53 import org.jboss.logging.Logger; 54 import org.jboss.security.RealmMapping; 55 import org.jboss.security.RunAsIdentity; 56 import org.jboss.security.SecurityAssociation; 57 import org.jboss.security.SimplePrincipal; 58 import org.jboss.util.NestedRuntimeException; 59 60 66 public class BaseSessionContext implements SessionContext , MessageDrivenContext , Externalizable 67 { 68 private static final Logger log = Logger.getLogger(BaseSessionContext.class); 69 protected transient Container container; 70 protected transient RealmMapping rm; 71 protected BaseContext baseContext; 72 73 public BaseSessionContext() 74 { 75 } 76 77 public void setBaseContext(BaseContext baseContext) 78 { 79 this.baseContext = baseContext; 80 } 81 82 public Container getContainer() 83 { 84 return container; 85 } 86 87 public void setContainer(Container container) 88 { 89 this.container = container; 90 try 91 { 92 InitialContext ctx = container.getInitialContext(); 93 setupSecurityDomain(container, ctx); 94 } 95 catch (NamingException e) 96 { 97 throw new RuntimeException (e); 98 } 99 } 100 101 private void setupSecurityDomain(Container container, InitialContext ctx) 102 throws NamingException 103 { 104 SecurityDomain securityAnnotation = (SecurityDomain) ((Advisor) container).resolveAnnotation(SecurityDomain.class); 105 if (securityAnnotation == null) return; 106 Object domain = ctx.lookup("java:/jaas/" + securityAnnotation.value()); 107 rm = (RealmMapping) domain; 108 } 109 110 protected RealmMapping getRm() 111 { 112 return rm; 113 } 114 115 public void writeExternal(ObjectOutput out) throws IOException 116 { 117 out.writeUTF(container.getObjectName().getCanonicalName()); 118 } 119 120 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 121 { 122 container = Ejb3Registry.getContainer(in.readUTF()); 123 InitialContext ctx = container.getInitialContext(); 124 try 125 { 126 setupSecurityDomain(container, ctx); 127 } 128 catch (NamingException e) 129 { 130 throw new RuntimeException (e); 131 } 132 133 } 134 135 136 138 public Object lookup(String name) 139 { 140 String newName; 141 if (name.startsWith("/")) 142 { 143 newName = Container.ENC_CTX_NAME + "/env" + name; 144 } 145 else 146 { 147 newName = Container.ENC_CTX_NAME + "/env/" + name; 148 } 149 try 150 { 151 return getContainer().getInitialContext().lookup(newName); 152 } 153 catch (NamingException ignored) 154 { 155 try 156 { 157 return getContainer().getInitialContext().lookup(name); 158 } 159 catch (NamingException ignored2) 160 { 161 162 } 163 } 164 return null; 165 } 166 167 public Identity getCallerIdentity() 168 { 169 throw new IllegalStateException ("deprecated"); 170 } 171 172 public Principal getCallerPrincipal() 173 { 174 Principal principal = SecurityAssociation.getCallerPrincipal(); 175 if (getRm() != null) 176 { 177 principal = getRm().getPrincipal(principal); 178 } 179 180 if (principal == null) 182 throw new java.lang.IllegalStateException ("No valid security context for the caller identity"); 183 184 return principal; 185 } 186 187 public boolean isCallerInRole(Identity role) 188 { 189 throw new IllegalStateException ("deprecated"); 190 } 191 192 public boolean isCallerInRole(String roleName) 193 { 194 Principal principal = getCallerPrincipal(); 196 RunAsIdentity runAsIdentity = SecurityActions.peekRunAsIdentity(1); 199 200 if (principal == null && runAsIdentity == null) 201 return false; 202 203 if (getRm() == null) 204 { 205 String msg = "isCallerInRole() called with no security context. " 206 + "Check that a security-domain has been set for the application."; 207 throw new IllegalStateException (msg); 208 } 209 210 HashSet set = new HashSet (); 211 set.add(new SimplePrincipal(roleName)); 212 213 if (runAsIdentity == null) 214 return getRm().doesUserHaveRole(principal, set); 215 else 216 return runAsIdentity.doesUserHaveRole(set); 217 } 218 219 public TimerService getTimerService() throws IllegalStateException 220 { 221 return getContainer().getTimerService(); 222 } 223 224 public UserTransaction getUserTransaction() throws IllegalStateException 225 { 226 TransactionManagementType type = TxUtil.getTransactionManagementType(((Advisor) getContainer())); 227 if (type != TransactionManagementType.BEAN) throw new IllegalStateException ("Container " + getContainer().getEjbName() + ": it is illegal to inject UserTransaction into a CMT bean"); 228 229 return new UserTransactionImpl(); 230 } 231 232 public EJBHome getEJBHome() 233 { 234 throw new EJBException ("EJB 3.0 does not have a home type."); 235 } 236 237 public EJBLocalHome getEJBLocalHome() 238 { 239 throw new EJBException ("EJB 3.0 does not have a home type."); 240 } 241 242 public Properties getEnvironment() 243 { 244 throw new EJBException ("Deprecated"); 245 } 246 247 public void setRollbackOnly() throws IllegalStateException 248 { 249 TransactionManagementType type = TxUtil.getTransactionManagementType(((Advisor) getContainer())); 251 if (type != TransactionManagementType.CONTAINER) throw new IllegalStateException ("Container " + getContainer().getEjbName() + ": it is illegal to call setRollbackOnly from BMT: " + type); 252 253 try 254 { 255 TransactionManager tm = TxUtil.getTransactionManager(); 256 257 if (tm.getTransaction() == null) 260 throw new IllegalStateException ("setRollbackOnly() not allowed without a transaction."); 261 262 tm.setRollbackOnly(); 263 } 264 catch (SystemException e) 265 { 266 log.warn("failed to set rollback only; ignoring", e); 267 } 268 } 269 270 public boolean getRollbackOnly() throws IllegalStateException 271 { 272 TransactionManagementType type = TxUtil.getTransactionManagementType(((Advisor) getContainer())); 274 if (type != TransactionManagementType.CONTAINER) throw new IllegalStateException ("Container " + getContainer().getEjbName() + ": it is illegal to call getRollbackOnly from BMT: " + type); 275 276 try 277 { 278 TransactionManager tm = TxUtil.getTransactionManager(); 279 280 if (tm.getTransaction() == null) 283 throw new IllegalStateException ("setRollbackOnly() not allowed without a transaction."); 284 285 return tm.getStatus() == Status.STATUS_MARKED_ROLLBACK; 286 } 287 catch (SystemException e) 288 { 289 log.warn("failed to set rollback only; ignoring", e); 290 return true; 291 } 292 } 293 294 public EJBLocalObject getEJBLocalObject() throws IllegalStateException 295 { 296 try 297 { 298 return (EJBLocalObject )container.getInitialContext().lookup(ProxyFactoryHelper.getLocalJndiName(container, false)); 299 } 300 catch (NamingException e) 301 { 302 throw new IllegalStateException (e); 303 } 304 } 305 306 public EJBObject getEJBObject() throws IllegalStateException 307 { 308 try 309 { 310 return (EJBObject )container.getInitialContext().lookup(ProxyFactoryHelper.getRemoteJndiName(container, false)); 311 } 312 catch (NamingException e) 313 { 314 throw new IllegalStateException (e); 315 } 316 } 317 318 public Object getBusinessObject(Class businessInterface) throws IllegalStateException 319 { 320 return ((EJBContainer)container).getBusinessObject(baseContext, businessInterface); 321 } 322 323 public Class getInvokedBusinessInterface() throws IllegalStateException 324 { 325 return ((SessionContainer)container).getInvokedBusinessInterface(); 326 } 327 328 public MessageContext getMessageContext() throws IllegalStateException 329 { 330 throw new RuntimeException ("NOT IMPLEMENTED"); 331 } 332 333 } 334 | Popular Tags |