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 quickstart-web.xml in WEB-INF of webapp
76          Resource quickStartWebXml = getQuickStartWebXml(context);
77          LOG.debug("quickStartWebXml={}",quickStartWebXml);
78          
79          context.getMetaData().setWebXml(quickStartWebXml);
80      }
81  
82      
83      /**
84       * Get the quickstart-web.xml file as a Resource.
85       * 
86       * @param context
87       * @return
88       * @throws Exception
89       */
90      public Resource getQuickStartWebXml (WebAppContext context) throws Exception
91      {
92          Resource webInf = context.getWebInf();
93          if (webInf == null || !webInf.exists())
94              throw new IllegalStateException("No WEB-INF");
95          LOG.debug("webinf={}",webInf);
96    
97          Resource quickStartWebXml = webInf.addPath("quickstart-web.xml");
98          if (!quickStartWebXml.exists())
99              throw new IllegalStateException ("No WEB-INF/quickstart-web.xml");
100         return quickStartWebXml;
101     }
102     
103     
104     
105     /**
106      * @see org.eclipse.jetty.webapp.AbstractConfiguration#configure(org.eclipse.jetty.webapp.WebAppContext)
107      */
108     @Override
109     public void configure(WebAppContext context) throws Exception
110     {
111         LOG.debug("configure {}",this);
112         if (context.isStarted())
113         {
114             LOG.warn("Cannot configure webapp after it is started");
115             return;
116         }
117         
118         //Temporary:  set up the classpath here. This should be handled by the QuickStartDescriptorProcessor
119         Resource webInf = context.getWebInf();
120 
121         if (webInf != null && webInf.isDirectory() && context.getClassLoader() instanceof WebAppClassLoader)
122         {
123             // Look for classes directory
124             Resource classes= webInf.addPath("classes/");
125             if (classes.exists())
126                 ((WebAppClassLoader)context.getClassLoader()).addClassPath(classes);
127 
128             // Look for jars
129             Resource lib= webInf.addPath("lib/");
130             if (lib.exists() || lib.isDirectory())
131                 ((WebAppClassLoader)context.getClassLoader()).addJars(lib);
132         }
133 
134         //add the processor to handle normal web.xml content
135         context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());
136         
137         //add a processor to handle extended web.xml format
138         context.getMetaData().addDescriptorProcessor(new QuickStartDescriptorProcessor());
139         
140         //add a decorator that will find introspectable annotations
141         context.addDecorator(new AnnotationDecorator(context)); //this must be the last Decorator because they are run in reverse order!
142         
143         //add a context bean that will run ServletContainerInitializers as the context starts
144         ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);
145         if (starter != null)
146             throw new IllegalStateException("ServletContainerInitializersStarter already exists");
147         starter = new ServletContainerInitializersStarter(context);
148         context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER, starter);
149         context.addBean(starter, true);       
150 
151         LOG.debug("configured {}",this);
152     }
153 
154 }