View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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 org.eclipse.jetty.annotations.AnnotationConfiguration;
22  import org.eclipse.jetty.annotations.AnnotationDecorator;
23  import org.eclipse.jetty.annotations.ServletContainerInitializersStarter;
24  import org.eclipse.jetty.util.log.Log;
25  import org.eclipse.jetty.util.log.Logger;
26  import org.eclipse.jetty.util.resource.Resource;
27  import org.eclipse.jetty.webapp.StandardDescriptorProcessor;
28  import org.eclipse.jetty.webapp.WebAppClassLoader;
29  import org.eclipse.jetty.webapp.WebAppContext;
30  import org.eclipse.jetty.webapp.WebInfConfiguration;
31  
32  /**
33   * QuickStartConfiguration
34   * 
35   * Re-inflate a deployable webapp from a saved effective-web.xml
36   * which combines all pre-parsed web xml descriptors and annotations.
37   * 
38   */
39  public class QuickStartConfiguration extends WebInfConfiguration
40  {
41      private static final Logger LOG = Log.getLogger(QuickStartConfiguration.class);
42  
43      /**
44       * @see org.eclipse.jetty.webapp.AbstractConfiguration#preConfigure(org.eclipse.jetty.webapp.WebAppContext)
45       */
46      @Override
47      public void preConfigure(WebAppContext context) throws Exception
48      {
49          //check that webapp is suitable for quick start - it is not a packed war
50          String war = context.getWar();
51          if (war == null || war.length()<=0)
52              throw new IllegalStateException ("No location for webapp");  
53          
54          //Make a temp directory for the webapp if one is not already set
55          resolveTempDirectory(context);
56  
57          Resource webApp = context.newResource(war);
58  
59          // Accept aliases for WAR files
60          if (webApp.getAlias() != null)
61          {
62              LOG.debug(webApp + " anti-aliased to " + webApp.getAlias());
63              webApp = context.newResource(webApp.getAlias());
64          }
65  
66          // Is the WAR usable directly?
67          if (!webApp.exists() || !webApp.isDirectory() || webApp.toString().startsWith("jar:"))
68              throw new IllegalStateException("Webapp does not exist or is not unpacked");
69          
70          context.setBaseResource(webApp);
71  
72          LOG.debug("webapp={}",webApp);
73  
74          
75          //look for effective-web.xml in WEB-INF of webapp
76          Resource webInf = context.getWebInf();
77          if (webInf == null || !webInf.exists())
78              throw new IllegalStateException("No WEB-INF");
79          LOG.debug("webinf={}",webInf);
80    
81          Resource quickStartWebXml = webInf.addPath("quickstart-web.xml");
82          if (!quickStartWebXml.exists())
83              throw new IllegalStateException ("No WEB-INF/quickstart-web.xml");
84          LOG.debug("quickStartWebXml={}",quickStartWebXml);
85          
86          context.getMetaData().setWebXml(quickStartWebXml);
87      }
88  
89      
90      /**
91       * @see org.eclipse.jetty.webapp.AbstractConfiguration#configure(org.eclipse.jetty.webapp.WebAppContext)
92       */
93      @Override
94      public void configure(WebAppContext context) throws Exception
95      {
96          LOG.debug("configure {}",this);
97          if (context.isStarted())
98          {
99              LOG.warn("Cannot configure webapp after it is started");
100             return;
101         }
102         
103         //Temporary:  set up the classpath here. This should be handled by the QuickStartDescriptorProcessor
104         Resource webInf = context.getWebInf();
105 
106         if (webInf != null && webInf.isDirectory() && context.getClassLoader() instanceof WebAppClassLoader)
107         {
108             // Look for classes directory
109             Resource classes= webInf.addPath("classes/");
110             if (classes.exists())
111                 ((WebAppClassLoader)context.getClassLoader()).addClassPath(classes);
112 
113             // Look for jars
114             Resource lib= webInf.addPath("lib/");
115             if (lib.exists() || lib.isDirectory())
116                 ((WebAppClassLoader)context.getClassLoader()).addJars(lib);
117         }
118 
119         //add the processor to handle normal web.xml content
120         context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
121         
122         //add a processor to handle extended web.xml format
123         context.getMetaData().addDescriptorProcessor(new QuickStartDescriptorProcessor());
124         
125         //add a decorator that will find introspectable annotations
126         context.addDecorator(new AnnotationDecorator(context)); //this must be the last Decorator because they are run in reverse order!
127         
128         //add a context bean that will run ServletContainerInitializers as the context starts
129         ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);
130         if (starter != null)
131             throw new IllegalStateException("ServletContainerInitializersStarter already exists");
132         starter = new ServletContainerInitializersStarter(context);
133         context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER, starter);
134         context.addBean(starter, true);       
135 
136         LOG.debug("configured {}",this);
137     }
138 
139 }