001: //========================================================================
002: //$Id: WebAppDeployer.java 1237 2006-11-17 09:32:17Z gregw $
003: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty.deployer;
017:
018: import java.util.ArrayList;
019:
020: import org.mortbay.component.AbstractLifeCycle;
021: import org.mortbay.jetty.Handler;
022: import org.mortbay.jetty.HandlerContainer;
023: import org.mortbay.jetty.handler.ContextHandler;
024: import org.mortbay.jetty.handler.ContextHandlerCollection;
025: import org.mortbay.jetty.webapp.WebAppContext;
026: import org.mortbay.resource.Resource;
027:
028: /**
029: * Web Application Deployer.
030: *
031: * The class searches a directory for and deploys standard web application.
032: * At startup, the directory specified by {@link #setWebAppDir(String)} is searched
033: * for subdirectories (excluding hidden and CVS) or files ending with ".zip"
034: * or "*.war". For each webapp discovered is passed to a new instance
035: * of {@link WebAppContext} (or a subclass specified by {@link #getContexts()}.
036: * {@link ContextHandlerCollection#getContextClass()}
037: *
038: * This deployer does not do hot deployment or undeployment. Nor does
039: * it support per webapplication configuration. For these features
040: * see {@link ContextDeployer}.
041: *
042: * @see {@link ContextDeployer}
043: */
044: public class WebAppDeployer extends AbstractLifeCycle {
045: private HandlerContainer _contexts;
046: private String _webAppDir;
047: private String _defaultsDescriptor;
048: private String[] _configurationClasses;
049: private boolean _extract;
050: private boolean _parentLoaderPriority;
051: private boolean _allowDuplicates;
052: private ArrayList _deployed;
053:
054: public String[] getConfigurationClasses() {
055: return _configurationClasses;
056: }
057:
058: public void setConfigurationClasses(String[] configurationClasses) {
059: _configurationClasses = configurationClasses;
060: }
061:
062: public HandlerContainer getContexts() {
063: return _contexts;
064: }
065:
066: public void setContexts(HandlerContainer contexts) {
067: _contexts = contexts;
068: }
069:
070: public String getDefaultsDescriptor() {
071: return _defaultsDescriptor;
072: }
073:
074: public void setDefaultsDescriptor(String defaultsDescriptor) {
075: _defaultsDescriptor = defaultsDescriptor;
076: }
077:
078: public boolean isExtract() {
079: return _extract;
080: }
081:
082: public void setExtract(boolean extract) {
083: _extract = extract;
084: }
085:
086: public boolean isParentLoaderPriority() {
087: return _parentLoaderPriority;
088: }
089:
090: public void setParentLoaderPriority(
091: boolean parentPriorityClassLoading) {
092: _parentLoaderPriority = parentPriorityClassLoading;
093: }
094:
095: public String getWebAppDir() {
096: return _webAppDir;
097: }
098:
099: public void setWebAppDir(String dir) {
100: _webAppDir = dir;
101: }
102:
103: public boolean getAllowDuplicates() {
104: return _allowDuplicates;
105: }
106:
107: /* ------------------------------------------------------------ */
108: /**
109: * @param allowDuplicates If false, do not deploy webapps that have already been deployed or duplicate context path
110: */
111: public void setAllowDuplicates(boolean allowDuplicates) {
112: _allowDuplicates = allowDuplicates;
113: }
114:
115: /* ------------------------------------------------------------ */
116: /**
117: * @throws Exception
118: */
119: public void doStart() throws Exception {
120: _deployed = new ArrayList();
121:
122: scan();
123:
124: }
125:
126: /* ------------------------------------------------------------ */
127: /** Scan for webapplications.
128: *
129: * @throws Exception
130: */
131: public void scan() throws Exception {
132: if (_contexts == null)
133: throw new IllegalArgumentException("No HandlerContainer");
134:
135: Resource r = Resource.newResource(_webAppDir);
136: if (!r.exists())
137: throw new IllegalArgumentException(
138: "No such webapps resource " + r);
139:
140: if (!r.isDirectory())
141: throw new IllegalArgumentException(
142: "Not directory webapps resource " + r);
143:
144: String[] files = r.list();
145:
146: files: for (int f = 0; files != null && f < files.length; f++) {
147: String context = files[f];
148:
149: if (context.equalsIgnoreCase("CVS/")
150: || context.equalsIgnoreCase("CVS")
151: || context.startsWith("."))
152: continue;
153:
154: Resource app = r.addPath(r.encode(context));
155:
156: if (context.toLowerCase().endsWith(".war")
157: || context.toLowerCase().endsWith(".jar")) {
158: context = context.substring(0, context.length() - 4);
159: Resource unpacked = r.addPath(context);
160: if (unpacked != null && unpacked.exists()
161: && unpacked.isDirectory())
162: continue;
163: } else if (!app.isDirectory())
164: continue;
165:
166: if (context.equalsIgnoreCase("root")
167: || context.equalsIgnoreCase("root/"))
168: context = "/";
169: else
170: context = "/" + context;
171: if (context.endsWith("/") && context.length() > 0)
172: context = context.substring(0, context.length() - 1);
173:
174: // Check the context path has not already been added or the webapp itself is not already deployed
175: if (!_allowDuplicates) {
176: Handler[] installed = _contexts
177: .getChildHandlersByClass(ContextHandler.class);
178: for (int i = 0; i < installed.length; i++) {
179: ContextHandler c = (ContextHandler) installed[i];
180:
181: if (context.equals(c.getContextPath()))
182: continue files;
183:
184: if (c.getBaseResource() != null
185: && c.getBaseResource().getFile()
186: .getAbsolutePath().equals(
187: app.getFile()
188: .getAbsolutePath()))
189: continue files;
190:
191: }
192: }
193:
194: // create a webapp
195: WebAppContext wah = null;
196: if (_contexts instanceof ContextHandlerCollection
197: && WebAppContext.class
198: .isAssignableFrom(((ContextHandlerCollection) _contexts)
199: .getContextClass())) {
200: try {
201: wah = (WebAppContext) ((ContextHandlerCollection) _contexts)
202: .getContextClass().newInstance();
203: } catch (Exception e) {
204: throw new Error(e);
205: }
206: } else {
207: wah = new WebAppContext();
208: }
209:
210: // configure it
211: wah.setContextPath(context);
212: if (_configurationClasses != null)
213: wah.setConfigurationClasses(_configurationClasses);
214: if (_defaultsDescriptor != null)
215: wah.setDefaultsDescriptor(_defaultsDescriptor);
216: wah.setExtractWAR(_extract);
217: wah.setWar(app.toString());
218: wah.setParentLoaderPriority(_parentLoaderPriority);
219:
220: // add it
221: _contexts.addHandler(wah);
222: _deployed.add(wah);
223:
224: if (_contexts.isStarted())
225: _contexts.start(); // TODO Multi exception
226: }
227: }
228:
229: public void doStop() throws Exception {
230: for (int i = _deployed.size(); i-- > 0;) {
231: WebAppContext wac = (WebAppContext) _deployed.get(i);
232: wac.stop();// TODO Multi exception
233: }
234: }
235: }
|