View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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     @Override
108     protected void doStart() throws Exception
109     {
110         // unpack and Adjust paths.
111         Resource war = null;
112         Resource dir = null;
113 
114         Resource base = getBaseResource();
115         if (base==null)
116             base=Resource.newResource(getWar());
117 
118         if (base.isDirectory())
119             dir=base;
120         else if (base.toString().toLowerCase().endsWith(".war"))
121         {
122             war=base;
123             String w=war.toString();
124             dir=Resource.newResource(w.substring(0,w.length()-4));
125 
126             if (!dir.exists())
127             {                       
128                 LOG.info("Quickstart Extract " + war + " to " + dir);
129                 dir.getFile().mkdirs();
130                 JarResource.newJarResource(war).copyTo(dir.getFile());
131             }
132 
133             setWar(null);
134             setBaseResource(dir);
135         }
136         else 
137             throw new IllegalArgumentException();
138 
139 
140         Resource qswebxml=dir.addPath("/WEB-INF/quickstart-web.xml");
141         
142         if (isPreconfigure())
143         {
144             _preconfigProcessor = new PreconfigureDescriptorProcessor();
145             getMetaData().addDescriptorProcessor(_preconfigProcessor);
146             _startWebapp=false;
147         }
148         else if (qswebxml.exists())
149         {
150             setConfigurationClasses(__configurationClasses);
151             _startWebapp=true;
152         }
153         else if (_autoPreconfigure)
154         {   
155             LOG.info("Quickstart preconfigure: {}(war={},dir={})",this,war,dir);
156 
157             _preconfigProcessor = new PreconfigureDescriptorProcessor();    
158             getMetaData().addDescriptorProcessor(_preconfigProcessor);
159             setPreconfigure(true);
160             _startWebapp=true;
161         }
162         else
163             _startWebapp=true;
164             
165         super.doStart();
166     }
167 
168 
169     public void generateQuickstartWebXml(String extraXML) throws Exception
170     {
171         Resource descriptor = getWebInf().addPath(QuickStartDescriptorGenerator.DEFAULT_QUICKSTART_DESCRIPTOR_NAME);
172         if (!descriptor.exists())
173             descriptor.getFile().createNewFile();
174         QuickStartDescriptorGenerator generator = new QuickStartDescriptorGenerator(this, extraXML);
175         try (FileOutputStream fos = new FileOutputStream(descriptor.getFile()))
176         {
177             generator.generateQuickStartWebXml(fos);
178         }
179     }
180 
181   
182 }