1
// ========================================================================
2
// Copyright 2006-2007 Sabre Holdings.
3
// ------------------------------------------------------------------------
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13
// ========================================================================
15
package org.mortbay.jetty.ant;
18
import java.io.IOException;
19
import java.net.MalformedURLException;
20
import java.util.Iterator;
21
import java.util.List;
23
import org.mortbay.jetty.ant.types.FileMatchingConfiguration;
24
import org.mortbay.jetty.ant.utils.TaskLog;
25
import org.mortbay.jetty.ant.utils.WebApplicationProxy;
26
import org.mortbay.jetty.handler.ContextHandler;
27
import org.mortbay.jetty.handler.ContextHandlerCollection;
28
import org.mortbay.jetty.plus.webapp.EnvConfiguration;
29
import org.mortbay.jetty.webapp.Configuration;
30
import org.mortbay.jetty.webapp.JettyWebXmlConfiguration;
31
import org.mortbay.jetty.webapp.TagLibConfiguration;
32
import org.mortbay.jetty.webapp.WebAppClassLoader;
33
import org.mortbay.jetty.webapp.WebAppContext;
34
import org.mortbay.jetty.webapp.WebInfConfiguration;
37
* An abstraction layer over Jetty WebAppContext.
39
* @author Jakub Pawlowicz
41
public class WebApplicationProxyImpl implements WebApplicationProxy
44
/** Common root temp directory for all web applications. */
45
static File baseTempDirectory = new File(".");
47
/** Name of this web application. */
50
/** Location of WAR file (either expanded or not). */
53
/** Application context path. */
54
private String contextPath;
56
/** Location of web.xml file. */
57
private File webXmlFile;
59
/** Location of jetty-env.xml file. */
60
private File jettyEnvXml;
62
/** List of classpath files. */
63
private List classPathFiles;
65
/** Jetty6 Web Application Context. */
66
private WebAppContext webAppContext;
68
/** Extra scan targets. */
69
private FileMatchingConfiguration extraScanTargetsConfiguration;
71
/** Extra context handlers. */
72
private List contextHandlers;
74
Configuration[] configurations;
76
private FileMatchingConfiguration librariesConfiguration;
78
public static void setBaseTempDirectory(File tempDirectory)
80
baseTempDirectory = tempDirectory;
84
* Default constructor. Takes application name as an argument.
86
* @param name web application name.
88
public WebApplicationProxyImpl(String name) throws Exception
91
TaskLog.log("\nConfiguring Jetty for web application: " + name);
93
this.configurations = new Configuration[] { new WebInfConfiguration(),
94
new EnvConfiguration(), new JettyWebAppConfiguration(), new JettyWebXmlConfiguration(),
95
new TagLibConfiguration() };
98
public List getClassPathFiles()
100
return classPathFiles;
103
public String getContextPath()
108
public String getName()
113
public File getSourceDirectory()
118
public File getWebXmlFile()
123
public void setSourceDirectory(File warFile)
125
this.warFile = warFile;
126
TaskLog.log("Webapp source directory = " + warFile);
129
public void setContextPath(String contextPath)
131
if (!contextPath.startsWith("/"))
133
contextPath = "/" + contextPath;
135
this.contextPath = contextPath;
136
TaskLog.log("Context path = " + contextPath);
140
public void setWebXml(File webXmlFile)
142
this.webXmlFile = webXmlFile;
145
public void setJettyEnvXml(File jettyEnvXml)
147
this.jettyEnvXml = jettyEnvXml;
148
if (this.jettyEnvXml != null)
150
TaskLog.log("jetty-env.xml file: = " + jettyEnvXml.getAbsolutePath());
154
public void setClassPathFiles(List classPathFiles)
156
this.classPathFiles = classPathFiles;
157
TaskLog.log("Classpath = " + classPathFiles);
161
* Checks if a given file is scanned according to the internal
162
* configuration. This may be difficult due to use of 'includes' and
163
* 'excludes' statements.
165
* @param pathToFile a fully qualified path to file.
166
* @return true if file is being scanned, false otherwise.
168
public boolean isFileScanned(String pathToFile)
170
return librariesConfiguration.isIncluded(pathToFile)
171
|| extraScanTargetsConfiguration.isIncluded(pathToFile);
174
public void setLibrariesConfiguration(FileMatchingConfiguration classesConfiguration)
176
TaskLog.log("Default scanned paths = " + classesConfiguration.getBaseDirectories());
177
this.librariesConfiguration = classesConfiguration;
180
public List getLibraries()
182
return librariesConfiguration.getBaseDirectories();
185
public void setExtraScanTargetsConfiguration(
186
FileMatchingConfiguration extraScanTargetsConfiguration)
188
this.extraScanTargetsConfiguration = extraScanTargetsConfiguration;
189
TaskLog.log("Extra scan targets = " + extraScanTargetsConfiguration.getBaseDirectories());
192
public List getExtraScanTargets()
194
return extraScanTargetsConfiguration.getBaseDirectories();
197
public List getContextHandlers()
199
return contextHandlers;
202
public void setContextHandlers(List contextHandlers)
204
this.contextHandlers = contextHandlers;
208
* @see com.sabre.ant.jetty.WebApplicationProxy#getProxiedObject()
210
public Object getProxiedObject()
212
return webAppContext;
216
* @see com.sabre.ant.jetty.WebApplicationProxy#start()
222
TaskLog.logWithTimestamp("Starting web application " + name + " ...\n");
223
webAppContext.setShutdown(false);
224
webAppContext.start();
228
TaskLog.log(e.toString());
233
* @see com.sabre.ant.jetty.WebApplicationProxy#stop()
239
TaskLog.logWithTimestamp("Stopping web application " + name + " ...");
240
webAppContext.setShutdown(true);
241
Thread.currentThread().sleep(500L);
242
webAppContext.stop();
244
catch (InterruptedException e)
246
TaskLog.log(e.toString());
250
TaskLog.log(e.toString());
255
* @see com.sabre.ant.jetty.WebApplicationProxy#createApplicationContext(org.mortbay.jetty.handler.ContextHandlerCollection)
257
public void createApplicationContext(ContextHandlerCollection contexts)
259
webAppContext = new WebAppContext(contexts, warFile.getAbsolutePath(), contextPath);
260
webAppContext.setDisplayName(name);
263
configureHandlers(contexts);
265
applyConfiguration();
268
private void configureHandlers(ContextHandlerCollection contexts)
270
// adding extra context handlers
271
Iterator handlersIterator = contextHandlers.iterator();
272
while (handlersIterator.hasNext())
274
ContextHandler contextHandler = (ContextHandler) handlersIterator.next();
275
contexts.addHandler(contextHandler);
279
private void configurePaths()
281
// configuring temp directory
282
File tempDir = new File(baseTempDirectory, contextPath);
283
if (!tempDir.exists())
287
webAppContext.setTempDirectory(tempDir);
288
tempDir.deleteOnExit();
289
TaskLog.log("Temp directory = " + tempDir.getAbsolutePath());
291
// configuring WAR directory for packaged web applications
292
if (warFile.isFile())
294
warFile = new File(tempDir, "webapp");
295
webXmlFile = new File(new File(warFile, "WEB-INF"), "web.xml");
300
* Applies web application configuration at the end of configuration process
301
* or after application restart.
303
void applyConfiguration()
305
for (int i = 0; i < configurations.length; i++)
307
if (configurations[i] instanceof EnvConfiguration)
311
if (jettyEnvXml != null && jettyEnvXml.exists())
313
((EnvConfiguration) configurations[i]).setJettyEnvXml(jettyEnvXml.toURL());
316
catch (MalformedURLException e)
318
throw new RuntimeException(e);
321
else if (configurations[i] instanceof JettyWebAppConfiguration)
323
((JettyWebAppConfiguration) configurations[i]).setClassPathFiles(classPathFiles);
324
((JettyWebAppConfiguration) configurations[i]).setWebAppBaseDir(warFile);
325
((JettyWebAppConfiguration) configurations[i]).setWebXmlFile(webXmlFile);
326
((JettyWebAppConfiguration) configurations[i]).setWebDefaultXmlFile(webDefaultXmlFile);
332
ClassLoader loader = new WebAppClassLoader(this.getClass().getClassLoader(),
334
webAppContext.setParentLoaderPriority(true);
335
webAppContext.setClassLoader(loader);
336
if (webDefaultXmlFile != null)
337
webAppContext.setDefaultsDescriptor(webDefaultXmlFile.getCanonicalPath());
340
catch (IOException e)
342
TaskLog.log(e.toString());
345
webAppContext.setConfigurations(configurations);
348
private File webDefaultXmlFile;
350
public File getWebDefaultXmlFile()
352
return this.webDefaultXmlFile;
355
public void setWebDefaultXmlFile(File webDefaultXmlfile)
357
this.webDefaultXmlFile = webDefaultXmlfile;