1 22 package org.jboss.ejb3; 23 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import javax.ejb.EJBContext ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingEnumeration ; 29 30 import org.jboss.aop.metadata.SimpleMetaData; 31 import org.jboss.ejb3.interceptor.InterceptorInfo; 32 import org.jboss.ejb3.interceptor.InterceptorInjector; 33 import org.jboss.logging.Logger; 34 import org.jboss.naming.Util; 35 import org.jboss.security.RealmMapping; 36 37 43 public abstract class BaseContext implements BeanContext 44 { 45 protected static Logger log = Logger.getLogger(BaseContext.class); 46 protected Container container; 47 protected Object bean; 48 protected RealmMapping rm; 49 protected SimpleMetaData metadata; 50 protected EJBContext ejbContext; 51 52 protected HashMap <Class , Object > interceptorInstances; 53 54 public BaseContext() 55 { 56 } 57 58 public Object getInstance() 59 { 60 return bean; 61 } 62 63 public void setInstance(Object instance) 64 { 65 bean = instance; 66 } 67 68 public Container getContainer() 69 { 70 return container; 71 } 72 73 public SimpleMetaData getMetaData() 74 { 75 if (metadata == null) metadata = new SimpleMetaData(); 76 return metadata; 77 } 78 79 public void setContainer(Container container) 80 { 81 this.container = container; 82 bindEJBContext(); 83 } 84 85 public void bindEJBContext() 86 { 87 try 88 { 89 Util.rebind(container.getEnc(), "EJBContext", getEJBContext()); 90 } 91 catch (javax.naming.NamingException e) 92 { 93 e.printStackTrace(); 94 throw new RuntimeException (e); 95 } 96 } 97 98 public void initialiseInterceptorInstances() 99 { 100 HashSet <InterceptorInfo> interceptors = ((EJBContainer)container).getApplicableInterceptors(); 101 if (interceptors != null && interceptors.size() > 0 && interceptorInstances == null) 102 { 103 HashMap <Class ,InterceptorInjector> interceptorInjectors = ((EJBContainer)container).getInterceptorInjectors(); 104 interceptorInstances = new HashMap <Class , Object >(); 105 for (InterceptorInfo info : interceptors) 106 { 107 try 108 { 109 Object instance = info.getClazz().newInstance(); 110 interceptorInstances.put(info.getClazz(), instance); 111 interceptorInjectors.get(info.getClazz()).inject(this, instance); 112 } 113 catch (Exception e) 114 { 115 log.warn("Interceptors must have a public noargs constructor: " + info.getClazz().getName()); 116 } 117 } 118 } 119 } 120 121 public EJBContext getEJBContext() 122 { 123 if (ejbContext == null) 124 { 125 BaseSessionContext bsc = new BaseSessionContext(); 126 bsc.setContainer(getContainer()); 127 bsc.setBaseContext(this); 128 ejbContext = bsc; 129 } 130 return ejbContext; 131 } 132 133 public Object [] getInterceptorInstances(InterceptorInfo[] interceptorInfos) 134 { 135 Object [] interceptors = new Object [interceptorInfos.length]; 136 int i = 0; 137 for (InterceptorInfo info : interceptorInfos) 138 { 139 interceptors[i++] = interceptorInstances.get(info.getClazz()); 140 } 141 return interceptors; 142 } 143 144 public Object getInvokedMethodKey() 145 { 146 return container; 147 } 148 } 149 | Popular Tags |