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