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