1 package org.apache.tomcat.maven.runner;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
37
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 if ( line.hasOption( httpPort.getOpt() ) )
142 {
143 tomcat7Runner.httpPort = Integer.parseInt( line.getOptionValue( httpPort.getOpt() ) );
144 }
145
146 if ( line.hasOption( httpsPort.getOpt() ) )
147 {
148 tomcat7Runner.httpsPort = Integer.parseInt( line.getOptionValue( httpsPort.getOpt() ) );
149 }
150 if ( line.hasOption( ajpPort.getOpt() ) )
151 {
152 tomcat7Runner.ajpPort = Integer.parseInt( line.getOptionValue( ajpPort.getOpt() ) );
153 }
154 if ( line.hasOption( resetExtract.getOpt() ) )
155 {
156 tomcat7Runner.resetExtract = true;
157 }
158 if ( line.hasOption( debug.getOpt() ) )
159 {
160 tomcat7Runner.debug = true;
161 }
162
163 if ( line.hasOption( httpProtocol.getOpt() ) )
164 {
165 tomcat7Runner.httpProtocol = line.getOptionValue( httpProtocol.getOpt() );
166 }
167
168 if ( line.hasOption( sysProps.getOpt() ) )
169 {
170 Properties systemProperties = line.getOptionProperties( sysProps.getOpt() );
171 if ( systemProperties != null && !systemProperties.isEmpty() )
172 {
173 for ( Map.Entry<Object, Object> sysProp : systemProperties.entrySet() )
174 {
175 System.setProperty( (String) sysProp.getKey(), (String) sysProp.getValue() );
176 }
177 }
178 }
179 if ( line.hasOption( clientAuth.getOpt() ) )
180 {
181 tomcat7Runner.clientAuth = clientAuth.getOpt();
182 }
183 if ( line.hasOption( keyAlias.getOpt() ) )
184 {
185 tomcat7Runner.keyAlias = line.getOptionValue( keyAlias.getOpt() );
186 }
187
188 if ( line.hasOption( extractDirectory.getOpt() ) )
189 {
190 tomcat7Runner.extractDirectory = line.getOptionValue( extractDirectory.getOpt() );
191 }
192
193 if ( line.hasOption( loggerName.getOpt() ) )
194 {
195 tomcat7Runner.loggerName = line.getOptionValue( loggerName.getOpt() );
196 }
197
198 if ( line.hasOption( uriEncoding.getOpt() ) )
199 {
200 tomcat7Runner.uriEncoding = line.getOptionValue( uriEncoding.getOpt() );
201 }
202
203
204 tomcat7Runner.run();
205 }
206
207 private static Properties buildStandaloneProperties()
208 throws IOException
209 {
210 InputStream is =
211 Thread.currentThread().getContextClassLoader().getResourceAsStream( STAND_ALONE_PROPERTIES_FILENAME );
212 Properties properties = new Properties();
213 properties.load( is );
214 return properties;
215 }
216
217 public static String getCmdLineSyntax()
218 {
219 return "java -jar [path to your exec war jar]";
220 }
221 }