KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > deploy > DeployGenerator


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.deploy;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.lifecycle.Lifecycle;
34 import com.caucho.loader.Environment;
35 import com.caucho.loader.EnvironmentClassLoader;
36 import com.caucho.loader.EnvironmentListener;
37 import com.caucho.util.L10N;
38 import com.caucho.util.Log;
39 import com.caucho.vfs.Dependency;
40
41 import javax.annotation.PostConstruct;
42 import java.util.Set JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * The generator for the deploy
48  */

49 abstract public class DeployGenerator<E extends DeployController>
50   implements Dependency, EnvironmentListener {
51   private static final Logger JavaDoc log = Log.open(DeployGenerator.class);
52   private static final L10N L = new L10N(DeployGenerator.class);
53
54   // The owning deployment container
55
private DeployContainer<E> _container;
56
57   private ClassLoader JavaDoc _parentClassLoader;
58
59   private String JavaDoc _startupMode = DeployController.STARTUP_AUTOMATIC;
60   private String JavaDoc _redeployMode = DeployController.REDEPLOY_AUTOMATIC;
61
62   private Throwable JavaDoc _configException;
63
64   private final Lifecycle _lifecycle = new Lifecycle(getLog());
65
66   /**
67    * Creates the deploy.
68    */

69   public DeployGenerator(DeployContainer<E> container)
70   {
71     _parentClassLoader = Thread.currentThread().getContextClassLoader();
72     _container = container;
73
74     _lifecycle.setName(toString());
75     _lifecycle.setLevel(Level.FINER);
76   }
77
78   /**
79    * Returns the deploy container.
80    */

81   public DeployContainer<E> getDeployContainer()
82   {
83     return _container;
84   }
85
86   /**
87    * Returns the parent class loader.
88    */

89   public ClassLoader JavaDoc getParentClassLoader()
90   {
91     return _parentClassLoader;
92   }
93
94   /**
95    * Sets the startup mode.
96    */

97   public void setStartupMode(String JavaDoc mode)
98     throws ConfigException
99   {
100     _startupMode = DeployController.toStartupCode(mode);
101   }
102
103   /**
104    * Gets the startup mode.
105    */

106   public String JavaDoc getStartupMode()
107     throws ConfigException
108   {
109     return _startupMode;
110   }
111
112   /**
113    * Sets the redeploy mode.
114    */

115   public void setRedeployMode(String JavaDoc mode)
116     throws ConfigException
117   {
118     _redeployMode = DeployController.toRedeployCode(mode);
119   }
120
121   /**
122    * Gets the redeploy mode.
123    */

124   public String JavaDoc getRedeployMode()
125     throws ConfigException
126   {
127     return _redeployMode;
128   }
129
130   @PostConstruct
131   final public void init()
132     throws ConfigException
133   {
134     if (! _lifecycle.toInitializing())
135       return;
136
137     try {
138       initImpl();
139     }
140     catch (RuntimeException JavaDoc ex) {
141       _configException = ex;
142       throw ex;
143     }
144
145     _lifecycle.setName(toString());
146
147     _lifecycle.toInit();
148   }
149
150   /**
151    * Derived class implementation of init
152    */

153   protected void initImpl()
154   {
155   }
156
157   /**
158    * Returns true if the deployment has modified.
159    */

160   public boolean isModified()
161   {
162     return false;
163   }
164
165   public String JavaDoc getState()
166   {
167     return _lifecycle.getStateName();
168   }
169
170   /**
171    * Starts the deployment.
172    */

173   final public void start()
174   {
175     try {
176       init();
177     } catch (Throwable JavaDoc e) {
178       log.log(Level.WARNING, e.toString(), e);
179     }
180
181     if (!_lifecycle.toStarting())
182       return;
183
184     startImpl();
185
186     _lifecycle.toActive();
187   }
188
189   public boolean isActive()
190   {
191     return _lifecycle.isActive();
192   }
193
194   /**
195    * Derived class implentation of start.
196    */

197   protected void startImpl()
198   {
199     Environment.addEnvironmentListener(this);
200   }
201
202   /**
203    * lazy-start
204    */

205   public void request()
206   {
207   }
208
209   /**
210    * Forces an update.
211    */

212   public void update()
213   {
214   }
215
216   /**
217    * Returns the deployed keys.
218    */

219   protected void fillDeployedKeys(Set JavaDoc<String JavaDoc> keys)
220   {
221   }
222
223   /**
224    * Generates the controller.
225    */

226   protected E generateController(String JavaDoc key)
227   {
228     return null;
229   }
230
231   /**
232    * Merges the entry with other matching entries, returning the
233    * new entry.
234    */

235   protected E mergeController(E controller, String JavaDoc key)
236   {
237     return controller;
238   }
239
240   /**
241    * Returns the log.
242    */

243   protected Logger JavaDoc getLog()
244   {
245     return log;
246   }
247
248   /**
249    * Stops the deploy
250    */

251   final public void stop()
252   {
253     if (!_lifecycle.toStopping())
254       return;
255
256     stopImpl();
257
258     _lifecycle.toStop();
259   }
260
261   /**
262    * Derived class implentation of stop.
263    */

264   protected void stopImpl()
265   {
266   }
267
268   public Throwable JavaDoc getConfigException()
269   {
270     return _configException;
271   }
272
273   /**
274    * Closes the deploy
275    */

276   final public void destroy()
277   {
278     try {
279       stop();
280     } catch (Throwable JavaDoc e) {
281       log.log(Level.WARNING, e.toString(), e);
282     }
283
284     if (!_lifecycle.toDestroying())
285       return;
286
287     destroyImpl();
288
289     _lifecycle.toDestroy();
290   }
291
292   /**
293    * Derived class implentation of destroy.
294    */

295   protected void destroyImpl()
296   {
297     _container.remove(this);
298   }
299
300   /**
301    * Handles the case where the environment is starting (after init).
302    */

303   public void environmentStart(EnvironmentClassLoader loader)
304   {
305     start();
306   }
307
308   /**
309    * Handles the case where the environment is stopping
310    */

311   public void environmentStop(EnvironmentClassLoader loader)
312   {
313     destroy();
314   }
315
316   public String JavaDoc toString()
317   {
318     String JavaDoc name = getClass().getName();
319     int p = name.lastIndexOf('.');
320     if (p > 0)
321       name = name.substring(p + 1);
322
323     return name + "[]";
324   }
325
326 }
327
Popular Tags