KickJava   Java API By Example, From Geeks To Geeks.

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


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.BuilderProgram;
33 import com.caucho.config.Config;
34 import com.caucho.config.ConfigELContext;
35 import com.caucho.config.ConfigException;
36 import com.caucho.config.types.PathBuilder;
37 import com.caucho.el.EL;
38 import com.caucho.jmx.Jmx;
39 import com.caucho.loader.Environment;
40 import com.caucho.loader.EnvironmentClassLoader;
41 import com.caucho.loader.EnvironmentListener;
42 import com.caucho.log.Log;
43 import com.caucho.util.L10N;
44 import com.caucho.vfs.Path;
45 import com.caucho.vfs.Vfs;
46
47 import javax.el.ELContext;
48 import javax.el.ELException;
49 import javax.el.ELResolver;
50 import javax.management.ObjectName JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.LinkedHashMap JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.logging.Level JavaDoc;
56 import java.util.logging.Logger JavaDoc;
57
58 /**
59  * A deploy controller for an environment.
60  */

61 abstract public class
62   EnvironmentDeployController<I extends EnvironmentDeployInstance,
63                                         C extends DeployConfig>
64   extends ExpandDeployController<I>
65   implements EnvironmentListener
66 {
67   private static final L10N L = new L10N(EnvironmentDeployController.class);
68   private static final Logger JavaDoc log
69     = Log.open(EnvironmentDeployController.class);
70
71   // The JMX identity
72
private LinkedHashMap JavaDoc<String JavaDoc,String JavaDoc> _jmxContext;
73
74   private Object JavaDoc _mbean;
75
76   private ObjectName JavaDoc _objectName;
77
78   // The default configurations
79
private ArrayList JavaDoc<C> _configDefaults = new ArrayList JavaDoc<C>();
80
81   // The primary configuration
82
private C _config;
83   private DeployConfig _prologue;
84
85   // The configuration variable resolver
86
private ELResolver _parentELResolver;
87
88   // The variable mapping
89
private HashMap JavaDoc<String JavaDoc,Object JavaDoc> _variableMap = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
90
91   // Config exception passed in from parent, e.g. .ear
92
private Throwable JavaDoc _configException;
93
94   public EnvironmentDeployController()
95   {
96     this("", (Path) null);
97   }
98
99   public EnvironmentDeployController(C config)
100   {
101     this(config.getId(), config.calculateRootDirectory());
102
103     setConfig(config);
104   }
105
106   public EnvironmentDeployController(String JavaDoc id, C config)
107   {
108     this(id, config.calculateRootDirectory());
109
110     setConfig(config);
111   }
112
113   public EnvironmentDeployController(String JavaDoc id, Path rootDirectory)
114   {
115     super(id, null, rootDirectory);
116
117     ELContext elContext = EL.getEnvironment(getParentClassLoader());
118
119     if (elContext != null)
120       _parentELResolver = elContext.getELResolver();
121
122     _jmxContext = Jmx.copyContextProperties(getParentClassLoader());
123   }
124
125   /**
126    * Sets the primary configuration.
127    */

128   public void setConfig(C config)
129   {
130     if (config == null)
131       return;
132
133     if (_config != null && ! _configDefaults.contains(_config))
134       addConfigDefault(_config);
135
136     addConfigMode(config);
137
138     _config = config;
139
140     if (_prologue == null)
141       setPrologue(config.getPrologue());
142
143     /* XXX: config is at the end
144     if (config != null) {
145       addConfigDefault(config);
146     }
147     */

148   }
149
150   /**
151    * Gets the primary configuration
152    */

153   public C getConfig()
154   {
155     return _config;
156   }
157
158   /**
159    * Gets the prologue configuration
160    */

161   public DeployConfig getPrologue()
162   {
163     return _prologue;
164   }
165
166   /**
167    * Sets the prologue configuration
168    */

169   public void setPrologue(DeployConfig prologue)
170   {
171     _prologue = prologue;
172   }
173
174   /**
175    * Returns the configure exception.
176    */

177   public Throwable JavaDoc getConfigException()
178   {
179     Throwable JavaDoc configException = super.getConfigException();
180
181     if (configException == null)
182       configException = _configException;
183
184     if (configException == null) {
185       DeployInstance deploy = getDeployInstance();
186
187       if (deploy != null)
188         configException = deploy.getConfigException();
189     }
190
191     return configException;
192   }
193
194   /**
195    * Adds a default config.
196    */

197   public void addConfigDefault(C config)
198   {
199     if (! _configDefaults.contains(config)) {
200       _configDefaults.add(config);
201
202       addConfigMode(config);
203     }
204   }
205
206   private void addConfigMode(C config)
207   {
208     if (config.getStartupMode() != null)
209       setStartupMode(config.getStartupMode());
210
211     if (config.getRedeployCheckInterval() != null)
212       setRedeployCheckInterval(config.getRedeployCheckInterval());
213
214     if (config.getRedeployMode() != null)
215       setRedeployMode(config.getRedeployMode());
216   }
217
218   /**
219    * Returns the path variable map.
220    */

221   public HashMap JavaDoc<String JavaDoc,Object JavaDoc> getVariableMap()
222   {
223     return _variableMap;
224   }
225
226   /**
227    * Returns the mbean.
228    */

229   public Object JavaDoc getMBean()
230   {
231     return _mbean;
232   }
233
234   /**
235    * Returns the object name.
236    */

237   public ObjectName JavaDoc getObjectName()
238   {
239     return _objectName;
240   }
241
242   /**
243    * Sets a parent config exception (e.g. from a .ear)
244    */

245   public void setConfigException(Throwable JavaDoc e)
246   {
247     _configException = e;
248   }
249
250   /**
251    * Initialize the controller.
252    */

253   protected void initEnd()
254   {
255     super.initEnd();
256
257     if (getDeployAdmin() != null)
258       getDeployAdmin().register();
259   }
260
261   /**
262    * Returns true if the entry matches.
263    */

264   public boolean isNameMatch(String JavaDoc url)
265   {
266     return url.equals(getId());
267   }
268
269   /**
270    * Merges with the old controller.
271    */

272   protected void mergeController(DeployController oldControllerV)
273   {
274     super.mergeController(oldControllerV);
275
276     EnvironmentDeployController<I,C> oldController;
277     oldController = (EnvironmentDeployController) oldControllerV;
278     // setId(oldController.getId());
279

280     _configDefaults.addAll(oldController._configDefaults);
281
282     if (getConfig() == null)
283       setConfig(oldController.getConfig());
284     else if (oldController.getConfig() != null) {
285       _configDefaults.add(getConfig());
286
287       setConfig(oldController.getConfig());
288     }
289
290     if (getPrologue() == null)
291       setPrologue(oldController.getPrologue());
292     else if (oldController.getPrologue() != null) {
293       _configDefaults.add(0, (C) getPrologue()); // XXX: must be first
294

295       setPrologue(oldController.getPrologue());
296     }
297
298     if (oldController.getArchivePath() != null)
299       setArchivePath(oldController.getArchivePath());
300
301     mergeStartupMode(oldController.getStartupMode());
302
303     mergeRedeployCheckInterval(oldController.getRedeployCheckInterval());
304
305     mergeRedeployMode(oldController.getRedeployMode());
306   }
307
308   /**
309    * Returns the application object.
310    */

311   public boolean destroy()
312   {
313     if (! super.destroy())
314       return false;
315
316     Environment.removeEnvironmentListener(this, getParentClassLoader());
317
318     Thread JavaDoc thread = Thread.currentThread();
319     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
320
321     try {
322       thread.setContextClassLoader(getParentClassLoader());
323       
324       if (getDeployAdmin() != null)
325     getDeployAdmin().unregister();
326     } finally {
327       thread.setContextClassLoader(oldLoader);
328     }
329
330     return true;
331   }
332
333   /**
334    * Configures the instance.
335    */

336   protected void configureInstance(I instance)
337     throws Throwable JavaDoc
338   {
339     Thread JavaDoc thread = Thread.currentThread();
340     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
341
342     try {
343       ClassLoader JavaDoc classLoader = instance.getClassLoader();
344
345       thread.setContextClassLoader(classLoader);
346
347       log.fine(instance + " initializing");
348
349       // set from external error, like .ear
350
instance.setConfigException(_configException);
351
352       HashMap JavaDoc<String JavaDoc,Object JavaDoc> varMap = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
353       varMap.putAll(_variableMap);
354
355       for (Map.Entry JavaDoc<String JavaDoc,Object JavaDoc> entry : varMap.entrySet()) {
356     Config.setCurrentVar(entry.getKey(), entry.getValue());
357       }
358
359       EL.setVariableMap(varMap, classLoader);
360       ConfigELContext elContext = new ConfigELContext(varMap);
361       
362       EL.setEnvironment(elContext, classLoader);
363       Config.setELContext(elContext);
364
365       configureInstanceVariables(instance);
366
367       extendJMXContext(_jmxContext);
368
369       Jmx.setContextProperties(_jmxContext, classLoader);
370
371       ArrayList JavaDoc<DeployConfig> initList = new ArrayList JavaDoc<DeployConfig>();
372
373       if (getPrologue() != null)
374         initList.add(getPrologue());
375
376       fillInitList(initList);
377
378       thread.setContextClassLoader(instance.getClassLoader());
379       Vfs.setPwd(getRootDirectory());
380
381       if (getArchivePath() != null)
382         Environment.addDependency(getArchivePath());
383
384       for (DeployConfig config : initList) {
385         BuilderProgram program = config.getBuilderProgram();
386
387         if (program != null)
388           program.configure(instance);
389       }
390
391       instance.init();
392     } finally {
393       thread.setContextClassLoader(oldLoader);
394     }
395   }
396
397   protected void extendJMXContext(Map JavaDoc<String JavaDoc,String JavaDoc> context)
398   {
399     // _jmxContext.put(getMBeanTypeName(), getMBeanId());
400
}
401
402   protected void fillInitList(ArrayList JavaDoc<DeployConfig> initList)
403   {
404     initList.addAll(_configDefaults);
405
406     if (_config != null && ! initList.contains(_config))
407       initList.add(_config);
408   }
409
410   protected void configureInstanceVariables(I instance)
411     throws Throwable JavaDoc
412   {
413     Path rootDirectory = getRootDirectory();
414
415     if (rootDirectory == null)
416       throw new NullPointerException JavaDoc("Null root directory");
417
418     if (! rootDirectory.isFile()) {
419     }
420     else if (rootDirectory.getPath().endsWith(".jar") ||
421              rootDirectory.getPath().endsWith(".war")) {
422       throw new ConfigException(L.l("root-directory `{0}' must specify a directory. It may not be a .jar or .war.",
423                                     rootDirectory.getPath()));
424     }
425     else
426       throw new ConfigException(L.l("root-directory `{0}' may not be a file. root-directory must specify a directory.",
427                                     rootDirectory.getPath()));
428     Vfs.setPwd(rootDirectory);
429
430     if (log.isLoggable(Level.FINE))
431       log.fine(instance + " root-directory=" + rootDirectory);
432
433     instance.setRootDirectory(rootDirectory);
434   }
435
436   public Path getArchivePath()
437   {
438     Path path = super.getArchivePath();
439
440     if (path != null)
441       return path;
442
443     if (_config != null) {
444       String JavaDoc pathString = _config.getArchivePath();
445
446       if (pathString != null) {
447         try {
448           path = PathBuilder.lookupPath(pathString);
449         } catch (ELException e) {
450           throw new RuntimeException JavaDoc(e);
451         }
452       }
453
454       setArchivePath(path);
455     }
456
457     return path;
458   }
459
460   /**
461    * Handles the case where the environment is starting (after init).
462    */

463   public void environmentStart(EnvironmentClassLoader loader)
464   {
465     try {
466       start();
467     } catch (Throwable JavaDoc e) {
468       log.log(Level.WARNING, e.toString(), e);
469     }
470   }
471
472   /**
473    * Handles the case where the environment is stopping
474    */

475   public void environmentStop(EnvironmentClassLoader loader)
476   {
477     stop();
478   }
479
480   /**
481    * Returns a printable view.
482    */

483   public String JavaDoc toString()
484   {
485     String JavaDoc name = getClass().getName();
486
487     name = name.substring(name.lastIndexOf('.') + 1);
488
489     return name + "" + System.identityHashCode(this) + "[" + getId() + "]";
490   }
491 }
492
Popular Tags