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.config.ConfigELContext;
034: import com.caucho.config.ConfigException;
035: import com.caucho.config.types.RawString;
036: import com.caucho.el.EL;
037: import com.caucho.el.MapVariableResolver;
038: import com.caucho.log.Log;
039: import com.caucho.server.deploy.DeployContainer;
040: import com.caucho.server.deploy.ExpandDeployGenerator;
041: import com.caucho.vfs.Path;
042:
043: import javax.el.ELContext;
044: import javax.el.ELResolver;
045: import java.util.ArrayList;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048:
049: /**
050: * The generator for the host deploy
051: */
052: public class HostExpandDeployGenerator extends
053: ExpandDeployGenerator<HostController> {
054: private static final Logger log = Log
055: .open(HostExpandDeployGenerator.class);
056:
057: private final HostExpandDeployGeneratorAdmin _admin = new HostExpandDeployGeneratorAdmin(
058: this );
059:
060: private HostContainer _container;
061:
062: private ArrayList<HostConfig> _hostDefaults = new ArrayList<HostConfig>();
063:
064: private String _hostName;
065:
066: /**
067: * Creates the new host deploy.
068: */
069: public HostExpandDeployGenerator(
070: DeployContainer<HostController> container,
071: HostContainer hostContainer) {
072: super (container, hostContainer.getRootDirectory());
073:
074: _container = hostContainer;
075: }
076:
077: /**
078: * Gets the host container.
079: */
080: public HostContainer getContainer() {
081: return _container;
082: }
083:
084: /**
085: * Sets the host name.
086: */
087: public void setHostName(RawString name) {
088: _hostName = name.getValue();
089: }
090:
091: /**
092: * Gets the host name.
093: */
094: public String getHostName() {
095: return _hostName;
096: }
097:
098: /**
099: * Sets true for a lazy-init.
100: */
101: public void setLazyInit(boolean lazyInit) throws ConfigException {
102: log
103: .config("lazy-init is deprecated. Use <startup>lazy</startup> instead.");
104: if (lazyInit)
105: setStartupMode("lazy");
106: else
107: setStartupMode("automatic");
108: }
109:
110: /**
111: * Adds a default.
112: */
113: public void addHostDefault(HostConfig config) {
114: _hostDefaults.add(config);
115: }
116:
117: @Override
118: protected void initImpl() throws ConfigException {
119: super .initImpl();
120: }
121:
122: @Override
123: protected void startImpl() throws ConfigException {
124: super .startImpl();
125:
126: _admin.register();
127: }
128:
129: /**
130: * Returns the log.
131: */
132: protected Logger getLog() {
133: return log;
134: }
135:
136: /**
137: * Returns the current array of application entries.
138: */
139: public HostController createController(String name) {
140: // server/13g3
141: if (name.equals(""))
142: return null;
143:
144: /*
145: if (! isDeployedKey(name))
146: return null;
147: */
148:
149: Path rootDirectory = getExpandDirectory().lookup("./" + name);
150:
151: HostController controller = new HostController(name,
152: rootDirectory, _container);
153:
154: Path jarPath = getArchiveDirectory().lookup(
155: "./" + name + ".jar");
156: controller.setArchivePath(jarPath);
157:
158: if (rootDirectory.isDirectory()
159: && !isValidDirectory(rootDirectory, name))
160: return null;
161: else if (!rootDirectory.isDirectory() && !jarPath.isFile())
162: return null;
163:
164: try {
165: String hostName = getHostName();
166:
167: if (hostName != null) {
168: ELContext parentEnv = Config.getEnvironment();
169: ELResolver resolver = new MapVariableResolver(
170: controller.getVariableMap());
171:
172: ELContext env = new ConfigELContext(resolver);
173:
174: controller.setHostName(EL.evalString(hostName, env));
175: } else
176: controller.setHostName(name);
177:
178: controller.addDepend(jarPath);
179: } catch (Throwable e) {
180: log.log(Level.WARNING, e.toString(), e);
181:
182: controller.setConfigException(e);
183: }
184:
185: return controller;
186: }
187:
188: /**
189: * Adds configuration to the current controller
190: */
191: protected HostController mergeController(HostController controller,
192: String key) {
193: try {
194: controller.setStartupMode(getStartupMode());
195:
196: for (int i = 0; i < _hostDefaults.size(); i++)
197: controller.addConfigDefault(_hostDefaults.get(i));
198: } catch (ConfigException e) {
199: log.warning(e.toString());
200: log.log(Level.FINER, e.toString(), e);
201:
202: controller.setConfigException(e);
203: } catch (Throwable e) {
204: log.log(Level.WARNING, e.toString(), e);
205:
206: controller.setConfigException(e);
207: }
208:
209: return controller;
210: }
211:
212: @Override
213: protected void destroyImpl() {
214: _admin.unregister();
215:
216: super .destroyImpl();
217: }
218:
219: public boolean equals(Object o) {
220: if (o == null || !getClass().equals(o.getClass()))
221: return false;
222:
223: HostExpandDeployGenerator deploy = (HostExpandDeployGenerator) o;
224:
225: Path expandPath = getExpandDirectory();
226: Path deployExpandPath = deploy.getExpandDirectory();
227: if (expandPath != deployExpandPath
228: && (expandPath == null || !expandPath
229: .equals(deployExpandPath)))
230: return false;
231:
232: return true;
233: }
234:
235: public String toString() {
236: return "HostExpandDeployGenerator[" + getExpandDirectory()
237: + "]";
238: }
239: }
|