KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > j2ee > deployclient > DeploymentManagerImpl


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.j2ee.deployclient;
30
31 import com.caucho.hessian.client.HessianProxyFactory;
32 import com.caucho.util.L10N;
33
34 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
35 import javax.enterprise.deploy.shared.DConfigBeanVersionType JavaDoc;
36 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
37 import javax.enterprise.deploy.spi.DeploymentConfiguration JavaDoc;
38 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
39 import javax.enterprise.deploy.spi.Target JavaDoc;
40 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
41 import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException JavaDoc;
42 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException JavaDoc;
43 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException JavaDoc;
44 import javax.enterprise.deploy.spi.exceptions.TargetException JavaDoc;
45 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
46 import java.io.File JavaDoc;
47 import java.io.FileInputStream JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.util.Locale JavaDoc;
50 import java.util.logging.Level JavaDoc;
51 import java.util.logging.Logger JavaDoc;
52
53 /**
54  * Manager for the deployments.
55  */

56 public class DeploymentManagerImpl implements DeploymentManager JavaDoc {
57   private static final L10N L = new L10N(DeploymentManagerImpl.class);
58   private static final Logger JavaDoc log = Logger.getLogger(DeploymentManagerImpl.class.getName());
59
60   private String JavaDoc _uri;
61
62   private DeploymentProxyAPI _proxy;
63
64   DeploymentManagerImpl(String JavaDoc uri)
65   {
66     int p = uri.indexOf("http");
67     if (p < 0)
68       throw new IllegalArgumentException JavaDoc(uri);
69
70     _uri = uri.substring(p);
71   }
72
73   /**
74    * Connect to the manager.
75    */

76   void connect(String JavaDoc user, String JavaDoc password)
77     throws DeploymentManagerCreationException JavaDoc
78   {
79     try {
80       HessianProxyFactory factory = new HessianProxyFactory();
81
82       factory.setUser(user);
83       factory.setPassword(password);
84       factory.setReadTimeout(120000);
85
86       _proxy =
87         (DeploymentProxyAPI) factory.create(DeploymentProxyAPI.class, _uri);
88     } catch (Exception JavaDoc e) {
89       log.log(Level.FINE, e.toString(), e);
90
91       DeploymentManagerCreationException JavaDoc exn;
92
93       exn = new DeploymentManagerCreationException JavaDoc(e.getMessage());
94       exn.initCause(e);
95       throw exn;
96     }
97   }
98
99   /**
100    * Returns the targets supported by the manager.
101    */

102   public Target JavaDoc []getTargets()
103     throws IllegalStateException JavaDoc
104   {
105     if (_proxy == null)
106       throw new IllegalStateException JavaDoc("DeploymentManager is disconnected");
107
108     Thread JavaDoc thread = Thread.currentThread();
109     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
110     try {
111       thread.setContextClassLoader(getClass().getClassLoader());
112
113       Target JavaDoc []targets = _proxy.getTargets();
114
115       if (targets == null)
116         return new Target JavaDoc[0];
117
118       return targets;
119     } catch (Throwable JavaDoc e) {
120       log.log(Level.INFO, e.toString(), e);
121
122       return new Target JavaDoc[0];
123     } finally {
124       thread.setContextClassLoader(oldLoader);
125     }
126   }
127
128   /**
129    * Returns the current running modules.
130    */

131   public TargetModuleID JavaDoc []getRunningModules(ModuleType JavaDoc moduleType,
132                                             Target JavaDoc []targetList)
133     throws TargetException JavaDoc, IllegalStateException JavaDoc
134   {
135     return new TargetModuleID JavaDoc[0];
136   }
137
138   /**
139    * Returns the current non-running modules.
140    */

141   public TargetModuleID JavaDoc []getNonRunningModules(ModuleType JavaDoc moduleType,
142                                                Target JavaDoc []targetList)
143     throws TargetException JavaDoc, IllegalStateException JavaDoc
144   {
145     return new TargetModuleID JavaDoc[0];
146   }
147
148   /**
149    * Returns all available modules.
150    */

151   public TargetModuleID JavaDoc []getAvailableModules(ModuleType JavaDoc moduleType,
152                                               Target JavaDoc []targetList)
153     throws TargetException JavaDoc, IllegalStateException JavaDoc
154   {
155     if (_proxy == null)
156       throw new IllegalStateException JavaDoc("DeploymentManager is disconnected");
157
158     Thread JavaDoc thread = Thread.currentThread();
159     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
160     try {
161       thread.setContextClassLoader(getClass().getClassLoader());
162
163       return _proxy.getAvailableModules(moduleType.toString());
164     } finally {
165       thread.setContextClassLoader(oldLoader);
166     }
167   }
168
169   /**
170    * Returns a configuration for the deployable object.
171    */

172   public DeploymentConfiguration JavaDoc createConfiguration(DeployableObject JavaDoc dObj)
173     throws InvalidModuleException JavaDoc
174   {
175     throw new UnsupportedOperationException JavaDoc();
176   }
177
178   /**
179    * Deploys the object.
180    */

181   public ProgressObject JavaDoc distribute(Target JavaDoc []targetList,
182                                    File JavaDoc archive,
183                                    File JavaDoc deploymentPlan)
184     throws IllegalStateException JavaDoc
185   {
186     InputStream JavaDoc archiveIn = null;
187     InputStream JavaDoc ddIn = null;
188
189     try {
190       archiveIn = new FileInputStream JavaDoc(archive);
191       ddIn = new FileInputStream JavaDoc(deploymentPlan);
192
193       return distribute(targetList, archiveIn, ddIn);
194
195     } catch (Exception JavaDoc e) {
196       throw new RuntimeException JavaDoc(e);
197     } finally {
198       try {
199         if (archiveIn != null)
200           archiveIn.close();
201       } catch (Throwable JavaDoc e) {
202         log.log(Level.FINE, e.toString(), e);
203       }
204
205       try {
206         if (ddIn != null)
207           ddIn.close();
208       } catch (Throwable JavaDoc e) {
209         log.log(Level.FINE, e.toString(), e);
210       }
211     }
212   }
213
214   /**
215    * Deploys the object.
216    */

217   public ProgressObject JavaDoc distribute(Target JavaDoc []targetList,
218                                    InputStream JavaDoc archive,
219                                    InputStream JavaDoc deploymentPlan)
220     throws IllegalStateException JavaDoc
221   {
222     if (_proxy == null) {
223       String JavaDoc message = L.l("DeploymentManager is disconnected");
224
225       log.log(Level.FINE, message);
226
227       ProgressObjectImpl progress = new ProgressObjectImpl(new TargetModuleID JavaDoc[] {});
228       progress.failed(message);
229
230       return progress;
231     }
232
233     if (deploymentPlan == null) {
234       String JavaDoc message = L.l("{0} is required", "deployment plan");
235
236       log.log(Level.FINE, message);
237
238       ProgressObjectImpl progress = new ProgressObjectImpl(new TargetModuleID JavaDoc[] {});
239       progress.failed(message);
240
241       return progress;
242     }
243
244     Thread JavaDoc thread = Thread.currentThread();
245     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
246     try {
247       thread.setContextClassLoader(getClass().getClassLoader());
248
249
250       return _proxy.distribute(targetList, deploymentPlan, archive);
251     } finally {
252       thread.setContextClassLoader(oldLoader);
253     }
254   }
255
256   /**
257    * Starts the modules.
258    */

259   public ProgressObject JavaDoc start(TargetModuleID JavaDoc []moduleIDList)
260     throws IllegalStateException JavaDoc
261   {
262     if (_proxy == null)
263       throw new IllegalStateException JavaDoc("DeploymentManager is disconnected");
264
265     Thread JavaDoc thread = Thread.currentThread();
266     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
267     try {
268       thread.setContextClassLoader(getClass().getClassLoader());
269
270       return _proxy.start(moduleIDList);
271     } finally {
272       thread.setContextClassLoader(oldLoader);
273     }
274   }
275
276   /**
277    * Stops the modules.
278    */

279   public ProgressObject JavaDoc stop(TargetModuleID JavaDoc []moduleIDList)
280     throws IllegalStateException JavaDoc
281   {
282     if (_proxy == null)
283       throw new IllegalStateException JavaDoc("DeploymentManager is disconnected");
284
285     Thread JavaDoc thread = Thread.currentThread();
286     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
287     try {
288       thread.setContextClassLoader(getClass().getClassLoader());
289
290       return _proxy.stop(moduleIDList);
291     } finally {
292       thread.setContextClassLoader(oldLoader);
293     }
294   }
295
296   /**
297    * Undeploys the modules.
298    */

299   public ProgressObject JavaDoc undeploy(TargetModuleID JavaDoc []moduleIDList)
300     throws IllegalStateException JavaDoc
301   {
302     if (_proxy == null)
303       throw new IllegalStateException JavaDoc("DeploymentManager is disconnected");
304
305     Thread JavaDoc thread = Thread.currentThread();
306     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
307     try {
308       thread.setContextClassLoader(getClass().getClassLoader());
309
310       return _proxy.undeploy(moduleIDList);
311     } finally {
312       thread.setContextClassLoader(oldLoader);
313     }
314   }
315
316   /**
317    * Returns true if the redeploy is supported.
318    */

319   public boolean isRedeploySupported()
320   {
321     return false;
322   }
323
324   /**
325    * Redeploys the object.
326    */

327   public ProgressObject JavaDoc redeploy(TargetModuleID JavaDoc []targetList,
328                                  File JavaDoc archive,
329                                  File JavaDoc deploymentPlan)
330     throws IllegalStateException JavaDoc
331   {
332     throw new UnsupportedOperationException JavaDoc();
333   }
334
335   /**
336    * Redeploys the object.
337    */

338   public ProgressObject JavaDoc redeploy(TargetModuleID JavaDoc []targetList,
339                                  InputStream JavaDoc archive,
340                                  InputStream JavaDoc deploymentPlan)
341     throws IllegalStateException JavaDoc
342   {
343     throw new UnsupportedOperationException JavaDoc();
344   }
345
346   /**
347    * Frees any resources.
348    */

349   public void release()
350   {
351   }
352
353   /**
354    * Returns the default locale.
355    */

356   public Locale JavaDoc getDefaultLocale()
357   {
358     throw new UnsupportedOperationException JavaDoc();
359   }
360
361   /**
362    * Returns the current locale.
363    */

364   public Locale JavaDoc getCurrentLocale()
365   {
366     throw new UnsupportedOperationException JavaDoc();
367   }
368
369   /**
370    * Sets the default locale.
371    */

372   public void setLocale(Locale JavaDoc locale)
373   {
374     throw new UnsupportedOperationException JavaDoc();
375   }
376
377   /**
378    * Returns the supported locales.
379    */

380   public Locale JavaDoc []getSupportedLocales()
381   {
382     throw new UnsupportedOperationException JavaDoc();
383   }
384
385   /**
386    * Returns true if the locale is supported.
387    */

388   public boolean isLocaleSupported(Locale JavaDoc locale)
389   {
390     return false;
391   }
392
393   /**
394    * Returns the bean's J2EE version.
395    */

396   public DConfigBeanVersionType JavaDoc getDConfigBeanVersion()
397   {
398     return DConfigBeanVersionType.V1_4;
399   }
400
401   /**
402    * Returns true if the given version is supported.
403    */

404   public boolean isDConfigBeanVersionSupported(DConfigBeanVersionType JavaDoc version)
405   {
406     return true;
407   }
408
409   /**
410    * Sets true if the given version is supported.
411    */

412   public void setDConfigBeanVersionSupported(DConfigBeanVersionType JavaDoc version)
413     throws DConfigBeanVersionUnsupportedException JavaDoc
414   {
415   }
416
417   /**
418    * Return the debug view of the manager.
419    */

420   public String JavaDoc toString()
421   {
422     return "DeploymentManagerImpl[" + _uri + "]";
423   }
424 }
425
426
Popular Tags