1 9 10 package org.netbeans.modules.j2ee.sun.ide.avk.actions; 11 12 import java.io.File ; 13 import javax.enterprise.deploy.shared.ModuleType ; 14 import javax.management.ObjectName ; 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 35 public abstract class CommonNodeAction extends NodeAction { 36 37 public static String curResultsDir = null; 39 protected boolean enable(Node[] node) { 40 return true; 41 } 42 43 public String 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 getDeployDirFromDomainXml(Project project) { 86 String deployDir = null; 87 Lookup projectLookup = project.getLookup(); 88 J2eeModuleProvider moduleProvider = (J2eeModuleProvider)projectLookup.lookup(J2eeModuleProvider.class); 89 String 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 ex) { 95 moduleName = moduleProvider.getDeploymentName().toLowerCase(); 96 ex.printStackTrace(); 97 } 98 String 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 objectName = "com.sun.appserv:type=" + type + ",name=" + moduleName + ",category=config"; 107 String attrName = "location"; 108 try { 109 deployDir = (String )getDeploymentManager(project).getManagement().getMBeanServerConnection().getAttribute(new ObjectName (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 ex) { 113 ex.printStackTrace(); 114 } 115 return deployDir; 116 } 117 118 private String stringReplace(String s, String oldString, String newString, boolean replaceAll) { 119 int index = -1; 120 while((index = s.indexOf(oldString)) != -1) { 121 StringBuilder sb = new StringBuilder (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 getDeployDirUsingNetbeansAPIs(Project project) { 130 Lookup projectLookup = project.getLookup(); 131 J2eeModuleProvider moduleProvider = (J2eeModuleProvider)projectLookup.lookup(J2eeModuleProvider.class); 132 String 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 ex) { 138 moduleName = moduleProvider.getDeploymentName().toLowerCase(); 139 ex.printStackTrace(); 140 } 141 String domainDir = getDomainDir(project); 142 String moduleDir = null; 143 if(ModuleType.EAR.equals(moduleProvider.getJ2eeModule().getModuleType())) { 144 moduleDir = "j2ee-apps"; 145 } else { 146 moduleDir = "j2ee-modules"; 147 } 148 String deploymentDirName = domainDir + File.separator + 149 "applications" + File.separator + 150 moduleDir + File.separator + 151 moduleName; 152 if(!(new File (deploymentDirName).exists())) { try { 154 deploymentDirName = FileUtil.toFile(moduleProvider.getJ2eeModule().getContentDirectory()).getAbsolutePath(); 155 } catch(Exception ex) { 156 ex.printStackTrace(); 157 deploymentDirName = null; 158 } 159 } 160 return deploymentDirName; 161 } 162 163 protected String getDeployDir(Project project) { 164 return getDeployDirFromDomainXml(project); 165 } 167 168 protected String 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 targetName = "redeploy"; 178 179 Thread t = new Thread () { 180 public synchronized void run() { 181 ap.invokeAction(targetName, projectLookup); 183 } 184 }; 185 t.start(); 186 try { 187 t.join(); 188 } catch(Exception ex) { 189 ex.printStackTrace(); 190 } 191 192 194 Thread [] children = new Thread [t.activeCount()]; 195 t.enumerate(children); 196 198 for(Thread child : children) { 199 try { 200 if(child.getName().indexOf("run-deploy") != -1) { 202 AdminTasks.debug("Waiting for thread = " + child.getName()); 203 child.join(); 204 } 205 } catch(Exception ex) { 206 ex.printStackTrace(); 207 } 208 } 209 } 210 211 protected void showError(String msgKey) { 212 String msg = msgKey; 213 try { 214 msg = NbBundle.getMessage(CommonNodeAction.class, msgKey); 215 } catch(Exception 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 msgKey) { 225 String msg = msgKey; 226 try { 227 msg = NbBundle.getMessage(CommonNodeAction.class, msgKey); 228 } catch(Exception 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 |