KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > nodes > actions > UndeployModuleCookieImpl


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.jboss4.nodes.actions;
21
22 import java.io.File JavaDoc;
23 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
24 import javax.management.MalformedObjectNameException JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import org.netbeans.api.progress.ProgressHandle;
27 import org.netbeans.api.progress.ProgressHandleFactory;
28 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager;
29 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties;
30 import org.netbeans.modules.j2ee.jboss4.nodes.Util;
31 import org.openide.util.Lookup;
32 import org.openide.util.NbBundle;
33 import org.openide.util.RequestProcessor;
34 import org.openide.util.RequestProcessor.Task;
35
36 /**
37  *
38  * @author Michal Mocnak
39  */

40 public class UndeployModuleCookieImpl implements UndeployModuleCookie {
41     
42     private String JavaDoc fileName;
43     private Lookup lookup;
44     private ModuleType JavaDoc type;
45     private final boolean isEJB3;
46     private boolean isRunning;
47     
48     public UndeployModuleCookieImpl(String JavaDoc fileName, ModuleType JavaDoc type, Lookup lookup) {
49         this(fileName, type, false, lookup);
50     }
51
52     public UndeployModuleCookieImpl(String JavaDoc fileName, Lookup lookup) {
53         this(fileName, ModuleType.EJB, true, lookup);
54     }
55
56     private UndeployModuleCookieImpl(String JavaDoc fileName, ModuleType JavaDoc type, boolean isEJB3, Lookup lookup) {
57         this.lookup = lookup;
58         this.fileName = fileName;
59         this.type = type;
60         this.isEJB3 = isEJB3;
61         this.isRunning = false;
62     }
63     
64     public Task undeploy() {
65         final JBDeploymentManager dm = (JBDeploymentManager) lookup.lookup(JBDeploymentManager.class);
66         final String JavaDoc nameWoExt = fileName.substring(0, fileName.lastIndexOf('.'));
67         final ProgressHandle handle = ProgressHandleFactory.createHandle(NbBundle.getMessage(UndeployModuleCookieImpl.class,
68                 "LBL_UndeployProgress", nameWoExt));
69         
70         Runnable JavaDoc r = new Runnable JavaDoc() {
71             public void run() {
72                 isRunning = true;
73                 String JavaDoc deployDir = dm.getInstanceProperties().getProperty(JBPluginProperties.PROPERTY_DEPLOY_DIR);
74                 File JavaDoc file = new File JavaDoc(deployDir, fileName);
75                 
76                 if(file.exists() && file.canWrite()) {
77                     file.delete();
78                     
79                     try {
80                         ObjectName JavaDoc searchPattern = null;
81                         if (Util.isRemoteManagementSupported(lookup) && !isEJB3) {
82                             searchPattern = new ObjectName JavaDoc("jboss.management.local:"+(!type.equals(ModuleType.EAR) ?
83                                 "J2EEApplication=null," : "")+"j2eeType="+Util.getModuleTypeString(type)+",name=" + fileName + ",*");
84                         }
85                         else {
86                             if (type.equals(ModuleType.EAR)) {
87                                 searchPattern = new ObjectName JavaDoc("jboss.j2ee:service=EARDeployment,url='" + fileName + "'"); // NOI18N
88
}
89                             else
90                             if (type.equals(ModuleType.WAR)) {
91                                 searchPattern = new ObjectName JavaDoc("jboss.web:j2eeType=WebModule,J2EEApplication=none,name=//localhost/" + nameWoExt + ",*"); // NOI18N
92
}
93                             else
94                             if (type.equals(ModuleType.EJB)) {
95                                 searchPattern = new ObjectName JavaDoc("jboss.j2ee:service=" + (isEJB3 ? "EJB3" : "EjbModule") + ",module=" + fileName); // NOI18N
96
}
97                         }
98                         
99                         int time = 0;
100                         while(dm.refreshRMIServer() != null && Util.isObjectDeployed(dm.getRMIServer(), searchPattern) && time < 30000) {
101                             try {
102                                 Thread.sleep(2000);
103                                 time += 2000;
104                             } catch (InterruptedException JavaDoc ex) {
105                                 // Nothing to do
106
}
107                         }
108                     } catch (MalformedObjectNameException JavaDoc ex) {
109                     } catch (NullPointerException JavaDoc ex) {
110                         // Nothing to do
111
}
112                 }
113                 
114                 handle.finish();
115                 isRunning = false;
116             }
117         };
118         
119         handle.start();
120         return RequestProcessor.getDefault().post(r);
121     }
122     
123     public boolean isRunning() {
124         return isRunning;
125     }
126     
127 }
Popular Tags