KickJava   Java API By Example, From Geeks To Geeks.

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


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.lifecycle.Lifecycle;
33 import com.caucho.loader.Environment;
34 import com.caucho.make.CachedDependency;
35 import com.caucho.util.L10N;
36 import com.caucho.vfs.Dependency;
37
38 import javax.annotation.PostConstruct;
39 import java.util.ArrayList JavaDoc;
40 import java.util.HashSet JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.LinkedHashSet JavaDoc;
43 import java.util.Set JavaDoc;
44 import java.util.TreeSet JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * A container of deploy objects.
49  */

50 public class DeployContainer<C extends DeployController>
51   extends CachedDependency
52   implements Dependency
53 {
54   private static final L10N L = new L10N(DeployContainer.class);
55   private static final Logger JavaDoc log
56     = Logger.getLogger(DeployContainer.class.getName());
57
58   private final DeployListGenerator<C> _deployListGenerator
59     = new DeployListGenerator<C>(this);
60
61   private final ArrayList JavaDoc<C> _controllerList = new ArrayList JavaDoc<C>();
62
63   private final Lifecycle _lifecycle = new Lifecycle();
64
65   /**
66    * Creates the deploy container.
67    */

68   public DeployContainer()
69   {
70     setCheckInterval(Environment.getDependencyCheckInterval());
71   }
72   
73   /**
74    * Adds a deploy generator.
75    */

76   public void add(DeployGenerator<C> generator)
77   {
78     Set JavaDoc<String JavaDoc> names = new TreeSet JavaDoc<String JavaDoc>();
79     generator.fillDeployedKeys(names);
80
81     _deployListGenerator.add(generator);
82
83     if (_lifecycle.isActive())
84       update(names);
85   }
86   
87   /**
88    * Removes a deploy.
89    */

90   public void remove(DeployGenerator<C> generator)
91   {
92     Set JavaDoc<String JavaDoc> names = new TreeSet JavaDoc<String JavaDoc>();
93     generator.fillDeployedKeys(names);
94
95     _deployListGenerator.remove(generator);
96
97     if (_lifecycle.isActive())
98       update(names);
99   }
100
101   /**
102    * Returns true if the deployment has modified.
103    */

104   public boolean isModifiedImpl()
105   {
106     return _deployListGenerator.isModified();
107   }
108
109   /**
110    * Forces updates.
111    */

112   public void update()
113   {
114     _deployListGenerator.update();
115   }
116
117   /**
118    * Initialize the container.
119    */

120   @PostConstruct
121   public void init()
122   {
123     if (! _lifecycle.toInit())
124       return;
125   }
126
127   /**
128    * Start the container.
129    */

130   public void start()
131   {
132     init();
133
134     if (! _lifecycle.toActive())
135       return;
136
137     _deployListGenerator.start();
138
139     HashSet JavaDoc<String JavaDoc> keys = new LinkedHashSet JavaDoc<String JavaDoc>();
140
141     _deployListGenerator.fillDeployedKeys(keys);
142
143     for (String JavaDoc key : keys) {
144       startImpl(key);
145     }
146
147     ArrayList JavaDoc<C> controllerList;
148
149     synchronized (_controllerList) {
150       controllerList = new ArrayList JavaDoc<C>(_controllerList);
151     }
152
153     for (int i = 0; i < controllerList.size(); i++) {
154       C controller = controllerList.get(i);
155
156       controller.startOnInit();
157     }
158   }
159
160   /**
161    * Returns the matching entry.
162    */

163   public C findController(String JavaDoc name)
164   {
165     C controller = findDeployedController(name);
166
167     if (controller != null)
168       return controller;
169
170     controller = generateController(name);
171
172     if (controller == null)
173       return null;
174     // server/10tm
175
else if (controller.isNameMatch(name)) {
176       return controller;
177     }
178     else {
179       return null;
180     }
181   }
182
183   /**
184    * Returns the deployed entries.
185    */

186   public ArrayList JavaDoc<C> getControllers()
187   {
188     ArrayList JavaDoc<C> list = new ArrayList JavaDoc<C>();
189
190     synchronized (_controllerList) {
191       list.addAll(_controllerList);
192     }
193
194     return list;
195   }
196
197   /**
198    * Updates all the names.
199    */

200   private void update(Set JavaDoc<String JavaDoc> names)
201   {
202     Iterator JavaDoc<String JavaDoc> iter = names.iterator();
203     while (iter.hasNext()) {
204       String JavaDoc name = iter.next();
205
206       update(name);
207     }
208   }
209
210   /**
211    * Callback from the DeployGenerator when the deployment changes.
212    * <code>update</code> is only called when a deployment is added
213    * or removed, e.g. with a new .war file.
214    *
215    * The entry handles its own internal changes, e.g. a modification to
216    * a web.xml file.
217    */

218   public C update(String JavaDoc name)
219   {
220     C newController = updateImpl(name);
221     
222     if (_lifecycle.isActive() && newController != null)
223       newController.startOnInit();
224
225     return newController;
226   }
227
228   /**
229    * Callback from the DeployGenerator when the deployment changes.
230    * <code>update</code> is only called when a deployment is added
231    * or removed, e.g. with a new .war file.
232    *
233    * The entry handles its own internal changes, e.g. a modification to
234    * a web.xml file.
235    */

236   C updateImpl(String JavaDoc name)
237   {
238     C oldController = null;
239
240     synchronized (_controllerList) {
241       oldController = findDeployedController(name);
242       
243       if (oldController != null)
244     _controllerList.remove(oldController);
245     }
246     
247     if (oldController != null)
248       oldController.destroy();
249
250     // destroy must be before generate because of JMX unregistration
251

252     C newController = generateController(name);
253
254     return newController;
255   }
256
257   /**
258    * Starts a particular controller.
259    *
260    * @param name the domain-specified name matching the controller, e.g. the
261    * hostname or the context-path.
262    */

263   private C startImpl(String JavaDoc name)
264   {
265     C oldController = null;
266
267     synchronized (_controllerList) {
268       oldController = findDeployedController(name);
269     }
270
271     if (oldController != null)
272       return oldController;
273       
274     return generateController(name);
275   }
276
277   /**
278    * Called to explicitly remove an entry from the cache.
279    */

280   public void remove(String JavaDoc name)
281   {
282     C oldController = null;
283
284     synchronized (_controllerList) {
285       oldController = findDeployedController(name);
286
287       if (oldController != null)
288     _controllerList.remove(oldController);
289     }
290
291     if (oldController != null)
292       oldController.destroy();
293   }
294
295   /**
296    * Generates the controller.
297    */

298   private C generateController(String JavaDoc name)
299   {
300     if (! _lifecycle.isActive())
301       return null;
302     
303     C newController = _deployListGenerator.generateController(name);
304
305     // server/1h00,13g4
306
// generated controller might match the name, e.g.
307
// when webapps deploy has an overriding explicit <web-app>
308
if (newController == null)
309       return null;
310
311     // the new entry might already be generated by another thread
312
synchronized (_controllerList) {
313       C controller = findDeployedController(newController.getId());
314
315       if (controller != null)
316     return controller;
317
318       // server/10tl
319
/*
320       // server/0523
321       // Controllers with the same root directory are merged
322       controller = mergeWithDeployedController(newController);
323
324       if (controller != null)
325     return controller;
326       */

327
328       _controllerList.add(newController);
329     }
330     
331     init(newController);
332
333     return newController;
334   }
335   
336
337   private void init(C controller)
338   {
339     Thread JavaDoc thread = Thread.currentThread();
340     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
341     
342     try {
343       thread.setContextClassLoader(controller.getParentClassLoader());
344
345       controller.init();
346     } finally {
347       thread.setContextClassLoader(oldLoader);
348     }
349   }
350
351   /**
352    * Returns an already deployed entry.
353    */

354   private C findDeployedController(String JavaDoc name)
355   {
356     synchronized (_controllerList) {
357       for (int i = 0; i < _controllerList.size(); i++) {
358     C controller = _controllerList.get(i);
359
360     if (controller.isNameMatch(name)) {
361       return controller;
362     }
363       }
364     }
365
366     return null;
367   }
368   
369   /**
370    * Closes the stop.
371    */

372   public void stop()
373   {
374     if (! _lifecycle.toStop())
375       return;
376
377     ArrayList JavaDoc<C> controllers;
378
379     synchronized (_controllerList) {
380       controllers = new ArrayList JavaDoc<C>(_controllerList);
381     }
382
383     for (int i = 0; i < controllers.size(); i++)
384       controllers.get(i).stop();
385   }
386   
387   /**
388    * Closes the deploys.
389    */

390   public void destroy()
391   {
392     stop();
393     
394     if (! _lifecycle.toDestroy())
395       return;
396     
397     _deployListGenerator.destroy();
398
399     ArrayList JavaDoc<C> controllerList;
400
401     synchronized (_controllerList) {
402       controllerList = new ArrayList JavaDoc<C>(_controllerList);
403       _controllerList.clear();
404     }
405
406     for (int i = 0; i < controllerList.size(); i++) {
407       C controller = controllerList.get(i);
408
409       controller.destroy();
410     }
411   }
412
413   public String JavaDoc toString()
414   {
415     return "DeployContainer$" + System.identityHashCode(this) + "[]";
416   }
417 }
418
Popular Tags