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.types.PathBuilder;
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.DeployGenerator;
038: import com.caucho.vfs.Path;
039:
040: import java.util.ArrayList;
041: import java.util.Set;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: /**
046: * The generator for the web-app deploy
047: */
048: public class WebAppSingleDeployGenerator extends
049: DeployGenerator<WebAppController> implements
050: EnvironmentListener {
051: private static final Logger log = Log
052: .open(WebAppSingleDeployGenerator.class);
053:
054: private WebAppContainer _container;
055:
056: private WebAppController _parentWebApp;
057:
058: private String _urlPrefix = "";
059:
060: private Path _archivePath;
061: private Path _rootDirectory;
062:
063: private ArrayList<WebAppConfig> _defaultList = new ArrayList<WebAppConfig>();
064: private WebAppConfig _config;
065:
066: private ClassLoader _parentLoader;
067:
068: private WebAppController _controller;
069:
070: /**
071: * Creates the new host deploy.
072: */
073: public WebAppSingleDeployGenerator(
074: DeployContainer<WebAppController> deployContainer) {
075: super (deployContainer);
076: }
077:
078: /**
079: * Creates the new web-app deploy.
080: */
081: public WebAppSingleDeployGenerator(
082: DeployContainer<WebAppController> deployContainer,
083: WebAppContainer container, WebAppConfig config)
084: throws Exception {
085: super (deployContainer);
086:
087: setContainer(container);
088:
089: String contextPath = config.getContextPath();
090:
091: if (contextPath.equals("/"))
092: contextPath = "";
093:
094: setURLPrefix(config.getContextPath());
095:
096: _config = config;
097: }
098:
099: /**
100: * Gets the webApp container.
101: */
102: public WebAppContainer getContainer() {
103: return _container;
104: }
105:
106: /**
107: * Sets the webApp container.
108: */
109: public void setContainer(WebAppContainer container) {
110: _container = container;
111:
112: if (_parentLoader == null)
113: _parentLoader = container.getClassLoader();
114: }
115:
116: /**
117: * Sets the parent webApp.
118: */
119: public void setParentWebApp(WebAppController parent) {
120: _parentWebApp = parent;
121: }
122:
123: /**
124: * Sets the parent loader.
125: */
126: public void setParentClassLoader(ClassLoader loader) {
127: _parentLoader = loader;
128: }
129:
130: /**
131: * Sets the url prefix.
132: */
133: public void setURLPrefix(String prefix) {
134: if (!prefix.startsWith("/"))
135: prefix = "/" + prefix;
136:
137: while (prefix.endsWith("/")) {
138: prefix = prefix.substring(0, prefix.length() - 1);
139: }
140:
141: _urlPrefix = prefix;
142: }
143:
144: /**
145: * Gets the url prefix.
146: */
147: public String getURLPrefix() {
148: return _urlPrefix;
149: }
150:
151: /**
152: * Sets the root directory.
153: */
154: public void setRootDirectory(Path rootDirectory) {
155: _rootDirectory = rootDirectory;
156: }
157:
158: /**
159: * Adds a default.
160: */
161: public void addWebAppDefault(WebAppConfig config) {
162: _defaultList.add(config);
163: }
164:
165: /**
166: * Returns the log.
167: */
168: protected Logger getLog() {
169: return log;
170: }
171:
172: /**
173: * Initializes the controller.
174: */
175: @Override
176: protected void initImpl() {
177: super .initImpl();
178:
179: if (_controller != null)
180: return;
181:
182: String appDir = _config.getDocumentDirectory();
183:
184: if (appDir == null)
185: appDir = "./" + _urlPrefix;
186:
187: if (_rootDirectory == null) {
188: _rootDirectory = PathBuilder.lookupPath(appDir, null,
189: _container.getDocumentDirectory());
190: }
191:
192: String archivePath = _config.getArchivePath();
193:
194: if (archivePath != null) {
195: _archivePath = PathBuilder.lookupPath(archivePath, null,
196: _container.getRootDirectory());
197: }
198:
199: _controller = new WebAppController(_urlPrefix, _urlPrefix,
200: _rootDirectory, _container);
201:
202: _controller.setArchivePath(_archivePath);
203:
204: if (_archivePath != null)
205: _controller.addDepend(_archivePath);
206:
207: _controller.setParentWebApp(_parentWebApp);
208:
209: for (WebAppConfig config : _defaultList)
210: _controller.addConfigDefault(config);
211:
212: // _controller.setConfig(_config);
213: // server/1h13
214: _controller.addConfigDefault(_config);
215:
216: _controller.setPrologue(_config.getPrologue());
217:
218: _controller.setStartupPriority(_config.getStartupPriority());
219:
220: _controller.setSourceType("single");
221:
222: Environment.addEnvironmentListener(this , _parentLoader);
223: }
224:
225: /**
226: * Returns the deployed keys.
227: */
228: protected void fillDeployedKeys(Set<String> keys) {
229: keys.add(_controller.getContextPath());
230: }
231:
232: /**
233: * Returns the current array of webApp entries.
234: */
235: public WebAppController generateController(String name) {
236: if (name.equals(_controller.getContextPath())) {
237: WebAppController webApp;
238:
239: webApp = new WebAppController(_urlPrefix, _urlPrefix,
240: _rootDirectory, _container);
241:
242: webApp.setArchivePath(_controller.getArchivePath());
243:
244: return webApp;
245: } else
246: return null;
247: }
248:
249: /**
250: * Merges the controllers.
251: */
252: public WebAppController mergeController(
253: WebAppController controller, String name) {
254: // if directory matches, merge the two controllers. The
255: // last controller has priority.
256: if (controller.getRootDirectory().equals(
257: _controller.getRootDirectory())) {
258: // server/1h10
259: controller.setContextPath(_controller.getContextPath());
260:
261: controller.setDynamicDeploy(false);
262:
263: return controller.merge(_controller);
264: }
265: // else if the names don't match, return the new controller
266: else if (!_controller.isNameMatch(name))
267: return controller;
268: // otherwise, the single deploy overrides
269: else
270: return _controller;
271: }
272:
273: /**
274: * Initialize the deployment.
275: */
276: public void deploy() {
277: try {
278: init();
279: } catch (Exception e) {
280: log.log(Level.WARNING, e.toString(), e);
281: }
282: }
283:
284: public Throwable getConfigException() {
285: Throwable configException = super .getConfigException();
286:
287: if (configException == null && _controller != null)
288: configException = _controller.getConfigException();
289:
290: return configException;
291: }
292:
293: /**
294: * Destroy the deployment.
295: */
296: @Override
297: protected void destroyImpl() {
298: Environment.removeEnvironmentListener(this , _parentLoader);
299:
300: _container.removeWebAppDeploy(this );
301:
302: super .destroyImpl();
303: }
304:
305: public String toString() {
306: return "WebAppSingleDeployGenerator[" + _urlPrefix + "]";
307: }
308: }
|