{
// FIXME restore previous value ?
System.setProperty( "java.security.policy", catalinaPolicy.getAbsolutePath() );
}
final Embedded container;
if ( serverXml != null )
{
if ( !serverXml.exists() )
{
throw new MojoExecutionException( serverXml.getPath() + " not exists" );
}
container = new Catalina();
container.setCatalinaHome( configurationDir.getAbsolutePath() );
container.setCatalinaBase( configurationDir.getAbsolutePath() );
( (Catalina) container ).setConfigFile( serverXml.getPath() );
( (Catalina) container ).setRedirectStreams( true );
( (Catalina) container ).setUseNaming( this.useNaming );
container.start();
}
else
{
// create server
container = new Embedded();
container.setCatalinaHome( configurationDir.getAbsolutePath() );
MemoryRealm memoryRealm = new MemoryRealm();
if ( tomcatUsers != null )
{
if ( !tomcatUsers.exists() )
{
throw new MojoExecutionException( " tomcatUsers " + tomcatUsers.getPath() + " not exists" );
}
getLog().info( "use tomcat-users.xml from " + tomcatUsers.getAbsolutePath() );
memoryRealm.setPathname( tomcatUsers.getAbsolutePath() );
}
container.setRealm( memoryRealm );
container.setUseNaming( useNaming );
//container.createLoader( getTomcatClassLoader() ).
// create context
Context context = createContext( container );
// create host
String appBase = new File( configurationDir, "webapps" ).getAbsolutePath();
Host host = container.createHost( "localHost", appBase );
if ( hostName != null )
{
host.setName( hostName );
}
if ( aliases != null )
{
for ( String alias : aliases )
{
host.addAlias( alias );
}
}
host.addChild( context );
createStaticContext( container, context, host );
if ( addContextWarDependencies || !getAdditionalWebapps().isEmpty() )
{
Collection<Context> dependencyContexts = createDependencyContexts( container );
for ( Context extraContext : dependencyContexts )
{
host.addChild( extraContext );
}
}
// create engine
Engine engine = container.createEngine();
engine.setName( "localEngine-" + port );
engine.addChild( host );
engine.setDefaultHost( host.getName() );
container.addEngine( engine );
getLog().debug( "start tomcat instance on http port:" + port + " and protocol: " + protocol );
// create http connector
Connector httpConnector = container.createConnector( (InetAddress) null, port, protocol );
if ( httpsPort > 0 )
{
httpConnector.setRedirectPort( httpsPort );
}
httpConnector.setURIEncoding( uriEncoding );
container.addConnector( httpConnector );
// create https connector
if ( httpsPort > 0 )
{
Connector httpsConnector = container.createConnector( (InetAddress) null, httpsPort, true );
httpsConnector.setSecure( true );
httpsConnector.setProperty( "SSLEnabled", "true" );
// should be default but configure it anyway
httpsConnector.setProperty( "sslProtocol", "TLS" );
if ( keystoreFile != null )
{
httpsConnector.setAttribute( "keystoreFile", keystoreFile );
}
if ( keystorePass != null )
{
httpsConnector.setAttribute( "keystorePass", keystorePass );
}
if ( keystoreType != null )
{
httpsConnector.setAttribute( "keystoreType", keystoreType );
}
container.addConnector( httpsConnector );
}
// create ajp connector
if ( ajpPort > 0 )
{
Connector ajpConnector = container.createConnector( (InetAddress) null, ajpPort, ajpProtocol );
ajpConnector.setURIEncoding( uriEncoding );
container.addConnector( ajpConnector );
}
if ( useSeparateTomcatClassLoader )
{
Thread.currentThread().setContextClassLoader( getTomcatClassLoader() );
engine.setParentClassLoader( getTomcatClassLoader() );
}
container.start();
}
EmbeddedRegistry.getInstance().register( container );
}
finally