View Javadoc

1   package org.apache.tomcat.maven.plugin.tomcat6;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.catalina.Context;
23  import org.apache.catalina.loader.WebappLoader;
24  import org.apache.catalina.startup.Embedded;
25  import org.apache.commons.io.FileUtils;
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugins.annotations.Component;
30  import org.apache.maven.plugins.annotations.Execute;
31  import org.apache.maven.plugins.annotations.LifecyclePhase;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.plugins.annotations.ResolutionScope;
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 Jurgen Lust
57   * @author Mark Hobson <markhobson@gmail.com>
58   */
59  @Mojo( name = "run", requiresDependencyResolution = ResolutionScope.TEST )
60  @Execute( phase = LifecyclePhase.PROCESS_CLASSES )
61  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-6.0-doc/api/org/apache/catalina/loader/WebappLoader.html#setDelegate(boolean)
86       * @since 1.0
87       */
88      @Parameter( property = "tomcat.delegate", defaultValue = "true" )
89      private boolean delegate = true;
90  
91      /**
92       * @since 2.0
93       */
94      @Component( role = ClassLoaderEntriesCalculator.class )
95      private ClassLoaderEntriesCalculator classLoaderEntriesCalculator;
96  
97      /**
98       * will add /WEB-INF/lib/*.jar and /WEB-INF/classes from war dependencies in the webappclassloader
99       *
100      * @since 2.0
101      */
102     @Parameter( property = "maven.tomcat.addWarDependenciesInClassloader", defaultValue = "true" )
103     private boolean addWarDependenciesInClassloader;
104 
105     /**
106      * will use the test classpath rather than the compile one and will add test dependencies too
107      *
108      * @since 2.0
109      */
110     @Parameter( property = "maven.tomcat.useTestClasspath", defaultValue = "false" )
111     private boolean useTestClasspath;
112 
113     /**
114      * Additional optional directories to add to the embedded tomcat classpath.
115      *
116      * @since 2.0-beta-1
117      */
118     @Parameter( alias = "additionalClassesDirs" )
119     private List<String> additionalClasspathDirs;
120 
121     // ----------------------------------------------------------------------
122     // AbstractRunMojo Implementation
123     // ----------------------------------------------------------------------
124 
125     /**
126      * {@inheritDoc}
127      *
128      * @throws MojoExecutionException
129      */
130     @Override
131     protected Context createContext( Embedded container )
132         throws IOException, MojoExecutionException
133     {
134         Context context = super.createContext( container );
135 
136         context.setReloadable( isContextReloadable() );
137 
138         return context;
139     }
140 
141     /**
142      * {@inheritDoc}
143      *
144      * @throws MojoExecutionException
145      */
146     @Override
147     protected WebappLoader createWebappLoader()
148         throws IOException, MojoExecutionException
149     {
150         WebappLoader loader = super.createWebappLoader();
151         if ( useSeparateTomcatClassLoader )
152         {
153             loader.setDelegate( delegate );
154         }
155 
156         try
157         {
158             ClassLoaderEntriesCalculatorRequest request =
159                 new ClassLoaderEntriesCalculatorRequest().setDependencies( dependencies ).setLog(
160                     getLog() ).setMavenProject( project ).setAddWarDependenciesInClassloader(
161                     addWarDependenciesInClassloader ).setUseTestClassPath( useTestClasspath );
162             ClassLoaderEntriesCalculatorResult classLoaderEntriesCalculatorResult =
163                 classLoaderEntriesCalculator.calculateClassPathEntries( request );
164             List<String> classLoaderEntries = classLoaderEntriesCalculatorResult.getClassPathEntries();
165             final List<File> tmpDirectories = classLoaderEntriesCalculatorResult.getTmpDirectories();
166 
167             Runtime.getRuntime().addShutdownHook( new Thread()
168             {
169                 @Override
170                 public void run()
171                 {
172                     for ( File tmpDir : tmpDirectories )
173                     {
174                         try
175                         {
176                             FileUtils.deleteDirectory( tmpDir );
177                         }
178                         catch ( IOException e )
179                         {
180                             // ignore
181                         }
182                     }
183                 }
184             } );
185 
186             if ( classLoaderEntries != null )
187             {
188                 for ( String classLoaderEntry : classLoaderEntries )
189                 {
190                     loader.addRepository( classLoaderEntry );
191                 }
192             }
193             if ( additionalClasspathDirs != null && !additionalClasspathDirs.isEmpty() )
194             {
195                 for ( String additionalClasspathDir : additionalClasspathDirs )
196                 {
197                     if ( StringUtils.isNotBlank( additionalClasspathDir ) )
198                     {
199                         File file = new File( additionalClasspathDir );
200                         if ( file.exists() )
201                         {
202                             String fileUri = file.toURI().toString();
203                             getLog().debug( "add file:" + fileUri + " as a additionalClasspathDir" );
204                             loader.addRepository( fileUri );
205                         }
206                     }
207                 }
208             }
209         }
210         catch ( TomcatRunException e )
211         {
212             throw new MojoExecutionException( e.getMessage(), e );
213         }
214 
215         return loader;
216     }
217 
218 
219     /**
220      * {@inheritDoc}
221      */
222     @Override
223     protected File getDocBase()
224     {
225         return warSourceDirectory;
226     }
227 
228     /**
229      * {@inheritDoc}
230      */
231     @Override
232     protected File getContextFile()
233         throws MojoExecutionException
234     {
235         File temporaryContextFile = null;
236 
237         //----------------------------------------------------------------------------
238         // context attributes backgroundProcessorDelay reloadable cannot be modified at runtime.
239         // It looks only values from the file ared used
240         // so here we create a temporary file with values modified
241         //----------------------------------------------------------------------------
242         FileReader fr = null;
243         FileWriter fw = null;
244         StringWriter sw = new StringWriter();
245         try
246         {
247             temporaryContextFile = File.createTempFile( "tomcat-maven-plugin", "temp-ctx-file" );
248             temporaryContextFile.deleteOnExit();
249             fw = new FileWriter( temporaryContextFile );
250             // format to modify/create <Context backgroundProcessorDelay="5" reloadable="false">
251             if ( contextFile != null && contextFile.exists() )
252             {
253                 fr = new FileReader( contextFile );
254                 Xpp3Dom xpp3Dom = Xpp3DomBuilder.build( fr );
255                 xpp3Dom.setAttribute( "backgroundProcessorDelay", Integer.toString( backgroundProcessorDelay ) );
256                 xpp3Dom.setAttribute( "reloadable", Boolean.toString( isContextReloadable() ) );
257                 Xpp3DomWriter.write( fw, xpp3Dom );
258                 Xpp3DomWriter.write( sw, xpp3Dom );
259                 getLog().debug( " generated context file " + sw.toString() );
260             }
261             else
262             {
263                 if ( contextReloadable )
264                 {
265                     // don't care about using a complicated xml api to create one xml line :-)
266                     StringBuilder sb = new StringBuilder( "<Context " ).append( "backgroundProcessorDelay=\"" ).append(
267                         Integer.toString( backgroundProcessorDelay ) ).append( "\"" ).append(
268                         " reloadable=\"" + Boolean.toString( isContextReloadable() ) + "\"/>" );
269 
270                     getLog().debug( " generated context file " + sb.toString() );
271 
272                     fw.write( sb.toString() );
273                 }
274                 else
275                 {
276                     // no user context file and contextReloadable false so no need about creating a hack one 
277                     return null;
278                 }
279             }
280         }
281         catch ( IOException e )
282         {
283             getLog().error( "error creating fake context.xml : " + e.getMessage(), e );
284             throw new MojoExecutionException( "error creating fake context.xml : " + e.getMessage(), e );
285         }
286         catch ( XmlPullParserException e )
287         {
288             getLog().error( "error creating fake context.xml : " + e.getMessage(), e );
289             throw new MojoExecutionException( "error creating fake context.xml : " + e.getMessage(), e );
290         }
291         finally
292         {
293             IOUtil.close( fw );
294             IOUtil.close( fr );
295             IOUtil.close( sw );
296         }
297 
298         return temporaryContextFile;
299     }
300 
301 }