KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > oc4j > OC4JDeploymentManager


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
20 package org.netbeans.modules.j2ee.oc4j;
21
22 import java.io.File JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Vector JavaDoc;
31 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
32 import javax.enterprise.deploy.shared.ActionType JavaDoc;
33 import javax.enterprise.deploy.shared.CommandType JavaDoc;
34 import javax.enterprise.deploy.shared.DConfigBeanVersionType JavaDoc;
35 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
36 import javax.enterprise.deploy.shared.StateType 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.OperationUnsupportedException JavaDoc;
45 import javax.enterprise.deploy.spi.exceptions.TargetException JavaDoc;
46 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
47 import javax.enterprise.deploy.spi.status.ClientConfiguration JavaDoc;
48 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
49 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
50 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
51 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
52 import javax.management.MBeanServerConnection JavaDoc;
53 import javax.management.remote.JMXConnector JavaDoc;
54 import javax.management.remote.JMXConnectorFactory JavaDoc;
55 import javax.management.remote.JMXServiceURL JavaDoc;
56 import org.netbeans.api.java.classpath.ClassPath;
57 import org.netbeans.modules.j2ee.dd.api.application.Application;
58 import org.netbeans.modules.j2ee.dd.api.application.DDProvider;
59 import org.netbeans.modules.j2ee.dd.api.application.Module;
60 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
61 import org.netbeans.modules.j2ee.oc4j.config.EarDeploymentConfiguration;
62 import org.netbeans.modules.j2ee.oc4j.config.EjbDeploymentConfiguration;
63 import org.netbeans.modules.j2ee.oc4j.config.WarDeploymentConfiguration;
64 import org.netbeans.modules.j2ee.oc4j.config.gen.OrionWebApp;
65 import org.netbeans.modules.j2ee.oc4j.ide.OC4JDeploymentStatus;
66 import org.netbeans.modules.j2ee.oc4j.ide.OC4JErrorManager;
67 import org.netbeans.modules.j2ee.oc4j.ide.OC4JJ2eePlatformImpl;
68 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginProperties;
69 import org.netbeans.modules.j2ee.oc4j.util.OC4JDebug;
70 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginUtils;
71 import org.netbeans.modules.web.api.webmodule.WebModule;
72 import org.openide.ErrorManager;
73 import org.openide.filesystems.FileObject;
74 import org.openide.filesystems.FileUtil;
75 import org.openide.filesystems.JarFileSystem;
76 import org.openide.util.NbBundle;
77 import org.openide.util.RequestProcessor;
78
79 /**
80  *
81  * @author pblaha
82  */

