View Javadoc

1   package org.apache.tomcat.maven.runner;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.commons.cli.CommandLine;
22  import org.apache.commons.cli.CommandLineParser;
23  import org.apache.commons.cli.GnuParser;
24  import org.apache.commons.cli.HelpFormatter;
25  import org.apache.commons.cli.Option;
26  import org.apache.commons.cli.OptionBuilder;
27  import org.apache.commons.cli.Options;
28  import org.apache.commons.cli.ParseException;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.util.Map;
33  import java.util.Properties;
34  
35  /**
36   * @author Olivier Lamy
37   * @since 2.0
38   */
39  @SuppressWarnings( "static-access" )
40  public class Tomcat7RunnerCli
41  {
42  
43      public static final String STAND_ALONE_PROPERTIES_FILENAME = "tomcat.standalone.properties";
44  
45      static Option httpPort =
46          OptionBuilder.withArgName( "httpPort" ).hasArg().withDescription( "http port to use" ).create( "httpPort" );
47  
48      static Option httpsPort =
49          OptionBuilder.withArgName( "httpsPort" ).hasArg().withDescription( "https port to use" ).create( "httpsPort" );
50  
51      static Option ajpPort =
52          OptionBuilder.withArgName( "ajpPort" ).hasArg().withDescription( "ajp port to use" ).create( "ajpPort" );
53  
54      static Option serverXmlPath =
55          OptionBuilder.withArgName( "serverXmlPath" ).hasArg().withDescription( "server.xml to use, optional" ).create(
56              "serverXmlPath" );
57  
58      static Option resetExtract =
59          OptionBuilder.withArgName( "resetExtract" ).withDescription( "clean previous extract directory" ).create(
60              "resetExtract" );
61  
62      static Option help = OptionBuilder.withLongOpt( "help" ).withDescription( "help" ).create( 'h' );
63  
64      static Option debug = OptionBuilder.withLongOpt( "debug" ).withDescription( "debug" ).create( 'X' );
65  
66      static Option sysProps = OptionBuilder.withDescription( "use value for given property" ).hasArgs().withDescription(
67          "key=value" ).withValueSeparator().create( 'D' );
68  
69      static Option clientAuth =
70          OptionBuilder.withArgName( "clientAuth" ).withDescription( "enable client authentication for https" ).create(
71              "clientAuth" );
72  
73      static Option keyAlias =
74          OptionBuilder.withArgName( "keyAlias" ).hasArgs().withDescription( "alias from keystore for ssl" ).create(
75              "keyAlias" );
76  
77      static Option obfuscate =
78          OptionBuilder.withArgName( "password" ).hasArgs().withDescription( "obfuscate the password and exit" ).create(
79              "obfuscate" );
80  
81      static Option httpProtocol = OptionBuilder.withArgName( "httpProtocol" ).hasArg().withDescription(
82          "http protocol to use: HTTP/1.1 or org.apache.coyote.http11.Http11NioProtocol" ).create( "httpProtocol" );
83  
84      static Option extractDirectory = OptionBuilder.withArgName( "extractDirectory" ).hasArg().withDescription(
85          "path to extract war content, default value: .extract" ).create( "extractDirectory" );
86  
87      static Option loggerName = OptionBuilder.withArgName( "loggerName" ).hasArg().withDescription(
88          "logger to use: slf4j to use slf4j bridge on top of jul" ).create( "loggerName" );
89  
90      static Option uriEncoding = OptionBuilder.withArgName( "uriEncoding" ).hasArg().withDescription(
91          "connector uriEncoding default ISO-8859-1" ).create( "uriEncoding" );
92  
93      static Options options = new Options();
94  
95      static
96      {
97          options.addOption( httpPort ).addOption( httpsPort ).addOption( ajpPort ).addOption( serverXmlPath ).addOption(
98              resetExtract ).addOption( help ).addOption( debug ).addOption( sysProps ).addOption(
99              httpProtocol ).addOption( clientAuth ).addOption( keyAlias ).addOption( obfuscate ).addOption(
100             extractDirectory ).addOption( loggerName ).addOption( uriEncoding );
101     }
102 
103 
104     public static void main( String[] args )
105         throws Exception
106     {
107         CommandLineParser parser = new GnuParser();
108         CommandLine line = null;
109         try
110         {
111             line = parser.parse( Tomcat7RunnerCli.options, args );
112         }
113         catch ( ParseException e )
114         {
115             System.err.println( "Parsing failed.  Reason: " + e.getMessage() );
116             HelpFormatter formatter = new HelpFormatter();
117             formatter.printHelp( getCmdLineSyntax(), Tomcat7RunnerCli.options );
118             System.exit( 1 );
119         }
120 
121         if ( line.hasOption( help.getOpt() ) )
122         {
123             HelpFormatter formatter = new HelpFormatter();
124             formatter.printHelp( getCmdLineSyntax(), Tomcat7RunnerCli.options );
125             System.exit( 0 );
126         }
127 
128         if ( line.hasOption( obfuscate.getOpt() ) )
129         {
130             System.out.println( PasswordUtil.obfuscate( line.getOptionValue( obfuscate.getOpt() ) ) );
131             System.exit( 0 );
132         }
133         Tomcat7Runner tomcat7Runner = new Tomcat7Runner();
134 
135         tomcat7Runner.runtimeProperties = buildStandaloneProperties();
136 
137         if ( line.hasOption( serverXmlPath.getOpt() ) )
138         {
139             tomcat7Runner.serverXmlPath = line.getOptionValue( serverXmlPath.getOpt() );
140         }
141 
142         String port = tomcat7Runner.runtimeProperties.getProperty( Tomcat7Runner.HTTP_PORT_KEY );
143         if ( port != null)
144         {
145             tomcat7Runner.httpPort = Integer.parseInt( port );
146         }
147 
148         // cli win for the port
149         if ( line.hasOption( httpPort.getOpt() ) )
150         {
151             tomcat7Runner.httpPort = Integer.parseInt( line.getOptionValue( httpPort.getOpt() ) );
152         }
153 
154         if ( line.hasOption( httpsPort.getOpt() ) )
155         {
156             tomcat7Runner.httpsPort = Integer.parseInt( line.getOptionValue( httpsPort.getOpt() ) );
157         }
158         if ( line.hasOption( ajpPort.getOpt() ) )
159         {
160             tomcat7Runner.ajpPort = Integer.parseInt( line.getOptionValue( ajpPort.getOpt() ) );
161         }
162         if ( line.hasOption( resetExtract.getOpt() ) )
163         {
164             tomcat7Runner.resetExtract = true;
165         }
166         if ( line.hasOption( debug.getOpt() ) )
167         {
168             tomcat7Runner.debug = true;
169         }
170 
171         if ( line.hasOption( httpProtocol.getOpt() ) )
172         {
173             tomcat7Runner.httpProtocol = line.getOptionValue( httpProtocol.getOpt() );
174         }
175 
176         if ( line.hasOption( sysProps.getOpt() ) )
177         {
178             Properties systemProperties = line.getOptionProperties( sysProps.getOpt() );
179             if ( systemProperties != null && !systemProperties.isEmpty() )
180             {
181                 for ( Map.Entry<Object, Object> sysProp : systemProperties.entrySet() )
182                 {
183                     System.setProperty( (String) sysProp.getKey(), (String) sysProp.getValue() );
184                 }
185             }
186         }
187         if ( line.hasOption( clientAuth.getOpt() ) )
188         {
189             tomcat7Runner.clientAuth = clientAuth.getOpt();
190         }
191         if ( line.hasOption( keyAlias.getOpt() ) )
192         {
193             tomcat7Runner.keyAlias = line.getOptionValue( keyAlias.getOpt() );
194         }
195 
196         if ( line.hasOption( extractDirectory.getOpt() ) )
197         {
198             tomcat7Runner.extractDirectory = line.getOptionValue( extractDirectory.getOpt() );
199         }
200 
201         if ( line.hasOption( loggerName.getOpt() ) )
202         {
203             tomcat7Runner.loggerName = line.getOptionValue( loggerName.getOpt() );
204         }
205 
206         if ( line.hasOption( uriEncoding.getOpt() ) )
207         {
208             tomcat7Runner.uriEncoding = line.getOptionValue( uriEncoding.getOpt() );
209         }
210 
211         // here we go
212         tomcat7Runner.run();
213     }
214 
215     private static Properties buildStandaloneProperties()
216         throws IOException
217     {
218         InputStream is =
219             Thread.currentThread().getContextClassLoader().getResourceAsStream( STAND_ALONE_PROPERTIES_FILENAME );
220         Properties properties = new Properties();
221         properties.load( is );
222         return properties;
223     }
224 
225     public static String getCmdLineSyntax()
226     {
227         return "java -jar [path to your exec war jar]";
228     }
229 }