Coverage Report - org.apache.tomcat.maven.plugin.tomcat7.run.RunMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
RunMojo
0 %
0/71
0 %
0/22
6,5
RunMojo$1
0 %
0/6
0 %
0/2
6,5
 
 1  
 package org.apache.tomcat.maven.plugin.tomcat7.run;
 2  
 /*
 3  
  * Licensed to the Apache Software Foundation (ASF) under one
 4  
  * or more contributor license agreements.  See the NOTICE file
 5  
  * distributed with this work for additional information
 6  
  * regarding copyright ownership.  The ASF licenses this file
 7  
  * to you under the Apache License, Version 2.0 (the
 8  
  * "License"); you may not use this file except in compliance
 9  
  * with the License.  You may obtain a copy of the License at
 10  
  *
 11  
  *   http://www.apache.org/licenses/LICENSE-2.0
 12  
  *
 13  
  * Unless required by applicable law or agreed to in writing,
 14  
  * software distributed under the License is distributed on an
 15  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 16  
  * KIND, either express or implied.  See the License for the
 17  
  * specific language governing permissions and limitations
 18  
  * under the License.
 19  
  */
 20  
 
 21  
 import org.apache.catalina.loader.WebappLoader;
 22  
 import org.apache.commons.io.FileUtils;
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.apache.maven.artifact.Artifact;
 25  
 import org.apache.maven.plugin.MojoExecutionException;
 26  
 import org.apache.maven.plugins.annotations.Component;
 27  
 import org.apache.maven.plugins.annotations.Execute;
 28  
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 29  
 import org.apache.maven.plugins.annotations.Mojo;
 30  
 import org.apache.maven.plugins.annotations.Parameter;
 31  
 import org.apache.maven.plugins.annotations.ResolutionScope;
 32  
 import org.apache.maven.shared.filtering.MavenFileFilter;
 33  
 import org.apache.maven.shared.filtering.MavenFileFilterRequest;
 34  
 import org.apache.maven.shared.filtering.MavenFilteringException;
 35  
 import org.apache.tomcat.maven.common.run.ClassLoaderEntriesCalculator;
 36  
 import org.apache.tomcat.maven.common.run.ClassLoaderEntriesCalculatorRequest;
 37  
 import org.apache.tomcat.maven.common.run.ClassLoaderEntriesCalculatorResult;
 38  
 import org.apache.tomcat.maven.common.run.TomcatRunException;
 39  
 import org.codehaus.plexus.util.IOUtil;
 40  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 41  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 42  
 import org.codehaus.plexus.util.xml.Xpp3DomWriter;
 43  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 44  
 
 45  
 import java.io.File;
 46  
 import java.io.FileReader;
 47  
 import java.io.FileWriter;
 48  
 import java.io.IOException;
 49  
 import java.io.StringWriter;
 50  
 import java.util.List;
 51  
 import java.util.Set;
 52  
 
 53  
 /**
 54  
  * Runs the current project as a dynamic web application using an embedded Tomcat server.
 55  
  *
 56  
  * @author Olivier Lamy
 57  
  * @since 2.0
 58  
  */
 59  
 @Mojo( name = "run", requiresDependencyResolution = ResolutionScope.TEST )
 60  
 @Execute( phase = LifecyclePhase.COMPILE )
 61  0
 public class RunMojo
 62  
     extends AbstractRunMojo
 63  
 {
 64  
     // ----------------------------------------------------------------------
 65  
     // Mojo Parameters
 66  
     // ----------------------------------------------------------------------
 67  
 
 68  
 
 69  
     /**
 70  
      * The set of dependencies for the web application being run.
 71  
      */
 72  
     @Parameter( defaultValue = "${project.artifacts}", required = true, readonly = true )
 73  
     private Set<Artifact> dependencies;
 74  
 
 75  
     /**
 76  
      * The web resources directory for the web application being run.
 77  
      */
 78  
     @Parameter( defaultValue = "${basedir}/src/main/webapp", property = "tomcat.warSourceDirectory" )
 79  
     private File warSourceDirectory;
 80  
 
 81  
 
 82  
     /**
 83  
      * Set the "follow standard delegation model" flag used to configure our ClassLoader.
 84  
      *
 85  
      * @see http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/loader/WebappLoader.html#setDelegate(boolean)
 86  
      * @since 1.0
 87  
      */
 88  0
     @Parameter( property = "tomcat.delegate", defaultValue = "true" )
 89  
     private boolean delegate = true;
 90  
 
 91  
     /**
 92  
      * represents the delay in seconds between each classPathScanning change invocation
 93  
      *
 94  
      * @see <a href="http://tomcat.apache.org/tomcat-7.0-doc/config/context.html">http://tomcat.apache.org/tomcat-7.0-doc/config/context.html</a>
 95  
      */
 96  0
     @Parameter( property = "maven.tomcat.backgroundProcessorDelay", defaultValue = "-1" )
 97  
     protected int backgroundProcessorDelay = -1;
 98  
 
 99  
     /**
 100  
      * @since 2.0
 101  
      */
 102  
     @Component
 103  
     private ClassLoaderEntriesCalculator classLoaderEntriesCalculator;
 104  
 
 105  
     /**
 106  
      * will add /WEB-INF/lib/*.jar and /WEB-INF/classes from war dependencies in the webappclassloader
 107  
      *
 108  
      * @since 2.0
 109  
      */
 110  
     @Parameter( property = "maven.tomcat.addWarDependenciesInClassloader", defaultValue = "true" )
 111  
     private boolean addWarDependenciesInClassloader;
 112  
 
 113  
     /**
 114  
      * will use the test classpath rather than the compile one and will add test dependencies too
 115  
      *
 116  
      * @since 2.0
 117  
      */
 118  
     @Parameter( property = "maven.tomcat.useTestClasspath", defaultValue = "false" )
 119  
     private boolean useTestClasspath;
 120  
 
 121  
     /**
 122  
      * Additional optional directories to add to the embedded tomcat classpath.
 123  
      *
 124  
      * @since 2.0
 125  
      */
 126  
     @Parameter( alias = "additionalClassesDirs" )
 127  
     private List<String> additionalClasspathDirs;
 128  
 
 129  
     @Component( role = MavenFileFilter.class, hint = "default" )
 130  
     private MavenFileFilter mavenFileFilter;
 131  
 
 132  
     /**
 133  
      * {@inheritDoc}
 134  
      */
 135  
     @Override
 136  
     protected File getDocBase()
 137  
     {
 138  0
         return warSourceDirectory;
 139  
     }
 140  
 
 141  
     /**
 142  
      * {@inheritDoc}
 143  
      */
 144  
     @Override
 145  
     protected File getContextFile()
 146  
         throws MojoExecutionException
 147  
     {
 148  0
         File temporaryContextFile = null;
 149  
 
 150  
         //----------------------------------------------------------------------------
 151  
         // context attributes backgroundProcessorDelay reloadable cannot be modified at runtime.
 152  
         // It looks only values from the file are used
 153  
         // so here we create a temporary file with values modified
 154  
         //----------------------------------------------------------------------------
 155  0
         FileReader fr = null;
 156  0
         FileWriter fw = null;
 157  0
         StringWriter sw = new StringWriter();
 158  
         try
 159  
         {
 160  0
             temporaryContextFile = File.createTempFile( "tomcat-maven-plugin", "temp-ctx-file" );
 161  0
             temporaryContextFile.deleteOnExit();
 162  
 
 163  
             // format to modify/create <Context backgroundProcessorDelay="5" reloadable="false">
 164  0
             if ( contextFile != null && contextFile.exists() )
 165  
             {
 166  0
                 MavenFileFilterRequest mavenFileFilterRequest = new MavenFileFilterRequest();
 167  0
                 mavenFileFilterRequest.setFrom( contextFile );
 168  0
                 mavenFileFilterRequest.setTo( temporaryContextFile );
 169  0
                 mavenFileFilterRequest.setMavenProject( project );
 170  0
                 mavenFileFilterRequest.setMavenSession( session );
 171  0
                 mavenFileFilterRequest.setFiltering( true );
 172  
 
 173  0
                 mavenFileFilter.copyFile( mavenFileFilterRequest );
 174  
 
 175  0
                 fr = new FileReader( temporaryContextFile );
 176  0
                 Xpp3Dom xpp3Dom = Xpp3DomBuilder.build( fr );
 177  0
                 xpp3Dom.setAttribute( "backgroundProcessorDelay", Integer.toString( backgroundProcessorDelay ) );
 178  0
                 xpp3Dom.setAttribute( "reloadable", Boolean.toString( isContextReloadable() ) );
 179  0
                 fw = new FileWriter( temporaryContextFile );
 180  0
                 Xpp3DomWriter.write( fw, xpp3Dom );
 181  0
                 Xpp3DomWriter.write( sw, xpp3Dom );
 182  0
                 getLog().debug( " generated context file " + sw.toString() );
 183  0
             }
 184  
             else
 185  
             {
 186  0
                 if ( contextReloadable )
 187  
                 {
 188  
                     // don't care about using a complicated xml api to create one xml line :-)
 189  0
                     StringBuilder sb = new StringBuilder( "<Context " ).append( "backgroundProcessorDelay=\"" ).append(
 190  
                         Integer.toString( backgroundProcessorDelay ) ).append( "\"" ).append(
 191  
                         " reloadable=\"" + Boolean.toString( isContextReloadable() ) + "\"/>" );
 192  
 
 193  0
                     getLog().debug( " generated context file " + sb.toString() );
 194  
 
 195  0
                     fw.write( sb.toString() );
 196  0
                 }
 197  
                 else
 198  
                 {
 199  
                     // no user context file and contextReloadable false so no need about creating a hack one
 200  0
                     return null;
 201  
                 }
 202  
             }
 203  
         }
 204  0
         catch ( IOException e )
 205  
         {
 206  0
             getLog().error( "error creating fake context.xml : " + e.getMessage(), e );
 207  0
             throw new MojoExecutionException( "error creating fake context.xml : " + e.getMessage(), e );
 208  
         }
 209  0
         catch ( XmlPullParserException e )
 210  
         {
 211  0
             getLog().error( "error creating fake context.xml : " + e.getMessage(), e );
 212  0
             throw new MojoExecutionException( "error creating fake context.xml : " + e.getMessage(), e );
 213  
         }
 214  0
         catch ( MavenFilteringException e )
 215  
         {
 216  0
             getLog().error( "error filtering context.xml : " + e.getMessage(), e );
 217  0
             throw new MojoExecutionException( "error filtering context.xml : " + e.getMessage(), e );
 218  
         }
 219  
         finally
 220  
         {
 221  0
             IOUtil.close( fw );
 222  0
             IOUtil.close( fr );
 223  0
             IOUtil.close( sw );
 224  0
         }
 225  
 
 226  0
         return temporaryContextFile;
 227  
     }
 228  
 
 229  
     /**
 230  
      * {@inheritDoc}
 231  
      *
 232  
      * @throws MojoExecutionException
 233  
      */
 234  
     @Override
 235  
     protected WebappLoader createWebappLoader()
 236  
         throws IOException, MojoExecutionException
 237  
     {
 238  0
         WebappLoader loader = super.createWebappLoader();
 239  0
         if ( useSeparateTomcatClassLoader )
 240  
         {
 241  0
             loader.setDelegate( delegate );
 242  
         }
 243  
 
 244  
         try
 245  
         {
 246  0
             ClassLoaderEntriesCalculatorRequest request =
 247  
                 new ClassLoaderEntriesCalculatorRequest().setDependencies( dependencies ).setLog(
 248  
                     getLog() ).setMavenProject( project ).setAddWarDependenciesInClassloader(
 249  
                     addWarDependenciesInClassloader ).setUseTestClassPath( useTestClasspath );
 250  0
             ClassLoaderEntriesCalculatorResult classLoaderEntriesCalculatorResult =
 251  
                 classLoaderEntriesCalculator.calculateClassPathEntries( request );
 252  0
             List<String> classLoaderEntries = classLoaderEntriesCalculatorResult.getClassPathEntries();
 253  0
             final List<File> tmpDirectories = classLoaderEntriesCalculatorResult.getTmpDirectories();
 254  
 
 255  0
             Runtime.getRuntime().addShutdownHook( new Thread()
 256  0
             {
 257  
                 @Override
 258  
                 public void run()
 259  
                 {
 260  0
                     for ( File tmpDir : tmpDirectories )
 261  
                     {
 262  
                         try
 263  
                         {
 264  0
                             FileUtils.deleteDirectory( tmpDir );
 265  
                         }
 266  0
                         catch ( IOException e )
 267  
                         {
 268  
                             // ignore
 269  0
                         }
 270  
                     }
 271  0
                 }
 272  
             } );
 273  
 
 274  0
             if ( classLoaderEntries != null )
 275  
             {
 276  0
                 for ( String classLoaderEntry : classLoaderEntries )
 277  
                 {
 278  0
                     loader.addRepository( classLoaderEntry );
 279  
                 }
 280  
             }
 281  
 
 282  0
             if ( additionalClasspathDirs != null && !additionalClasspathDirs.isEmpty() )
 283  
             {
 284  0
                 for ( String additionalClasspathDir : additionalClasspathDirs )
 285  
                 {
 286  0
                     if ( StringUtils.isNotBlank( additionalClasspathDir ) )
 287  
                     {
 288  0
                         File file = new File( additionalClasspathDir );
 289  0
                         if ( file.exists() )
 290  
                         {
 291  0
                             String fileUri = file.toURI().toString();
 292  0
                             getLog().debug( "add file:" + fileUri + " as a additionalClasspathDir" );
 293  0
                             loader.addRepository( fileUri );
 294  
                         }
 295  0
                     }
 296  
                 }
 297  
             }
 298  
         }
 299  0
         catch ( TomcatRunException e )
 300  
         {
 301  0
             throw new MojoExecutionException( e.getMessage(), e );
 302  0
         }
 303  
 
 304  0
         return loader;
 305  
     }
 306  
 }