001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.webapp;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.loader.Environment;
034: import com.caucho.loader.EnvironmentListener;
035: import com.caucho.log.Log;
036: import com.caucho.server.deploy.DeployContainer;
037: import com.caucho.server.deploy.ExpandDeployGenerator;
038: import com.caucho.vfs.CaseInsensitive;
039: import com.caucho.vfs.Path;
040:
041: import java.util.ArrayList;
042: import java.util.HashMap;
043: import java.util.Set;
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046:
047: /**
048: * The generator for the web-app deploy
049: */
050: public class WebAppExpandDeployGenerator extends
051: ExpandDeployGenerator<WebAppController> implements
052: EnvironmentListener {
053: private static final Logger log = Logger
054: .getLogger(WebAppExpandDeployGenerator.class.getName());
055:
056: private final WebAppExpandDeployGeneratorAdmin _admin;
057:
058: private WebAppContainer _container;
059:
060: private WebAppController _parent;
061:
062: private String _urlPrefix = "";
063:
064: private ArrayList<WebAppConfig> _webAppDefaults = new ArrayList<WebAppConfig>();
065:
066: private HashMap<Path, WebAppConfig> _webAppConfigMap = new HashMap<Path, WebAppConfig>();
067:
068: // Maps from the context-path to the webapps directory
069: private HashMap<String, Path> _contextPathMap = new HashMap<String, Path>();
070:
071: private ClassLoader _parentLoader;
072:
073: /**
074: * Creates the new expand deploy.
075: */
076: public WebAppExpandDeployGenerator(
077: DeployContainer<WebAppController> container,
078: WebAppContainer webAppContainer) {
079: super (container, webAppContainer.getRootDirectory());
080:
081: _container = webAppContainer;
082:
083: _parentLoader = webAppContainer.getClassLoader();
084:
085: try {
086: setExtension(".war");
087: } catch (Exception e) {
088: log.log(Level.WARNING, e.toString(), e);
089: }
090:
091: _admin = new WebAppExpandDeployGeneratorAdmin(this );
092: }
093:
094: /**
095: * Gets the webApp container.
096: */
097: public WebAppContainer getContainer() {
098: return _container;
099: }
100:
101: /**
102: * Sets the parent webApp.
103: */
104: public void setParent(WebAppController parent) {
105: _parent = parent;
106: }
107:
108: /**
109: * Sets the parent loader.
110: */
111: public void setParentClassLoader(ClassLoader loader) {
112: _parentLoader = loader;
113: }
114:
115: /**
116: * Sets the url prefix.
117: */
118: public void setURLPrefix(String prefix) {
119: if (prefix.equals("")) {
120: }
121:
122: while (prefix.endsWith("/"))
123: prefix = prefix.substring(0, prefix.length() - 1);
124:
125: _urlPrefix = prefix;
126: }
127:
128: /**
129: * Gets the url prefix.
130: */
131: public String getURLPrefix() {
132: return _urlPrefix;
133: }
134:
135: /**
136: * Sets true for a lazy-init.
137: */
138: public void setLazyInit(boolean lazyInit) throws ConfigException {
139: log
140: .config("lazy-init is deprecated. Use <startup>lazy</startup> instead.");
141: if (lazyInit)
142: setStartupMode("lazy");
143: else
144: setStartupMode("automatic");
145: }
146:
147: /**
148: * Adds an overriding web-app
149: */
150: public void addWebApp(WebAppConfig config) {
151: String docDir = config.getDocumentDirectory();
152:
153: Path appDir = getExpandDirectory().lookup(docDir);
154:
155: _webAppConfigMap.put(appDir, config);
156:
157: if (config.getContextPath() != null)
158: _contextPathMap.put(config.getContextPath(), appDir);
159: }
160:
161: /**
162: * Adds a default.
163: */
164: public void addWebAppDefault(WebAppConfig config) {
165: _webAppDefaults.add(config);
166: }
167:
168: @Override
169: protected void initImpl() {
170: super .initImpl();
171: }
172:
173: /**
174: * Returns the log.
175: */
176: @Override
177: protected Logger getLog() {
178: return log;
179: }
180:
181: /**
182: * Returns the deployed keys.
183: */
184: @Override
185: protected void fillDeployedKeys(Set<String> keys) {
186: super .fillDeployedKeys(keys);
187:
188: for (WebAppConfig cfg : _webAppConfigMap.values()) {
189: if (cfg.getContextPath() != null)
190: keys.add(cfg.getContextPath());
191: }
192: }
193:
194: /**
195: * Start the deploy.
196: */
197: @Override
198: protected void startImpl() {
199: super .startImpl();
200:
201: Environment.addEnvironmentListener(this , _parentLoader);
202:
203: _admin.register();
204: }
205:
206: /**
207: * Returns the new controller.
208: */
209: @Override
210: protected WebAppController createController(String name) {
211: if (!name.startsWith(_urlPrefix))
212: return null;
213:
214: String segmentName = name.substring(_urlPrefix.length());
215:
216: Path webAppRoot = _contextPathMap.get(segmentName);
217:
218: if (webAppRoot != null)
219: segmentName = "/" + webAppRoot.getTail();
220: else if (segmentName.indexOf('/', 1) > 0)
221: return null;
222:
223: String contextPath = segmentName;
224:
225: if (segmentName.equals("")) {
226: if (CaseInsensitive.isCaseInsensitive())
227: segmentName = "/root";
228: else
229: segmentName = "/ROOT";
230: }
231:
232: // server/26cg
233: if (contextPath.equals("/ROOT"))
234: contextPath = "";
235: else if (contextPath.equalsIgnoreCase("/root")
236: && CaseInsensitive.isCaseInsensitive())
237: contextPath = "";
238:
239: ArrayList<String> versionNames = getVersionNames(segmentName);
240:
241: if (versionNames == null || versionNames.size() == 0) {
242: return makeController(name, _urlPrefix + contextPath,
243: _urlPrefix + segmentName);
244: }
245:
246: /*
247: else if (versionNames.size() == 1) {
248: String versionName = versionNames.get(0);
249:
250: if (versionName.equals(segmentName))
251: return makeController(name, segmentName);
252: else
253: return _container.getWebAppGenerator().findController(versionName);
254: }
255: */
256:
257: WebAppController controller = new WebAppVersioningController(
258: name, this , _container);
259:
260: return controller;
261: }
262:
263: private WebAppController makeController(String name,
264: String contextPath, String versionName) {
265: String version = "";
266: String baseName = contextPath;
267:
268: if (isVersioning()) {
269: int p = versionName.lastIndexOf('-');
270:
271: if (p > 0) {
272: version = versionName.substring(p + 1);
273: baseName = versionName.substring(0, p);
274: }
275: }
276:
277: int p = versionName.lastIndexOf('/');
278:
279: String segmentName = versionName.substring(p + 1);
280:
281: String expandName = getExpandName(segmentName);
282:
283: String archiveName = segmentName + ".war";
284: Path jarPath = getArchiveDirectory().lookup("./" + archiveName);
285:
286: Path rootDirectory;
287:
288: if (jarPath.isDirectory()) {
289: //rootDirectory = getExpandDirectory().lookup("./" + archiveName);
290: // server/10kw
291: rootDirectory = jarPath;
292: jarPath = null;
293: } else {
294: // server/003j
295: rootDirectory = getExpandDirectory().lookup(
296: "./" + expandName);
297: }
298:
299: if (!rootDirectory.isDirectory()
300: && (jarPath == null || !jarPath.isFile()))
301: return null;
302: else if (rootDirectory.isDirectory()
303: && !isValidDirectory(rootDirectory, versionName
304: .substring(1)))
305: return null;
306:
307: WebAppConfig cfg = _webAppConfigMap.get(rootDirectory);
308:
309: if (cfg != null && cfg.getContextPath() != null) {
310: baseName = contextPath = cfg.getContextPath();
311: }
312:
313: WebAppController controller = new WebAppController(versionName,
314: contextPath, rootDirectory, _container);
315:
316: controller.setWarName(versionName.substring(1));
317:
318: controller.setParentWebApp(_parent);
319:
320: controller.setDynamicDeploy(true);
321: controller.setSourceType("expand");
322:
323: controller.setVersion(version);
324:
325: if (!baseName.equals(contextPath)) {
326: WebAppController versionController = _container
327: .getWebAppGenerator().findController(baseName);
328:
329: if (versionController instanceof WebAppVersioningController) {
330: ((WebAppVersioningController) versionController)
331: .setModified(true);
332: }
333: }
334:
335: return controller;
336: }
337:
338: /**
339: * Returns the current array of webApp entries.
340: */
341: @Override
342: protected WebAppController mergeController(
343: WebAppController controller, String key) {
344: try {
345: Path expandDirectory = getExpandDirectory();
346: Path rootDirectory = controller.getRootDirectory();
347:
348: if (!expandDirectory.equals(rootDirectory.getParent()))
349: return controller;
350:
351: controller = super .mergeController(controller, key);
352:
353: if (controller.getArchivePath() == null) {
354: String archiveName = rootDirectory.getTail() + ".war";
355:
356: Path jarPath = getArchiveDirectory()
357: .lookup(archiveName);
358:
359: if (!jarPath.isDirectory()) {
360: controller.setArchivePath(jarPath);
361: controller.addDepend(jarPath);
362: }
363: }
364:
365: controller.setStartupMode(getStartupMode());
366: // controller.setRedeployMode(getRedeployMode());
367:
368: for (int i = 0; i < _webAppDefaults.size(); i++)
369: controller.addConfigDefault(_webAppDefaults.get(i));
370:
371: WebAppConfig cfg = _webAppConfigMap.get(rootDirectory);
372:
373: if (cfg != null)
374: controller.addConfigDefault(cfg);
375: } catch (ConfigException e) {
376: log.warning(e.toString());
377: log.log(Level.FINEST, e.toString(), e);
378: controller.setConfigException(e);
379: } catch (Throwable e) {
380: log.log(Level.WARNING, e.toString(), e);
381: controller.setConfigException(e);
382: }
383:
384: return controller;
385: }
386:
387: /**
388: * Converts the name.
389: */
390: @Override
391: protected String pathNameToEntryName(String name) {
392: String entryName = super .pathNameToEntryName(name);
393:
394: if (entryName == null)
395: return null;
396:
397: if (CaseInsensitive.isCaseInsensitive()) {
398: try {
399: String[] list = getExpandDirectory().list();
400:
401: String matchName = null;
402:
403: for (int i = 0; i < list.length; i++) {
404: if (list[i].equalsIgnoreCase(entryName))
405: matchName = list[i];
406: }
407:
408: if (matchName == null)
409: matchName = entryName.toLowerCase();
410: } catch (Exception e) {
411: entryName = entryName.toLowerCase();
412: }
413: }
414:
415: if (entryName.equalsIgnoreCase("root"))
416: return _urlPrefix;
417: else
418: return _urlPrefix + "/" + entryName;
419: }
420:
421: @Override
422: protected String entryNameToArchiveName(String entryName) {
423: String prefix = _urlPrefix + "/";
424:
425: if (entryName.equals(_urlPrefix))
426: return "ROOT" + getExtension();
427: else if (entryName.startsWith(prefix))
428: return entryName.substring(prefix.length())
429: + getExtension();
430: else
431: return null;
432: }
433:
434: /**
435: * Destroy the deployment.
436: */
437: @Override
438: protected void destroyImpl() {
439: _admin.unregister();
440:
441: _container.removeWebAppDeploy(this);
442:
443: Environment.removeEnvironmentListener(this, _parentLoader);
444:
445: super.destroyImpl();
446: }
447: }
|