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 java.net.URI;
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.eclipse.jetty.util.Loader;
21  import org.eclipse.jetty.util.log.Log;
22  import org.eclipse.jetty.util.resource.Resource;
23  import org.eclipse.jetty.webapp.Configuration;
24  import org.eclipse.jetty.webapp.WebAppContext;
25  import org.eclipse.jetty.webapp.WebXmlProcessor;
26  import org.eclipse.jetty.webapp.WebXmlProcessor.Descriptor;
27  import org.eclipse.jetty.webapp.WebInfConfiguration;
28  
29  
30  public abstract class AbstractConfiguration implements Configuration
31  {
32      public static final String CONTAINER_JAR_RESOURCES = WebInfConfiguration.CONTAINER_JAR_RESOURCES;
33      public static final String WEB_INF_JAR_RESOURCES = WebInfConfiguration.WEB_INF_JAR_RESOURCES;
34      public static final String WEBXML_VERSION = WebXmlProcessor.WEBXML_VERSION;
35      public static final String METADATA_COMPLETE = WebXmlProcessor.METADATA_COMPLETE;
36      public static final String WEBXML_CLASSNAMES = WebXmlProcessor.WEBXML_CLASSNAMES;
37  
38      public void parseContainerPath (final WebAppContext context, final AnnotationParser parser)
39      throws Exception
40      {
41          //if no pattern for the container path is defined, then by default scan NOTHING
42          Log.debug("Scanning container jars");
43             
44          //Convert from Resource to URI
45          ArrayList<URI> containerUris = new ArrayList<URI>();
46          List<Resource> jarResources = (List<Resource>)context.getAttribute(CONTAINER_JAR_RESOURCES);
47          for (Resource r : jarResources)
48          {
49              URI uri = r.getURI();
50                  containerUris.add(uri);          
51          }
52          
53          parser.parse (containerUris.toArray(new URI[containerUris.size()]),
54                  new ClassNameResolver ()
55                  {
56                      public boolean isExcluded (String name)
57                      {
58                          if (context.isSystemClass(name)) return false;
59                          if (context.isServerClass(name)) return true;
60                          return false;
61                      }
62  
63                      public boolean shouldOverride (String name)
64                      { 
65                          //looking at system classpath
66                          if (context.isParentLoaderPriority())
67                              return true;
68                          return false;
69                      }
70                  });
71      }
72      
73      
74      public void parseWebInfLib (final WebAppContext context, final AnnotationParser parser)
75      throws Exception
76      {  
77          WebXmlProcessor webXmlProcessor = (WebXmlProcessor)context.getAttribute(WebXmlProcessor.WEB_PROCESSOR); 
78          if (webXmlProcessor == null)
79             throw new IllegalStateException ("No processor for web xml");
80          
81          List<Descriptor> frags = webXmlProcessor.getFragments();
82          
83          //Get the web-inf lib jars who have a web-fragment.xml that is not metadata-complete (or is not set)
84          ArrayList<URI> webInfUris = new ArrayList<URI>();
85          List<Resource> jarResources = (List<Resource>)context.getAttribute(WEB_INF_JAR_RESOURCES);
86          
87          for (Resource r : jarResources)
88          {          
89              URI uri  = r.getURI();
90              Descriptor d = null;
91              for (Descriptor frag: frags)
92              {
93                  Resource fragResource = frag.getResource(); //eg jar:file:///a/b/c/foo.jar!/META-INF/web-fragment.xml
94                  if (Resource.isContainedIn(fragResource,r))
95                  {
96                      d = frag;
97                      break;
98                  }
99              }
100 
101             //if there was no web-fragment.xml for the jar, or there was one 
102             //and its metadata is NOT complete, we want to exame it for annotations
103             if (d == null || (d != null && !d.isMetaDataComplete()))
104                 webInfUris.add(uri);
105         }
106  
107        parser.parse(webInfUris.toArray(new URI[webInfUris.size()]), 
108                 new ClassNameResolver()
109                 {
110                     public boolean isExcluded (String name)
111                     {    
112                         if (context.isSystemClass(name)) return true;
113                         if (context.isServerClass(name)) return false;
114                         return false;
115                     }
116 
117                     public boolean shouldOverride (String name)
118                     {
119                         //looking at webapp classpath, found already-parsed class of same name - did it come from system or duplicate in webapp?
120                         if (context.isParentLoaderPriority())
121                             return false;
122                         return true;
123                     }
124                 });  
125     }
126      
127     public void parseWebInfClasses (final WebAppContext context, final AnnotationParser parser)
128     throws Exception
129     {
130         Log.debug("Scanning classes in WEB-INF/classes");
131         parser.parse(context.getWebInf().addPath("classes/"), 
132                     new ClassNameResolver()
133         {
134             public boolean isExcluded (String name)
135             {
136                 if (context.isSystemClass(name)) return true;
137                 if (context.isServerClass(name)) return false;
138                 return false;
139             }
140 
141             public boolean shouldOverride (String name)
142             {
143                 //looking at webapp classpath, found already-parsed class of same name - did it come from system or duplicate in webapp?
144                 if (context.isParentLoaderPriority())
145                     return false;
146                 return true;
147             }
148         });
149     }
150     
151     public void parse25Classes (final WebAppContext context, final AnnotationParser parser)
152     throws Exception
153     {
154         //only parse servlets, filters and listeners from web.xml
155         if (Log.isDebugEnabled()) Log.debug("Scanning only classes from web.xml");
156         ArrayList<String> classNames = (ArrayList<String>)context.getAttribute(WEBXML_CLASSNAMES);
157         for (String s : classNames)
158         {
159             Class clazz = Loader.loadClass(null, s);
160             parser.parse(clazz,  new ClassNameResolver()
161             {
162                 public boolean isExcluded (String name)
163                 {
164                     if (context.isSystemClass(name)) return true;
165                     if (context.isServerClass(name)) return false;
166                     return false;
167                 }
168 
169                 public boolean shouldOverride (String name)
170                 {
171                     //looking at webapp classpath, found already-parsed class of same name - did it come from system or duplicate in webapp?
172                     if (context.isParentLoaderPriority())
173                         return false;
174                     return true;
175                 }
176             }, true);
177         }
178         
179     }
180 }