1 22 package org.jboss.ejb3.clientmodule; 23 24 import java.lang.annotation.Annotation ; 25 import java.lang.reflect.AccessibleObject ; 26 import java.lang.reflect.Field ; 27 import java.lang.reflect.Method ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.management.MalformedObjectNameException ; 35 import javax.management.ObjectName ; 36 import javax.naming.Context ; 37 import javax.naming.NameNotFoundException ; 38 import javax.naming.NamingException ; 39 40 import org.jboss.deployers.spi.deployer.DeploymentUnit; 41 import org.jboss.ejb3.Container; 42 import org.jboss.ejb3.DependencyPolicy; 43 import org.jboss.ejb3.DeploymentScope; 44 import org.jboss.ejb3.Ejb3Module; 45 import org.jboss.ejb3.deployers.JBoss5DependencyPolicy; 46 import org.jboss.ejb3.deployers.JBoss5DeploymentScope; 47 import org.jboss.ejb3.enc.DeploymentEjbResolver; 48 import org.jboss.ejb3.entity.PersistenceUnitDeployment; 49 import org.jboss.ejb3.metamodel.ApplicationClientDD; 50 import org.jboss.injection.DependsHandler; 51 import org.jboss.injection.EJBHandler; 52 import org.jboss.injection.EncInjector; 53 import org.jboss.injection.InjectionContainer; 54 import org.jboss.injection.InjectionHandler; 55 import org.jboss.injection.InjectionUtil; 56 import org.jboss.injection.Injector; 57 import org.jboss.injection.JndiInjectHandler; 58 import org.jboss.injection.PersistenceContextHandler; 59 import org.jboss.injection.PersistenceUnitHandler; 60 import org.jboss.injection.ResourceHandler; 61 import org.jboss.injection.WebServiceRefHandler; 62 import org.jboss.logging.Logger; 63 import org.jboss.metamodel.descriptor.EnvironmentRefGroup; 64 import org.jboss.naming.Util; 65 66 73 public class ClientENCInjectionContainer implements InjectionContainer 74 { 75 private static final Logger log = Logger.getLogger(ClientENCInjectionContainer.class); 76 77 private ApplicationClientDD xml; 78 private Class <?> mainClass; 79 private String applicationClientName; 80 private ClassLoader classLoader; 81 82 private List <Injector> injectors = new ArrayList <Injector>(); 84 private Map <String , Map <AccessibleObject , Injector>> encInjections = new HashMap <String , Map <AccessibleObject , Injector>>(); 85 private Map <String , EncInjector> encInjectors = new HashMap <String , EncInjector>(); 86 87 private Context enc; 88 private Context encEnv; 89 90 private DeploymentEjbResolver ejbResolver; 91 private ObjectName objectName; 92 private DependencyPolicy dependencyPolicy = new JBoss5DependencyPolicy(); 93 94 public ClientENCInjectionContainer(DeploymentUnit unit, ApplicationClientDD xml, Class <?> mainClass, String applicationClientName, ClassLoader classLoader, Context encCtx) throws NamingException 95 { 96 if(mainClass == null) 97 throw new NullPointerException ("mainClass is mandatory"); 98 if(applicationClientName == null) 99 throw new NullPointerException ("applicationClientName is mandatory"); 100 if(classLoader == null) 101 throw new NullPointerException ("classLoader is mandatory"); 102 103 this.xml = xml; 104 this.mainClass = mainClass; 105 this.applicationClientName = applicationClientName; 106 this.classLoader = classLoader; 107 108 this.enc = encCtx; 109 110 encEnv = Util.createSubcontext(enc, "env"); 111 112 131 132 DeploymentScope scope = null; 133 if (unit.getDeploymentContext().getParent() != null) 134 { 135 scope = new JBoss5DeploymentScope(unit.getDeploymentContext().getParent()); 136 } 137 138 ejbResolver = new ClientEjbResolver(scope, unit.getDeploymentContext().getRoot().getName()); 139 140 String on = Ejb3Module.BASE_EJB3_JMX_NAME + createScopeKernelName(unit, scope) + ",name=" + applicationClientName; 141 try 142 { 143 this.objectName = new ObjectName (on); 144 } 145 catch(MalformedObjectNameException e) 146 { 147 throw new RuntimeException (e); 149 } 150 151 processMetaData(); 152 } 153 154 private String createScopeKernelName(DeploymentUnit unit, DeploymentScope ear) 155 { 156 String scopedKernelName = ""; 157 if (ear != null) scopedKernelName += ",ear=" + ear.getShortName(); 158 scopedKernelName += ",jar=" + unit.getDeploymentContext().getRoot().getName(); 159 return scopedKernelName; 160 } 161 162 public <T extends Annotation > T getAnnotation(Class <T> annotationType, Class <?> clazz) 163 { 164 return clazz.getAnnotation(annotationType); 165 } 166 167 public <T extends Annotation > T getAnnotation(Class <T> annotationType, Class <?> clazz, Method method) 168 { 169 return method.getAnnotation(annotationType); 170 } 171 172 public <T extends Annotation > T getAnnotation(Class <T> annotationType, Method method) 173 { 174 return method.getAnnotation(annotationType); 175 } 176 177 public <T extends Annotation > T getAnnotation(Class <T> annotationType, Class <?> clazz, Field field) 178 { 179 return field.getAnnotation(annotationType); 180 } 181 182 public <T extends Annotation > T getAnnotation(Class <T> annotationType, Field field) 183 { 184 return field.getAnnotation(annotationType); 185 } 186 187 public ClassLoader getClassloader() 188 { 189 return classLoader; 190 } 191 192 public DependencyPolicy getDependencyPolicy() 193 { 194 return dependencyPolicy; 195 } 196 197 public String getDeploymentDescriptorType() 198 { 199 return "application-client.xml"; 200 } 201 202 public String getEjbJndiName(Class businessInterface) throws NameNotFoundException 203 { 204 return ejbResolver.getEjbJndiName(businessInterface); 205 } 206 207 public String getEjbJndiName(String link, Class businessInterface) 208 { 209 return ejbResolver.getEjbJndiName(link, businessInterface); 210 } 211 212 public Context getEnc() 213 { 214 return enc; 215 } 216 217 public Context getEncEnv() 218 { 219 return encEnv; 220 } 221 222 public Map <String , Map <AccessibleObject , Injector>> getEncInjections() 223 { 224 return encInjections; 225 } 226 227 public Map <String , EncInjector> getEncInjectors() 228 { 229 return encInjectors; 230 } 231 232 public EnvironmentRefGroup getEnvironmentRefGroup() 233 { 234 return xml; 235 } 236 237 public String getIdentifier() 238 { 239 return applicationClientName; 240 } 241 242 246 public List <Injector> getInjectors() 247 { 248 return injectors; 250 } 251 252 public Class <?> getMainClass() 253 { 254 return mainClass; 255 } 256 257 public ObjectName getObjectName() 258 { 259 return objectName; 260 } 261 262 public PersistenceUnitDeployment getPersistenceUnitDeployment(String unitName) throws NameNotFoundException 263 { 264 throw new RuntimeException ("NYI"); 265 } 266 267 private void populateEnc() 268 { 269 for (EncInjector injector : encInjectors.values()) 270 { 271 log.trace("encInjector: " + injector); 272 injector.inject(this); 273 } 274 } 275 276 private void processMetaData() 277 { 278 for(String dependency : xml.getDependencies()) 279 { 280 getDependencyPolicy().addDependency(dependency); 281 } 282 283 Collection <InjectionHandler> handlers = new ArrayList <InjectionHandler>(); 285 handlers.add(new EJBHandler()); 286 handlers.add(new DependsHandler()); 287 handlers.add(new JndiInjectHandler()); 288 handlers.add(new PersistenceContextHandler()); 289 handlers.add(new PersistenceUnitHandler()); 290 handlers.add(new ResourceHandler()); 291 handlers.add(new WebServiceRefHandler()); 292 293 ClassLoader old = Thread.currentThread().getContextClassLoader(); 294 Thread.currentThread().setContextClassLoader(classLoader); 295 try 296 { 297 for (InjectionHandler handler : handlers) handler.loadXml(xml, this); 299 300 Map <AccessibleObject , Injector> tmp = InjectionUtil.processAnnotations(this, handlers, getMainClass()); 301 injectors.addAll(tmp.values()); 302 303 } 318 finally 319 { 320 Thread.currentThread().setContextClassLoader(old); 321 } 322 } 323 324 public Container resolveEjbContainer(String link, Class businessIntf) 325 { 326 return ejbResolver.getEjbContainer(link, businessIntf); 327 } 328 329 public Container resolveEjbContainer(Class businessIntf) throws NameNotFoundException 330 { 331 return ejbResolver.getEjbContainer(businessIntf); 332 } 333 334 public void start() 335 { 336 log.trace("start"); 337 338 populateEnc(); 339 340 } 342 343 public void stop() 344 { 345 log.trace("stop"); 346 } 347 } 348 | Popular Tags |