View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.quickstart;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.HashSet;
24  import java.util.List;
25  
26  import javax.servlet.ServletContext;
27  
28  import org.eclipse.jetty.annotations.AnnotationConfiguration;
29  import org.eclipse.jetty.annotations.ServletContainerInitializersStarter;
30  import org.eclipse.jetty.plus.annotation.ContainerInitializer;
31  import org.eclipse.jetty.util.QuotedStringTokenizer;
32  import org.eclipse.jetty.util.resource.Resource;
33  import org.eclipse.jetty.util.resource.ResourceCollection;
34  import org.eclipse.jetty.webapp.Descriptor;
35  import org.eclipse.jetty.webapp.IterativeDescriptorProcessor;
36  import org.eclipse.jetty.webapp.MetaInfConfiguration;
37  import org.eclipse.jetty.webapp.WebAppContext;
38  import org.eclipse.jetty.xml.XmlParser;
39  
40  /**
41   * QuickStartDescriptorProcessor
42   * 
43   * Handle  extended elements for quickstart-web.xml
44   */
45  public class QuickStartDescriptorProcessor extends IterativeDescriptorProcessor
46  {
47      public QuickStartDescriptorProcessor()
48      {
49          try
50          {
51              registerVisitor("context-param", this.getClass().getMethod("visitContextParam", __signature));
52          }    
53          catch (Exception e)
54          {
55              throw new IllegalStateException(e);
56          }
57      }
58  
59      /**
60       * @see org.eclipse.jetty.webapp.IterativeDescriptorProcessor#start(org.eclipse.jetty.webapp.WebAppContext, org.eclipse.jetty.webapp.Descriptor)
61       */
62      @Override
63      public void start(WebAppContext context, Descriptor descriptor)
64      {
65      }
66  
67      /**
68       * @see org.eclipse.jetty.webapp.IterativeDescriptorProcessor#end(org.eclipse.jetty.webapp.WebAppContext, org.eclipse.jetty.webapp.Descriptor)
69       */
70      @Override
71      public void end(WebAppContext context, Descriptor descriptor)
72      { 
73      }
74  
75      public void visitContextParam (WebAppContext context, Descriptor descriptor, XmlParser.Node node)
76              throws Exception
77      {
78          String name = node.getString("param-name", false, true);
79          String value = node.getString("param-value", false, true);
80          List<String> values = new ArrayList<>();
81          
82          // extract values
83          switch(name)
84          {
85              case ServletContext.ORDERED_LIBS:
86              case AnnotationConfiguration.CONTAINER_INITIALIZERS:
87              case MetaInfConfiguration.METAINF_TLDS:
88              case MetaInfConfiguration.METAINF_RESOURCES:
89  
90                  context.removeAttribute(name);
91                  
92                  QuotedStringTokenizer tok = new QuotedStringTokenizer(value,",");
93                  while(tok.hasMoreElements())
94                      values.add(tok.nextToken().trim());
95                  
96                  break;
97                  
98              default:
99                  values.add(value);
100         }
101 
102         AttributeNormalizer normalizer = new AttributeNormalizer(context.getBaseResource());
103         // handle values
104         switch(name)
105         {
106             case ServletContext.ORDERED_LIBS:
107             {
108                 List<Object> libs = new ArrayList<>();
109                 Object o=context.getAttribute(ServletContext.ORDERED_LIBS);
110                 if (o instanceof Collection<?>)
111                     libs.addAll((Collection<?>)o);
112                 libs.addAll(values);
113                 if (libs.size()>0)
114                     context.setAttribute(ServletContext.ORDERED_LIBS,libs);
115                 
116                 break;
117             }
118                 
119             case AnnotationConfiguration.CONTAINER_INITIALIZERS:
120             {
121                 for (String i : values)
122                     visitContainerInitializer(context, new ContainerInitializer(Thread.currentThread().getContextClassLoader(), i));
123                 break;
124             }
125             
126             case MetaInfConfiguration.METAINF_TLDS:
127             {
128                 List<Object> tlds = new ArrayList<>();
129                 Object o=context.getAttribute(MetaInfConfiguration.METAINF_TLDS);
130                 if (o instanceof Collection<?>)
131                     tlds.addAll((Collection<?>)o);
132                 for (String i : values)
133                 {
134                     Resource r = Resource.newResource(normalizer.expand(i));
135                     if (r.exists())
136                         tlds.add(r.getURI().toURL());
137                     else
138                         throw new IllegalArgumentException("TLD not found: "+r);                    
139                 }
140                 
141                 if (tlds.size()>0)
142                     context.setAttribute(MetaInfConfiguration.METAINF_TLDS,tlds);
143                 break;
144             }
145             
146             case MetaInfConfiguration.METAINF_RESOURCES:
147             {
148                 for (String i : values)
149                 {
150                     Resource r = Resource.newResource(normalizer.expand(i));
151                     if (r.exists())
152                         visitMetaInfResource(context,r); 
153                     else
154                         throw new IllegalArgumentException("Resource not found: "+r);                    
155                 }
156                 break;
157             }
158                 
159             default:
160                 
161         }
162     }
163     
164 
165     public void visitContainerInitializer (WebAppContext context, ContainerInitializer containerInitializer)
166     {
167         if (containerInitializer == null)
168             return;
169         
170         //add the ContainerInitializer to the list of container initializers
171         List<ContainerInitializer> containerInitializers = (List<ContainerInitializer>)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZERS);
172         if (containerInitializers == null)
173         {
174             containerInitializers = new ArrayList<ContainerInitializer>();
175             context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZERS, containerInitializers);
176         }
177         
178         containerInitializers.add(containerInitializer);
179 
180         //Ensure a bean is set up on the context that will invoke the ContainerInitializers as the context starts
181         ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);
182         if (starter == null)
183         {
184             starter = new ServletContainerInitializersStarter(context);
185             context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER, starter);
186             context.addBean(starter, true);
187         }
188     }
189     
190     
191     public void visitMetaInfResource (WebAppContext context, Resource dir)
192     {
193         Collection<Resource> metaInfResources =  (Collection<Resource>)context.getAttribute(MetaInfConfiguration.METAINF_RESOURCES);
194         if (metaInfResources == null)
195         {
196             metaInfResources = new HashSet<Resource>();
197             context.setAttribute(MetaInfConfiguration.METAINF_RESOURCES, metaInfResources);
198         }
199         metaInfResources.add(dir);
200         //also add to base resource of webapp
201         Resource[] collection=new Resource[metaInfResources.size()+1];
202         int i=0;
203         collection[i++]=context.getBaseResource();
204         for (Resource resource : metaInfResources)
205             collection[i++]=resource;
206         context.setBaseResource(new ResourceCollection(collection));
207     }
208 }