admin
, an empty
* password and ISO-8859-1 URL encoding.
*
* @param url the full URL of the Tomcat manager instance to use
*/
public TomcatManager( URL url )
{
this( url, "admin" );
}
/**
* Creates a Tomcat manager wrapper for the specified URL and username that uses an empty password and ISO-8859-1
* URL encoding.
*
* @param url the full URL of the Tomcat manager instance to use
* @param username the username to use when authenticating with Tomcat manager
*/
public TomcatManager( URL url, String username )
{
this( url, username, "" );
}
/**
* Creates a Tomcat manager wrapper for the specified URL, username and password that uses ISO-8859-1 URL encoding.
*
* @param url the full URL of the Tomcat manager instance to use
* @param username the username to use when authenticating with Tomcat manager
* @param password the password to use when authenticating with Tomcat manager
*/
public TomcatManager( URL url, String username, String password )
{
this( url, username, password, "ISO-8859-1" );
}
/**
* Creates a Tomcat manager wrapper for the specified URL, username, password and URL encoding.
*
* @param url the full URL of the Tomcat manager instance to use
* @param username the username to use when authenticating with Tomcat manager
* @param password the password to use when authenticating with Tomcat manager
* @param charset the URL encoding charset to use when communicating with Tomcat manager
*/
public TomcatManager( URL url, String username, String password, String charset )
{
this.url = url;
this.username = username;
this.password = password;
this.charset = charset;
}
// ----------------------------------------------------------------------
// Public Methods
// ----------------------------------------------------------------------
/**
* Gets the full URL of the Tomcat manager instance.
*
* @return the full URL of the Tomcat manager instance
*/
public URL getURL()
{
return url;
}
/**
* Gets the username to use when authenticating with Tomcat manager.
*
* @return the username to use when authenticating with Tomcat manager
*/
public String getUserName()
{
return username;
}
/**
* Gets the password to use when authenticating with Tomcat manager.
*
* @return the password to use when authenticating with Tomcat manager
*/
public String getPassword()
{
return password;
}
/**
* Gets the URL encoding charset to use when communicating with Tomcat manager.
*
* @return the URL encoding charset to use when communicating with Tomcat manager
*/
public String getCharset()
{
return charset;
}
/**
* Gets the user agent name to use when communicating with Tomcat manager.
*
* @return the user agent name to use when communicating with Tomcat manager
*/
public String getUserAgent()
{
return userAgent;
}
/**
* Sets the user agent name to use when communicating with Tomcat manager.
*
* @param userAgent the user agent name to use when communicating with Tomcat manager
*/
public void setUserAgent( String userAgent )
{
this.userAgent = userAgent;
}
/**
* Deploys the specified WAR as a URL to the specified context path.
*
* @param path the webapp context path to deploy to
* @param war the URL of the WAR to deploy
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deploy( String path, URL war )
throws TomcatManagerException, IOException
{
return deploy( path, war, false );
}
/**
* Deploys the specified WAR as a URL to the specified context path, optionally undeploying the webapp if it already
* exists.
*
* @param path the webapp context path to deploy to
* @param war the URL of the WAR to deploy
* @param update whether to first undeploy the webapp if it already exists
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deploy( String path, URL war, boolean update )
throws TomcatManagerException, IOException
{
return deploy( path, war, update, null );
}
/**
* Deploys the specified WAR as a URL to the specified context path, optionally undeploying the webapp if it already
* exists and using the specified tag name.
*
* @param path the webapp context path to deploy to
* @param war the URL of the WAR to deploy
* @param update whether to first undeploy the webapp if it already exists
* @param tag the tag name to use
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deploy( String path, URL war, boolean update, String tag )
throws TomcatManagerException, IOException
{
return deployImpl( path, null, war, null, update, tag );
}
/**
* Deploys the specified WAR as a HTTP PUT to the specified context path.
*
* @param path the webapp context path to deploy to
* @param war an input stream to the WAR to deploy
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deploy( String path, InputStream war )
throws TomcatManagerException, IOException
{
return deploy( path, war, false );
}
/**
* Deploys the specified WAR as a HTTP PUT to the specified context path, optionally undeploying the webapp if it
* already exists.
*
* @param path the webapp context path to deploy to
* @param war an input stream to the WAR to deploy
* @param update whether to first undeploy the webapp if it already exists
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deploy( String path, InputStream war, boolean update )
throws TomcatManagerException, IOException
{
return deploy( path, war, update, null );
}
/**
* Deploys the specified WAR as a HTTP PUT to the specified context path, optionally undeploying the webapp if it
* already exists and using the specified tag name.
*
* @param path the webapp context path to deploy to
* @param war an input stream to the WAR to deploy
* @param update whether to first undeploy the webapp if it already exists
* @param tag the tag name to use
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deploy( String path, InputStream war, boolean update, String tag )
throws TomcatManagerException, IOException
{
return deployImpl( path, null, null, war, update, tag );
}
/**
* Deploys the specified context XML configuration to the specified context path.
*
* @param path the webapp context path to deploy to
* @param config the URL of the context XML configuration to deploy
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deployContext( String path, URL config )
throws TomcatManagerException, IOException
{
return deployContext( path, config, false );
}
/**
* Deploys the specified context XML configuration to the specified context path, optionally undeploying the webapp
* if it already exists.
*
* @param path the webapp context path to deploy to
* @param config the URL of the context XML configuration to deploy
* @param update whether to first undeploy the webapp if it already exists
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deployContext( String path, URL config, boolean update )
throws TomcatManagerException, IOException
{
return deployContext( path, config, update, null );
}
/**
* Deploys the specified context XML configuration to the specified context path, optionally undeploying the webapp
* if it already exists and using the specified tag name.
*
* @param path the webapp context path to deploy to
* @param config the URL of the context XML configuration to deploy
* @param update whether to first undeploy the webapp if it already exists
* @param tag the tag name to use
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deployContext( String path, URL config, boolean update, String tag )
throws TomcatManagerException, IOException
{
return deployContext( path, config, null, update, tag );
}
/**
* Deploys the specified context XML configuration and WAR as a URL to the specified context path.
*
* @param path the webapp context path to deploy to
* @param config the URL of the context XML configuration to deploy
* @param war the URL of the WAR to deploy
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deployContext( String path, URL config, URL war )
throws TomcatManagerException, IOException
{
return deployContext( path, config, war, false );
}
/**
* Deploys the specified context XML configuration and WAR as a URL to the specified context path, optionally
* undeploying the webapp if it already exists.
*
* @param path the webapp context path to deploy to
* @param config the URL of the context XML configuration to deploy
* @param war the URL of the WAR to deploy
* @param update whether to first undeploy the webapp if it already exists
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deployContext( String path, URL config, URL war, boolean update )
throws TomcatManagerException, IOException
{
return deployContext( path, config, war, update, null );
}
/**
* Deploys the specified context XML configuration and WAR as a URL to the specified context path, optionally
* undeploying the webapp if it already exists and using the specified tag name.
*
* @param path the webapp context path to deploy to
* @param config the URL of the context XML configuration to deploy
* @param war the URL of the WAR to deploy
* @param update whether to first undeploy the webapp if it already exists
* @param tag the tag name to use
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String deployContext( String path, URL config, URL war, boolean update, String tag )
throws TomcatManagerException, IOException
{
return deployImpl( path, config, war, null, update, tag );
}
/**
* Undeploys the webapp at the specified context path.
*
* @param path the webapp context path to undeploy
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String undeploy( String path )
throws TomcatManagerException, IOException
{
return invoke( "/undeploy?path=" + URLEncoder.encode( path, charset ) );
}
/**
* Reloads the webapp at the specified context path.
*
* @param path the webapp context path to reload
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String reload( String path )
throws TomcatManagerException, IOException
{
return invoke( "/reload?path=" + URLEncoder.encode( path, charset ) );
}
/**
* Starts the webapp at the specified context path.
*
* @param path the webapp context path to start
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String start( String path )
throws TomcatManagerException, IOException
{
return invoke( "/start?path=" + URLEncoder.encode( path, charset ) );
}
/**
* Stops the webapp at the specified context path.
*
* @param path the webapp context path to stop
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String stop( String path )
throws TomcatManagerException, IOException
{
return invoke( "/stop?path=" + URLEncoder.encode( path, charset ) );
}
/**
* Lists all the currently deployed web applications.
*
* @return the list of currently deployed applications
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String list()
throws TomcatManagerException, IOException
{
return invoke( "/list" );
}
/**
* Lists information about the Tomcat version, OS, and JVM properties.
*
* @return the server information
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String getServerInfo()
throws TomcatManagerException, IOException
{
return invoke( "/serverinfo" );
}
/**
* Lists all of the global JNDI resources.
*
* @return the list of all global JNDI resources
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String getResources()
throws TomcatManagerException, IOException
{
return getResources( null );
}
/**
* Lists the global JNDI resources of the given type.
*
* @param type the class name of the resources to list, or null
for all
* @return the list of global JNDI resources of the given type
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String getResources( String type )
throws TomcatManagerException, IOException
{
StringBuffer buffer = new StringBuffer();
buffer.append( "/resources" );
if ( type != null )
{
buffer.append( "?type=" + URLEncoder.encode( type, charset ) );
}
return invoke( buffer.toString() );
}
/**
* Lists the security role names and corresponding descriptions that are available.
*
* @return the list of security role names and corresponding descriptions
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String getRoles()
throws TomcatManagerException, IOException
{
return invoke( "/roles" );
}
/**
* Lists the default session timeout and the number of currently active sessions for the given context path.
*
* @param path the context path to list session information for
* @return the default session timeout and the number of currently active sessions
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
public String getSessions( String path )
throws TomcatManagerException, IOException
{
return invoke( "/sessions?path=" + URLEncoder.encode( path, charset ) );
}
// ----------------------------------------------------------------------
// Protected Methods
// ----------------------------------------------------------------------
/**
* Invokes Tomcat manager with the specified command.
*
* @param path the Tomcat manager command to invoke
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
protected String invoke( String path )
throws TomcatManagerException, IOException
{
return invoke( path, null );
}
/**
* Invokes Tomcat manager with the specified command and content data.
*
* @param path the Tomcat manager command to invoke
* @param data an input stream to the content data
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
protected String invoke( String path, InputStream data )
throws TomcatManagerException, IOException
{
HttpURLConnection connection = (HttpURLConnection) new URL( url + path ).openConnection();
connection.setAllowUserInteraction( false );
connection.setDoInput( true );
connection.setUseCaches( false );
if ( data == null )
{
connection.setDoOutput( false );
connection.setRequestMethod( "GET" );
}
else
{
connection.setDoOutput( true );
connection.setRequestMethod( "PUT" );
connection.setRequestProperty( "Content-Type", "application/octet-stream" );
}
if ( userAgent != null )
{
connection.setRequestProperty( "User-Agent", userAgent );
}
connection.setRequestProperty( "Authorization", toAuthorization( username, password ) );
connection.connect();
if ( data != null )
{
pipe( data, connection.getOutputStream() );
}
String response = toString( connection.getInputStream(), MANAGER_CHARSET );
if ( !response.startsWith( "OK -" ) )
{
throw new TomcatManagerException( response );
}
return response;
}
// ----------------------------------------------------------------------
// Private Methods
// ----------------------------------------------------------------------
/**
* Deploys the specified WAR.
*
* @param path the webapp context path to deploy to
* @param config the URL of the context XML configuration to deploy, or null for none
* @param war the URL of the WAR to deploy, or null to use data
* @param data an input stream to the WAR to deploy, or null to use war
* @param update whether to first undeploy the webapp if it already exists
* @param tag the tag name to use
* @return the Tomcat manager response
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
private String deployImpl( String path, URL config, URL war, InputStream data, boolean update, String tag )
throws TomcatManagerException, IOException
{
StringBuffer buffer = new StringBuffer( "/deploy" );
buffer.append( "?path=" ).append( URLEncoder.encode( path, charset ) );
if ( config != null )
{
buffer.append( "&config=" ).append( URLEncoder.encode( config.toString(), charset ) );
}
if ( war != null )
{
buffer.append( "&war=" ).append( URLEncoder.encode( war.toString(), charset ) );
}
else
{
// for Tomcat 5.0.27
buffer.append( "&war=" );
}
if ( update )
{
buffer.append( "&update=true" );
}
if ( tag != null )
{
buffer.append( "&tag=" ).append( URLEncoder.encode( tag, charset ) );
}
return invoke( buffer.toString(), data );
}
/**
* Gets the HTTP Basic Authorization header value for the supplied username and password.
*
* @param username the username to use for authentication
* @param password the password to use for authentication
* @return the HTTP Basic Authorization header value
*/
private String toAuthorization( String username, String password )
{
StringBuffer buffer = new StringBuffer();
buffer.append( username ).append( ':' );
if ( password != null )
{
buffer.append( password );
}
return "Basic " + new String( Base64.encodeBase64( buffer.toString().getBytes() ) );
}
/**
* Reads all the data from the specified input stream and writes it to the specified output stream. Both streams are
* also closed.
*
* @param in the input stream to read from
* @param out the output stream to write to
* @throws IOException if an i/o error occurs
*/
private void pipe( InputStream in, OutputStream out )
throws IOException
{
out = new BufferedOutputStream( out );
int n;
byte[] bytes = new byte[1024 * 4];
while ( ( n = in.read( bytes ) ) != -1 )
{
out.write( bytes, 0, n );
}
out.flush();
out.close();
in.close();
}
/**
* Gets the data from the specified input stream as a string using the specified charset.
*
* @param in the input stream to read from
* @param charset the charset to use when constructing the string
* @return a string representation of the data read from the input stream
* @throws IOException if an i/o error occurs
*/
private String toString( InputStream in, String charset )
throws IOException
{
InputStreamReader reader = new InputStreamReader( in, charset );
StringBuffer buffer = new StringBuffer();
char[] chars = new char[1024];
int n;
while ( ( n = reader.read( chars, 0, chars.length ) ) != -1 )
{
buffer.append( chars, 0, n );
}
return buffer.toString();
}
}
./tomcat-maven-plugin-1.1/src/main/java/org/codehaus/mojo/tomcat/UndeployMojo.java 0000600 0001750 0001750 00000004473 11453106424 026263 0 ustar neo neo package org.codehaus.mojo.tomcat;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.plugin.MojoExecutionException;
import java.io.IOException;
/**
* Undeploy a WAR from Tomcat.
*
* @goal undeploy
* @author Mark Hobson AbstractI18NMojo
.
*/
public AbstractI18NMojo()
{
String packageName = getClass().getPackage().getName();
messages = ResourceBundle.getBundle( packageName + ".messages" );
}
// ----------------------------------------------------------------------
// Protected Methods
// ----------------------------------------------------------------------
/**
* Gets the message for the given key from this packages resource bundle.
*
* @param key the key for the required message
* @return the message
*/
protected String getMessage( String key )
{
try
{
return messages.getString( key );
}
catch ( NullPointerException exception )
{
return "???" + key + "???";
}
catch ( MissingResourceException exception )
{
return "???" + key + "???";
}
catch ( ClassCastException exception )
{
return "???" + key + "???";
}
}
/**
* Gets the message for the given key from this packages resource bundle and formats it with the given parameter.
*
* @param key the key for the required message
* @param param the parameter to be used to format the message with
* @return the formatted message
*/
protected String getMessage( String key, Object param )
{
return MessageFormat.format( getMessage( key ), new Object[] { param } );
}
/**
* Gets the message for the given key from this packages resource bundle and formats it with the given parameters.
*
* @param key the key for the required message
* @param param1 the first parameter to be used to format the message with
* @param param2 the second parameter to be used to format the message with
* @return the formatted message
*/
protected String getMessage( String key, Object param1, Object param2 )
{
return MessageFormat.format( getMessage( key ), new Object[] { param1, param2 } );
}
/**
* Gets the message for the given key from this packages resource bundle and formats it with the given parameters.
*
* @param key the key for the required message
* @param params the parameters to be used to format the message with
* @return the formatted message
*/
protected String getMessage( String key, Object[] params )
{
return MessageFormat.format( getMessage( key ), params );
}
}
./tomcat-maven-plugin-1.1/src/main/java/org/codehaus/mojo/tomcat/TomcatManagerException.java 0000600 0001750 0001750 00000004455 11453106424 030240 0 ustar neo neo package org.codehaus.mojo.tomcat;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* Indicates an error received from Tomcat manager.
*
* @author Mark Hobson TomcatManagerException
with no message or cause.
*/
public TomcatManagerException()
{
super();
}
/**
* Creates a new TomcatManagerException
with the specified message and no cause.
*
* @param message the message for this exception
*/
public TomcatManagerException( String message )
{
super( message );
}
/**
* Creates a new TomcatManagerException
with the specified message and cause.
*
* @param message the message for this exception
* @param cause the cause of this exception
*/
public TomcatManagerException( String message, Throwable cause )
{
super( message, cause );
}
}
./tomcat-maven-plugin-1.1/src/main/java/org/codehaus/mojo/tomcat/ShutdownMojo.java 0000600 0001750 0001750 00000003700 11453106424 026267 0 ustar neo neo package org.codehaus.mojo.tomcat;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.catalina.LifecycleException;
import org.apache.maven.plugin.MojoExecutionException;
/**
* * Shuts down all possibly started embedded tomcat servers. This will be automatically down * through a shutdown hook or you may call this Mojo to shut them down explictly. *
*
* By default the shutdown
goal is not bound to any phase. For integration tests
* you might want to bind it to post-integration-test
.
*
true
a new classLoader separated from maven core will be created to start tomcat.
* @parameter expression="${tomcat.useSeparateTomcatClassLoader}" default-value="false"
* @since 1.0
*/
protected boolean useSeparateTomcatClassLoader;
/**
* @parameter expression="${plugin.artifacts}"
* @required
* @since 1.0
*/
@SuppressWarnings("rawtypes")
private List pluginArtifacts;
/**
* If set to true ignore if packaging of project is not 'war'.
* @since 1.0
* @parameter expression="${tomcat.ignorePackaging}" default-value="false"
*/
private boolean ignorePackaging;
/**
* Override the default keystoreFile for the HTTPS connector (if enabled)
* @since 1.1
* @parameter
*/
private String keystoreFile;
/**
* Override the default keystorePass for the HTTPS connector (if enabled)
* @since 1.1
* @parameter
*/
private String keystorePass;
// ----------------------------------------------------------------------
// Fields
// ----------------------------------------------------------------------
/**
* The embedded Tomcat server.
*/
private Embedded container;
/**
* @since 1.0
*/
private ClassRealm tomcatRealm;
// ----------------------------------------------------------------------
// Mojo Implementation
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
public void execute()
throws MojoExecutionException, MojoFailureException
{
// ensure project is a web application
if ( !isWar() )
{
getLog().info( getMessage( "AbstractRunMojo.nonWar" ) );
return;
}
ClassLoader originalClassLoaser = Thread.currentThread().getContextClassLoader();
try
{
if ( useSeparateTomcatClassLoader )
{
Thread.currentThread().setContextClassLoader( getTomcatClassLoader() );
}
getLog().info( getMessage( "AbstractRunMojo.runningWar", getWebappUrl() ) );
initConfiguration();
startContainer();
if ( !fork )
{
waitIndefinitely();
}
}
catch ( LifecycleException exception )
{
throw new MojoExecutionException( getMessage( "AbstractRunMojo.cannotStart" ), exception );
}
catch ( IOException exception )
{
throw new MojoExecutionException( getMessage( "AbstractRunMojo.cannotCreateConfiguration" ), exception );
} finally
{
if (useSeparateTomcatClassLoader)
{
Thread.currentThread().setContextClassLoader( originalClassLoaser );
}
}
}
// ----------------------------------------------------------------------
// Protected Methods
// ----------------------------------------------------------------------
/**
* Gets the webapp context path to use for the web application being run.
*
* @return the webapp context path
*/
protected String getPath()
{
return path;
}
/**
* Gets the context to run this web application under for the specified embedded Tomcat.
*
* @param container the embedded Tomcat container being used
* @return the context to run this web application under
* @throws IOException if the context could not be created
* @throws MojoExecutionException
*/
protected Context createContext( Embedded container )
throws IOException, MojoExecutionException
{
String contextPath = getPath();
Context context = container.createContext( "/".equals( contextPath ) ? "" : contextPath, getDocBase()
.getAbsolutePath() );
if ( useSeparateTomcatClassLoader )
{
context.setParentClassLoader( getTomcatClassLoader() );
}
context.setLoader( createWebappLoader() );
context.setConfigFile( getContextFile().getAbsolutePath() );
return context;
}
/**
* Gets the webapp loader to run this web application under.
*
* @return the webapp loader to use
* @throws IOException if the webapp loader could not be created
* @throws MojoExecutionException
*/
protected WebappLoader createWebappLoader()
throws IOException, MojoExecutionException
{
if ( useSeparateTomcatClassLoader )
{
return new WebappLoader( getTomcatClassLoader() );
}
return new WebappLoader( Thread.currentThread().getContextClassLoader() );
}
/**
* Gets the webapp directory to run.
*
* @return the webapp directory
*/
protected abstract File getDocBase();
/**
* Gets the Tomcat context XML file to use.
*
* @return the context XML file
*/
protected abstract File getContextFile();
// ----------------------------------------------------------------------
// Private Methods
// ----------------------------------------------------------------------
/**
* Gets whether this project uses WAR packaging.
*
* @return whether this project uses WAR packaging
*/
protected boolean isWar()
{
return "war".equals( packaging ) || ignorePackaging;
}
/**
* Gets the URL of the running webapp.
*
* @return the URL of the running webapp
* @throws MalformedURLException if the running webapp URL is invalid
*/
private URL getWebappUrl()
throws MalformedURLException
{
return new URL( "http", "localhost", port, getPath() );
}
/**
* Creates the Tomcat configuration directory with the necessary resources.
*
* @throws IOException if the Tomcat configuration could not be created
* @throws MojoExecutionException if the Tomcat configuration could not be created
*/
private void initConfiguration()
throws IOException, MojoExecutionException
{
if ( configurationDir.exists() )
{
getLog().info( getMessage( "AbstractRunMojo.usingConfiguration", configurationDir ) );
}
else
{
getLog().info( getMessage( "AbstractRunMojo.creatingConfiguration", configurationDir ) );
configurationDir.mkdirs();
File confDir = new File( configurationDir, "conf" );
confDir.mkdir();
copyFile( "/conf/tomcat-users.xml", new File( confDir, "tomcat-users.xml" ) );
if ( tomcatWebXml != null )
{
if ( !tomcatWebXml.exists() )
{
throw new MojoExecutionException( " tomcatWebXml " + tomcatWebXml.getPath() + " not exists" );
}
//MTOMCAT-42 here it's a real file resources not a one coming with the mojo
FileUtils.copyFile( tomcatWebXml, new File( confDir, "web.xml" ) );
//copyFile( tomcatWebXml.getPath(), new File( confDir, "web.xml" ) );
}
else
{
copyFile( "/conf/web.xml", new File( confDir, "web.xml" ) );
}
File logDir = new File( configurationDir, "logs" );
logDir.mkdir();
File webappsDir = new File( configurationDir, "webapps" );
webappsDir.mkdir();
if ( additionalConfigFilesDir != null && additionalConfigFilesDir.exists() )
{
DirectoryScanner scanner = new DirectoryScanner();
scanner.addDefaultExcludes();
scanner.setBasedir( additionalConfigFilesDir.getPath() );
scanner.scan();
String[] files = scanner.getIncludedFiles();
if ( files != null && files.length > 0 )
{
getLog().info( "Coping additional tomcat config files" );
for ( int i = 0; i < files.length; i++ )
{
File file = new File( additionalConfigFilesDir, files[i] );
getLog().info( " copy " + file.getName() );
FileUtils.copyFileToDirectory( file, confDir );
}
}
}
}
}
/**
* Copies the specified class resource to the specified file.
*
* @param fromPath the path of the class resource to copy
* @param toFile the file to copy to
* @throws IOException if the file could not be copied
*/
private void copyFile( String fromPath, File toFile )
throws IOException
{
URL fromURL = getClass().getResource( fromPath );
if ( fromURL == null )
{
throw new FileNotFoundException( fromPath );
}
FileUtils.copyURLToFile( fromURL, toFile );
}
/**
* Starts the embedded Tomcat server.
*
* @throws IOException if the server could not be configured
* @throws LifecycleException if the server could not be started
* @throws MojoExecutionException if the server could not be configured
*/
private void startContainer()
throws IOException, LifecycleException, MojoExecutionException
{
// Set the system properties
setupSystemProperties();
try
{
if ( serverXml != null )
{
if ( !serverXml.exists() )
{
throw new MojoExecutionException( serverXml.getPath() + " not exists" );
}
container = new Catalina();
container.setCatalinaHome( configurationDir.getAbsolutePath() );
( (Catalina) container ).setConfigFile( serverXml.getPath() );
container.start();
}
else
{
// create server
container = new Embedded();
container.setCatalinaHome( configurationDir.getAbsolutePath() );
container.setRealm( new MemoryRealm() );
//container.createLoader( getTomcatClassLoader() ).
// create context
Context context = createContext( container );
// create host
String appBase = new File( configurationDir, "webapps" ).getAbsolutePath();
Host host = container.createHost( "localHost", appBase );
host.addChild( context );
if ( addContextWarDependencies )
{
Collectionnull
to use
* defaults of username admin
and no password.
*
* @parameter expression="${maven.tomcat.server}"
*/
private String server;
/**
* The URL encoding charset to use when communicating with Tomcat manager.
*
* @parameter expression="${maven.tomcat.charset}" default-value="ISO-8859-1"
* @required
*/
private String charset;
/**
* The tomcat username to use for deployment
*
* @parameter expression="${tomcat.username}"
* @since 1.0-alpha-2
*/
private String username;
/**
* The password to use for deployment
*
* @parameter expression="${tomcat.password}"
* @since 1.0-alpha-2
*/
private String password;
/**
* @parameter expression="${plugin.version}"
* @required
* @readonly
*/
private String version;
// ----------------------------------------------------------------------
// Fields
// ----------------------------------------------------------------------
/**
* The Tomcat manager wrapper object.
*/
private TomcatManager manager;
// ----------------------------------------------------------------------
// Mojo Implementation
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
public void execute()
throws MojoExecutionException
{
try
{
invokeManager();
}
catch ( TomcatManagerException exception )
{
throw new MojoExecutionException( getMessage( "AbstractCatalinaMojo.managerError", exception.getMessage() ) );
}
catch ( IOException exception )
{
throw new MojoExecutionException( getMessage( "AbstractCatalinaMojo.managerIOError" ), exception );
}
}
// ----------------------------------------------------------------------
// Protected Methods
// ----------------------------------------------------------------------
/**
* Invokes Tomcat manager when this Mojo is executed.
*
* @throws MojoExecutionException if there was a problem executing this goal
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
protected abstract void invokeManager()
throws MojoExecutionException, TomcatManagerException, IOException;
/**
* Gets the Tomcat manager wrapper object configured for this goal.
*
* @return the Tomcat manager wrapper object
* @throws MojoExecutionException if there was a problem obtaining the authentication details
*/
protected TomcatManager getManager()
throws MojoExecutionException
{
// lazily instantiate when config values have been injected
if ( manager == null )
{
String userName;
String password;
if ( server == null )
{
// no server set, use defaults
getLog().debug( getMessage( "AbstractCatalinaMojo.defaultAuth" ) );
userName = DEFAULT_USERNAME;
password = DEFAULT_PASSWORD;
}
else
{
// obtain authenication details for specified server from wagon
AuthenticationInfo info = wagonManager.getAuthenticationInfo( server );
if ( info == null )
{
throw new MojoExecutionException( getMessage( "AbstractCatalinaMojo.unknownServer", server ) );
}
// derive username
userName = info.getUserName();
if ( userName == null )
{
getLog().debug( getMessage( "AbstractCatalinaMojo.defaultUserName" ) );
userName = DEFAULT_USERNAME;
}
// derive password
password = info.getPassword();
if ( password == null )
{
getLog().debug( getMessage( "AbstractCatalinaMojo.defaultPassword" ) );
password = DEFAULT_PASSWORD;
}
}
// if userName/password are defined in the mojo or the cli they override
if ( !StringUtils.isEmpty( this.username ) )
{
userName = this.username;
password = this.password == null ? "" : this.password;
}
manager = new TomcatManager( url, userName, password, charset );
manager.setUserAgent( name + "/" + version );
}
return manager;
}
/**
* Gets the full URL of the Tomcat manager instance.
*
* @return the full URL of the Tomcat manager instance to use
*/
protected URL getURL()
{
return url;
}
/**
* Gets the webapp context path to use when communicating with Tomcat manager.
*
* @return the webapp context path to use
*/
protected String getPath()
{
return path;
}
/**
* Gets the URL of the deployed webapp.
*
* @return the URL of the deployed webapp
* @throws MalformedURLException if the deployed webapp URL is invalid
*/
protected URL getDeployedURL()
throws MalformedURLException
{
return new URL( getURL(), getPath() );
}
/**
* Splits the given string into lines and writes each one separately to the log at info level.
*
* @param string the string to write
*/
protected void log( String string )
{
StringTokenizer tokenizer = new StringTokenizer( string, "\n\r" );
while ( tokenizer.hasMoreTokens() )
{
getLog().info( tokenizer.nextToken() );
}
}
}
./tomcat-maven-plugin-1.1/src/main/java/org/codehaus/mojo/tomcat/RunMojo.java 0000600 0001750 0001750 00000015547 11453106424 025234 0 ustar neo neo package org.codehaus.mojo.tomcat;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.apache.catalina.Context;
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.startup.Embedded;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils;
/**
* Runs the current project as a dynamic web application using an embedded Tomcat server.
*
* @goal run
* @execute phase="compile"
* @requiresDependencyResolution runtime
* @author Jurgen Lust
* @author Mark Hobson null
for all.
*
* @parameter expression = "${maven.tomcat.type}"
*/
private String type;
// ----------------------------------------------------------------------
// Protected Methods
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
protected void invokeManager()
throws MojoExecutionException, TomcatManagerException, IOException
{
if ( type == null )
{
getLog().info( getMessage( "ResourcesMojo.listAllResources", getURL() ) );
}
else
{
getLog().info( getMessage( "ResourcesMojo.listTypedResources", type, getURL() ) );
}
log( getManager().getResources( type ) );
}
}
./tomcat-maven-plugin-1.1/src/main/java/org/codehaus/mojo/tomcat/ExplodedMojo.java 0000600 0001750 0001750 00000004330 11453106424 026220 0 ustar neo neo package org.codehaus.mojo.tomcat;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import org.apache.maven.plugin.MojoExecutionException;
/**
* Deploy an exploded WAR to Tomcat.
*
* @goal exploded
* @author Mark Hobson war
to deploy the war, context
to
* deploy the context XML file, or both
to deploy the war with the context XML file.
*
* @parameter expression = "${maven.tomcat.mode}" default-value = "war"
* @required
*/
private String mode;
/**
* The path of the Tomcat context XML file. This is not used for war deployment mode.
*
* @parameter expression = "${project.build.directory}/${project.build.finalName}/META-INF/context.xml"
*/
private File contextFile;
/**
* Whether Tomcat should automatically undeploy webapps that already exist when deploying.
*
* @parameter expression = "${maven.tomcat.update}" default-value = "false"
* @required
*/
private boolean update;
/**
* The Tomcat webapp tag name to use.
*
* @parameter expression = "${maven.tomcat.tag}"
*/
private String tag;
// ----------------------------------------------------------------------
// Protected Methods
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public void invokeManager()
throws MojoExecutionException, TomcatManagerException, IOException
{
if ( "war".equals( mode ) )
{
deployWar();
}
else if ( "context".equals( mode ) )
{
deployContext();
}
else if ( "both".equals( mode ) )
{
deployWarAndContext();
}
else
{
throw new MojoExecutionException( getMessage( "AbstractDeployMojo.unknownMode", mode ) );
}
}
/**
* Gets the Tomcat WAR file. This may be a file or a directory depending on the deployment mode.
*
* @return the Tomcat WAR file.
*/
protected abstract File getWarFile();
/**
* Ensures that the Tomcat WAR file exists and is the correct type for the deployment mode.
*
* @throws MojoExecutionException if the WAR file does not exist or is not the correct type for the deployment mode
*/
protected abstract void validateWarFile()
throws MojoExecutionException;
/**
* Gets the Tomcat context XML file.
*
* @return the Tomcat context XML file.
*/
protected File getContextFile()
{
return contextFile;
}
/**
* Ensures that the Tomcat context XML file exists and is indeed a file.
*
* @throws MojoExecutionException if the context file does not exist or is not a file
*/
protected void validateContextFile()
throws MojoExecutionException
{
if ( !contextFile.exists() || !contextFile.isFile() )
{
throw new MojoExecutionException( getMessage( "AbstractDeployMojo.missingContext", contextFile.getPath() ) );
}
}
/**
* Gets whether Tomcat should automatically undeploy webapps that already exist when deploying.
*
* @return whether Tomcat should automatically undeploy webapps that already exist when deploying
*/
protected boolean isUpdate()
{
return update;
}
/**
* Gets the Tomcat webapp tag name to use.
*
* @return the Tomcat webapp tag name to use
*/
protected String getTag()
{
return tag;
}
/**
* Deploys the WAR to Tomcat.
*
* @throws MojoExecutionException if there was a problem locating the WAR
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
protected void deployWar()
throws MojoExecutionException, TomcatManagerException, IOException
{
validateWarFile();
getLog().info( getMessage( "AbstractDeployMojo.deployingWar", getDeployedURL() ) );
URL warURL = getWarFile().toURL();
log( getManager().deploy( getPath(), warURL, isUpdate(), getTag() ) );
}
/**
* Deploys the context XML file to Tomcat.
*
* @throws MojoExecutionException if there was a problem locating the context XML file
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
protected void deployContext()
throws MojoExecutionException, TomcatManagerException, IOException
{
validateContextFile();
getLog().info( getMessage( "AbstractDeployMojo.deployingContext", getDeployedURL() ) );
URL contextURL = getContextFile().toURL();
log( getManager().deployContext( getPath(), contextURL, isUpdate(), getTag() ) );
}
/**
* Deploys the WAR and context XML file to Tomcat.
*
* @throws MojoExecutionException if there was a problem locating either the WAR or the context XML file
* @throws TomcatManagerException if the Tomcat manager request fails
* @throws IOException if an i/o error occurs
*/
protected void deployWarAndContext()
throws MojoExecutionException, TomcatManagerException, IOException
{
validateWarFile();
validateContextFile();
getLog().info( getMessage( "AbstractDeployMojo.deployingWarContext", getDeployedURL() ) );
URL warURL = getWarFile().toURL();
URL contextURL = getContextFile().toURL();
log( getManager().deployContext( getPath(), contextURL, warURL, isUpdate(), getTag() ) );
}
}
./tomcat-maven-plugin-1.1/src/main/java/org/codehaus/mojo/tomcat/AbstractDeployWarMojo.java 0000600 0001750 0001750 00000005014 11453106424 030046 0 ustar neo neo package org.codehaus.mojo.tomcat;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.maven.plugin.MojoExecutionException;
/**
* @author olamy
* @version $Id: AbstractDeployWarMojo.java 8853 2009-01-21 09:25:22Z mark $
* @since 1.0-alpha-2
*/
public class AbstractDeployWarMojo
extends AbstractDeployMojo
{
// ----------------------------------------------------------------------
// Mojo Parameters
// ----------------------------------------------------------------------
/**
* The path of the WAR file to deploy.
*
* @parameter expression = "${project.build.directory}/${project.build.finalName}.war"
* @required
*/
private File warFile;
// ----------------------------------------------------------------------
// Protected Methods
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
protected File getWarFile()
{
return warFile;
}
/**
* {@inheritDoc}
*/
@Override
protected void validateWarFile()
throws MojoExecutionException
{
if ( !warFile.exists() || !warFile.isFile() )
{
throw new MojoExecutionException( getMessage( "DeployMojo.missingWar", warFile.getPath() ) );
}
}
/**
* {@inheritDoc}
*/
@Override
protected void deployWar()
throws MojoExecutionException, TomcatManagerException, IOException
{
validateWarFile();
getLog().info( getMessage( "AbstractDeployMojo.deployingWar", getDeployedURL() ) );
log( getManager().deploy( getPath(), new FileInputStream( warFile ), isUpdate(), getTag() ) );
}
}
./tomcat-maven-plugin-1.1/src/test/ 0000700 0001750 0001750 00000000000 11453106422 015255 5 ustar neo neo ./tomcat-maven-plugin-1.1/src/test/resources/ 0000700 0001750 0001750 00000000000 11453106422 017267 5 ustar neo neo ./tomcat-maven-plugin-1.1/src/test/java/ 0000700 0001750 0001750 00000000000 11453106422 016176 5 ustar neo neo ./tomcat-maven-plugin-1.1/src/it/ 0000700 0001750 0001750 00000000000 11453106426 014716 5 ustar neo neo ./tomcat-maven-plugin-1.1/src/it/simple-war-project/ 0000700 0001750 0001750 00000000000 11453106430 020435 5 ustar neo neo ./tomcat-maven-plugin-1.1/src/it/simple-war-project/pom.xml 0000600 0001750 0001750 00000005040 11453106430 021753 0 ustar neo neo