83 public class OC4JDeploymentManager implements DeploymentManager JavaDoc, ProgressObject JavaDoc, Runnable JavaDoc {
84     
85     private static enum COMMAND { DEPLOY, START }
86     
87     private String JavaDoc uri;
88     
89     private Object JavaDoc oc4jPropDm;
90     private DeploymentManager JavaDoc oc4jDm;
91     private OC4JPluginProperties ip;
92     private InstanceProperties instanceProperties;
93     private OC4JJ2eePlatformImpl oc4jPlatform;
94     
95     private OC4JTargetModuleID module_id;
96     private MBeanServerConnection JavaDoc jmxConnection;
97     private Vector JavaDoc listeners = new Vector JavaDoc();
98     private TargetModuleID JavaDoc[] modules;
99     private DeploymentStatus JavaDoc deploymentStatus;
100     private File JavaDoc file;
101     private COMMAND command;
102     private boolean connected = false;
103     
104     /**
105      * Creates an instance of OC4JDeploymentManager
106      *
107      * @param uri uri of the oc4j server
108      */

109     public OC4JDeploymentManager(String JavaDoc uri) {
110         this.uri = uri;
111         
112         ip = new OC4JPluginProperties(this);
113     }
114     
115     /**
116      * Return URI
117      *
118      * @return uri of the oc4j server
119      */

120     public String JavaDoc getUri() {
121         return uri;
122     }
123     
124     /**
125      *
126      * @return
127      */

128     public String JavaDoc getUsername() {
129         return getInstanceProperties().getProperty(InstanceProperties.USERNAME_ATTR);
130     }
131     
132     /**
133      *
134      * @return
135      */

136     public String JavaDoc getPassword() {
137         return getInstanceProperties().getProperty(InstanceProperties.PASSWORD_ATTR);
138     }
139     
140     /**
141      * Returns OC4JPluginProperties
142      *
143      * @return OC4JPluginProperties
144      */

145     public OC4JPluginProperties getProperties() {
146         return ip;
147     }
148     
149     /**
150      * Returns InstanceProperties
151      *
152      * @return InstanceProperties
153      */

154     public InstanceProperties getInstanceProperties() {
155         if (instanceProperties == null)
156             instanceProperties = InstanceProperties.getInstanceProperties(getUri());
157         
158         return instanceProperties;
159     }
160     
161     /**
162      * Distributes application into the server
163      *
164      * @param target server target
165      * @param file distributed file
166      * @param file2 deployment descriptor
167      * @return ProgressObject
168      * @throws java.lang.IllegalStateException
169      */

170     public ProgressObject JavaDoc distribute(Target JavaDoc[] target, File JavaDoc file, File JavaDoc file2) throws IllegalStateException JavaDoc {
171         // release proprietary objects
172
releaseProprietaryObjects();
173         
174         module_id = new OC4JTargetModuleID(target[0], file.getName());
175         this.file = file;
176         InstanceProperties ip = getProperties().getInstanceProperties();
177         
178         try{
179             String JavaDoc server_url = "http://" + ip.getProperty(OC4JPluginProperties.PROPERTY_HOST) + ":" +
180                     ip.getProperty(InstanceProperties.HTTP_PORT_NUMBER);
181             if (file.getName().endsWith(".war")) { // NOI18N
182
module_id.setContextURL(server_url + OrionWebApp.createGraph(file2).getContextRoot());
183             } else if (file.getName().endsWith(".ear")) { // NOI18N
184
JarFileSystem jfs = new JarFileSystem();
185                 jfs.setJarFile(file);
186                 FileObject appXml = jfs.getRoot().getFileObject("META-INF/application.xml"); // NOI18N
187
if (appXml != null) {
188                     Application ear = DDProvider.getDefault().getDDRoot(appXml);
189                     Module modules [] = ear.getModule();
190                     for (int i = 0; i < modules.length; i++) {
191                         OC4JTargetModuleID mod_id = new OC4JTargetModuleID(target[0]);
192                         if (modules[i].getWeb() != null) {
193                             mod_id.setContextURL(server_url + modules[i].getWeb().getContextRoot());
194                         }
195                         module_id.addChild(mod_id);
196                     }
197                 } else {
198                     ErrorManager.getDefault().log("Cannot file META-INF/application.xml in " + file); // NOI18N
199
}
200             }
201         } catch (Exception JavaDoc e){
202             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
203         }
204         
205         command = COMMAND.DEPLOY;
206         fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, NbBundle.getMessage(OC4JDeploymentManager.class, "MSG_DEPLOYING", file.getAbsolutePath())));
207         RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY);
208         
209         return this;
210     }
211     
212     /**
213      * Distributes application into the server
214      *
215      * @param target server target
216      * @param type type of the module
217      * @param inputStream application file input stream
218      * @param inputStream2 deployment descriptor input stream
219      * @return ProgressObject
220      * @throws java.lang.IllegalStateException
221      */

222     public ProgressObject JavaDoc distribute(Target JavaDoc[] target, ModuleType JavaDoc type,
223             InputStream JavaDoc inputStream, InputStream JavaDoc inputStream2) throws IllegalStateException JavaDoc {
224         return distribute(target, inputStream, inputStream2);
225     }
226     
227     /**
228      *
229      * @param deployableObject
230      * @return
231      * @throws javax.enterprise.deploy.spi.exceptions.InvalidModuleException
232      */

