00001
00026 package org.objectweb.jonas_ejb.container;
00027
00028 import java.security.Identity;
00029 import java.security.Principal;
00030 import java.util.Iterator;
00031 import java.util.List;
00032 import java.util.Properties;
00033
00034 import javax.ejb.EJBContext;
00035 import javax.ejb.EJBHome;
00036 import javax.ejb.EJBLocalHome;
00037 import javax.ejb.EnterpriseBean;
00038 import javax.ejb.TimerService;
00039 import javax.transaction.Status;
00040 import javax.transaction.SystemException;
00041 import javax.transaction.UserTransaction;
00042 import javax.naming.Context;
00043 import javax.naming.InitialContext;
00044 import javax.naming.NamingException;
00045 import org.objectweb.jonas_lib.deployment.api.SecurityRoleRefDesc;
00046 import org.objectweb.jonas_lib.naming.ContainerNaming;
00047 import org.objectweb.jonas.naming.NamingManager;
00048 import org.objectweb.transaction.jta.TransactionManager;
00049 import org.objectweb.util.monolog.api.BasicLevel;
00050
00057 public abstract class JContext implements EJBContext {
00058
00059 protected EnterpriseBean instance = null;
00060 protected JFactory bf = null;
00061 protected JHome home = null;
00062 protected JLocalHome localhome = null;
00063 protected JContainer cont = null;
00064 protected TransactionManager tm = null;
00065
00069 private PermissionManager permissionManager = null;
00070
00071
00076 int instanceState = 0;
00077
00078
00079
00080
00081
00087 public JContext(JFactory bf, EnterpriseBean i) {
00088 this.bf = bf;
00089 this.instance = i;
00090 if (i == null) {
00091 TraceEjb.logger.log(BasicLevel.ERROR, "null EnterpriseBean!");
00092 }
00093 this.cont = bf.getContainer();
00094 this.tm = bf.getTransactionManager();
00095 this.permissionManager = cont.getPermissionManager();
00096
00097 }
00098
00102 public void setState(int newState) {
00103 instanceState = newState;
00104 TraceEjb.context.log(BasicLevel.DEBUG, "" + instanceState);
00105 }
00106
00111 public int getState() {
00112 TraceEjb.context.log(BasicLevel.DEBUG, "" + instanceState);
00113 return instanceState;
00114 }
00115
00116
00117
00118
00119
00125 public Identity getCallerIdentity() {
00126 throw new RuntimeException ("getCallerIdentity() method deprecated. use instead getCallerPrincipal()");
00127 }
00128
00129
00136 public Principal getCallerPrincipal() throws IllegalStateException {
00137
00138 if (getState() == 0) {
00139 throw new IllegalStateException("the instance is not allowed to call this method");
00140 }
00141 boolean inRunAs = false;
00142 if (bf.dd.getRunAsRole() != null) {
00143 inRunAs = true;
00144 }
00145
00146 Principal principal = cont.getPrincipalFactory().getCallerPrincipal(inRunAs);
00147 if (principal == null) {
00148 throw new IllegalStateException("no security context exists");
00149 }
00150 return principal;
00151
00152 }
00153
00159 public EJBHome getEJBHome() throws IllegalStateException {
00160 return home;
00161 }
00162
00168 public EJBLocalHome getEJBLocalHome() throws IllegalStateException {
00169 if (!bf.dd.hasDefinedLocalInterface()) {
00170 TraceEjb.logger.log(BasicLevel.ERROR, "No Local Interface declared for this bean");
00171 throw new IllegalStateException("No Local Interface declared for this bean");
00172 }
00173 return localhome;
00174 }
00175
00185 public Properties getEnvironment() {
00186 TraceEjb.interp.log(BasicLevel.DEBUG, "");
00187 return bf.getEjb10Environment();
00188 }
00189
00194 public boolean getRollbackOnly() throws IllegalStateException {
00195 TraceEjb.interp.log(BasicLevel.DEBUG, "");
00196
00197 if (getState() == 0) {
00198 throw new IllegalStateException("the instance is not allowed to call this method");
00199 }
00200
00201 try {
00202 switch (tm.getStatus()) {
00203 case Status.STATUS_MARKED_ROLLBACK:
00204 case Status.STATUS_ROLLING_BACK:
00205 return true;
00206 case Status.STATUS_ACTIVE:
00207 case Status.STATUS_COMMITTING:
00208 case Status.STATUS_PREPARED:
00209 case Status.STATUS_PREPARING:
00210 return false;
00211 case Status.STATUS_ROLLEDBACK:
00212 throw new IllegalStateException("Transaction already rolled back");
00213 case Status.STATUS_COMMITTED:
00214 throw new IllegalStateException("Transaction already committed");
00215 case Status.STATUS_NO_TRANSACTION:
00216 case Status.STATUS_UNKNOWN:
00217 throw new IllegalStateException("Cannot getRollbackOnly outside transaction");
00218 }
00219 } catch (SystemException e) {
00220 TraceEjb.logger.log(BasicLevel.ERROR, "cannot get transaction status:", e);
00221 throw new IllegalStateException("Cannot get transaction status");
00222 }
00223 return true;
00224 }
00225
00232 public abstract TimerService getTimerService() throws IllegalStateException;
00233
00242 public UserTransaction getUserTransaction() throws IllegalStateException {
00243
00244 TraceEjb.interp.log(BasicLevel.DEBUG, "");
00245
00246 if (!bf.isTxBeanManaged()) {
00247 throw new IllegalStateException("This bean is not allowed to use UserTransaction interface");
00248 }
00249 if (getState() == 0) {
00250 throw new IllegalStateException("the instance is not allowed to call this method");
00251 }
00252 return (UserTransaction) tm;
00253 }
00254
00261 public boolean isCallerInRole(Identity role) {
00262 throw new RuntimeException ("isCallerInRole(Identity) method deprecated. use instead isCallerInRole(String)");
00263 }
00264
00274 public boolean isCallerInRole(String roleName) throws IllegalStateException {
00275 TraceEjb.security.log(BasicLevel.DEBUG, "");
00276
00277 if (getState() == 0) {
00278 throw new IllegalStateException("the instance is not allowed to call this method");
00279 }
00280
00281
00282
00283
00284
00285 List list = bf.dd.getSecurityRoleRefDescList();
00286
00287 if (list == null) {
00288 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.");
00289 return false;
00290 }
00291 boolean foundItem = false;
00292 Iterator it = bf.dd.getSecurityRoleRefDescList().iterator();
00293 String tmpRoleName = null;
00294 SecurityRoleRefDesc sRoleRefDesc = null;
00295 while (!foundItem && it.hasNext()) {
00296 sRoleRefDesc = (SecurityRoleRefDesc) it.next();
00297 tmpRoleName = sRoleRefDesc.getRoleName();
00298 if (tmpRoleName.equals(roleName)) {
00299 foundItem = true;
00300 }
00301 }
00302
00303 if (!foundItem) {
00304 TraceEjb.security.log(BasicLevel.DEBUG, "No security-role-ref with role name '" + roleName
00305 + "' was found in the deployment descriptor of bean '"
00306 + bf.getEJBName() + ".");
00307 return false;
00308 }
00309
00310 boolean inRunAs = false;
00311 if (bf.dd.getRunAsRole() != null) {
00312 inRunAs = true;
00313 }
00314
00315 boolean inRole = permissionManager.isCallerInRole(bf.getEJBName(), roleName, inRunAs);
00316
00317 TraceEjb.security.log(BasicLevel.DEBUG, "isCallerInRole: " + inRole);
00318 return inRole;
00319
00320 }
00321
00326 public void setRollbackOnly() throws IllegalStateException {
00327
00328 TraceEjb.interp.log(BasicLevel.DEBUG, "");
00329
00330
00331
00332
00333 boolean ir = getRollbackOnly();
00334
00335 try {
00336 tm.setRollbackOnly();
00337 } catch (IllegalStateException e) {
00338 TraceEjb.logger.log(BasicLevel.ERROR, "current thread not associated with transaction");
00339 throw e;
00340 } catch (SystemException e) {
00341 TraceEjb.logger.log(BasicLevel.ERROR, "setRollbackOnly unexpected exception:", e);
00342 }
00343 }
00344
00345 }