Author: olamy Date: Sat Oct 8 17:45:36 2011 New Revision: 1180427 URL: http://svn.apache.org/viewvc?rev=1180427&view=rev Log: [MTOMCAT-62] tomcat7 support: move embedded registry and ExternalRepositoriesReloadableWebappLoader to common package.
Added: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/EmbeddedRegistry.java (with props) tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ExternalRepositoriesReloadableWebappLoader.java (with props) Modified: tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/AbstractRunMojo.java tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/RunMojo.java tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/ShutdownMojo.java Added: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/EmbeddedRegistry.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/EmbeddedRegistry.java?rev=1180427&view=auto ============================================================================== --- tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/EmbeddedRegistry.java (added) +++ tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/EmbeddedRegistry.java Sat Oct 8 17:45:36 2011 @@ -0,0 +1,181 @@ +package org.apache.tomcat.maven.common.run; + +/* + * 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.catalina.startup.Embedded; +import org.apache.maven.plugin.logging.Log; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +/** + * Registry which collects all embedded Tomcat Servers so that they will be shutdown + * through a shutdown hook when the JVM terminates or you can ask the registry to + * shutdown all started servers. + * + * @author Mark Michaelis + * @since 1.1 + */ +public final class EmbeddedRegistry +{ + private static EmbeddedRegistry instance; + + private Set<Object> containers = new HashSet<Object>(1); + + /** + * Don't instantiate - use the instance through {@link #getInstance()}. + */ + private EmbeddedRegistry() + { + // no op + } + + /** + * Retrieve the lazily initialized instance of the registry. + * + * @return singleton instance of the registry + */ + public static EmbeddedRegistry getInstance() + { + if ( instance == null ) + { + instance = new EmbeddedRegistry(); + Runtime.getRuntime().addShutdownHook(new Thread() + { + @Override + public void run() + { + try + { + getInstance().shutdownAll(null); + } + catch ( Exception e ) + { + // ignore, the exception should already have been reported + } + } + }); + } + return instance; + } + + /** + * Adds the given container to the registry which automatically registers it for the shutdown + * hook. + * + * @param container the container to register + * @return true if it got added; false if not + */ + public synchronized boolean register(final Embedded container) + { + return containers.add(container); + } + + /** + * Shuts down all registered embedded tomcats. All tomcats which successfully shut down will be + * removed from the registry. + * + * @param log the log to write possible shutdown exceptions to + * @throws org.apache.catalina.LifecycleException + * the first exception which occurred will be rethrown + */ + public synchronized void shutdownAll(final Log log) + throws Exception + { + Exception firstException = null; + for ( Iterator<Object> iterator = containers.iterator(); iterator.hasNext(); ) + { + Object embedded = iterator.next(); + try + { + Method method = embedded.getClass().getMethod("stop", null); + method.invoke(embedded, null); + iterator.remove(); + } + catch ( NoSuchMethodException e ) + { + if ( firstException == null ) + { + firstException = e; + error(log, e, "no stop method in class " + embedded.getClass().getName()); + } + else + { + error(log, e, "Error while shutting down embedded Tomcat."); + } + } + catch ( IllegalAccessException e ) + { + if ( firstException == null ) + { + firstException = e; + error(log, e, "IllegalAccessException for stop method in class " + embedded.getClass().getName()); + } + else + { + error(log, e, "Error while shutting down embedded Tomcat."); + } + } + catch ( InvocationTargetException e ) + { + + if ( firstException == null ) + { + firstException = e; + error(log, e, "IllegalAccessException for stop method in class " + embedded.getClass().getName()); + } + else + { + error(log, e, "Error while shutting down embedded Tomcat."); + } + } + } + if ( firstException != null ) + { + throw firstException; + } + } + + /** + * Reports the exception. If a log is given (typically when called from within a Mojo) the + * message will be printed to the log. Otherwise it will be printed to StdErr. + * + * @param log the log to write the message to; null to write to stderr + * @param e exception which shall be reported + * @param message message which shall be reported + */ + private void error(final Log log, final Exception e, final String message) + { + if ( log == null ) + { + System.err.println("ERROR: " + message); + e.printStackTrace(); + } + else + { + log.error(message, e); + } + } + +} Propchange: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/EmbeddedRegistry.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/EmbeddedRegistry.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ExternalRepositoriesReloadableWebappLoader.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ExternalRepositoriesReloadableWebappLoader.java?rev=1180427&view=auto ============================================================================== --- tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ExternalRepositoriesReloadableWebappLoader.java (added) +++ tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ExternalRepositoriesReloadableWebappLoader.java Sat Oct 8 17:45:36 2011 @@ -0,0 +1,164 @@ +package org.apache.tomcat.maven.common.run; + +/* + * 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.loader.WebappLoader; +import org.apache.maven.plugin.logging.Log; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * A {@linkplain WebappLoader} implementation that allows scanning for changes to project classpath in support of + * context reloads. + * + * @author Ryan Connolly + * @since 2.0 + * @version $Id: ExternalRepositoriesReloadableWebappLoader.java 13551 2011-02-09 16:05:47Z olamy $ + */ +public class ExternalRepositoriesReloadableWebappLoader + extends WebappLoader +{ + + /** Last modification times of all jar and class files. */ + private Map<String, Long> modificationTimeMap = new HashMap<String, Long>(); + + private Log log; + + /** Default Constructor. */ + public ExternalRepositoriesReloadableWebappLoader() + { + super(); + } + + + /** + * Convenience Constructor allows setting of a parent ClassLoader. + * @param parent the ClassLoader instance to set as this Loader's parent ClassLoader. + */ + public ExternalRepositoriesReloadableWebappLoader(ClassLoader parent, Log log) + { + super( parent ); + this.log = log; + } + + /** + * {@inheritDoc} + */ + @Override + public void addRepository( String repository ) + { + super.addRepository( repository ); + try + { + File file = new File( new URL( repository ).getPath().replaceAll( "%20", " " ) ); + if ( file.isDirectory() ) + { + addClassDirectory( file ); + } + else if ( file.isFile() && file.getName().endsWith( ".jar" ) ) + { + addFile( file ); + } + } + catch ( MalformedURLException muex ) + { + throw new RuntimeException( muex ); + } + } + + /** + * Tracks modification times of files in the given class directory. + * @param directory the File directory to track modification times for. + */ + private void addClassDirectory( File directory ) + { + for ( File file : directory.listFiles() ) + { + if ( file.isDirectory() ) + { + //remember also directory last modification time + addFile( file ); + addClassDirectory( file ); + } + else if ( file.isFile() ) + { + addFile( file ); + } + } + } + + /** + * Tracks last modification time of the given File. + * @param file the File for which to track last modification time. + */ + private void addFile( File file ) + { + modificationTimeMap.put( file.getAbsolutePath(), file.lastModified() ); + } + + /** + * Check if {@link WebappLoader} says modified(), if not then check files from added repositories. + */ + @Override + public boolean modified() + { + boolean modified = super.modified(); + if ( !modified ) + { + if (log != null ) + { + log.debug( "classPath scanning started at " + new Date().toString() ); + } + for ( Map.Entry<String, Long> entry : modificationTimeMap.entrySet() ) + { + String key = entry.getKey(); + File file = new File( key ); + if ( file.exists() ) + { + // file could be deleted. + Long savedLastModified = modificationTimeMap.get( key ); + if ( file.lastModified() > savedLastModified ) + { + modified = true; + modificationTimeMap.put( key, file.lastModified() ); + + // directory last modification time can change when some class, + // jar or subdirectory was added or deleted. + if ( file.isDirectory() ) + { + addClassDirectory( file ); + } + } + } + } + } + if (log != null ) + { + log.debug( "context " + modified + " at " + new Date().toString() ); + } + return modified; + } + +} Propchange: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ExternalRepositoriesReloadableWebappLoader.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ExternalRepositoriesReloadableWebappLoader.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/AbstractRunMojo.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/AbstractRunMojo.java?rev=1180427&r1=1180426&r2=1180427&view=diff ============================================================================== --- tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/AbstractRunMojo.java (original) +++ tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/AbstractRunMojo.java Sat Oct 8 17:45:36 2011 @@ -34,6 +34,7 @@ import org.apache.maven.artifact.resolve import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; +import org.apache.tomcat.maven.common.run.EmbeddedRegistry; import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.UnArchiver; import org.codehaus.plexus.archiver.manager.ArchiverManager; @@ -730,7 +731,7 @@ public abstract class AbstractRunMojo container.start( ); } - EmbeddedRegistry.getInstance( ).register( container ); + EmbeddedRegistry.getInstance().register( container ); } finally { Modified: tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/RunMojo.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/RunMojo.java?rev=1180427&r1=1180426&r2=1180427&view=diff ============================================================================== --- tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/RunMojo.java (original) +++ tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/RunMojo.java Sat Oct 8 17:45:36 2011 @@ -57,12 +57,6 @@ public class RunMojo // Mojo Parameters // ---------------------------------------------------------------------- - /** - * The classes directory for the web application being run. - * - * @parameter expression = "${project.build.outputDirectory}" - */ - private File classesDir; /** * The set of dependencies for the web application being run. @@ -186,7 +180,7 @@ public class RunMojo return temporaryContextFile; } //---------------------------------------------------------------------------- - // context attributes backgroundProcessorDelay reloadable cannot be modifiec at runtime. + // context attributes backgroundProcessorDelay reloadable cannot be modified at runtime. // It looks only values from the file ared used // so here we create a temporary file with values modified //---------------------------------------------------------------------------- Modified: tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/ShutdownMojo.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/ShutdownMojo.java?rev=1180427&r1=1180426&r2=1180427&view=diff ============================================================================== --- tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/ShutdownMojo.java (original) +++ tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/ShutdownMojo.java Sat Oct 8 17:45:36 2011 @@ -21,6 +21,7 @@ package org.apache.tomcat.maven.plugin.t import org.apache.catalina.LifecycleException; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.tomcat.maven.common.run.EmbeddedRegistry; /** @@ -51,7 +52,7 @@ public class ShutdownMojo { try { - EmbeddedRegistry.getInstance( ).shutdownAll( getLog( ) ); + EmbeddedRegistry.getInstance().shutdownAll( getLog( ) ); } catch ( LifecycleException e ) { --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org