233     public DeploymentConfiguration JavaDoc createConfiguration(DeployableObject JavaDoc deployableObject) throws InvalidModuleException JavaDoc {
234         ModuleType JavaDoc type = deployableObject.getType();
235         if (type == ModuleType.WAR) {
236             return new WarDeploymentConfiguration(deployableObject);
237         } else if (type == ModuleType.EAR) {
238             return new EarDeploymentConfiguration(deployableObject);
239         } else if (type == ModuleType.EJB) {
240             return new EjbDeploymentConfiguration(deployableObject);
241         } else {
242             throw new InvalidModuleException JavaDoc("Unsupported module type: " + type.toString()); // NOI18N
243
}
244         
245     }
246     
247     /**
248      *
249      * @param targetModuleID
250      * @param inputStream
251      * @param inputStream2
252      * @return
253      * @throws java.lang.UnsupportedOperationException
254      * @throws java.lang.IllegalStateException
255      */

256     public ProgressObject JavaDoc redeploy(TargetModuleID JavaDoc[] targetModuleID, InputStream JavaDoc inputStream, InputStream JavaDoc inputStream2) throws UnsupportedOperationException JavaDoc, IllegalStateException JavaDoc {
257         if (!isConnected()) {
258             throw new IllegalStateException JavaDoc(NbBundle.getMessage(
259                     OC4JDeploymentManager.class, "MSG_ERROR_DISC_MANAGER")); // NOI18N
260
}
261         
262         // release proprietary objects
263
releaseProprietaryObjects();
264         
265         DeploymentManager JavaDoc manager = getOC4JDeploymentManager();
266         
267         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
268         
269         try {
270             return manager.redeploy(targetModuleID, inputStream, inputStream2);
271         } finally {
272             OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
273         }
274     }
275     
276     /**
277      *
278      * @param target
279      * @param inputStream
280      * @param inputStream2
281      * @return
282      * @throws java.lang.IllegalStateException
283      */

284     public ProgressObject JavaDoc distribute(Target JavaDoc[] target, InputStream JavaDoc inputStream, InputStream JavaDoc inputStream2) throws IllegalStateException JavaDoc {
285         if (!isConnected()) {
286             throw new IllegalStateException JavaDoc(NbBundle.getMessage(
287                     OC4JDeploymentManager.class, "MSG_ERROR_DISC_MANAGER")); // NOI18N
288
}
289         
290         // release proprietary objects
291
releaseProprietaryObjects();
292         
293         DeploymentManager JavaDoc manager = getOC4JDeploymentManager();
294         
295         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
296         
297         try {
298             return manager.distribute(target, inputStream, inputStream2);
299         } finally {
300             OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
301         }
302     }
303     
304     /**
305      *
306      * @param targetModuleID
307      * @return
308      * @throws java.lang.IllegalStateException
309      */

310     public ProgressObject JavaDoc undeploy(TargetModuleID JavaDoc[] targetModuleID) throws IllegalStateException JavaDoc {
311         if (!isConnected()) {
312             throw new IllegalStateException JavaDoc(NbBundle.getMessage(
313                     OC4JDeploymentManager.class, "MSG_ERROR_DISC_MANAGER")); // NOI18N
314
}
315         
316         // release proprietary objects
317
releaseProprietaryObjects();
318         
319         DeploymentManager JavaDoc manager = getOC4JDeploymentManager();
320         
321         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
322         
323         try {
324             return manager.undeploy(targetModuleID);
325         } finally {
326             OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
327         }
328     }
329     
330     /**
331      *
332      * @param targetModuleID
333      * @param file
334      * @param file2
335      * @return
336      * @throws java.lang.UnsupportedOperationException
337      * @throws java.lang.IllegalStateException
338      */

