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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.webapp;
030:
031: import com.caucho.config.types.PathBuilder;
032: import com.caucho.log.Log;
033: import com.caucho.server.deploy.DeployContainer;
034: import com.caucho.server.deploy.DeployGenerator;
035: import com.caucho.vfs.Path;
036:
037: import java.util.ArrayList;
038: import java.util.HashMap;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041: import java.util.regex.Matcher;
042: import java.util.regex.Pattern;
043:
044: /**
045: * The generator for the web-app deploy
046: */
047: public class WebAppRegexpDeployGenerator extends
048: DeployGenerator<WebAppController> {
049: private static final Logger log = Log
050: .open(WebAppSingleDeployGenerator.class);
051:
052: private WebAppContainer _container;
053:
054: private WebAppController _parent;
055:
056: private WebAppConfig _config;
057:
058: private ArrayList<WebAppConfig> _webAppDefaults = new ArrayList<WebAppConfig>();
059:
060: private ArrayList<WebAppController> _entries = new ArrayList<WebAppController>();
061:
062: /**
063: * Creates the new host deploy.
064: */
065: public WebAppRegexpDeployGenerator(
066: DeployContainer<WebAppController> deployContainer) {
067: super (deployContainer);
068: }
069:
070: /**
071: * Creates the new host deploy.
072: */
073: public WebAppRegexpDeployGenerator(
074: DeployContainer<WebAppController> deployContainer,
075: WebAppContainer container, WebAppConfig config) {
076: super (deployContainer);
077:
078: setContainer(container);
079:
080: _config = config;
081: }
082:
083: /**
084: * Gets the webApp container.
085: */
086: public WebAppContainer getContainer() {
087: return _container;
088: }
089:
090: /**
091: * Sets the webApp container.
092: */
093: public void setContainer(WebAppContainer container) {
094: _container = container;
095: }
096:
097: /**
098: * Sets the parent webApp.
099: */
100: public void setParent(WebAppController parent) {
101: _parent = parent;
102: }
103:
104: /**
105: * Returns the current array of webApp entries.
106: */
107: public WebAppController generateController(String name) {
108: Pattern regexp = _config.getURLRegexp();
109: Matcher matcher = regexp.matcher(name);
110:
111: if (!matcher.find() || matcher.start() != 0) {
112: return null;
113: }
114:
115: int length = matcher.end() - matcher.start();
116:
117: String contextPath = matcher.group();
118:
119: ArrayList<String> vars = new ArrayList<String>();
120:
121: //WebAppDeployControlleryController entry = new WebAppDeployControlleryController(this, contextPath);
122: HashMap<String, Object> varMap = new HashMap<String, Object>();
123: // entry.getVariableMap();
124:
125: for (int j = 0; j <= matcher.groupCount(); j++) {
126: vars.add(matcher.group(j));
127: varMap.put("app" + j, matcher.group(j));
128: }
129:
130: varMap.put("regexp", vars);
131:
132: Path appDir = null;
133:
134: try {
135: String appDirPath = _config.getDocumentDirectory();
136:
137: if (appDirPath == null)
138: appDirPath = "./" + matcher.group(0);
139:
140: appDir = PathBuilder.lookupPath(appDirPath, varMap);
141:
142: if (!appDir.isDirectory() || !appDir.canRead()) {
143: return null;
144: }
145: } catch (Exception e) {
146: log.log(Level.FINER, e.toString(), e);
147:
148: return null;
149: }
150:
151: WebAppController controller = null;
152:
153: Thread thread = Thread.currentThread();
154: ClassLoader oldLoader = thread.getContextClassLoader();
155:
156: try {
157: thread.setContextClassLoader(getParentClassLoader());
158:
159: synchronized (_entries) {
160: for (int i = 0; i < _entries.size(); i++) {
161: controller = _entries.get(i);
162:
163: if (appDir.equals(controller.getRootDirectory()))
164: return controller;
165: }
166:
167: controller = new WebAppController(name, name, appDir,
168: _container);
169:
170: // XXX: not dynamic-deploy in the sense that the mappings are known
171: //controller.setDynamicDeploy(true);
172: controller.getVariableMap().putAll(varMap);
173: controller.setRegexpValues(vars);
174: controller.setConfig(_config);
175: // _controller.setJarPath(_archivePath);
176:
177: for (int i = 0; i < _webAppDefaults.size(); i++)
178: controller.addConfigDefault(_webAppDefaults.get(i));
179:
180: _entries.add(controller);
181: }
182: } finally {
183: thread.setContextClassLoader(oldLoader);
184: }
185:
186: controller.setSourceType("regexp");
187:
188: //controller.deploy();
189:
190: return controller;
191: }
192:
193: public String toString() {
194: if (_config == null)
195: return "WebAppRegexpDeployGenerator[]";
196: else
197: return "WebAppRegexpDeployGenerator["
198: + _config.getURLRegexp().pattern() + "]";
199: }
200: }
|