1 19 25 26 package org.netbeans.modules.j2ee.sun.ide.avk; 27 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.FileWriter ; 31 import java.io.PrintWriter ; 32 import java.io.IOException ; 33 import java.io.BufferedWriter ; 34 import java.io.BufferedReader ; 35 import java.text.MessageFormat ; 36 import java.util.ResourceBundle ; 37 import java.lang.reflect.Method ; 38 import java.io.InputStreamReader ; 39 40 import javax.enterprise.deploy.spi.DeploymentManager ; 41 42 import org.openide.ErrorManager; 43 import org.openide.NotifyDescriptor; 44 import org.openide.awt.StatusDisplayer; 45 import org.openide.modules.InstalledFileLocator; 46 import org.openide.awt.HtmlBrowser.URLDisplayer; 47 import org.netbeans.modules.j2ee.sun.ide.Installer; 48 import org.netbeans.modules.j2ee.sun.ide.j2ee.ui.Util; 49 import org.netbeans.modules.j2ee.sun.api.InstrumentAVK; 50 import org.netbeans.modules.j2ee.sun.api.ExtendedClassLoader; 51 import org.netbeans.modules.j2ee.sun.ide.j2ee.PluginProperties; 52 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface; 53 import org.netbeans.modules.j2ee.sun.ide.j2ee.DeploymentManagerProperties; 54 import org.netbeans.modules.j2ee.sun.ide.j2ee.runtime.actions.ViewLogAction; 55 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager; 56 57 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 58 59 63 public class AVKSupport implements InstrumentAVK { 64 65 private static final String FILE_BACKUP_EXTENSION = "backup"; 66 private static SunDeploymentManagerInterface sunDm; 67 private static boolean serverRunning = true; 68 69 protected static final ResourceBundle bundle = ResourceBundle.getBundle("org.netbeans.modules.j2ee.sun.ide.avk.actions.Bundle"); 71 72 public AVKSupport() { 73 } 74 75 public AVKSupport(SunDeploymentManagerInterface sdm) { 76 setDeploymentManager(sdm); 77 } 78 79 public void setDeploymentManager(SunDeploymentManagerInterface sdm){ 80 this.sunDm = sdm; 81 } 82 83 public void setAVK(boolean onOff){ 84 if(onOff){ 85 instrumentAVK(); 87 }else{ 88 uninstrumentAVK(); 90 } 91 } 92 93 public void generateReport() { 94 File report = null; 95 try{ 96 boolean success = runReportTool(); 97 if(success){ 98 report = new File (getAVKReportLocation() + File.separator + "results" + File.separator + "suiteSummary.html"); URLDisplayer.getDefault().showURL(report.toURI().toURL()); 100 } 101 }catch(Exception ex){ 102 if(report != null){ 103 String message = MessageFormat.format(bundle.getString("Err_ShowReport"), new Object []{report.getAbsolutePath()}); Util.showError(message); 105 } 106 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 107 return; 108 } 109 } 110 111 private void instrumentAVK() { 112 if(this.sunDm.isLocal() == false){ 113 return; 114 } 115 try{ 116 DeploymentManagerProperties dmProps = new DeploymentManagerProperties((DeploymentManager ) this.sunDm); 117 if(!dmProps.getAVKOn()){ 118 setTopManagerStatus(bundle.getString("MSG_Instrument")); FileUtil.clearResults(this.sunDm, dmProps); 120 if(this.sunDm.isRunning()){ 121 stopStartForInstrument(dmProps); 122 }else{ 123 instrument(dmProps); 124 startAfterInstrument(dmProps); 125 } 126 }else{ 127 if(! this.sunDm.isRunning()){ 128 startAfterInstrument(dmProps); 129 } 130 } 131 }catch(Exception ex){ 132 Util.showError(bundle.getString("Err_Instrument")); ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getLocalizedMessage()); 134 } 135 } 136 137 private void stopStartForInstrument(DeploymentManagerProperties dmProps) throws Exception { 138 try{ 139 setTopManagerStatus(bundle.getString("MSG_Stop")); stopServer(dmProps, this.sunDm); 141 dmProps.getInstanceProperties().refreshServerInstance(); 142 instrument(dmProps); 143 startAfterInstrument(dmProps); 144 }catch(Exception ex){ 145 throw ex; 146 } 147 } 148 149 private void startAfterInstrument(DeploymentManagerProperties dmProps) throws Exception { 150 try{ 151 setTopManagerStatus(bundle.getString("MSG_Start")); startServer(dmProps, this.sunDm); 153 dmProps.getInstanceProperties().refreshServerInstance(); 154 setTopManagerStatus(bundle.getString("MSG_AVK_Running")); }catch(Exception ex){ 156 throw ex; 157 } 158 } 159 160 private void instrument(DeploymentManagerProperties dmProps){ 161 editPolicyFile(dmProps); 162 editStoppedDomainConfig(dmProps, true); 163 dmProps.setAVKOn(true); 164 setTopManagerStatus(bundle.getString("MSG_AVK_Stopped")); } 166 167 public void uninstrumentAVK() { 168 DeploymentManagerProperties dmProps = new DeploymentManagerProperties((DeploymentManager ) this.sunDm); 169 try{ 170 setTopManagerStatus(bundle.getString("MSG_Generating")); editStoppedDomainConfig(dmProps, false); 173 }catch(Exception ex){ 174 Util.showError(bundle.getString("Err_UnInstrument")); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 176 return; 177 } 178 } 179 180 public Object loadClass(String methodName, SunDeploymentManagerInterface sdm) throws Exception { 181 Object result = null; 182 File avkHome = getAVKHome(); 183 DeploymentManager deployMgr = (DeploymentManager )sdm; 184 ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader(); 185 try{ 186 Class [] argClass = new Class [2]; 187 argClass[0] = javax.enterprise.deploy.spi.DeploymentManager .class; 188 argClass[1] = java.io.File .class; 189 Object [] argObject = new Object [2]; 190 argObject[0] = deployMgr; 191 argObject[1] = avkHome; 192 Class controllerUtilClass = ServerLocationManager.getNetBeansAndServerClassLoader(sdm.getPlatformRoot()). 193 loadClass("org.netbeans.modules.j2ee.sun.util.InstrumentAVK"); 195 196 Method method = controllerUtilClass.getMethod(methodName, argClass); 197 198 Thread.currentThread().setContextClassLoader( 199 ServerLocationManager.getNetBeansAndServerClassLoader(sdm.getPlatformRoot())); 200 201 result = method.invoke(controllerUtilClass.newInstance(), argObject); 202 203 } catch (Exception e){ 204 throw e; 205 } finally { 206 Thread.currentThread().setContextClassLoader(origClassLoader); 207 } 208 return result; 209 } 210 211 public boolean runReportTool(){ 212 try{ 213 System.setProperty("j2ee.appverification.home", getAVKHome().getAbsolutePath()); 214 System.setProperty("com.sun.aas.installRoot", sunDm.getPlatformRoot().getAbsolutePath()); 215 DeploymentManagerProperties dmProps = new DeploymentManagerProperties((DeploymentManager ) this.sunDm); 216 ExtendedClassLoader loader = (ExtendedClassLoader) ServerLocationManager.getServerOnlyClassLoader(sunDm.getPlatformRoot()); 217 if(loader != null){ 218 File f = getAVKJarLocation(); 219 loader.addURL(f); 220 } 221 222 if(loader != null){ 223 String domainDir = dmProps.getLocation() + File.separator + dmProps.getDomainName(); 224 String args[] = {"-result", "-domainDir", domainDir, "-resultsDir", getAVKReportLocation()}; 225 Class cc = loader.loadClass("com.sun.enterprise.appverification.tools.ReportTool"); 226 227 Method method = cc.getMethod("main", new Class [] {args.getClass()}); 228 method.invoke(null, new Object [] { args }); 229 } 230 return true; 231 }catch(Exception ex){ 232 return false; 233 } 234 } 235 236 public File getAVKHome(){ 237 File f = getAVKJarLocation(); 238 if(f != null) 239 f = f.getParentFile().getParentFile(); 240 return f; 241 } 242 243 private File getAVKJarLocation(){ 244 File f = InstalledFileLocator.getDefault().locate("javke142/lib/javke.jar", null, true); 245 return f; 246 } 247 248 private String getAVKReportLocation(){ 249 String resultsDir = System.getProperty("netbeans.user") + File.separator + "avkreport"; return resultsDir; 251 } 252 253 public void stopServer(DeploymentManagerProperties dmProps, SunDeploymentManagerInterface sdm) throws Exception { 254 String asadminCmd = sdm.getPlatformRoot() + File.separator + 255 "bin" + 256 File.separator + 257 "asadmin"; 259 if (File.separator.equals("\\")) { 260 asadminCmd = asadminCmd + ".bat"; } 262 String args[]={asadminCmd, "stop-domain", 263 dmProps.getDomainName()}; 264 ViewLogAction.viewLog(sdm); 265 exec(args); 266 StringBuffer sb=new StringBuffer (); 267 for(int i=0;i<args.length;++i){ 268 sb.append(args[i]+ " "); 269 } 270 } 271 272 public void startServer(DeploymentManagerProperties dmProps, SunDeploymentManagerInterface sdm) throws Exception { 273 String asadminCmd = sdm.getPlatformRoot() + File.separator + 274 "bin" + 275 File.separator + 276 "asadmin"; 278 if (File.separator.equals("\\")) { 279 asadminCmd = asadminCmd + ".bat"; } 281 String args[]={asadminCmd, "start-domain", 282 dmProps.getDomainName()}; 283 ViewLogAction.viewLog(sdm); 284 exec(args); 285 StringBuffer sb=new StringBuffer (); 286 for(int i=0;i<args.length;++i){ 287 sb.append(args[i]+ " "); 288 } 289 } 290 291 private int exec(String [] arr) throws Exception { 292 final Process subProcess = Runtime.getRuntime().exec(arr); 293 new Thread (){ public void run(){ 296 try{ 297 BufferedReader br = new BufferedReader (new InputStreamReader (subProcess.getInputStream())); 298 String line=null; 299 while ( (line = br.readLine()) != null){ 300 } 302 } catch (IOException ioe) { 303 } 305 } }.start(); 307 new Thread (){ public void run(){ 310 try{ 311 BufferedReader br = new BufferedReader (new InputStreamReader (subProcess.getErrorStream())); 312 String line=null; 313 while ( (line = br.readLine()) != null) { 314 } 316 } catch (IOException ioe) { 317 } 319 } }.start(); 321 while(true){ 322 try{ 323 int exitValue=subProcess.waitFor(); 324 return exitValue; 326 }catch(InterruptedException e){ 327 } 328 } 329 } 330 331 public void editPolicyFile(DeploymentManagerProperties dmProps){ 332 System.setProperty("j2ee.appverification.home", getAVKHome().getAbsolutePath()); String policyFile = dmProps.getLocation() + File.separator + dmProps.getDomainName() + File.separator + "config" + File.separator + "server.policy"; 334 if(policyFile == null) 335 return; 336 try{ 337 DomainParser.backupFile(policyFile); 338 BufferedReader textIn = new BufferedReader (new FileReader (policyFile)); 339 String line; 340 boolean edited = false; 341 while ((line = textIn.readLine()) != null){ 342 if(line.indexOf("j2ee.appverification.home") != -1){ edited = true; 344 break; 345 } 346 347 } 348 textIn.close(); 349 if(! edited){ 350 PrintWriter textOut = new PrintWriter (new BufferedWriter (new FileWriter (policyFile, true))); 351 textOut.println("// permissions for avkit classes"); textOut.println("grant codeBase \"file:${j2ee.appverification.home}/-\" {"); textOut.println("permission java.security.AllPermission;"); textOut.println("};"); textOut.flush(); 356 textOut.close(); 357 } 358 }catch(Exception ex){ 359 DomainParser.restoreFile(policyFile); 360 } 361 } 362 363 public void editStoppedDomainConfig(DeploymentManagerProperties dmProps, boolean onOff){ 364 File avkHome = getAVKHome(); 365 String domainXmlLoc = getDomainConfigLoc(dmProps) + File.separator + "domain.xml"; 366 DomainParser.backupFile(domainXmlLoc); 367 boolean success = DomainParser.editSupportInDomain(domainXmlLoc, getAVKJarLocation().getAbsolutePath(), avkHome.getAbsolutePath(), onOff); 368 if(!success){ 369 DomainParser.restoreFile(domainXmlLoc); 370 } 371 } 372 373 private String getDomainConfigLoc(DeploymentManagerProperties dmProps){ 374 String configLoc = dmProps.getLocation() + File.separator + dmProps.getDomainName() + File.separator + "config"; return configLoc; 376 } 377 378 private void setTopManagerStatus(String msg){ 379 StatusDisplayer.getDefault().setStatusText(msg); 380 } 381 382 public boolean createAVKSupport(DeploymentManager dm, J2eeModuleProvider modProvider){ 383 AddAVKSupport support = new AddAVKSupport(); 384 return support.createAVKSupport(dm, modProvider); 385 } 386 387 } 388 | Popular Tags |