1 package org.apache.tomcat.maven.plugin.tomcat7.run;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.catalina.loader.WebappLoader;
22 import org.apache.commons.io.FileUtils;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.tomcat.maven.common.run.ClassLoaderEntriesCalculator;
26 import org.apache.tomcat.maven.common.run.ClassLoaderEntriesCalculatorRequest;
27 import org.apache.tomcat.maven.common.run.ClassLoaderEntriesCalculatorResult;
28 import org.apache.tomcat.maven.common.run.TomcatRunException;
29 import org.codehaus.plexus.util.IOUtil;
30 import org.codehaus.plexus.util.xml.Xpp3Dom;
31 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
32 import org.codehaus.plexus.util.xml.Xpp3DomWriter;
33 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34
35 import java.io.File;
36 import java.io.FileReader;
37 import java.io.FileWriter;
38 import java.io.IOException;
39 import java.io.StringWriter;
40 import java.util.List;
41 import java.util.Set;
42
43
44
45
46
47
48
49
50
51
52 public class RunMojo
53 extends AbstractRunMojo
54 {
55
56
57
58
59
60
61
62
63
64
65
66
67 private Set<Artifact> dependencies;
68
69
70
71
72
73
74 private File warSourceDirectory;
75
76
77
78
79
80
81
82
83
84 private boolean delegate = true;
85
86
87
88
89
90
91
92 protected int backgroundProcessorDelay = -1;
93
94
95
96
97
98
99 private ClassLoaderEntriesCalculator classLoaderEntriesCalculator;
100
101
102
103
104
105
106
107 private boolean addWarDependenciesInClassloader;
108
109
110
111
112
113
114
115 private boolean useTestClasspath;
116
117
118
119
120
121
122
123 private List<File> additionalClasspathDirs;
124
125 private File temporaryContextFile = null;
126
127
128
129
130 @Override
131 protected File getDocBase()
132 {
133 return warSourceDirectory;
134 }
135
136
137
138
139 @Override
140 protected File getContextFile()
141 throws MojoExecutionException
142 {
143 if ( temporaryContextFile != null )
144 {
145 return temporaryContextFile;
146 }
147
148
149
150
151
152 FileReader fr = null;
153 FileWriter fw = null;
154 StringWriter sw = new StringWriter();
155 try
156 {
157 temporaryContextFile = File.createTempFile( "tomcat-maven-plugin", "temp-ctx-file" );
158 temporaryContextFile.deleteOnExit();
159 fw = new FileWriter( temporaryContextFile );
160
161 if ( contextFile != null && contextFile.exists() )
162 {
163 fr = new FileReader( contextFile );
164 Xpp3Dom xpp3Dom = Xpp3DomBuilder.build( fr );
165 xpp3Dom.setAttribute( "backgroundProcessorDelay", Integer.toString( backgroundProcessorDelay ) );
166 xpp3Dom.setAttribute( "reloadable", Boolean.toString( isContextReloadable() ) );
167 Xpp3DomWriter.write( fw, xpp3Dom );
168 Xpp3DomWriter.write( sw, xpp3Dom );
169 getLog().debug( " generated context file " + sw.toString() );
170 }
171 else
172 {
173 if ( contextReloadable )
174 {
175
176 StringBuilder sb = new StringBuilder( "<Context " ).append( "backgroundProcessorDelay=\"" ).append(
177 Integer.toString( backgroundProcessorDelay ) ).append( "\"" ).append(
178 " reloadable=\"" + Boolean.toString( isContextReloadable() ) + "\"/>" );
179
180 getLog().debug( " generated context file " + sb.toString() );
181
182 fw.write( sb.toString() );
183 }
184 else
185 {
186
187 return null;
188 }
189 }
190 }
191 catch ( IOException e )
192 {
193 getLog().error( "error creating fake context.xml : " + e.getMessage(), e );
194 throw new MojoExecutionException( "error creating fake context.xml : " + e.getMessage(), e );
195 }
196 catch ( XmlPullParserException e )
197 {
198 getLog().error( "error creating fake context.xml : " + e.getMessage(), e );
199 throw new MojoExecutionException( "error creating fake context.xml : " + e.getMessage(), e );
200 }
201 finally
202 {
203 IOUtil.close( fw );
204 IOUtil.close( fr );
205 IOUtil.close( sw );
206 }
207
208 return temporaryContextFile;
209 }
210
211
212
213
214
215
216 @Override
217 protected WebappLoader createWebappLoader()
218 throws IOException, MojoExecutionException
219 {
220 WebappLoader loader = super.createWebappLoader();
221 if ( useSeparateTomcatClassLoader )
222 {
223 loader.setDelegate( delegate );
224 }
225
226 try
227 {
228 ClassLoaderEntriesCalculatorRequest request =
229 new ClassLoaderEntriesCalculatorRequest().setDependencies( dependencies ).setLog(
230 getLog() ).setMavenProject( project ).setAddWarDependenciesInClassloader(
231 addWarDependenciesInClassloader ).setUseTestClassPath( useTestClasspath );
232 ClassLoaderEntriesCalculatorResult classLoaderEntriesCalculatorResult =
233 classLoaderEntriesCalculator.calculateClassPathEntries( request );
234 List<String> classLoaderEntries = classLoaderEntriesCalculatorResult.getClassPathEntries();
235 final List<File> tmpDirectories = classLoaderEntriesCalculatorResult.getTmpDirectories();
236
237 Runtime.getRuntime().addShutdownHook( new Thread()
238 {
239 @Override
240 public void run()
241 {
242 for ( File tmpDir : tmpDirectories )
243 {
244 try
245 {
246 FileUtils.deleteDirectory( tmpDir );
247 }
248 catch ( IOException e )
249 {
250
251 }
252 }
253 }
254 } );
255
256 if ( classLoaderEntries != null )
257 {
258 for ( String classLoaderEntry : classLoaderEntries )
259 {
260 loader.addRepository( classLoaderEntry );
261 }
262 }
263
264 if ( additionalClasspathDirs != null && !additionalClasspathDirs.isEmpty() )
265 {
266 for ( File additionalClasspathDir : additionalClasspathDirs )
267 {
268 if ( additionalClasspathDir.exists() )
269 {
270 loader.addRepository( additionalClasspathDir.toURI().toString() );
271 }
272 }
273 }
274 }
275 catch ( TomcatRunException e )
276 {
277 throw new MojoExecutionException( e.getMessage(), e );
278 }
279
280 return loader;
281 }
282 }