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