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 maxPostSize =
52          OptionBuilder.withArgName( "maxPostSize" ).hasArg().withDescription( "max post size to use" ).create(
53              "maxPostSize" );
54  
55      static Option ajpPort =
56          OptionBuilder.withArgName( "ajpPort" ).hasArg().withDescription( "ajp port to use" ).create( "ajpPort" );
57  
58      static Option serverXmlPath =
59          OptionBuilder.withArgName( "serverXmlPath" ).hasArg().withDescription( "server.xml to use, optional" ).create(
60              "serverXmlPath" );
61  
62      static Option resetExtract =
63          OptionBuilder.withArgName( "resetExtract" ).withDescription( "clean previous extract directory" ).create(
64              "resetExtract" );
65  
66      static Option help = OptionBuilder.withLongOpt( "help" ).withDescription( "help" ).create( 'h' );
67  
68      static Option debug = OptionBuilder.withLongOpt( "debug" ).withDescription( "debug" ).create( 'X' );
69  
70      static Option sysProps = OptionBuilder.withDescription( "use value for given property" ).hasArgs().withDescription(
71          "key=value" ).withValueSeparator().create( 'D' );
72  
73      static Option clientAuth =
74          OptionBuilder.withArgName( "clientAuth" ).withDescription( "enable client authentication for https" ).create(
75              "clientAuth" );
76  
77      static Option keyAlias =
78          OptionBuilder.withArgName( "keyAlias" ).hasArgs().withDescription( "alias from keystore for ssl" ).create(
79              "keyAlias" );
80  
81      static Option obfuscate =
82          OptionBuilder.withArgName( "password" ).hasArgs().withDescription( "obfuscate the password and exit" ).create(
83              "obfuscate" );
84  
85      static Option httpProtocol = OptionBuilder.withArgName( "httpProtocol" ).hasArg().withDescription(
86          "http protocol to use: HTTP/1.1 or org.apache.coyote.http11.Http11NioProtocol" ).create( "httpProtocol" );
87  
88      static Option extractDirectory = OptionBuilder.withArgName( "extractDirectory" ).hasArg().withDescription(
89          "path to extract war content, default value: .extract" ).create( "extractDirectory" );
90  
91      static Option loggerName = OptionBuilder.withArgName( "loggerName" ).hasArg().withDescription(
92          "logger to use: slf4j to use slf4j bridge on top of jul" ).create( "loggerName" );
93  
94      static Option uriEncoding = OptionBuilder.withArgName( "uriEncoding" ).hasArg().withDescription(
95          "connector uriEncoding default ISO-8859-1" ).create( "uriEncoding" );
96  
97      static Options options = new Options();
98  
99      static
100     {
101         options.addOption( httpPort ).addOption( httpsPort ).addOption( ajpPort ).addOption( serverXmlPath ).addOption(
102             resetExtract ).addOption( help ).addOption( debug ).addOption( sysProps ).addOption(
103             httpProtocol ).addOption( clientAuth ).addOption( keyAlias ).addOption( obfuscate ).addOption(
104             extractDirectory ).addOption( loggerName ).addOption( uriEncoding ).addOption( maxPostSize );
105     }
106 
107 
108     public static void main( String[] args )
109         throws Exception
110     {
111         CommandLineParser parser = new GnuParser();
112         CommandLine line = null;
113         try
114         {
115             line = parser.parse( Tomcat7RunnerCli.options, args );
116         }
117         catch ( ParseException e )
118         {
119             System.err.println( "Parsing failed.  Reason: " + e.getMessage() );
120             HelpFormatter formatter = new HelpFormatter();
121             formatter.printHelp( getCmdLineSyntax(), Tomcat7RunnerCli.options );
122             System.exit( 1 );
123         }
124 
125         if ( line.hasOption( help.getOpt() ) )
126         {
127             HelpFormatter formatter = new HelpFormatter();
128             formatter.printHelp( getCmdLineSyntax(), Tomcat7RunnerCli.options );
129             System.exit( 0 );
130         }
131 
132         if ( line.hasOption( obfuscate.getOpt() ) )
133         {
134             System.out.println( PasswordUtil.obfuscate( line.getOptionValue( obfuscate.getOpt() ) ) );
135             System.exit( 0 );
136         }
137         Tomcat7Runner tomcat7Runner = new Tomcat7Runner();
138 
139         tomcat7Runner.runtimeProperties = buildStandaloneProperties();
140 
141         if ( line.hasOption( serverXmlPath.getOpt() ) )
142         {
143             tomcat7Runner.serverXmlPath = line.getOptionValue( serverXmlPath.getOpt() );
144         }
145 
146         String port = tomcat7Runner.runtimeProperties.getProperty( Tomcat7Runner.HTTP_PORT_KEY );
147         if ( port != null )
148         {
149             tomcat7Runner.httpPort = Integer.parseInt( port );
150         }
151 
152         // cli win for the port
153         if ( line.hasOption( httpPort.getOpt() ) )
154         {
155             tomcat7Runner.httpPort = Integer.parseInt( line.getOptionValue( httpPort.getOpt() ) );
156         }
157 
158         if ( line.hasOption( maxPostSize.getOpt() ) )
159         {
160             tomcat7Runner.maxPostSize = Integer.parseInt( line.getOptionValue( maxPostSize.getOpt() ) );
161         }
162 
163         if ( line.hasOption( httpsPort.getOpt() ) )
164         {
165             tomcat7Runner.httpsPort = Integer.parseInt( line.getOptionValue( httpsPort.getOpt() ) );
166         }
167         if ( line.hasOption( ajpPort.getOpt() ) )
168         {
169             tomcat7Runner.ajpPort = Integer.parseInt( line.getOptionValue( ajpPort.getOpt() ) );
170         }
171         if ( line.hasOption( resetExtract.getOpt() ) )
172         {
173             tomcat7Runner.resetExtract = true;
174         }
175         if ( line.hasOption( debug.getOpt() ) )
176         {
177             tomcat7Runner.debug = true;
178         }
179 
180         if ( line.hasOption( httpProtocol.getOpt() ) )
181         {
182             tomcat7Runner.httpProtocol = line.getOptionValue( httpProtocol.getOpt() );
183         }
184 
185         if ( line.hasOption( sysProps.getOpt() ) )
186         {
187             Properties systemProperties = line.getOptionProperties( sysProps.getOpt() );
188             if ( systemProperties != null && !systemProperties.isEmpty() )
189             {
190                 for ( Map.Entry<Object, Object> sysProp : systemProperties.entrySet() )
191                 {
192                     System.setProperty( (String) sysProp.getKey(), (String) sysProp.getValue() );
193                 }
194             }
195         }
196         if ( line.hasOption( clientAuth.getOpt() ) )
197         {
198             tomcat7Runner.clientAuth = clientAuth.getOpt();
199         }
200         if ( line.hasOption( keyAlias.getOpt() ) )
201         {
202             tomcat7Runner.keyAlias = line.getOptionValue( keyAlias.getOpt() );
203         }
204 
205         if ( line.hasOption( extractDirectory.getOpt() ) )
206         {
207             tomcat7Runner.extractDirectory = line.getOptionValue( extractDirectory.getOpt() );
208         }
209 
210         if ( line.hasOption( loggerName.getOpt() ) )
211         {
212             tomcat7Runner.loggerName = line.getOptionValue( loggerName.getOpt() );
213         }
214 
215         if ( line.hasOption( uriEncoding.getOpt() ) )
216         {
217             tomcat7Runner.uriEncoding = line.getOptionValue( uriEncoding.getOpt() );
218         }
219 
220         // here we go
221         tomcat7Runner.run();
222     }
223 
224     private static Properties buildStandaloneProperties()
225         throws IOException
226     {
227         InputStream is =
228             Thread.currentThread().getContextClassLoader().getResourceAsStream( STAND_ALONE_PROPERTIES_FILENAME );
229         Properties properties = new Properties();
230         properties.load( is );
231         return properties;
232     }
233 
234     public static String getCmdLineSyntax()
235     {
236         return "java -jar [path to your exec war jar]";
237     }
238 }