339     public ProgressObject JavaDoc redeploy(TargetModuleID JavaDoc[] targetModuleID, File JavaDoc file, File JavaDoc file2) throws UnsupportedOperationException JavaDoc, IllegalStateException JavaDoc {
340         if (!isConnected()) {
341             throw new IllegalStateException JavaDoc(NbBundle.getMessage(
342                     OC4JDeploymentManager.class, "MSG_ERROR_DISC_MANAGER")); // NOI18N
343
}
344         
345         // release proprietary objects
346
releaseProprietaryObjects();
347         
348         DeploymentManager JavaDoc manager = getOC4JDeploymentManager();
349         
350         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
351         
352         try {
353             return manager.redeploy(targetModuleID, file, file2);
354         } finally {
355             OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
356         }
357     }
358     
359     /**
360      *
361      * @param targetModuleID
362      * @return
363      * @throws java.lang.IllegalStateException
364      */

365     public ProgressObject JavaDoc stop(TargetModuleID JavaDoc[] targetModuleID) throws IllegalStateException JavaDoc {
366         if (!isConnected()) {
367             throw new IllegalStateException JavaDoc(NbBundle.getMessage(
368                     OC4JDeploymentManager.class, "MSG_ERROR_DISC_MANAGER")); // NOI18N
369
}
370         
371         // release proprietary objects
372
releaseProprietaryObjects();
373         
374         DeploymentManager JavaDoc manager = getOC4JDeploymentManager();
375         
376         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
377         
378         try {
379             return manager.stop(targetModuleID);
380         } finally {
381             OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
382         }
383     }
384     
385     /**
386      *
387      * @param targetModuleID
388      * @return
389      * @throws java.lang.IllegalStateException
390      */

391     public ProgressObject JavaDoc start(TargetModuleID JavaDoc[] targetModuleID) throws IllegalStateException JavaDoc {
392         // release proprietary objects
393
releaseProprietaryObjects();
394         
395         modules = targetModuleID;
396         command = COMMAND.START;
397         fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.RUNNING, NbBundle.getMessage(OC4JDeploymentManager.class, "MSG_STARTING_APP")));
398         RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY);
399         
400         return this;
401     }
402     
403     /**
404      *
405      * @param locale
406      * @throws java.lang.UnsupportedOperationException
407      */

408     public void setLocale(java.util.Locale JavaDoc locale) throws UnsupportedOperationException JavaDoc {
409         getOC4JDeploymentManager().setLocale(locale);
410     }
411     
412     /**
413      *
414      * @param locale
415      * @return
416      */

417     public boolean isLocaleSupported(java.util.Locale JavaDoc locale) {
418         return getOC4JDeploymentManager().isLocaleSupported(locale);
419     }
420     
421     /**
422      *
423      * @param moduleType
424      * @param target
425      * @return
426      * @throws javax.enterprise.deploy.spi.exceptions.TargetException
427      * @throws java.lang.IllegalStateException
428      */

429     public TargetModuleID JavaDoc[] getAvailableModules(ModuleType JavaDoc moduleType, Target JavaDoc[] target) throws TargetException JavaDoc, IllegalStateException JavaDoc {
430         if (!isConnected()) {
431             throw new IllegalStateException JavaDoc(NbBundle.getMessage(
432                     OC4JDeploymentManager.class, "MSG_ERROR_DISC_MANAGER")); // NOI18N
433
}
434         
435         // release proprietary objects
436
releaseProprietaryObjects();
437         
438         DeploymentManager JavaDoc manager = getOC4JDeploymentManager();
439         
440         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
441         
442         try {
443             return manager.getAvailableModules(moduleType, target);
444         } finally {
445             OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
446         }
447     }
448     
449     /**
450      *
451      * @param moduleType
452      * @param target
453      * @return
454      * @throws javax.enterprise.deploy.spi.exceptions.TargetException
455      * @throws java.lang.IllegalStateException
456      */

