1 22 package org.jboss.web.tomcat.tc6; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.lang.annotation.Annotation ; 27 import java.lang.reflect.AccessibleObject ; 28 import java.lang.reflect.Field ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.HashMap ; 34 import java.util.LinkedHashMap ; 35 import java.util.List ; 36 import java.util.Map ; 37 38 import javax.naming.Context ; 39 import javax.naming.InitialContext ; 40 import javax.naming.NameNotFoundException ; 41 import javax.naming.NamingException ; 42 43 import org.apache.AnnotationProcessor; 44 import org.jboss.deployers.spi.deployer.DeploymentUnit; 45 import org.jboss.ejb3.Container; 46 import org.jboss.ejb3.DependencyPolicy; 47 import org.jboss.ejb3.DeploymentScope; 48 import org.jboss.ejb3.JmxDependencyPolicy; 49 import org.jboss.ejb3.enc.DeploymentPersistenceUnitResolver; 50 import org.jboss.ejb3.entity.PersistenceUnitDeployment; 51 import org.jboss.injection.DependsHandler; 52 import org.jboss.injection.EJBHandler; 53 import org.jboss.injection.EncInjector; 54 import org.jboss.injection.InjectionContainer; 55 import org.jboss.injection.InjectionHandler; 56 import org.jboss.injection.InjectionUtil; 57 import org.jboss.injection.Injector; 58 import org.jboss.injection.JndiInjectHandler; 59 import org.jboss.injection.PersistenceContextHandler; 60 import org.jboss.injection.PersistenceUnitHandler; 61 import org.jboss.injection.ResourceHandler; 62 import org.jboss.injection.WebServiceRefHandler; 63 import org.jboss.logging.Logger; 64 import org.jboss.metamodel.descriptor.EnvironmentRefGroup; 65 import org.jboss.metamodel.descriptor.Listener; 66 import org.jboss.virtual.VirtualFile; 67 import org.jboss.web.WebApplication; 68 import org.jboss.web.metamodel.descriptor.Filter; 69 import org.jboss.web.metamodel.descriptor.JBossWebDDObjectFactory; 70 import org.jboss.web.metamodel.descriptor.Servlet; 71 import org.jboss.web.metamodel.descriptor.WebDD; 72 import org.jboss.web.metamodel.descriptor.WebDDObjectFactory; 73 import org.jboss.xb.binding.JBossXBException; 74 75 81 public class TomcatInjectionContainer implements InjectionContainer, AnnotationProcessor 82 { 83 private static final Logger log = Logger.getLogger(TomcatInjectionContainer.class); 84 85 private static class EncMap extends HashMap <String , EncInjector> 86 { 87 private HashMap <String , EncInjector> added; 88 89 public void recordAdded() 90 { 91 added = new HashMap <String , EncInjector>(); 92 } 93 94 public void clearAdded() 95 { 96 added = null; 97 } 98 99 public Map <String , EncInjector> getAdded() { return added; } 100 101 102 @Override 103 public EncInjector put(String key, EncInjector value) 104 { 105 if (added != null) added.put(key, value); 106 return super.put(key, value); 107 } 108 109 @Override 110 public void putAll(Map <? extends String , ? extends EncInjector> m) 111 { 112 if (added != null) added.putAll(m); 113 super.putAll(m); 114 } 115 } 116 117 protected EncMap encInjectors = new EncMap(); 118 protected Map <String , Map <AccessibleObject , Injector>> encInjections = new HashMap <String , Map <AccessibleObject , Injector>>(); 119 protected Map <String , Map <AccessibleObject , Injector>> resolvedClassInjections = new HashMap <String , Map <AccessibleObject , Injector>>(); 120 121 122 protected List <PersistenceUnitDeployment> persistenceUnitDeployments = new ArrayList <PersistenceUnitDeployment>(); 123 protected LinkedHashMap ejbContainers = new LinkedHashMap (); protected DeploymentPersistenceUnitResolver persistenceUnitResolver; 125 protected WarEjbResolver ejbResolver; 126 protected DependencyPolicy dependencyPolicy = new JmxDependencyPolicy(); 127 protected Collection <InjectionHandler> handlers; 128 protected DeploymentUnit unit; 129 protected ClassLoader webLoader; 130 protected WebApplication appInfo; 131 132 public TomcatInjectionContainer(WebApplication appInfo, DeploymentUnit unit) 133 { 134 this.unit = unit; 135 DeploymentScope deploymentScope = null; 136 this.appInfo = appInfo; 137 138 persistenceUnitResolver = new DeploymentPersistenceUnitResolver(persistenceUnitDeployments, deploymentScope, ejbContainers); 139 ejbResolver = new WarEjbResolver(deploymentScope, unit.getSimpleName()); 140 } 141 142 public EnvironmentRefGroup getEnvironmentRefGroup() 143 { 144 return null; 145 } 146 147 public void postConstruct(Object object) throws IllegalAccessException , InvocationTargetException 148 { 149 } 151 152 public void preDestroy(Object object) throws IllegalAccessException , InvocationTargetException 153 { 154 } 156 157 167 public void processAnnotations(Object object) throws IllegalAccessException , InvocationTargetException , NamingException 168 { 169 log.debug("**************** Processing annotations for: " + object.getClass().getName()); 170 Map <AccessibleObject , Injector> injectors = resolvedClassInjections.get(object.getClass().getName()); 171 if (injectors == null) 172 { 173 log.debug("-- there was no prior annotation preprocessing done"); 174 encInjectors.recordAdded(); 175 injectors = InjectionUtil.processAnnotations(this, handlers, object.getClass()); 177 resolvedClassInjections.put(object.getClass().getName(), injectors); 178 179 if (encInjectors.getAdded().size() > 0) 181 { 182 for (EncInjector encInjector : encInjectors.getAdded().values()) 183 { 184 encInjector.inject(this); 185 } 186 encInjectors.clearAdded(); 187 } 188 } 189 if (injectors == null || injectors.size() == 0) 190 { 191 log.debug("-- no injectors found: " + injectors); 192 return; 193 } 194 195 log.debug("-- doing injections"); 196 for (Injector injector : injectors.values()) 197 { 198 injector.inject(object); 199 } 200 } 201 202 public void populateEnc(ClassLoader loader) 203 { 204 for (EncInjector injector : encInjectors.values()) 205 { 206 injector.inject(this); 207 } 208 } 209 210 private InputStream [] getInputStreams() 211 { 212 InputStream jbossWebIS = null; 213 InputStream webIS = null; 214 215 try 216 { 217 VirtualFile webDD = unit.getMetaDataFile("web.xml"); 218 if (webDD != null) 219 webIS = webDD.openStream(); 220 } 221 catch (IOException e) 222 { 223 log.debug("Failed to find web.xml"); 224 } 225 try 226 { 227 VirtualFile webDD = unit.getMetaDataFile("jboss-web.xml"); 228 if (webDD != null) 229 jbossWebIS = webDD.openStream(); 230 } 231 catch (IOException e) 232 { 233 log.debug("Failed to find jboss-web.xml"); 234 } 235 236 InputStream [] streams = {webIS, jbossWebIS}; 237 return streams; 238 } 239 240 248 public void processMetadata() 249 { 250 WebDD xml = null; 251 InputStream [] streams = getInputStreams(); 252 if (streams[0] != null) 253 { 254 try 255 { 256 xml = WebDDObjectFactory.parse(streams[0]); 257 if (streams[1] != null) 258 { 259 xml = JBossWebDDObjectFactory.parse(xml, streams[1]); 260 } 261 } 262 catch (JBossXBException e) 263 { 264 throw new RuntimeException (e); 265 } 266 } 267 else 268 { 269 throw new RuntimeException ("web.xml is required"); 270 } 271 272 274 handlers = new ArrayList <InjectionHandler>(); 276 handlers.add(new EJBHandler()); 277 handlers.add(new DependsHandler()); 278 handlers.add(new JndiInjectHandler()); 279 handlers.add(new PersistenceContextHandler()); 280 handlers.add(new PersistenceUnitHandler()); 281 handlers.add(new ResourceHandler()); 282 handlers.add(new WebServiceRefHandler()); 283 284 ClassLoader old = Thread.currentThread().getContextClassLoader(); 285 ClassLoader webLoader = getClassloader(); 286 Thread.currentThread().setContextClassLoader(webLoader); 287 try 288 { 289 for (InjectionHandler handler : handlers) handler.loadXml(xml, this); 290 291 for (Object obj : xml.getServlets()) 292 { 293 Servlet servlet = (Servlet)obj; 294 try 295 { 296 if (servlet.getServletClass() == null) 297 continue; if (resolvedClassInjections.containsKey(servlet.getServletClass())) 299 continue; 300 Class servletClass = webLoader.loadClass(servlet.getServletClass()); 301 Map <AccessibleObject , Injector> tmp = InjectionUtil.processAnnotations(this, handlers, servletClass); 302 resolvedClassInjections.put(servlet.getServletClass(), tmp); 303 } 304 catch (ClassNotFoundException e) 305 { 306 log.warn("could not find servlet class " + servlet.getServletClass() + " in classpath when processing annotations."); 307 } 308 309 } 310 for (Object obj : xml.getFilters()) 311 { 312 Filter filter = (Filter)obj; 313 try 314 { 315 if (resolvedClassInjections.containsKey(filter.getFilterClass())) 316 continue; 317 Class servletClass = webLoader.loadClass(filter.getFilterClass()); 318 Map <AccessibleObject , Injector> tmp = InjectionUtil.processAnnotations(this, handlers, servletClass); 319 resolvedClassInjections.put(filter.getFilterClass(), tmp); 320 } 321 catch (ClassNotFoundException e) 322 { 323 throw new RuntimeException ("could not find filter class in classpath", e); 324 } 325 } 326 for (Object obj : xml.getListeners()) 327 { 328 Listener listener = (Listener)obj; 329 try 330 { 331 if (resolvedClassInjections.containsKey(listener.getListenerClass())) 332 continue; 333 Class servletClass = webLoader.loadClass(listener.getListenerClass()); 334 Map <AccessibleObject , Injector> tmp = InjectionUtil.processAnnotations(this, handlers, servletClass); 335 resolvedClassInjections.put(listener.getListenerClass(), tmp); 336 } 337 catch (ClassNotFoundException e) 338 { 339 throw new RuntimeException ("could not find listener class in classpath", e); 340 } 341 } 342 } 343 finally 344 { 345 Thread.currentThread().setContextClassLoader(old); 346 } 347 } 348 349 public Map <String , EncInjector> getEncInjectors() 350 { 351 return encInjectors; 352 } 353 354 public Map <String , Map <AccessibleObject , Injector>> getEncInjections() 355 { 356 return encInjections; 357 } 358 359 public List <Injector> getInjectors() 361 { 362 return new ArrayList <Injector>(); } 364 365 366 public String getIdentifier() 367 { 368 return unit.getSimpleName(); 369 } 370 371 public String getDeploymentDescriptorType() 372 { 373 return "web.xml"; 374 } 375 376 public ClassLoader getClassloader() 377 { 378 return webLoader; 379 } 380 381 public void setClassLoader(ClassLoader loader) 382 { 383 this.webLoader = loader; 384 } 385 386 public Context getEnc() 387 { 388 ClassLoader old = Thread.currentThread().getContextClassLoader(); 389 try 390 { 391 Thread.currentThread().setContextClassLoader(getClassloader()); 392 try 393 { 394 return (Context ) new InitialContext ().lookup("java:comp"); 395 } 396 catch (NamingException e) 397 { 398 throw new RuntimeException (e); 399 } 400 } 401 finally 402 { 403 Thread.currentThread().setContextClassLoader(old); 404 } 405 } 406 407 public Context getEncEnv() 408 { 409 ClassLoader old = Thread.currentThread().getContextClassLoader(); 410 try 411 { 412 Thread.currentThread().setContextClassLoader(getClassloader()); 413 try 414 { 415 return (Context ) new InitialContext ().lookup("java:comp/env"); 416 } 417 catch (NamingException e) 418 { 419 throw new RuntimeException (e); 420 } 421 } 422 finally 423 { 424 Thread.currentThread().setContextClassLoader(old); 425 } 426 } 427 428 public PersistenceUnitDeployment getPersistenceUnitDeployment(String unitName) throws NameNotFoundException 429 { 430 return persistenceUnitResolver.getPersistenceUnitDeployment(unitName); 431 } 432 433 public Container resolveEjbContainer(String link, Class businessIntf) 434 { 435 return ejbResolver.getEjbContainer(link, businessIntf); 436 } 437 438 public Container resolveEjbContainer(Class businessIntf) throws NameNotFoundException 439 { 440 return ejbResolver.getEjbContainer(businessIntf); 441 } 442 443 public String getEjbJndiName(Class businessInterface) throws NameNotFoundException 444 { 445 return ejbResolver.getEjbJndiName(businessInterface); 446 } 447 448 public String getEjbJndiName(String link, Class businessInterface) 449 { 450 return ejbResolver.getEjbJndiName(link, businessInterface); 451 } 452 453 public <T extends Annotation > T getAnnotation(Class <T> annotationType, Class <?> clazz) 454 { 455 return clazz.getAnnotation(annotationType); 456 } 457 458 public <T extends Annotation > T getAnnotation(Class <T> annotationType, Class <?> clazz, Method method) 459 { 460 return method.getAnnotation(annotationType); 461 } 462 463 public <T extends Annotation > T getAnnotation(Class <T> annotationType, Method method) 464 { 465 return method.getAnnotation(annotationType); 466 } 467 468 public <T extends Annotation > T getAnnotation(Class <T> annotationType, Class <?> clazz, Field field) 469 { 470 return field.getAnnotation(annotationType); 471 } 472 473 public <T extends Annotation > T getAnnotation(Class <T> annotationType, Field field) 474 { 475 return field.getAnnotation(annotationType); 476 } 477 478 public DependencyPolicy getDependencyPolicy() 479 { 480 return dependencyPolicy; 481 } 482 483 } | Popular Tags |