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