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.host;
031:
032: import com.caucho.config.Config;
033: import com.caucho.log.Log;
034: import com.caucho.server.deploy.DeployContainer;
035: import com.caucho.server.deploy.DeployGenerator;
036: import com.caucho.vfs.Path;
037:
038: import java.util.ArrayList;
039: import java.util.HashMap;
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042: import java.util.regex.Matcher;
043: import java.util.regex.Pattern;
044:
045: /**
046: * The generator for the web-app deploy
047: */
048: public class HostRegexpDeployGenerator extends
049: DeployGenerator<HostController> {
050: private static final Logger log = Log
051: .open(HostSingleDeployGenerator.class);
052:
053: private HostContainer _container;
054:
055: private HostConfig _config;
056:
057: private ArrayList<HostConfig> _hostDefaults = new ArrayList<HostConfig>();
058:
059: private ArrayList<HostController> _entries = new ArrayList<HostController>();
060:
061: /**
062: * Creates the new host deploy.
063: */
064: public HostRegexpDeployGenerator(
065: DeployContainer<HostController> container) {
066: super (container);
067: }
068:
069: /**
070: * Creates the new host deploy.
071: */
072: public HostRegexpDeployGenerator(
073: DeployContainer<HostController> container,
074: HostContainer hostContainer, HostConfig config) {
075: super (container);
076:
077: setContainer(hostContainer);
078:
079: _config = config;
080: }
081:
082: /**
083: * Gets the application container.
084: */
085: public HostContainer getContainer() {
086: return _container;
087: }
088:
089: /**
090: * Sets the application container.
091: */
092: public void setContainer(HostContainer container) {
093: _container = container;
094: }
095:
096: /**
097: * Returns the current array of application entries.
098: */
099: public HostController generateController(String name) {
100: Pattern regexp = _config.getRegexp();
101: Matcher matcher = regexp.matcher(name);
102:
103: if (!matcher.find() || matcher.start() != 0) {
104: return null;
105: }
106:
107: Thread thread = Thread.currentThread();
108: ClassLoader oldLoader = thread.getContextClassLoader();
109:
110: try {
111: thread.setContextClassLoader(getParentClassLoader());
112:
113: int length = matcher.end() - matcher.start();
114:
115: String hostName = matcher.group();
116:
117: ArrayList<String> vars = new ArrayList<String>();
118:
119: HashMap<String, Object> varMap = new HashMap<String, Object>();
120:
121: for (int j = 0; j <= matcher.groupCount(); j++) {
122: vars.add(matcher.group(j));
123: varMap.put("host" + j, matcher.group(j));
124: }
125:
126: varMap.put("regexp", vars);
127:
128: if (_config.getHostName() != null) {
129: try {
130: hostName = Config.evalString(_config.getHostName(),
131: varMap);
132: } catch (Exception e) {
133: log.log(Level.WARNING, e.toString(), e);
134: }
135: }
136:
137: HostController controller = new HostController(name,
138: _config, _container, varMap);
139:
140: controller.setRegexpName(name);
141:
142: controller.setRegexp(regexp);
143: controller.setRootDirectoryPattern(_config
144: .getRootDirectory());
145:
146: // XXX: not dynamic-deploy in the sense that the mappings are known
147: //controller.setDynamicDeploy(true);
148: //controller.setRegexpValues(vars);
149: //controller.setHostConfig(_config);
150: // _controller.setJarPath(_archivePath);
151:
152: for (int i = 0; i < _hostDefaults.size(); i++)
153: controller.addConfigDefault(_hostDefaults.get(i));
154:
155: controller.init();
156:
157: Path rootDir = controller.getRootDirectory();
158:
159: if (rootDir == null || !rootDir.isDirectory()) {
160: // server/0522
161: controller.destroy();
162: return null;
163: }
164:
165: synchronized (_entries) {
166: for (int i = 0; i < _entries.size(); i++) {
167: HostController oldController = _entries.get(i);
168:
169: if (rootDir
170: .equals(oldController.getRootDirectory()))
171: return oldController;
172: }
173:
174: _entries.add(controller);
175: }
176:
177: // registers mbean
178: /*
179: try {
180: controller.deployHost();
181: } catch (Exception e) {
182: log.log(Level.WARNING, e.toString(), e);
183: }
184: */
185:
186: return controller;
187: } finally {
188: thread.setContextClassLoader(oldLoader);
189: }
190: }
191:
192: public String toString() {
193: return "HostRegexpDeployGenerator[" + _config + "]";
194: }
195: }
|