457     public TargetModuleID JavaDoc[] getNonRunningModules(ModuleType JavaDoc moduleType, Target JavaDoc[] target) throws TargetException JavaDoc, IllegalStateException JavaDoc {
458         if (!isConnected()) {
459             throw new IllegalStateException JavaDoc(NbBundle.getMessage(
460                     OC4JDeploymentManager.class, "MSG_ERROR_DISC_MANAGER")); // NOI18N
461
}
462         
463         // release proprietary objects
464
releaseProprietaryObjects();
465         
466         DeploymentManager JavaDoc manager = getOC4JDeploymentManager();
467         
468         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
469         
470         try {
471             return manager.getNonRunningModules(moduleType, target);
472         } finally {
473             OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
474         }
475     }
476     
477     /**
478      *
479      * @param moduleType
480      * @param target
481      * @return
482      * @throws javax.enterprise.deploy.spi.exceptions.TargetException
483      * @throws java.lang.IllegalStateException
484      */

485     public TargetModuleID JavaDoc[] getRunningModules(ModuleType JavaDoc moduleType, Target JavaDoc[] target) throws TargetException JavaDoc, IllegalStateException JavaDoc {
486         if (!isConnected()) {
487             throw new IllegalStateException JavaDoc(NbBundle.getMessage(
488                     OC4JDeploymentManager.class, "MSG_ERROR_DISC_MANAGER")); // NOI18N
489
}
490         
491         // release proprietary objects
492
releaseProprietaryObjects();
493         
494         DeploymentManager JavaDoc manager = getOC4JDeploymentManager();
495         
496         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
497         
498         try {
499             return manager.getRunningModules(moduleType, target);
500         } finally {
501             OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
502         }
503     }
504     
505     /**
506      *
507      * @param dConfigBeanVersionType
508      * @throws javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException
509      */

510     public void setDConfigBeanVersion(DConfigBeanVersionType JavaDoc dConfigBeanVersionType) throws DConfigBeanVersionUnsupportedException JavaDoc {
511         getOC4JDeploymentManager().setDConfigBeanVersion(dConfigBeanVersionType);
512     }
513     
514     /**
515      *
516      * @param dConfigBeanVersionType
517      * @return
518      */

519     public boolean isDConfigBeanVersionSupported(DConfigBeanVersionType JavaDoc dConfigBeanVersionType) {
520         return getOC4JDeploymentManager().isDConfigBeanVersionSupported(dConfigBeanVersionType);
521     }
522     
523     /**
524      *
525      * @param connected
526      */

527     public void setConnected(boolean connected) {
528         this.connected = connected;
529     }
530     
531     /**
532      *
533      * @return
534      */

535     public boolean isConnected() {
536         return connected;
537     }
538     
539     /**
540      *
541      */

542     public void release() {
543         releaseProprietaryObjects();
544     }
545     
546     /**
547      *
548      * @return
549      */

550     public boolean isRedeploySupported() {
551         return getOC4JDeploymentManager().isRedeploySupported();
552     }
553     
554     /**
555      *
556      * @return
557      */

558     public java.util.Locale JavaDoc getCurrentLocale() {
559         return getOC4JDeploymentManager().getCurrentLocale();
560     }
561     
562     /**
563      *
564      * @return
565      */

566     public DConfigBeanVersionType JavaDoc getDConfigBeanVersion() {
567         return getOC4JDeploymentManager().getDConfigBeanVersion();
568     }
569     
570     /**
571      *
572      * @return
573      */

574     public java.util.Locale JavaDoc getDefaultLocale() {
575         return getOC4JDeploymentManager().getDefaultLocale();
576     }
577     
578     /**
579      *
580      * @return
581      */

582     public java.util.Locale JavaDoc[] getSupportedLocales() {
583         return getOC4JDeploymentManager().getSupportedLocales();
584     }
585     
586     /**
587      *
588      * @return
589      */

590     public Target JavaDoc[] getTargets() {
591         OC4JDebug.log(getClass().getName(), "getTargets for Deployment Maneger");
592         
593         // release proprietary objects
594
releaseProprietaryObjects();
595         
596         try{
597             return getOC4JDeploymentManager().getTargets();
598         } catch(Exception JavaDoc e) {
599             OC4JErrorManager.getInstance(this).error(uri, e, OC4JErrorManager.GENERIC_FAILURE);
600         }
601         
602         return null;
603     }
604     
605     /**
606      * Gets deployment manager
607      */

