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 org.eclipse.jetty.server.Server;
22  import org.eclipse.jetty.util.log.Log;
23  import org.eclipse.jetty.util.log.Logger;
24  import org.eclipse.jetty.util.resource.JarResource;
25  import org.eclipse.jetty.util.resource.Resource;
26  import org.eclipse.jetty.xml.XmlConfiguration;
27  
28  public class PreconfigureQuickStartWar
29  {
30      private static final Logger LOG = Log.getLogger(PreconfigureQuickStartWar.class);
31      static final boolean ORIGIN=LOG.isDebugEnabled();
32      
33      public static void main(String... args) throws Exception
34      {
35          Resource war = null;
36          Resource dir = null;
37          Resource xml = null;
38  
39          switch (args.length)
40          {
41              case 0:
42                  error("No WAR file or directory given");
43                  break;
44  
45              case 1:
46                  dir = Resource.newResource(args[0]);
47                  break;
48  
49              case 2:
50                  war = Resource.newResource(args[0]);
51                  if (war.isDirectory())
52                  {
53                      dir = war;
54                      war = null;
55                      xml = Resource.newResource(args[1]);
56                  }
57                  else
58                  {
59                      dir = Resource.newResource(args[1]);
60                  }
61  
62                  break;
63  
64              case 3:
65                  war = Resource.newResource(args[0]);
66                  dir = Resource.newResource(args[1]);
67                  xml = Resource.newResource(args[2]);
68                  break;
69  
70              default:
71                  error("Too many args");
72                  break;
73          }
74  
75          
76          preconfigure(war,dir,xml);
77      }
78  
79      /**
80       * @param war The war (or directory) to preconfigure
81       * @param dir The directory to expand the war into (or null if war is a directory)
82       * @param xml A context XML to apply (or null if none)
83       * @throws Exception if unable to pre configure
84       */
85      public static void preconfigure(Resource war, Resource dir, Resource xml) throws Exception 
86      {
87          // Do we need to unpack a war?
88          if (war != null)
89          {
90              if (war.isDirectory())
91                  error("war file is directory");
92  
93              if (!dir.exists())
94                  dir.getFile().mkdirs();
95              JarResource.newJarResource(war).copyTo(dir.getFile());
96          }
97          
98          final Server server = new Server();
99  
100         QuickStartWebApp webapp = new QuickStartWebApp();
101 
102         if (xml != null)
103         {
104             if (xml.isDirectory() || !xml.toString().toLowerCase().endsWith(".xml"))
105                 error("Bad context.xml: "+xml);
106             XmlConfiguration xmlConfiguration = new XmlConfiguration(xml.getURL());
107             xmlConfiguration.configure(webapp);
108         }
109         webapp.setResourceBase(dir.getFile().getAbsolutePath());
110         webapp.setPreconfigure(true);
111         server.setHandler(webapp);
112         server.start();
113         server.stop();
114     }
115     
116     
117     
118 
119     private static void error(String message)
120     {
121         System.err.println("ERROR: " + message);
122         System.err.println("Usage: java -jar PreconfigureQuickStartWar.jar <war-directory>");
123         System.err.println("       java -jar PreconfigureQuickStartWar.jar <war-directory> <context-xml-file>");
124         System.err.println("       java -jar PreconfigureQuickStartWar.jar <war-file> <target-war-directory>");
125         System.err.println("       java -jar PreconfigureQuickStartWar.jar <war-file> <target-war-directory> <context-xml-file>");
126         System.exit(1);
127     }
128 
129 }