KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > avk > actions > CommonNodeAction


1 /*
2  * CommonNodeAction.java
3  *
4  * Created on March 28, 2006, 11:33 AM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.j2ee.sun.ide.avk.actions;
11
12 import java.io.File JavaDoc;
13 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
14 import javax.management.ObjectName JavaDoc;
15 import org.netbeans.api.progress.ProgressHandle;
16 import org.netbeans.api.progress.ProgressHandleFactory;
17 import org.netbeans.api.project.Project;
18 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
19 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
20 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
21 import org.netbeans.spi.project.ActionProvider;
22 import org.openide.DialogDisplayer;
23 import org.openide.NotifyDescriptor;
24 import org.openide.filesystems.FileUtil;
25 import org.openide.nodes.Node;
26 import org.openide.util.HelpCtx;
27 import org.openide.util.Lookup;
28 import org.openide.util.NbBundle;
29 import org.openide.util.actions.NodeAction;
30
31 /**
32  *
33  * @author bshankar@sun.com
34  */

35 public abstract class CommonNodeAction extends NodeAction {
36     
37     public static String JavaDoc curResultsDir = null; // This variable is shared across all actions.
38

39     protected boolean enable(Node[] node) {
40         return true;
41     }
42     
43     public String JavaDoc getName() {
44         return "Common Node Action";
45     }
46     
47     public HelpCtx getHelpCtx() {
48         return HelpCtx.DEFAULT_HELP;
49     }
50     
51     protected boolean asynchronous() {
52         return false;
53     }
54     
55     protected SunDeploymentManagerInterface getDeploymentManager(Project project) {
56         SunDeploymentManagerInterface sdm = null;
57         InstanceProperties instProps = getInstanceProperties(project);
58         sdm = (SunDeploymentManagerInterface)instProps.getDeploymentManager();
59         return sdm;
60     }
61     
62     protected InstanceProperties getInstanceProperties(Project project) {
63         Lookup projectLookup = project.getLookup();
64         J2eeModuleProvider moduleProvider = (J2eeModuleProvider)projectLookup.lookup(J2eeModuleProvider.class);
65         InstanceProperties instProps = moduleProvider.getInstanceProperties();
66         if(instProps == null) {
67             instProps = InstanceProperties.getInstanceProperties(moduleProvider.getServerInstanceID());
68         }
69         return instProps;
70         
71     }
72     protected ActionProvider getActionProvider(Project project) {
73         Lookup projectLookup = project.getLookup();
74         ActionProvider ap = (ActionProvider)projectLookup.lookup(ActionProvider.class);
75         return ap;
76     }
77     
78     protected Lookup getLookup(Node projectNode) {
79         Lookup lookup = projectNode.getLookup();
80         Project project = (Project)lookup.lookup(Project.class);
81         Lookup projectLookup = project.getLookup();
82         return projectLookup;
83     }
84     
85     protected String JavaDoc getDeployDirFromDomainXml(Project project) {
86         String JavaDoc deployDir = null;
87         Lookup projectLookup = project.getLookup();
88         J2eeModuleProvider moduleProvider = (J2eeModuleProvider)projectLookup.lookup(J2eeModuleProvider.class);
89         String JavaDoc moduleName;
90         try {
91             moduleName = moduleProvider.getJ2eeModule().getArchive().toString();
92             moduleName = moduleName.substring(moduleName.lastIndexOf('/')+1);
93             moduleName = moduleName.substring(0,moduleName.lastIndexOf('.'));
94         } catch(Exception JavaDoc ex) {
95             moduleName = moduleProvider.getDeploymentName().toLowerCase();
96             ex.printStackTrace();
97         }
98         String JavaDoc type = "j2ee-application";
99         if(ModuleType.WAR.equals(moduleProvider.getJ2eeModule().getModuleType())) {
100             type = "web-module";
101         } else if(ModuleType.EJB.equals(moduleProvider.getJ2eeModule().getModuleType())) {
102             type = "ejb-module";
103         } else if(ModuleType.RAR.equals(moduleProvider.getJ2eeModule().getModuleType())) {
104             type = "connector-module";
105         }
106         String JavaDoc objectName = "com.sun.appserv:type=" + type + ",name=" + moduleName + ",category=config";
107         String JavaDoc attrName = "location";
108         try {
109             deployDir = (String JavaDoc)getDeploymentManager(project).getManagement().getMBeanServerConnection().getAttribute(new ObjectName JavaDoc(objectName), attrName);
110             deployDir = stringReplace(deployDir, "${com.sun.aas.instanceRoot}", getDomainDir(project), true);
111             deployDir = stringReplace(deployDir, "${com.sun.aas.installRoot}", getDeploymentManager(project).getPlatformRoot().getAbsolutePath(), true);
112         } catch(Exception JavaDoc ex) {
113             ex.printStackTrace();
114         }
115         return deployDir;
116     }
117     
118     private String JavaDoc stringReplace(String JavaDoc s, String JavaDoc oldString, String JavaDoc newString, boolean replaceAll) {
119         int index = -1;
120         while((index = s.indexOf(oldString)) != -1) {
121             StringBuilder JavaDoc sb = new StringBuilder JavaDoc(s);
122             sb.replace(index, oldString.length(), newString);
123             s = sb.toString();
124             if(!replaceAll) break;
125         }
126         return s;
127     }
128     
129     protected String JavaDoc getDeployDirUsingNetbeansAPIs(Project project) {
130         Lookup projectLookup = project.getLookup();
131         J2eeModuleProvider moduleProvider = (J2eeModuleProvider)projectLookup.lookup(J2eeModuleProvider.class);
132         String JavaDoc moduleName;
133         try {
134             moduleName = moduleProvider.getJ2eeModule().getArchive().toString();
135             moduleName = moduleName.substring(moduleName.lastIndexOf('/'));
136             moduleName = moduleName.substring(0,moduleName.lastIndexOf('.'));
137         } catch(Exception JavaDoc ex) {
138             moduleName = moduleProvider.getDeploymentName().toLowerCase();
139             ex.printStackTrace();
140         }
141         String JavaDoc domainDir = getDomainDir(project);
142         String JavaDoc moduleDir = null;
143         if(ModuleType.EAR.equals(moduleProvider.getJ2eeModule().getModuleType())) {
144             moduleDir = "j2ee-apps";
145         } else {
146             moduleDir = "j2ee-modules";
147         }
148         String JavaDoc deploymentDirName = domainDir + File.separator +
149                 "applications" + File.separator +
150                 moduleDir + File.separator +
151                 moduleName;
152         if(!(new File JavaDoc(deploymentDirName).exists())) { // in-place deployment.
153
try {
154                 deploymentDirName = FileUtil.toFile(moduleProvider.getJ2eeModule().getContentDirectory()).getAbsolutePath();
155             } catch(Exception JavaDoc ex) {
156                 ex.printStackTrace();
157                 deploymentDirName = null;
158             }
159         }
160         return deploymentDirName;
161     }
162     
163     protected String JavaDoc getDeployDir(Project project) {
164         return getDeployDirFromDomainXml(project);
165         //return getDeployDirUsingNetbeansAPIs(project);
166
}
167     
168     protected String JavaDoc getDomainDir(Project project) {
169         InstanceProperties instProps = getInstanceProperties(project);
170         return instProps.getProperty("LOCATION") + File.separator + instProps.getProperty("DOMAIN");
171     }
172     
173     protected void deployProject(Project project) {
174         
175         final Lookup projectLookup = project.getLookup();
176         final ActionProvider ap = getActionProvider(project);
177         final String JavaDoc targetName = "redeploy";
178         
179         Thread JavaDoc t = new Thread JavaDoc() {
180             public synchronized void run() {
181                 //AdminTasks.debug("Running build target = " + targetName);
182
ap.invokeAction(targetName, projectLookup);
183             }
184         };
185         t.start();
186         try {
187             t.join();
188         } catch(Exception JavaDoc ex) {
189             ex.printStackTrace();
190         }
191         
192         //AdminTasks.debug("Done running build target = " + targetName + ", now waiting for it to complete...");
193

194         Thread JavaDoc[] children = new Thread JavaDoc[t.activeCount()];
195         t.enumerate(children);
196         //AdminTasks.debug("children.length = " + children.length);
197

198         for(Thread JavaDoc child : children) {
199             try {
200                 //AdminTasks.debug("child = " + child.getName() + ", state = " + child.getState() + ", child.threadGroup = " + child.getThreadGroup());
201
if(child.getName().indexOf("run-deploy") != -1) {
202                     AdminTasks.debug("Waiting for thread = " + child.getName());
203                     child.join();
204                 }
205             } catch(Exception JavaDoc ex) {
206                 ex.printStackTrace();
207             }
208         }
209     }
210     
211     protected void showError(String JavaDoc msgKey) {
212         String JavaDoc msg = msgKey;
213         try {
214             msg = NbBundle.getMessage(CommonNodeAction.class, msgKey);
215         } catch(Exception JavaDoc ex) {
216             ex.printStackTrace();
217         }
218         NotifyDescriptor d = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
219         DialogDisplayer.getDefault().notify(d);
220     }
221     
222     private ProgressHandle handle = ProgressHandleFactory.createHandle("app-verification");
223     
224     protected void showProgress(String JavaDoc msgKey) {
225         String JavaDoc msg = msgKey;
226         try {
227             msg = NbBundle.getMessage(CommonNodeAction.class, msgKey);
228         } catch(Exception JavaDoc ex) {
229             ex.printStackTrace();
230         }
231         handle.setDisplayName(msg);
232         handle.start();
233     }
234     
235     protected void hideProgress() {
236         handle.finish();
237     }
238 }
239
Popular Tags