adding all of botlist, initial add
[botlist.git] / botlist_testserver / jetty / contrib / jetty-ant / src / main / java / org / mortbay / jetty / ant / WebApplicationProxyImpl.java
blob201307d1c5c5de781643f127eb42a93658ce3ca1
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;
17 import java.io.File;
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;
36 /**
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. */
48 private String name;
50 /** Location of WAR file (either expanded or not). */
51 private File warFile;
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 = new Configuration[] { new WebInfConfiguration(),
75 new EnvConfiguration(), new JettyWebAppConfiguration(), new JettyWebXmlConfiguration(),
76 new TagLibConfiguration() };
78 private FileMatchingConfiguration librariesConfiguration;
80 public static void setBaseTempDirectory(File tempDirectory)
82 baseTempDirectory = tempDirectory;
85 /**
86 * Default constructor. Takes application name as an argument.
88 * @param name web application name.
90 public WebApplicationProxyImpl(String name)
92 this.name = name;
93 TaskLog.log("\nConfiguring Jetty for web application: " + name);
96 public List getClassPathFiles()
98 return classPathFiles;
101 public String getContextPath()
103 return contextPath;
106 public String getName()
108 return name;
111 public File getSourceDirectory()
113 return warFile;
116 public File getWebXmlFile()
118 return webXmlFile;
121 public void setSourceDirectory(File warFile)
123 this.warFile = warFile;
124 TaskLog.log("Webapp source directory = " + warFile);
127 public void setContextPath(String contextPath)
129 if (!contextPath.startsWith("/"))
131 contextPath = "/" + contextPath;
133 this.contextPath = contextPath;
134 TaskLog.log("Context path = " + contextPath);
138 public void setWebXml(File webXmlFile)
140 this.webXmlFile = webXmlFile;
143 public void setJettyEnvXml(File jettyEnvXml)
145 this.jettyEnvXml = jettyEnvXml;
146 if (this.jettyEnvXml != null)
148 TaskLog.log("jetty-env.xml file: = " + jettyEnvXml.getAbsolutePath());
152 public void setClassPathFiles(List classPathFiles)
154 this.classPathFiles = classPathFiles;
155 TaskLog.log("Classpath = " + classPathFiles);
159 * Checks if a given file is scanned according to the internal
160 * configuration. This may be difficult due to use of 'includes' and
161 * 'excludes' statements.
163 * @param pathToFile a fully qualified path to file.
164 * @return true if file is being scanned, false otherwise.
166 public boolean isFileScanned(String pathToFile)
168 return librariesConfiguration.isIncluded(pathToFile)
169 || extraScanTargetsConfiguration.isIncluded(pathToFile);
172 public void setLibrariesConfiguration(FileMatchingConfiguration classesConfiguration)
174 TaskLog.log("Default scanned paths = " + classesConfiguration.getBaseDirectories());
175 this.librariesConfiguration = classesConfiguration;
178 public List getLibraries()
180 return librariesConfiguration.getBaseDirectories();
183 public void setExtraScanTargetsConfiguration(
184 FileMatchingConfiguration extraScanTargetsConfiguration)
186 this.extraScanTargetsConfiguration = extraScanTargetsConfiguration;
187 TaskLog.log("Extra scan targets = " + extraScanTargetsConfiguration.getBaseDirectories());
190 public List getExtraScanTargets()
192 return extraScanTargetsConfiguration.getBaseDirectories();
195 public List getContextHandlers()
197 return contextHandlers;
200 public void setContextHandlers(List contextHandlers)
202 this.contextHandlers = contextHandlers;
206 * @see com.sabre.ant.jetty.WebApplicationProxy#getProxiedObject()
208 public Object getProxiedObject()
210 return webAppContext;
214 * @see com.sabre.ant.jetty.WebApplicationProxy#start()
216 public void start()
220 TaskLog.logWithTimestamp("Starting web application " + name + " ...\n");
221 webAppContext.setShutdown(false);
222 webAppContext.start();
224 catch (Exception e)
226 TaskLog.log(e.toString());
231 * @see com.sabre.ant.jetty.WebApplicationProxy#stop()
233 public void stop()
237 TaskLog.logWithTimestamp("Stopping web application " + name + " ...");
238 webAppContext.setShutdown(true);
239 Thread.currentThread().sleep(500L);
240 webAppContext.stop();
242 catch (InterruptedException e)
244 TaskLog.log(e.toString());
246 catch (Exception e)
248 TaskLog.log(e.toString());
253 * @see com.sabre.ant.jetty.WebApplicationProxy#createApplicationContext(org.mortbay.jetty.handler.ContextHandlerCollection)
255 public void createApplicationContext(ContextHandlerCollection contexts)
257 webAppContext = new WebAppContext(contexts, warFile.getAbsolutePath(), contextPath);
258 webAppContext.setDisplayName(name);
260 configurePaths();
261 configureHandlers(contexts);
263 applyConfiguration();
266 private void configureHandlers(ContextHandlerCollection contexts)
268 // adding extra context handlers
269 Iterator handlersIterator = contextHandlers.iterator();
270 while (handlersIterator.hasNext())
272 ContextHandler contextHandler = (ContextHandler) handlersIterator.next();
273 contexts.addHandler(contextHandler);
277 private void configurePaths()
279 // configuring temp directory
280 File tempDir = new File(baseTempDirectory, contextPath);
281 if (!tempDir.exists())
283 tempDir.mkdirs();
285 webAppContext.setTempDirectory(tempDir);
286 tempDir.deleteOnExit();
287 TaskLog.log("Temp directory = " + tempDir.getAbsolutePath());
289 // configuring WAR directory for packaged web applications
290 if (warFile.isFile())
292 warFile = new File(tempDir, "webapp");
293 webXmlFile = new File(new File(warFile, "WEB-INF"), "web.xml");
298 * Applies web application configuration at the end of configuration process
299 * or after application restart.
301 void applyConfiguration()
303 for (int i = 0; i < configurations.length; i++)
305 if (configurations[i] instanceof EnvConfiguration)
309 if (jettyEnvXml != null && jettyEnvXml.exists())
311 ((EnvConfiguration) configurations[i]).setJettyEnvXml(jettyEnvXml.toURL());
314 catch (MalformedURLException e)
316 throw new RuntimeException(e);
319 else if (configurations[i] instanceof JettyWebAppConfiguration)
321 ((JettyWebAppConfiguration) configurations[i]).setClassPathFiles(classPathFiles);
322 ((JettyWebAppConfiguration) configurations[i]).setWebAppBaseDir(warFile);
323 ((JettyWebAppConfiguration) configurations[i]).setWebXmlFile(webXmlFile);
329 ClassLoader loader = new WebAppClassLoader(this.getClass().getClassLoader(),
330 webAppContext);
331 webAppContext.setParentLoaderPriority(true);
332 webAppContext.setClassLoader(loader);
334 catch (IOException e)
336 TaskLog.log(e.toString());
339 webAppContext.setConfigurations(configurations);