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      }
66  
67      public boolean isPreconfigure()
68      {
69          return _preconfigure;
70      }
71  
72      /** 
73       * Preconfigure webapp
74       * @param preconfigure  If true, then starting the webapp will generate 
75       * the WEB-INF/quickstart-web.xml rather than start the webapp.
76       */
77      public void setPreconfigure(boolean preconfigure)
78      {
79          _preconfigure = preconfigure;
80      }
81  
82      public boolean isAutoPreconfigure()
83      {
84          return _autoPreconfigure;
85      }
86      
87      public void setAutoPreconfigure(boolean autoPrecompile)
88      {
89          _autoPreconfigure = autoPrecompile;
90      }
91      
92      @Override
93      protected void startWebapp() throws Exception
94      {
95          if (isPreconfigure())
96              generateQuickstartWebXml(_preconfigProcessor.getXML());
97          
98          if (_startWebapp)
99              super.startWebapp();
100     }
101     
102     @Override
103     protected void stopWebapp() throws Exception
104     {
105         if (!_startWebapp)
106             return;
107         
108         super.stopWebapp();
109     }
110     
111     @Override
112     protected void doStart() throws Exception
113     {
114         // unpack and Adjust paths.
115         Resource war = null;
116         Resource dir = null;
117 
118         Resource base = getBaseResource();
119         if (base==null)
120             base=Resource.newResource(getWar());
121 
122         if (base.isDirectory())
123             dir=base;
124         else if (base.toString().toLowerCase(Locale.ENGLISH).endsWith(".war"))
125         {
126             war=base;
127             String w=war.toString();
128             dir=Resource.newResource(w.substring(0,w.length()-4));
129 
130             if (!dir.exists())
131             {                       
132                 LOG.info("Quickstart Extract " + war + " to " + dir);
133                 dir.getFile().mkdirs();
134                 JarResource.newJarResource(war).copyTo(dir.getFile());
135             }
136 
137             setWar(null);
138             setBaseResource(dir);
139         }
140         else 
141             throw new IllegalArgumentException();
142 
143 
144         Resource qswebxml=dir.addPath("/WEB-INF/quickstart-web.xml");
145         
146         if (isPreconfigure())
147         {
148             _preconfigProcessor = new PreconfigureDescriptorProcessor();
149             getMetaData().addDescriptorProcessor(_preconfigProcessor);
150             _startWebapp=false;
151         }
152         else if (qswebxml.exists())
153         {
154             setConfigurationClasses(__configurationClasses);
155             _startWebapp=true;
156         }
157         else if (_autoPreconfigure)
158         {   
159             LOG.info("Quickstart preconfigure: {}(war={},dir={})",this,war,dir);
160 
161             _preconfigProcessor = new PreconfigureDescriptorProcessor();    
162             getMetaData().addDescriptorProcessor(_preconfigProcessor);
163             setPreconfigure(true);
164             _startWebapp=true;
165         }
166         else
167             _startWebapp=true;
168             
169         super.doStart();
170     }
171 
172     public void generateQuickstartWebXml(String extraXML) throws Exception
173     {
174         Resource descriptor = getWebInf().addPath(QuickStartDescriptorGenerator.DEFAULT_QUICKSTART_DESCRIPTOR_NAME);
175         if (!descriptor.exists())
176             descriptor.getFile().createNewFile();
177         QuickStartDescriptorGenerator generator = new QuickStartDescriptorGenerator(this, extraXML);
178         try (FileOutputStream fos = new FileOutputStream(descriptor.getFile()))
179         {
180             generator.generateQuickStartWebXml(fos);
181         }
182     } 
183 }