608     private synchronized DeploymentManager JavaDoc getOC4JDeploymentManager() {
609         if (null == oc4jDm) {
610             OC4JDeploymentFactory factory = (OC4JDeploymentFactory) OC4JDeploymentFactory.getDefault();
611             
612             try {
613                 if(!OC4JPluginProperties.isRunning(getInstanceProperties().getProperty(OC4JPluginProperties.PROPERTY_HOST), getInstanceProperties().getProperty(InstanceProperties.HTTP_PORT_NUMBER)))
614                     throw new DeploymentManagerCreationException JavaDoc(uri);
615                 
616                 DeploymentFactory JavaDoc propFactory = factory.getOC4JDeploymentFactory(uri);
617                 
618                 OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
619                 
620                 oc4jDm = propFactory.getDeploymentManager(uri, getUsername(), getPassword());
621                 
622                 setConnected(true);
623             } catch (Exception JavaDoc e) {
624                 OC4JErrorManager.getInstance(this).error(uri, e, OC4JErrorManager.GENERIC_FAILURE);
625                 
626                 setConnected(false);
627             } finally {
628                 OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
629             }
630         }
631         
632         return oc4jDm;
633     }
634     
635     /**
636      * Gets proprietary deployment manager
637      */

638     private synchronized Object JavaDoc getOC4JProprietaryDeploymentManager() {
639         if (null == oc4jPropDm) {
640             OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
641             
642             try {
643                 Class JavaDoc cls = OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).
644                         loadClass("oracle.oc4j.admin.deploy.api.J2EEDeploymentManager");
645                 Class JavaDoc partypes[] = {String JavaDoc.class, String JavaDoc.class, String JavaDoc.class};
646                 Constructor JavaDoc ct = cls.getConstructor(partypes);
647                 Object JavaDoc arglist[] = {uri, getUsername(), getPassword()};
648                 oc4jPropDm = ct.newInstance(arglist);
649             } catch (NoSuchMethodException JavaDoc e) {
650                 ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
651             } catch (ClassNotFoundException JavaDoc e) {
652                 ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
653             } catch (InstantiationException JavaDoc e) {
654                 ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
655             } catch (IllegalAccessException JavaDoc e) {
656                 ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
657             } catch (InvocationTargetException JavaDoc e) {
658                 ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
659             } catch (Exception JavaDoc e) {
660                 ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
661             } finally {
662                 OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
663             }
664         }
665         
666         return oc4jPropDm;
667     }
668     
669     /**
670      * Invokes methods from proprietary deployment manager
671      */

