View Javadoc

1   // ========================================================================
2   // Copyright (c) 2010 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.util.ArrayList;
17  
18  import javax.servlet.annotation.WebInitParam;
19  import javax.servlet.annotation.WebServlet;
20  import javax.servlet.http.HttpServlet;
21  
22  import org.eclipse.jetty.servlet.Holder;
23  import org.eclipse.jetty.servlet.ServletHolder;
24  import org.eclipse.jetty.servlet.ServletMapping;
25  import org.eclipse.jetty.util.LazyList;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.webapp.DiscoveredAnnotation;
28  import org.eclipse.jetty.webapp.MetaData;
29  import org.eclipse.jetty.webapp.WebAppContext;
30  import org.eclipse.jetty.webapp.Origin;
31  
32  /**
33   * WebServletAnnotation
34   *
35   *
36   */
37  public class WebServletAnnotation extends DiscoveredAnnotation
38  {
39      
40      public WebServletAnnotation (WebAppContext context, String className)
41      {
42          super(context, className);
43      }
44  
45      /** 
46       * @see org.eclipse.jetty.annotations.ClassAnnotation#apply()
47       */
48      public void apply()
49      {
50          //TODO check this algorithm with new rules for applying descriptors and annotations in order
51          Class clazz = getTargetClass();
52          
53          if (clazz == null)
54          {
55              Log.warn(_className+" cannot be loaded");
56              return;
57          }
58          
59          //Servlet Spec 8.1.1
60          if (!HttpServlet.class.isAssignableFrom(clazz))
61          {
62              Log.warn(clazz.getName()+" is not assignable from javax.servlet.http.HttpServlet");
63              return;
64          }
65          
66          WebServlet annotation = (WebServlet)clazz.getAnnotation(WebServlet.class);
67          
68          if (annotation.urlPatterns().length > 0 && annotation.value().length > 0)
69          {
70              Log.warn(clazz.getName()+ " defines both @WebServlet.value and @WebServlet.urlPatterns");
71              return;
72          }
73          
74          String[] urlPatterns = annotation.value();
75          if (urlPatterns.length == 0)
76              urlPatterns = annotation.urlPatterns();
77          
78          if (urlPatterns.length == 0)
79          {
80              Log.warn(clazz.getName()+ " defines neither @WebServlet.value nor @WebServlet.urlPatterns");
81              return;
82          }
83          
84          //canonicalize the patterns
85          ArrayList<String> urlPatternList = new ArrayList<String>();
86          for (String p : urlPatterns)
87              urlPatternList.add(Util.normalizePattern(p));
88          
89          String servletName = (annotation.name().equals("")?clazz.getName():annotation.name());
90          
91          MetaData metaData = _context.getMetaData();
92  
93          //Find out if a <servlet>  of this type already exists with this name
94          ServletHolder[] holders = _context.getServletHandler().getServlets();
95          boolean isNew = true;
96          ServletHolder holder = null;
97          if (holders != null)
98          {
99              for (ServletHolder h : holders)
100             {
101                 if (h.getClassName().equals(clazz.getName()) && h.getName().equals(servletName))
102                 {
103                     holder = h;
104                     isNew = false;
105                     break;
106                 }
107             }
108         }
109 
110         if (isNew)
111         {
112             //No servlet of this name has already been defined, either by a descriptor
113             //or another annotation (which would be impossible).
114             holder = _context.getServletHandler().newServletHolder(Holder.Source.ANNOTATION);
115             holder.setHeldClass(clazz);   
116             metaData.setOrigin(servletName+".servlet.servlet-class");
117             
118             holder.setName(servletName);
119             holder.setDisplayName(annotation.displayName());
120             metaData.setOrigin(servletName+".servlet.display-name");
121             
122             holder.setInitOrder(annotation.loadOnStartup());
123             metaData.setOrigin(servletName+".servlet.load-on-startup");
124             
125             holder.setAsyncSupported(annotation.asyncSupported());
126             metaData.setOrigin(servletName+".servlet.async-supported");
127             
128             for (WebInitParam ip:annotation.initParams())
129             {
130                 holder.setInitParameter(ip.name(), ip.value());
131                 metaData.setOrigin(servletName+".servlet.init-param."+ip.name());
132             }
133           
134             _context.getServletHandler().addServlet(holder);
135             ServletMapping mapping = new ServletMapping();  
136             mapping.setServletName(holder.getName());
137             mapping.setPathSpecs( LazyList.toStringArray(urlPatternList));
138             _context.getServletHandler().addServletMapping(mapping);
139             metaData.setOrigin(servletName+".servlet.mappings");
140         }
141         else
142         {
143             //check if the existing servlet has each init-param from the annotation
144             //if not, add it
145             for (WebInitParam ip:annotation.initParams())
146             {
147               //if (holder.getInitParameter(ip.name()) == null)
148                 if (metaData.getOrigin(servletName+".servlet.init-param"+ip.name())==Origin.NotSet)
149                 {
150                     holder.setInitParameter(ip.name(), ip.value());
151                     metaData.setOrigin(servletName+".servlet.init-param."+ip.name());
152                 }  
153             }
154             
155             //check the url-patterns, if there annotation has a new one, add it
156             ServletMapping[] mappings = _context.getServletHandler().getServletMappings();
157 
158             //ServletSpec 3.0 p81 If a servlet already has url mappings from a 
159             //descriptor the annotation is ignored
160             if (mappings == null && metaData.getOriginDescriptor(servletName+".servlet.mappings") != null)
161             {
162                 ServletMapping mapping = new ServletMapping();
163                 mapping.setServletName(servletName);
164                 mapping.setPathSpecs(LazyList.toStringArray(urlPatternList));
165                 _context.getServletHandler().addServletMapping(mapping); 
166             }
167         }
168     }   
169 }