View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.annotations;
15  
16  import org.eclipse.jetty.util.log.Log;
17  import org.eclipse.jetty.webapp.WebAppContext;
18  
19  /**
20   * Configuration
21   *
22   *
23   */
24  public class AnnotationConfiguration extends AbstractConfiguration
25  {
26      public static final String CLASS_INHERITANCE_MAP  = "org.eclipse.jetty.classInheritanceMap";
27      
28      public void preConfigure(final WebAppContext context) throws Exception
29      {
30      }
31     
32      
33      public void configure(WebAppContext context) throws Exception
34      {
35         Boolean b = (Boolean)context.getAttribute(METADATA_COMPLETE);
36         boolean metadataComplete = (b != null && b.booleanValue());
37         Integer i = (Integer)context.getAttribute(WEBXML_VERSION);
38         int webxmlVersion = (i == null? 0 : i.intValue());
39        
40          if (metadataComplete)
41          {
42              //Never scan any jars or classes for annotations if metadata is complete
43              if (Log.isDebugEnabled()) Log.debug("Metadata-complete==true,  not processing annotations for context "+context);
44              return;
45          }
46          else 
47          {
48              //Only scan jars and classes if metadata is not complete and the web app is version 3.0, or
49              //a 2.5 version webapp that has specifically asked to discover annotations
50              if (Log.isDebugEnabled()) Log.debug("parsing annotations");
51                         
52              AnnotationParser parser = new AnnotationParser();
53  
54              parser.registerAnnotationHandler("javax.annotation.Resource", new ResourceAnnotationHandler (context));
55              parser.registerAnnotationHandler("javax.annotation.Resources", new ResourcesAnnotationHandler (context));
56              parser.registerAnnotationHandler("javax.annotation.PostConstruct", new PostConstructAnnotationHandler(context));
57              parser.registerAnnotationHandler("javax.annotation.PreDestroy", new PreDestroyAnnotationHandler(context));
58              parser.registerAnnotationHandler("javax.annotation.security.RunAs", new RunAsAnnotationHandler(context));
59  
60              ClassInheritanceHandler classHandler = new ClassInheritanceHandler();
61              parser.registerClassHandler(classHandler);
62              
63              
64              if (webxmlVersion >= 30 || context.isConfigurationDiscovered())
65              {
66                  if (Log.isDebugEnabled()) Log.debug("Scanning all classes for annotations: webxmlVersion="+webxmlVersion+" configurationDiscovered="+context.isConfigurationDiscovered());
67                  parseContainerPath(context, parser);
68                  parseWebInfLib (context, parser);
69                  parseWebInfClasses(context, parser);
70              } 
71              else
72              {
73                  if (Log.isDebugEnabled()) Log.debug("Scanning only classes in web.xml for annotations");
74                  parse25Classes(context, parser);
75              }
76              
77              //save the type inheritance map created by the parser for later reference
78              context.setAttribute(CLASS_INHERITANCE_MAP, classHandler.getMap());
79          }    
80      }
81  
82  
83  
84      public void deconfigure(WebAppContext context) throws Exception
85      {
86          
87      }
88  
89  
90  
91  
92      public void postConfigure(WebAppContext context) throws Exception
93      {
94          context.setAttribute(CLASS_INHERITANCE_MAP, null);
95      }
96    
97  }