1   //========================================================================
2   //$Id: Configuration.java 3601 2008-09-04 06:14:02Z janb $
3   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  
16  package org.mortbay.jetty.annotations;
17  
18  import java.util.ArrayList;
19  import java.util.EventListener;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.mortbay.jetty.servlet.FilterHolder;
24  import org.mortbay.jetty.servlet.FilterMapping;
25  import org.mortbay.jetty.servlet.ServletHolder;
26  import org.mortbay.jetty.servlet.ServletMapping;
27  import org.mortbay.log.Log;
28  import org.mortbay.util.LazyList;
29  
30  /**
31   * Configuration
32   *
33   *
34   */
35  public class Configuration extends org.mortbay.jetty.plus.webapp.Configuration
36  {
37      public static final String __web_inf_pattern = "org.mortbay.jetty.webapp.WebInfIncludeAnnotationJarPattern";
38      public static final String __container_pattern = "org.mortbay.jetty.webapp.ContainerIncludeAnnotationJarPattern";
39                                                        
40      
41      
42      public Configuration () throws ClassNotFoundException
43      {
44          super();
45      }
46  
47      /** 
48       * @see org.mortbay.jetty.plus.webapp.AbstractConfiguration#parseAnnotations()
49       */
50      public void parseAnnotations() throws Exception
51      {
52          /*
53           * TODO Need to also take account of hidden classes on system classpath that should never
54           * contribute annotations to a webapp (system and server classes):
55           * 
56           * --- when scanning system classpath:
57           *   + system classes : should always be scanned (subject to pattern)
58           *   + server classes : always ignored
59           *   
60           * --- when scanning webapp classpath:
61           *   + system classes : always ignored
62           *   + server classes : always scanned
63           * 
64           * 
65           * If same class is found in both container and in context then need to use
66           * webappcontext parentloaderpriority to work out which one contributes the
67           * annotation.
68           */
69         
70         
71          AnnotationFinder finder = new AnnotationFinder();
72  
73          //if no pattern for the container path is defined, then by default scan NOTHING
74          Log.debug("Scanning system jars");
75          finder.find(getWebAppContext().getClassLoader().getParent(), true, getWebAppContext().getInitParameter(__container_pattern), false, 
76                  new ClassNameResolver ()
77                  {
78                      public boolean isExcluded (String name)
79                      {
80                          if (getWebAppContext().isSystemClass(name)) return false;
81                          if (getWebAppContext().isServerClass(name)) return true;
82                          return false;
83                      }
84  
85                      public boolean shouldOverride (String name)
86                      { 
87                          //looking at system classpath
88                          if (getWebAppContext().isParentLoaderPriority())
89                              return true;
90                          return false;
91                      }
92                  });
93  
94          Log.debug("Scanning WEB-INF/lib jars");
95          //if no pattern for web-inf/lib is defined, then by default scan everything in it
96          finder.find (getWebAppContext().getClassLoader(), false, getWebAppContext().getInitParameter(__web_inf_pattern), true,
97                  new ClassNameResolver()
98                  {
99                      public boolean isExcluded (String name)
100                     {    
101                         if (getWebAppContext().isSystemClass(name)) return true;
102                         if (getWebAppContext().isServerClass(name)) return false;
103                         return false;
104                     }
105 
106                     public boolean shouldOverride (String name)
107                     {
108                         //looking at webapp classpath, found already-parsed class of same name - did it come from system or duplicate in webapp?
109                         if (getWebAppContext().isParentLoaderPriority())
110                             return false;
111                         return true;
112                     }
113                 });       
114         
115         Log.debug("Scanning classes in WEB-INF/classes");
116         finder.find(_context.getWebInf().addPath("classes/"), 
117                 new ClassNameResolver()
118                 {
119                     public boolean isExcluded (String name)
120                     {
121                         if (getWebAppContext().isSystemClass(name)) return true;
122                         if (getWebAppContext().isServerClass(name)) return false;
123                         return false;
124                     }
125 
126                     public boolean shouldOverride (String name)
127                     {
128                         //looking at webapp classpath, found already-parsed class of same name - did it come from system or duplicate in webapp?
129                         if (getWebAppContext().isParentLoaderPriority())
130                             return false;
131                         return true;
132                     }
133                 });
134         
135         AnnotationProcessor processor = new AnnotationProcessor(getWebAppContext(), finder, _runAsCollection, _injections, _callbacks, 
136                 LazyList.getList(_servlets), LazyList.getList(_filters), LazyList.getList(_listeners), 
137                 LazyList.getList(_servletMappings), LazyList.getList(_filterMappings));
138         processor.process();
139         _servlets = processor.getServlets();
140         _filters = processor.getFilters();
141         _servletMappings = processor.getServletMappings();
142         _filterMappings = processor.getFilterMappings();
143         _listeners = processor.getListeners();
144         _servletHandler.setFilters((FilterHolder[])LazyList.toArray(_filters,FilterHolder.class));
145         _servletHandler.setFilterMappings((FilterMapping[])LazyList.toArray(_filterMappings,FilterMapping.class));
146         _servletHandler.setServlets((ServletHolder[])LazyList.toArray(_servlets,ServletHolder.class));
147         _servletHandler.setServletMappings((ServletMapping[])LazyList.toArray(_servletMappings,ServletMapping.class));
148         getWebAppContext().setEventListeners((EventListener[])LazyList.toArray(_listeners,EventListener.class));
149     }
150 }