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.io.FileOutputStream;
22  import java.util.Locale;
23  
24  import org.eclipse.jetty.util.log.Log;
25  import org.eclipse.jetty.util.log.Logger;
26  import org.eclipse.jetty.util.resource.JarResource;
27  import org.eclipse.jetty.util.resource.Resource;
28  import org.eclipse.jetty.webapp.WebAppContext;
29  
30  /**
31   * QuickStartWar
32   */
33  public class QuickStartWebApp extends WebAppContext
34  {
35      private static final Logger LOG = Log.getLogger(QuickStartWebApp.class);
36      
37      public static final String[] __configurationClasses = new String[] 
38              {
39                  org.eclipse.jetty.quickstart.QuickStartConfiguration.class.getCanonicalName(),
40                  org.eclipse.jetty.plus.webapp.EnvConfiguration.class.getCanonicalName(),
41                  org.eclipse.jetty.plus.webapp.PlusConfiguration.class.getCanonicalName(),
42                  org.eclipse.jetty.webapp.JettyWebXmlConfiguration.class.getCanonicalName()
43              };
44      
45      private boolean _preconfigure=false;
46      private boolean _autoPreconfigure=false;
47      private boolean _startWebapp=false;
48      private PreconfigureDescriptorProcessor _preconfigProcessor;
49      
50      public static final String[] __preconfigurationClasses = new String[]
51      { 
52          org.eclipse.jetty.webapp.WebInfConfiguration.class.getCanonicalName(), 
53          org.eclipse.jetty.webapp.WebXmlConfiguration.class.getCanonicalName(),
54          org.eclipse.jetty.webapp.MetaInfConfiguration.class.getCanonicalName(), 
55          org.eclipse.jetty.webapp.FragmentConfiguration.class.getCanonicalName(),
56          org.eclipse.jetty.plus.webapp.EnvConfiguration.class.getCanonicalName(), 
57          org.eclipse.jetty.plus.webapp.PlusConfiguration.class.getCanonicalName(),
58          org.eclipse.jetty.annotations.AnnotationConfiguration.class.getCanonicalName(),
59      };
60      
61      public QuickStartWebApp()
62      {
63          super();
64          setConfigurationClasses(__preconfigurationClasses);
65          setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",".*\\.jar");
66      }
67  
68      public boolean isPreconfigure()
69      {
70          return _preconfigure;
71      }
72  
73      /** 
74       * Preconfigure webapp
75       * @param preconfigure  If true, then starting the webapp will generate 
76       * the WEB-INF/quickstart-web.xml rather than start the webapp.
77       */
78      public void setPreconfigure(boolean preconfigure)
79      {
80          _preconfigure = preconfigure;
81      }
82  
83      public boolean isAutoPreconfigure()
84      {
85          return _autoPreconfigure;
86      }
87      
88      public void setAutoPreconfigure(boolean autoPrecompile)
89      {
90          _autoPreconfigure = autoPrecompile;
91      }
92      
93      @Override
94      protected void startWebapp() throws Exception
95      {
96          if (isPreconfigure())
97              generateQuickstartWebXml(_preconfigProcessor.getXML());
98          
99          if (_startWebapp)
100             super.startWebapp();
101     }
102     
103     @Override
104     protected void stopWebapp() throws Exception
105     {
106         if (!_startWebapp)
107             return;
108         
109         super.stopWebapp();
110     }
111     
112     @Override
113     protected void doStart() throws Exception
114     {
115         // unpack and Adjust paths.
116         Resource war = null;
117         Resource dir = null;
118 
119         Resource base = getBaseResource();
120         if (base==null)
121             base=Resource.newResource(getWar());
122 
123         if (base.isDirectory())
124             dir=base;
125         else if (base.toString().toLowerCase(Locale.ENGLISH).endsWith(".war"))
126         {
127             war=base;
128             String w=war.toString();
129             dir=Resource.newResource(w.substring(0,w.length()-4));
130 
131             if (!dir.exists())
132             {                       
133                 LOG.info("Quickstart Extract " + war + " to " + dir);
134                 dir.getFile().mkdirs();
135                 JarResource.newJarResource(war).copyTo(dir.getFile());
136             }
137 
138             setWar(null);
139             setBaseResource(dir);
140         }
141         else 
142             throw new IllegalArgumentException();
143 
144 
145         Resource qswebxml=dir.addPath("/WEB-INF/quickstart-web.xml");
146         
147         if (isPreconfigure())
148         {
149             _preconfigProcessor = new PreconfigureDescriptorProcessor();
150             getMetaData().addDescriptorProcessor(_preconfigProcessor);
151             _startWebapp=false;
152         }
153         else if (qswebxml.exists())
154         {
155             setConfigurationClasses(__configurationClasses);
156             _startWebapp=true;
157         }
158         else if (_autoPreconfigure)
159         {   
160             LOG.info("Quickstart preconfigure: {}(war={},dir={})",this,war,dir);
161 
162             _preconfigProcessor = new PreconfigureDescriptorProcessor();    
163             getMetaData().addDescriptorProcessor(_preconfigProcessor);
164             setPreconfigure(true);
165             _startWebapp=true;
166         }
167         else
168             _startWebapp=true;
169             
170         super.doStart();
171     }
172 
173     public void generateQuickstartWebXml(String extraXML) throws Exception
174     {
175         Resource descriptor = getWebInf().addPath(QuickStartDescriptorGenerator.DEFAULT_QUICKSTART_DESCRIPTOR_NAME);
176         if (!descriptor.exists())
177             descriptor.getFile().createNewFile();
178         QuickStartDescriptorGenerator generator = new QuickStartDescriptorGenerator(this, extraXML);
179         try (FileOutputStream fos = new FileOutputStream(descriptor.getFile()))
180         {
181             generator.generateQuickStartWebXml(fos);
182         }
183     } 
184 }