672     private void invoke(String JavaDoc methodName, Class JavaDoc[] paramNames, Object JavaDoc[] args) throws Exception JavaDoc {
673         Object JavaDoc manager = getOC4JProprietaryDeploymentManager();
674         
675         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
676         
677         Method JavaDoc m = manager.getClass().getMethod(methodName, paramNames);
678         m.invoke(manager, args);
679         
680         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
681     }
682     
683     public void run() {
684         switch(command) {
685         case DEPLOY:
686             fireHandleProgressEvent(module_id, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.RUNNING, NbBundle.getMessage(OC4JDeploymentManager.class, "MSG_DEPLOYING", file.getAbsolutePath())));
687             String JavaDoc moduleID = module_id.getModuleID();
688             
689             // When JSF support included checks if there are libs on classpath
690
WebModule w = WebModule.getWebModule(FileUtil.toFileObject(file));
691             
692             if (null != w && OC4JPluginUtils.isJSFInWebModule(w)) {
693                 ClassPath cp = ClassPath.getClassPath(w.getDocumentBase(), ClassPath.COMPILE);
694                 
695                 // Checks JSF and JSTL
696
if (null == cp.findResource("javax/faces/FacesException.class")
697                         || null == cp.findResource("javax/servlet/jsp/jstl/core/Config.class")) {
698                     fireHandleProgressEvent(module_id, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.FAILED, NbBundle.getMessage(OC4JDeploymentManager.class, "MSG_DEPLOY_FAILED")));
699                     OC4JErrorManager.getInstance(this).reaction("com.evermind.server.http.deployment.WARAnnotationParser");
700                     return;
701                 }
702             }
703             
704             try{
705                 invoke("deploy", new Class JavaDoc[] {String JavaDoc.class, String JavaDoc.class, Map JavaDoc.class, boolean.class}, // NOI18N
706
new Object JavaDoc[] {file.getAbsolutePath(), moduleID, new HashMap JavaDoc(), true});
707                 // for web-apps we should bind
708
if(moduleID.endsWith(".war") || moduleID.endsWith(".ear")) { // NOI18N
709
invoke("bindWebApp", // NOI18N
710
new Class JavaDoc[] {String JavaDoc.class, String JavaDoc.class},
711                             new Object JavaDoc[] {moduleID, ip.getInstanceProperties().getProperty(OC4JPluginProperties.PROPERTY_WEB_SITE) + "-web-site"}); // NOI18N
712
}
713             } catch (Exception JavaDoc e) {
714                 fireHandleProgressEvent(module_id, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.FAILED, NbBundle.getMessage(OC4JDeploymentManager.class, "MSG_DEPLOY_FAILED")));
715                 OC4JErrorManager.getInstance(this).error(uri, e, OC4JErrorManager.GENERIC_FAILURE);
716                 return;
717             }
718             
719             fireHandleProgressEvent(module_id, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.COMPLETED, NbBundle.getMessage(OC4JDeploymentManager.class, "MSG_DEPLOYED")));
720             break;
721         case START:
722             fireHandleProgressEvent(module_id, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.RUNNING, NbBundle.getMessage(OC4JDeploymentManager.class, "MSG_STARTING_APP")));
723             for(int i = 0; i < modules.length; i++) {
724                 TargetModuleID JavaDoc module = modules[i];
725                 try{
726                     invoke("startApplication", new Class JavaDoc[] {String JavaDoc.class}, new Object JavaDoc[] {module.getModuleID()});
727                 } catch(Exception JavaDoc e) {
728                     fireHandleProgressEvent(module_id, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.DISTRIBUTE, StateType.FAILED, NbBundle.getMessage(OC4JDeploymentManager.class, "MSG_DEPLOY_FAILED")));
729                     OC4JErrorManager.getInstance(this).error(uri, e, OC4JErrorManager.GENERIC_FAILURE);
730                     return;
731                 }
732             }
733             
734             fireHandleProgressEvent(module_id, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.COMPLETED, NbBundle.getMessage(OC4JDeploymentManager.class, "MSG_STARTING_APP")));
735             break;
736         }
737     }
738     
739     /**
740      * JSR-77 implementation
741      *
742      * @return
743      */

744     public MBeanServerConnection JavaDoc getJMXConnector() {
745         OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).updateLoader();
746         
747         if(jmxConnection == null) {
748             try {
749                 if(!OC4JPluginProperties.isRunning(getInstanceProperties().getProperty(OC4JPluginProperties.PROPERTY_HOST), getInstanceProperties().getProperty(InstanceProperties.HTTP_PORT_NUMBER)))
750                     return null;
751                 
752                 Hashtable JavaDoc credentials= new Hashtable JavaDoc();
753                 credentials.put("login", ip.getInstanceProperties().getProperty(InstanceProperties.USERNAME_ATTR)); // NOI18N
754
credentials.put("password", ip.getInstanceProperties().getProperty(InstanceProperties.PASSWORD_ATTR)); // NOI18N
755
Hashtable JavaDoc env = new Hashtable JavaDoc();
756                 env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "oracle.oc4j.admin.jmx.remote");
757                 env.put(JMXConnector.CREDENTIALS, credentials);
758                 
759                 JMXServiceURL JavaDoc serviceUrl= new JMXServiceURL JavaDoc( "rmi", // NOI18N
760
ip.getInstanceProperties().getProperty(OC4JPluginProperties.PROPERTY_HOST),
761                         Integer.parseInt(ip.getInstanceProperties().getProperty(OC4JPluginProperties.PROPERTY_ADMIN_PORT)),
762                         "/oc4j"); // NOI18N
763
JMXConnector JavaDoc connection = JMXConnectorFactory.newJMXConnector(serviceUrl, env);
764                 connection.connect();
765                 jmxConnection = connection.getMBeanServerConnection();
766             } catch(Exception JavaDoc e) {
767                 OC4JErrorManager.getInstance(this).error(uri, e, OC4JErrorManager.GENERIC_FAILURE);
768             } finally {
769                 OC4JClassLoader.getInstance(getProperties().getOC4JHomeLocation()).restoreLoader();
770             }
771         }
772         
773         return jmxConnection;
774     }
775     
776     /**
777      * Releases all proprietary objects to be refreshed
778      */

