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.deploy;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.lifecycle.Lifecycle;
034: import com.caucho.loader.Environment;
035: import com.caucho.loader.EnvironmentClassLoader;
036: import com.caucho.loader.EnvironmentListener;
037: import com.caucho.util.L10N;
038: import com.caucho.vfs.Dependency;
039:
040: import javax.annotation.PostConstruct;
041: import java.util.Set;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: /**
046: * The generator for the deploy
047: */
048: abstract public class DeployGenerator<E extends DeployController>
049: implements Dependency, EnvironmentListener {
050: private static final Logger log = Logger
051: .getLogger(DeployGenerator.class.getName());
052: private static final L10N L = new L10N(DeployGenerator.class);
053:
054: // The owning deployment container
055: private DeployContainer<E> _container;
056:
057: private ClassLoader _parentClassLoader;
058:
059: private String _startupMode = DeployController.STARTUP_AUTOMATIC;
060: private String _redeployMode = DeployController.REDEPLOY_AUTOMATIC;
061:
062: private Throwable _configException;
063:
064: private final Lifecycle _lifecycle = new Lifecycle(getLog());
065:
066: /**
067: * Creates the deploy.
068: */
069: public DeployGenerator(DeployContainer<E> container) {
070: _parentClassLoader = Thread.currentThread()
071: .getContextClassLoader();
072: _container = container;
073:
074: _lifecycle.setName(toString());
075: _lifecycle.setLevel(Level.FINEST);
076: }
077:
078: /**
079: * Returns the deploy container.
080: */
081: public DeployContainer<E> getDeployContainer() {
082: return _container;
083: }
084:
085: /**
086: * Returns the parent class loader.
087: */
088: public ClassLoader getParentClassLoader() {
089: return _parentClassLoader;
090: }
091:
092: /**
093: * Sets the startup mode.
094: */
095: public void setStartupMode(String mode) throws ConfigException {
096: _startupMode = DeployController.toStartupCode(mode);
097: }
098:
099: /**
100: * Gets the startup mode.
101: */
102: public String getStartupMode() throws ConfigException {
103: return _startupMode;
104: }
105:
106: /**
107: * Sets the redeploy mode.
108: */
109: public void setRedeployMode(String mode) throws ConfigException {
110: _redeployMode = DeployController.toRedeployCode(mode);
111: }
112:
113: /**
114: * Gets the redeploy mode.
115: */
116: public String getRedeployMode() throws ConfigException {
117: return _redeployMode;
118: }
119:
120: @PostConstruct
121: final public void init() throws ConfigException {
122: try {
123: initImpl();
124: } catch (RuntimeException ex) {
125: _configException = ex;
126: throw ex;
127: }
128:
129: _lifecycle.setName(toString());
130: }
131:
132: /**
133: * Derived class implementation of init
134: */
135: protected void initImpl() {
136: }
137:
138: /**
139: * Returns true if the deployment has modified.
140: */
141: public boolean isModified() {
142: return false;
143: }
144:
145: /**
146: * Returns true if the deployment has modified.
147: */
148: public boolean logModified(Logger log) {
149: return false;
150: }
151:
152: public String getState() {
153: return _lifecycle.getStateName();
154: }
155:
156: /**
157: * Starts the deployment.
158: */
159: final public void start() {
160: try {
161: init();
162: } catch (Exception e) {
163: log.log(Level.WARNING, e.toString(), e);
164: }
165:
166: if (!_lifecycle.toActive())
167: return;
168:
169: startImpl();
170: }
171:
172: public boolean isActive() {
173: return _lifecycle.isActive();
174: }
175:
176: public boolean isDestroyed() {
177: return _lifecycle.isDestroyed();
178: }
179:
180: /**
181: * Derived class implentation of start.
182: */
183: protected void startImpl() {
184: Environment.addEnvironmentListener(this );
185: }
186:
187: /**
188: * lazy-start
189: */
190: public void request() {
191: }
192:
193: /**
194: * Forces an update.
195: */
196: public void update() {
197: }
198:
199: /**
200: * Returns the deployed keys.
201: */
202: protected void fillDeployedKeys(Set<String> keys) {
203: }
204:
205: /**
206: * Generates the controller.
207: */
208: protected E generateController(String key) {
209: return null;
210: }
211:
212: /**
213: * Merges the entry with other matching entries, returning the
214: * new entry.
215: */
216: protected E mergeController(E controller, String key) {
217: return controller;
218: }
219:
220: /**
221: * Returns the log.
222: */
223: protected Logger getLog() {
224: return log;
225: }
226:
227: /**
228: * Stops the deploy
229: */
230: final public void stop() {
231: if (!_lifecycle.toStop())
232: return;
233:
234: stopImpl();
235: }
236:
237: /**
238: * Derived class implentation of stop.
239: */
240: protected void stopImpl() {
241: }
242:
243: public Throwable getConfigException() {
244: return _configException;
245: }
246:
247: /**
248: * Closes the deploy
249: */
250: final public void destroy() {
251: try {
252: stop();
253: } catch (Throwable e) {
254: log.log(Level.WARNING, e.toString(), e);
255: }
256:
257: if (!_lifecycle.toDestroy())
258: return;
259:
260: destroyImpl();
261: }
262:
263: /**
264: * Derived class implentation of destroy.
265: */
266: protected void destroyImpl() {
267: _container.remove(this );
268: }
269:
270: /**
271: * Handles the case where the environment is starting (after init).
272: */
273: public void environmentConfig(EnvironmentClassLoader loader) {
274: }
275:
276: /**
277: * Handles the case where the environment is starting (after init).
278: */
279: public void environmentStart(EnvironmentClassLoader loader) {
280: start();
281: }
282:
283: /**
284: * Handles the case where the environment is stopping
285: */
286: public void environmentStop(EnvironmentClassLoader loader) {
287: destroy();
288: }
289:
290: public String toString() {
291: String name = getClass().getName();
292: int p = name.lastIndexOf('.');
293: if (p > 0)
294: name = name.substring(p + 1);
295:
296: return name + "[]";
297: }
298:
299: }
|