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