779     public void releaseProprietaryObjects() {
780         if (null != oc4jDm) {
781             oc4jDm.release();
782             oc4jDm = null;
783         }
784         
785         if (null != oc4jPropDm)
786             oc4jPropDm = null;
787         
788         if (null != jmxConnection)
789             jmxConnection = null;
790     }
791     
792     /**
793      *
794      * @return
795      */

796     public OC4JJ2eePlatformImpl getOC4JPlatform() {
797         if (oc4jPlatform == null) {
798             oc4jPlatform = (OC4JJ2eePlatformImpl) new OC4JJ2eePlatformImpl(this);
799         }
800         return oc4jPlatform;
801     }
802     
803     /**
804      *
805      * @param pl
806      */

807     public void addProgressListener(ProgressListener JavaDoc pl) {
808         listeners.add(pl);
809     }
810     
811     /**
812      *
813      * @param pl
814      */

815     public void removeProgressListener(ProgressListener JavaDoc pl) {
816         listeners.remove(pl);
817     }
818     
819     /**
820      *
821      * @throws javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException
822      */

823     public void stop() throws OperationUnsupportedException JavaDoc {
824         throw new OperationUnsupportedException JavaDoc("");
825     }
826     
827     /**
828      *
829      * @return
830      */

831     public boolean isStopSupported() {
832         return false;
833     }
834     
835     /**
836      *
837      * @throws javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException
838      */

839     public void cancel() throws OperationUnsupportedException JavaDoc {
840         throw new OperationUnsupportedException JavaDoc("");
841     }
842     
843     /**
844      *
845      * @return
846      */

847     public boolean isCancelSupported() {
848         return false;
849     }
850     
851     /**
852      *
853      * @param targetModuleID
854      * @return
855      */

856     public ClientConfiguration JavaDoc getClientConfiguration(TargetModuleID JavaDoc targetModuleID) {
857         return null;
858     }
859     
860     /**
861      *
862      * @return
863      */

864     public TargetModuleID JavaDoc[] getResultTargetModuleIDs() {
865         return new TargetModuleID JavaDoc[]{ module_id };
866     }
867     
868     /**
869      *
870      * @return
871      */

872     public DeploymentStatus JavaDoc getDeploymentStatus() {
873         return deploymentStatus;
874     }
875     
876     /** Report event to any registered listeners.
877      *
878      * @param targetModuleID
879      * @param deploymentStatus
880      */

881     public void fireHandleProgressEvent(TargetModuleID JavaDoc targetModuleID, DeploymentStatus JavaDoc deploymentStatus) {
882         this.deploymentStatus = deploymentStatus;
883         Vector JavaDoc targets = null;
884         ProgressEvent JavaDoc evt = new ProgressEvent JavaDoc(this, targetModuleID, deploymentStatus);
885         
886         synchronized (this) {
887             if (listeners != null) {
888                 targets = (Vector JavaDoc) listeners.clone();
889             }
890         }
891         
892         if (targets != null) {
893             for (int i = 0; i < targets.size(); i++) {
894                 ProgressListener JavaDoc target = (ProgressListener JavaDoc)targets.elementAt(i);
895                 target.handleProgressEvent(evt);
896             }
897         }
898     }
899 }
Popular Tags