KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > weblogic9 > WLDeploymentManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.weblogic9;
20
21
22 import java.io.*;
23 import java.util.*;
24
25 import javax.enterprise.deploy.model.*;
26 import javax.enterprise.deploy.shared.*;
27 import javax.enterprise.deploy.spi.*;
28 import javax.enterprise.deploy.spi.exceptions.*;
29 import javax.enterprise.deploy.spi.status.*;
30 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
31 import org.netbeans.modules.j2ee.weblogic9.config.EarDeploymentConfiguration;
32 import org.netbeans.modules.j2ee.weblogic9.config.EjbDeploymentConfiguration;
33 import org.netbeans.modules.j2ee.weblogic9.config.WarDeploymentConfiguration;
34
35 import org.netbeans.modules.j2ee.weblogic9.util.WLDebug;
36 import org.openide.ErrorManager;
37
38
39 /**
40  * Main class of the deployment process. This serves as a wrapper for the
41  * server's DeploymentManager implementation, all calls are delegated to the
42  * server's implementation, with the thread's context classloader updated
43  * if necessary.
44  *
45  * @author Kirill Sorokin
46  */

47 public class WLDeploymentManager implements DeploymentManager {
48     
49     private DeploymentManager dm;
50     private InstanceProperties instanceProperties;
51     private String JavaDoc uri;
52     private String JavaDoc username;
53     private String JavaDoc password;
54     private boolean isConnected;
55     private String JavaDoc host;
56     private String JavaDoc port;
57     
58     /** System process of the started WL server */
59     private Process JavaDoc process;
60     
61     /** Create connected DM */
62     public WLDeploymentManager(DeploymentManager dm, String JavaDoc uri, String JavaDoc username, String JavaDoc password, String JavaDoc host, String JavaDoc port) {
63         this.dm = dm;
64         this.uri = uri;
65         this.username = username;
66         this.password = password;
67         this.host = host;
68         this.port = port;
69         this.isConnected = true;
70     }
71     
72     /** Create disconnected DM */
73     public WLDeploymentManager(DeploymentManager dm, String JavaDoc uri, String JavaDoc host, String JavaDoc port) {
74         this.dm = dm;
75         this.uri = uri;
76         this.host = host;
77         this.port = port;
78         this.isConnected = false;
79     }
80     
81     ////////////////////////////////////////////////////////////////////////////
82
// Connection data methods
83
////////////////////////////////////////////////////////////////////////////
84

85     public boolean isConnected() {
86         return isConnected;
87     }
88     
89     /**
90      * Returns the stored server URI
91      */

92     public String JavaDoc getURI() {
93         return this.uri;
94     }
95     
96     /**
97      * Returns the server host stored in the instance properties
98      */

99     public String JavaDoc getHost() {
100         return host;
101     }
102     
103     public String JavaDoc getUsername () {
104         return getInstanceProperties().getProperty(InstanceProperties.USERNAME_ATTR);
105     }
106     
107     public String JavaDoc getPassword () {
108         return getInstanceProperties().getProperty(InstanceProperties.PASSWORD_ATTR);
109     }
110     
111     /**
112      * Returns the server port stored in the instance properties
113      */

114     public String JavaDoc getPort() {
115         return port;
116     }
117     
118     public boolean isLocal () {
119         return new Boolean JavaDoc(getInstanceProperties().getProperty(WLPluginProperties.IS_LOCAL_ATTR)).booleanValue();
120     }
121     /**
122      * Returns the InstanceProperties object for the current server instance
123      */

124     public InstanceProperties getInstanceProperties() {
125         if (instanceProperties == null) {
126             this.instanceProperties = InstanceProperties.getInstanceProperties(uri);
127             
128         }
129         return instanceProperties;
130     }
131     
132     /**
133      * Set the <code>Process</code> of the started WL server.
134      *
135      * @param <code>Process</code> of the started WL server.
136      */

137     public synchronized void setServerProcess(Process JavaDoc process) {
138         this.process = process;
139     }
140
141     /**
142      * Return <code>Process</code> of the started WL server.
143      *
144      * @return <code>Process</code> of the started WL server, <code>null</code> if
145      * WL wasn't started by IDE.
146      */

147     public synchronized Process JavaDoc getServerProcess() {
148         return process;
149     }
150     
151     public ProgressObject distribute(Target[] target, File file, File file2)
152             throws IllegalStateException JavaDoc {
153         if (WLDebug.isEnabled()) // debug output
154
WLDebug.notify(getClass(), "distribute(" + target + ", " + // NOI18N
155
file + ", " + file2 + ")"); // NOI18N
156

157         if (isLocal()) {
158             //autodeployment version
159
return new WLDeployer(uri).deploy(target, file, file2, getHost(), getPort());
160         } else {
161             //weblogic jsr88 version
162
modifiedLoader();
163             try {
164                 return new DelegatingProgressObject(dm.distribute(target, file, file2));
165             } finally {
166                 originalLoader();
167             }
168         }
169     }
170     
171     public ProgressObject distribute(Target[] target, ModuleType moduleType, InputStream inputStream, InputStream inputStream0) throws IllegalStateException JavaDoc {
172         return distribute(target, inputStream, inputStream0);
173     }
174     
175     private ClassLoader JavaDoc swapLoader;
176     
177     private void modifiedLoader() {
178         swapLoader = Thread.currentThread().getContextClassLoader();
179         String JavaDoc serverRoot = getInstanceProperties().getProperty(WLPluginProperties.SERVER_ROOT_ATTR);
180         // if serverRoot is null, then we are in a server instance registration process, thus this call
181
// is made from InstanceProperties creation -> WLPluginProperties singleton contains
182
// install location of the instance being registered
183
if (serverRoot == null)
184             serverRoot = WLPluginProperties.getInstance().getInstallLocation();
185
186         Thread.currentThread().setContextClassLoader(WLDeploymentFactory.getWLClassLoader(serverRoot));
187     }
188     private void originalLoader() {
189         Thread.currentThread().setContextClassLoader(swapLoader);
190     }
191     
192     /**
193      * Delegates the call to the server's deployment manager, checking whether
194      * the server is connected, updating the manager if neccessary and throwing
195      * the IllegalStateException is appropriate
196      *
197      * @return a wrapper for the server's DeploymentConfiguration implementation
198      */

199     public DeploymentConfiguration createConfiguration(
200         DeployableObject deployableObject) throws InvalidModuleException {
201         ModuleType type = deployableObject.getType();
202         if (type == ModuleType.WAR) {
203             return new WarDeploymentConfiguration(deployableObject);
204         } else if (type == ModuleType.EAR) {
205             return new EarDeploymentConfiguration(deployableObject);
206         } else if (type == ModuleType.EJB) {
207             return new EjbDeploymentConfiguration(deployableObject);
208         } else {
209             throw new InvalidModuleException("Unsupported module type: " + type.toString()); // NOI18N
210
}
211     }
212     
213     /**
214      * Delegates the call to the server's deployment manager, checking whether
215      * the server is connected, updating the manager if neccessary and throwing
216      * the IllegalStateException is appropriate
217      */

218     public ProgressObject redeploy(TargetModuleID[] targetModuleID,
219             InputStream inputStream, InputStream inputStream2)
220             throws UnsupportedOperationException JavaDoc, IllegalStateException JavaDoc {
221         if (WLDebug.isEnabled()) // debug output
222
WLDebug.notify("redeploy(" + targetModuleID + ", " + // NOI18N
223
inputStream + ", " + inputStream2 + ")"); // NOI18N
224
modifiedLoader();
225         try {
226             return new DelegatingProgressObject(dm.redeploy(targetModuleID, inputStream, inputStream2));
227         } finally {
228             originalLoader();
229         }
230     }
231
232     /**
233      * Delegates the call to the server's deployment manager, checking whether
234      * the server is connected, updating the manager if neccessary and throwing
235      * the IllegalStateException is appropriate
236      */

237     public ProgressObject distribute(Target[] target, InputStream inputStream,
238             InputStream inputStream2) throws IllegalStateException JavaDoc {
239         if (WLDebug.isEnabled()) // debug output
240
WLDebug.notify("distribute(" + target + ", " + // NOI18N
241
inputStream + ", " + inputStream2 + ")"); // NOI18N
242
modifiedLoader();
243         try {
244             return new DelegatingProgressObject(dm.distribute(target, inputStream, inputStream2));
245         } finally {
246             originalLoader();
247         }
248     }
249
250     /**
251      * Delegates the call to the server's deployment manager, checking whether
252      * the server is connected, updating the manager if neccessary and throwing
253      * the IllegalStateException is appropriate
254      */

255     public ProgressObject undeploy(TargetModuleID[] targetModuleID)
256             throws IllegalStateException JavaDoc {
257         if (WLDebug.isEnabled()) // debug output
258
WLDebug.notify("undeploy(" + targetModuleID + ")"); // NOI18N
259
modifiedLoader();
260         try {
261             return new DelegatingProgressObject(dm.undeploy(targetModuleID));
262         } finally {
263             originalLoader();
264         }
265     }
266
267     /**
268      * Delegates the call to the server's deployment manager, checking whether
269      * the server is connected, updating the manager if neccessary and throwing
270      * the IllegalStateException is appropriate
271      */

272     public ProgressObject stop(TargetModuleID[] targetModuleID)
273             throws IllegalStateException JavaDoc {
274         if (WLDebug.isEnabled()) // debug output
275
WLDebug.notify("stop(" + targetModuleID + ")"); // NOI18N
276

277         modifiedLoader();
278         try {
279             return new DelegatingProgressObject(dm.stop(targetModuleID));
280         } finally {
281             originalLoader();
282         }
283     }
284
285     /**
286      * Delegates the call to the server's deployment manager, checking whether
287      * the server is connected, updating the manager if neccessary and throwing
288      * the IllegalStateException is appropriate
289      */

290     public ProgressObject start(TargetModuleID[] targetModuleID)
291             throws IllegalStateException JavaDoc {
292         if (WLDebug.isEnabled()) // debug output
293
WLDebug.notify("start(" + targetModuleID + ")"); // NOI18N
294

295         modifiedLoader();
296         try {
297             return new DelegatingProgressObject(dm.start(targetModuleID));
298         } finally {
299             originalLoader();
300         }
301     }
302
303     /**
304      * Delegates the call to the server's deployment manager, checking whether
305      * the server is connected, updating the manager if neccessary and throwing
306      * the IllegalStateException is appropriate
307      */

308     public TargetModuleID[] getAvailableModules(ModuleType moduleType,
309             Target[] target) throws TargetException, IllegalStateException JavaDoc {
310         if (WLDebug.isEnabled()) // debug output
311
WLDebug.notify("getAvailableModules(" + moduleType + // NOI18N
312
", " + target + ")"); // NOI18N
313

314         modifiedLoader();
315         try {
316             TargetModuleID t[] = dm.getAvailableModules(moduleType, target);
317             return t;
318         } finally {
319             originalLoader();
320         }
321     }
322
323     /**
324      * Delegates the call to the server's deployment manager, checking whether
325      * the server is connected, updating the manager if neccessary and throwing
326      * the IllegalStateException is appropriate
327      */

328     public TargetModuleID[] getNonRunningModules(ModuleType moduleType,
329             Target[] target) throws TargetException, IllegalStateException JavaDoc {
330         if (WLDebug.isEnabled()) // debug output
331
WLDebug.notify("getNonRunningModules(" + moduleType + // NOI18N
332
", " + target + ")"); // NOI18N
333

334         modifiedLoader();
335         try {
336             TargetModuleID t[] = dm.getNonRunningModules(moduleType, target);
337             for (int i=0; i < t.length; i++) {
338                 System.out.println("non running module:" + t[i]);
339             }
340             return t;
341         } finally {
342             originalLoader();
343         }
344     }
345
346     /**
347      * Delegates the call to the server's deployment manager, checking whether
348      * the server is connected, updating the manager if neccessary and throwing
349      * the IllegalStateException is appropriate
350      */

351     public TargetModuleID[] getRunningModules(ModuleType moduleType,
352             Target[] target) throws TargetException, IllegalStateException JavaDoc {
353         if (WLDebug.isEnabled()) // debug output
354
WLDebug.notify("getRunningModules(" + moduleType + // NOI18N
355
", " + target + ")"); // NOI18N
356

357         modifiedLoader();
358         try {
359             TargetModuleID t[] = dm.getRunningModules(moduleType, target);
360             for (int i=0; i < t.length; i++) {
361                 System.out.println("running module:" + t[i]);
362             }
363             return t;
364         } finally {
365             originalLoader();
366         }
367     }
368
369     /**
370      * Delegates the call to the server's deployment manager, checking whether
371      * the server is connected, updating the manager if neccessary and throwing
372      * the IllegalStateException is appropriate
373      */

374     public ProgressObject redeploy(TargetModuleID[] targetModuleID, File file,
375             File file2) throws UnsupportedOperationException JavaDoc,
376             IllegalStateException JavaDoc {
377         if (WLDebug.isEnabled()) // debug output
378
WLDebug.notify("redeploy(" + targetModuleID + ", " + // NOI18N
379
file + ", " + file2 + ")"); // NOI18N
380

381         modifiedLoader();
382         try {
383             return new DelegatingProgressObject(dm.redeploy(targetModuleID, file, file2));
384         } finally {
385             originalLoader();
386         }
387     }
388     
389     /**
390      * Delegates the call to the server's deployment manager, checking whether
391      * the server is connected, updating the manager if neccessary and throwing
392      * the IllegalStateException is appropriate
393      */

394     public void setLocale(Locale locale) throws UnsupportedOperationException JavaDoc {
395         if (WLDebug.isEnabled()) // debug output
396
WLDebug.notify("setLocale(" + locale + ")"); // NOI18N
397

398         modifiedLoader();
399         try {
400             dm.setLocale(locale);
401         } finally {
402             originalLoader();
403         }
404     }
405
406     /**
407      * Delegates the call to the server's deployment manager, checking whether
408      * the server is connected, updating the manager if neccessary and throwing
409      * the IllegalStateException is appropriate
410      */

411     public boolean isLocaleSupported(Locale locale) {
412         if (WLDebug.isEnabled()) // debug output
413
WLDebug.notify("isLocaleSupported(" + locale + ")"); // NOI18N
414

415         modifiedLoader();
416         try {
417             return dm.isLocaleSupported(locale);
418         } finally {
419             originalLoader();
420         }
421     }
422
423     /**
424      * Delegates the call to the server's deployment manager, checking whether
425      * the server is connected, updating the manager if neccessary and throwing
426      * the IllegalStateException is appropriate
427      */

428     public void setDConfigBeanVersion(
429             DConfigBeanVersionType dConfigBeanVersionType)
430             throws DConfigBeanVersionUnsupportedException {
431         if (WLDebug.isEnabled()) // debug output
432
WLDebug.notify("setDConfigBeanVersion(" + // NOI18N
433
dConfigBeanVersionType + ")"); // NOI18N
434

435         modifiedLoader();
436         try {
437             dm.setDConfigBeanVersion(dConfigBeanVersionType);
438         } finally {
439             originalLoader();
440         }
441     }
442
443     /**
444      * Delegates the call to the server's deployment manager, checking whether
445      * the server is connected, updating the manager if neccessary and throwing
446      * the IllegalStateException is appropriate
447      */

448     public boolean isDConfigBeanVersionSupported(
449             DConfigBeanVersionType dConfigBeanVersionType) {
450         if (WLDebug.isEnabled()) // debug output
451
WLDebug.notify("isDConfigBeanVersionSupported(" + // NOI18N
452
dConfigBeanVersionType + ")"); // NOI18N
453

454         modifiedLoader();
455         try {
456             return dm.isDConfigBeanVersionSupported(dConfigBeanVersionType);
457         } finally {
458             originalLoader();
459         }
460     }
461
462     /**
463      * Delegates the call to the server's deployment manager, checking whether
464      * the server is connected, updating the manager if neccessary and throwing
465      * the IllegalStateException is appropriate
466      */

467     public void release() {
468         if (WLDebug.isEnabled()) // debug output
469
WLDebug.notify("release()"); // NOI18N
470

471         modifiedLoader();
472         try {
473             if (dm != null) {
474                 // delegate the call and clear the stored deployment manager
475
try {
476                     dm.release();
477                 }
478                 catch (Exception JavaDoc e) {
479                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
480                 }
481                 finally {
482                     dm = null;
483                 }
484             }
485         } finally {
486             originalLoader();
487         }
488     }
489
490     /**
491      * Delegates the call to the server's deployment manager, checking whether
492      * the server is connected, updating the manager if neccessary and throwing
493      * the IllegalStateException is appropriate
494      */

495     public boolean isRedeploySupported() {
496         if (WLDebug.isEnabled()) // debug output
497
WLDebug.notify("isRedeploySupported()"); // NOI18N
498

499         modifiedLoader();
500         try {
501             return dm.isRedeploySupported();
502         } finally {
503             originalLoader();
504         }
505     }
506
507     /**
508      * Delegates the call to the server's deployment manager, checking whether
509      * the server is connected, updating the manager if neccessary and throwing
510      * the IllegalStateException is appropriate
511      */

512     public Locale getCurrentLocale() {
513         if (WLDebug.isEnabled()) // debug output
514
WLDebug.notify("getCurrentLocale()"); // NOI18N
515

516         modifiedLoader();
517         try {
518             return dm.getCurrentLocale();
519         } finally {
520             originalLoader();
521         }
522     }
523
524     /**
525      * Delegates the call to the server's deployment manager, checking whether
526      * the server is connected, updating the manager if neccessary and throwing
527      * the IllegalStateException is appropriate
528      */

529     public DConfigBeanVersionType getDConfigBeanVersion() {
530         if (WLDebug.isEnabled()) // debug output
531
WLDebug.notify("getDConfigBeanVersion()"); // NOI18N
532

533         modifiedLoader();
534         try {
535             return dm.getDConfigBeanVersion();
536         } finally {
537             originalLoader();
538         }
539     }
540
541     /**
542      * Delegates the call to the server's deployment manager, checking whether
543      * the server is connected, updating the manager if neccessary and throwing
544      * the IllegalStateException is appropriate
545      */

546     public Locale getDefaultLocale() {
547         if (WLDebug.isEnabled()) // debug output
548
WLDebug.notify("getDefaultLocale()"); // NOI18N
549

550         modifiedLoader();
551         try {
552             return dm.getDefaultLocale();
553         } finally {
554             originalLoader();
555         }
556     }
557
558     /**
559      * Delegates the call to the server's deployment manager, checking whether
560      * the server is connected, updating the manager if neccessary and throwing
561      * the IllegalStateException is appropriate
562      */

563     public Locale[] getSupportedLocales() {
564         if (WLDebug.isEnabled()) // debug output
565
WLDebug.notify("getSupportedLocales()"); // NOI18N
566

567         modifiedLoader();
568         try {
569             return dm.getSupportedLocales();
570         } finally {
571             originalLoader();
572         }
573     }
574
575     /**
576      * Delegates the call to the server's deployment manager, checking whether
577      * the server is connected, updating the manager if neccessary and throwing
578      * the IllegalStateException is appropriate
579      */

580     public Target[] getTargets() throws IllegalStateException JavaDoc {
581         if (WLDebug.isEnabled()) // debug output
582
WLDebug.notify("getTargets()"); // NOI18N
583

584         modifiedLoader();
585         try {
586             return dm.getTargets();
587         } finally {
588             originalLoader();
589         }
590     }
591     
592     static class DelegatingProgressObject implements ProgressObject, ProgressListener {
593         ProgressObject original;
594         private Vector listeners = new Vector();
595         DelegatingProgressObject (ProgressObject original) {
596             this.original = original;
597             original.addProgressListener(this);
598         }
599         
600         public DeploymentStatus getDeploymentStatus() {
601             return original.getDeploymentStatus();
602         }
603
604         public TargetModuleID[] getResultTargetModuleIDs() {
605             return original.getResultTargetModuleIDs();
606         }
607
608         public ClientConfiguration getClientConfiguration(TargetModuleID targetModuleID) {
609             return getClientConfiguration(targetModuleID);
610         }
611
612         public boolean isCancelSupported() {
613             return original.isCancelSupported();
614         }
615
616         public void cancel() throws OperationUnsupportedException {
617             original.cancel();
618         }
619
620         public boolean isStopSupported() {
621             return original.isStopSupported();
622         }
623
624         public void stop() throws OperationUnsupportedException {
625             original.stop();
626         }
627
628         public void addProgressListener(ProgressListener progressListener) {
629             listeners.add(progressListener);
630         }
631
632         public void removeProgressListener(ProgressListener progressListener) {
633             listeners.remove(progressListener);
634         }
635
636         public void handleProgressEvent(ProgressEvent progressEvent) {
637             java.util.Vector JavaDoc targets = null;
638             synchronized (this) {
639                 if (listeners != null) {
640                     targets = (java.util.Vector JavaDoc) listeners.clone();
641                 }
642             }
643
644             if (targets != null) {
645                 for (int i = 0; i < targets.size(); i++) {
646                     ProgressListener target = (ProgressListener)targets.elementAt(i);
647                     target.handleProgressEvent(progressEvent);
648                 }
649         }
650         }
651         
652     }
653 }
Popular Tags