1 9 10 package org.netbeans.modules.j2ee.sun.ide.avk.actions; 11 12 import java.io.BufferedInputStream ; 13 import java.io.BufferedReader ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 import java.io.FileOutputStream ; 18 import java.io.IOException ; 19 import java.io.InputStreamReader ; 20 import java.lang.reflect.Method ; 21 import java.util.Enumeration ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 import java.util.Set ; 26 import javax.management.Attribute ; 27 import javax.management.MBeanServerConnection ; 28 import javax.management.ObjectName ; 29 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 30 import org.netbeans.modules.j2ee.sun.api.ExtendedClassLoader; 31 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager; 32 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface; 33 import org.netbeans.modules.j2ee.sun.api.SunURIManager; 34 import org.openide.awt.HtmlBrowser.URLDisplayer; 35 import org.openide.modules.InstalledFileLocator; 36 37 41 public class AdminTasks { 42 43 private SunDeploymentManagerInterface sdm; 44 private InstanceProperties instProps; 45 46 private static final String CALLFLOW_MBEAN_NAME = "amx:j2eeType=X-CallFlowMonitor,name=server,X-ServerRootMonitor=server"; 47 private static final String VERIFIER_MBEAN_NAME = "com.sun.enterprise.appverification.mbeans.AppVerification"; 48 private static final String VERIFIER_JAR_FILE = "javke.jar"; 49 50 private static final String CREATE_MBEAN_NAME = "com.sun.appserv:category=config,type=applications"; 51 private static final String CREATE_MBEAN_OPERATION_NAME = "createMBean"; 52 53 private static final String CREATE_RULE_MBEAN_NAME = "com.sun.appserv:type=management-rules,config=server-config,category=config"; 54 private static final String CREATE_RULE_OPERATION_NAME = "createManagementRule"; 55 56 public AdminTasks(SunDeploymentManagerInterface sdm) { 57 this.sdm = sdm; 58 this.instProps = getInstanceProperties(sdm); 59 printInstanaceProperties(); 60 } 61 62 private InstanceProperties getInstanceProperties(SunDeploymentManagerInterface sdm) { 63 InstanceProperties instProps = SunURIManager.getInstanceProperties(sdm.getPlatformRoot(), sdm.getHost(), sdm.getPort()); 64 if(instProps == null) { 65 try { 66 instProps = SunURIManager.createInstanceProperties( 67 sdm.getPlatformRoot(), 68 sdm.getHost(), 69 Integer.toString(sdm.getPort()), 70 sdm.getUserName(), 71 sdm.getPassword() , 72 sdm.getHost() + ":" + sdm.getPort()); 73 } catch(Exception ex) { 74 return null; 75 } 76 } 77 instProps.setProperty("httpportnumber", sdm.getNonAdminPortNumber()); 78 return instProps; 79 } 80 81 85 public boolean copyJar() { 86 File f = InstalledFileLocator.getDefault().locate("javke5/lib/javke.jar", null, true); 87 String targetFile = sdm.getPlatformRoot() + File.separator + "lib" + File.separator + VERIFIER_JAR_FILE; 88 AdminTasks.debug("javke.jar = " + f.getAbsoluteFile() + ", exists = " + f.exists()); 89 90 if(!f.exists()) return false; 91 try { 92 BufferedInputStream bis= new BufferedInputStream (new FileInputStream (f)); 93 ByteArrayOutputStream o = new ByteArrayOutputStream (); 94 try { 95 byte[] data = new byte[8*1024]; 96 int numOfBytesRead; 97 while((numOfBytesRead = bis.read(data,0,data.length)) != -1) o.write(data, 0, numOfBytesRead); 98 } catch(Exception ex) { } 100 FileOutputStream fo = new FileOutputStream (targetFile); 101 fo.write(o.toByteArray()); 102 fo.close(); 103 } catch(Exception ex) { 104 ex.printStackTrace(); 105 return false; 106 } 107 108 112 113 if(sdm.isRunning()) { 114 AdminTasks.debug("copyJar() - restarting server...."); 115 restartServer(); 116 } 117 return true; 118 } 119 120 123 public boolean createVerifierMBeanAndRule() { 124 125 createVerifierMBean(); 126 127 createVerifierRule(); 128 129 return true; 130 } 131 132 136 public boolean setup() { 137 140 copyJar(); 141 144 startServer(); 145 146 149 createVerifierMBeanAndRule(); 150 151 return true; 152 } 153 154 private boolean createVerifierMBean() { 155 Map <String ,String > params = new HashMap <String ,String >(); 156 params.put("impl-class-name", VERIFIER_MBEAN_NAME); 157 params.put("name", "AppVerification"); 158 159 Object [] paramsInfo = new Object [] {"server", params}; 160 String [] typesInfo = new String [] {String .class.getName(), Map .class.getName()}; 161 162 if (sdm == null) return false; 163 try { 164 MBeanServerConnection mbsc = sdm.getManagement().getMBeanServerConnection(); 165 mbsc.invoke(new ObjectName (CREATE_MBEAN_NAME), CREATE_MBEAN_OPERATION_NAME, paramsInfo, typesInfo); 166 } catch(Throwable t) { 167 t.printStackTrace(); 168 return false; 169 } 170 return true; 171 } 172 173 private boolean createVerifierRule() { 174 Properties p = new Properties (); 175 p.put("tracepoint","*"); 176 177 Object [] paramsInfo = new Object [] { 178 "app-verification", new Boolean (true), null, "trace", "FINEST", new Boolean (true), null, p, "AppVerification" }; 188 String [] typesInfo = new String [] { 189 String .class.getName(), 190 Boolean .class.getName(), 191 String .class.getName(), 192 String .class.getName(), 193 String .class.getName(), 194 Boolean .class.getName(), 195 String .class.getName(), 196 Properties .class.getName(), 197 String .class.getName() 198 }; 199 200 try { 201 MBeanServerConnection mbsc = sdm.getManagement().getMBeanServerConnection(); 202 mbsc.invoke(new ObjectName (CREATE_RULE_MBEAN_NAME), CREATE_RULE_OPERATION_NAME, paramsInfo, typesInfo); 203 } catch(Throwable t) { 204 t.printStackTrace(); 205 return false; 206 } 207 return true; 208 } 209 210 213 public boolean isSetup() { 214 File avkJar = new File (sdm.getPlatformRoot() + File.separator + "lib" + File.separator + VERIFIER_JAR_FILE); 215 return avkJar.exists(); 216 } 217 218 public boolean showReport(String resultsDir) { 219 File report = new File (resultsDir + File.separator + "results" + File.separator + "suiteSummary.html"); 220 try { 221 URLDisplayer.getDefault().showURL(report.toURI().toURL()); 222 } catch(Exception ex) { 223 ex.printStackTrace(); 224 } 225 return true; 226 } 227 228 public boolean generateReport(String resultsDir) { 229 235 if (sdm == null) return false; 236 File javkeInstallDir = InstalledFileLocator.getDefault().locate("javke5", null, true); 237 if(javkeInstallDir != null && javkeInstallDir.exists()) { 238 AdminTasks.debug("JAVKE_INSTALL_DIR = " + javkeInstallDir.getAbsolutePath()); 239 } 240 String domainDir = instProps.getProperty("LOCATION") + File.separator + instProps.getProperty("DOMAIN"); 241 242 245 try { 246 253 268 String fs = File.separator; 269 String ps = File.pathSeparator; 270 String commandArray[] = new String [] { 271 System.getProperty("java.home") + fs + "bin" + fs + "java", 272 "-classpath", 273 javkeInstallDir.getAbsolutePath() + fs + "lib" + fs + VERIFIER_JAR_FILE + ps + sdm.getPlatformRoot().getAbsolutePath() + fs + "lib" + fs + "appserv-rt.jar", 274 "-Dj2ee.appverification.home=" + javkeInstallDir.getAbsolutePath(), 275 "-Dcom.sun.aas.instanceRoot=" + domainDir, 276 "-DinputDir=" + resultsDir, 277 "-DoutputDir=" + resultsDir, 278 "com.sun.enterprise.appverification.report.ReportTool" 279 }; 280 exec(commandArray); 281 } catch(Throwable t) { 282 t.printStackTrace(); 283 } 284 285 return true; 286 287 } 288 289 public boolean storeIntrospectionResults(String deployDir, String resultsDir) { 290 296 if (sdm == null) return false; 297 File javkeInstallDir = InstalledFileLocator.getDefault().locate("javke5", null, true); 298 if(javkeInstallDir != null && javkeInstallDir.exists()) { 299 AdminTasks.debug("JAVKE_INSTALL_DIR = " + javkeInstallDir.getAbsolutePath()); 300 } 301 String domainDir = instProps.getProperty("LOCATION") + File.separator + instProps.getProperty("DOMAIN"); 302 303 306 try { 307 311 323 String fs = File.separator; 324 String ps = File.pathSeparator; 325 String [] commandArray = new String [] { 326 System.getProperty("java.home") + fs + "bin" + fs + "java", 327 "-classpath", 328 javkeInstallDir.getAbsolutePath() + fs + "lib" + fs + VERIFIER_JAR_FILE + ps + sdm.getPlatformRoot().getAbsolutePath() + fs + "lib" + fs + "appserv-rt.jar" + ps + sdm.getPlatformRoot().getAbsolutePath() + fs + "lib" + fs + "javaee.jar", 329 "-Dj2ee.appverification.home=" + javkeInstallDir.getAbsolutePath(), 330 "-Dcom.sun.aas.installRoot=" + sdm.getPlatformRoot().getAbsolutePath(), 331 "-Dcom.sun.aas.instanceRoot=" + domainDir, 332 "-DarchivePaths=" + deployDir, 333 "-DresultDir=" + resultsDir, 334 "com.sun.enterprise.appverification.introspector.Introspector" 335 }; 336 exec(commandArray); 337 } catch(Throwable t) { 338 t.printStackTrace(); 339 } 340 341 return true; 342 } 343 344 345 348 public boolean storeInstrumentationResults(String resultsDir, boolean cleanResults) { 349 if (sdm == null) return false; 350 try { 351 MBeanServerConnection mbsc = sdm.getManagement().getMBeanServerConnection(); 352 mbsc.invoke(lookupMBean(VERIFIER_MBEAN_NAME), "writeInstrumentationResults", new Object []{resultsDir, Boolean.valueOf(cleanResults)}, new String []{String .class.getName(), Boolean .class.getName()}); 353 } catch(Throwable t) { 354 t.printStackTrace(); 355 return false; 356 } 357 return true; 358 } 359 360 public boolean startAutoWriteInstrumentationResults(String resultsDir) { 361 if (sdm == null) return false; 362 try { 363 MBeanServerConnection mbsc = sdm.getManagement().getMBeanServerConnection(); 364 mbsc.invoke(lookupMBean(VERIFIER_MBEAN_NAME), "start", new Object []{resultsDir}, new String []{String .class.getName()}); 365 } catch(Throwable t) { 366 t.printStackTrace(); 367 return false; 368 } 369 return true; 370 } 371 372 public boolean stopAutoWriteInstrumentationResults() { 373 if (sdm == null) return false; 374 try { 375 MBeanServerConnection mbsc = sdm.getManagement().getMBeanServerConnection(); 376 mbsc.invoke(lookupMBean(VERIFIER_MBEAN_NAME), "stop", new Object []{}, new String []{}); 377 } catch(Throwable t) { 378 t.printStackTrace(); 379 return false; 380 } 381 return true; 382 } 383 384 private ObjectName lookupMBean(String objectNamePattern) throws Exception { 385 ObjectName objectName = null; 386 MBeanServerConnection mbsc = sdm.getManagement().getMBeanServerConnection(); 387 Set s = mbsc.queryNames(new ObjectName ("user*:*"), null); 388 for (Object o : s) { 389 String str = ((ObjectName ) o).toString(); 390 if (str.indexOf(objectNamePattern) != -1) { 391 objectName = (ObjectName ) o; 392 break; 393 } 394 } 395 return objectName; 396 } 397 398 private boolean setCallFlowStatus(boolean status) { 399 if (sdm == null) return false; 400 try { 401 MBeanServerConnection mbsc = sdm.getManagement().getMBeanServerConnection(); 402 mbsc.setAttribute( 403 new ObjectName (CALLFLOW_MBEAN_NAME), 404 new Attribute ("Enabled", status)); 405 } catch(Throwable t) { 406 t.printStackTrace(); 407 return false; 408 } 409 return true; 410 } 411 412 public boolean stopInstrumentation() { 413 if (!sdm.isRunning()) return true; 414 return setCallFlowStatus(false); 415 } 416 417 public boolean startInstrumentation(boolean startServer) { 418 if (startServer) startServer(); 419 return startInstrumentation(); 420 } 421 422 public boolean startInstrumentation() { 423 return setCallFlowStatus(true); 424 } 425 426 private boolean startServer() { 427 if(sdm.isRunning()) return true; 428 String [] command = new String [] { 429 sdm.getPlatformRoot() + File.separator + "bin" + File.separator + "asadmin", 430 "start-domain" 431 }; 432 try { 433 exec(command); 434 } catch(Exception ex) { 435 return false; 436 } 437 return true; 438 439 } 440 441 private boolean stopServer() { 442 if(!sdm.isRunning()) return true; 443 String [] command = new String [] { 444 sdm.getPlatformRoot() + File.separator + "bin" + File.separator + "asadmin", 445 "stop-domain" 446 }; 447 try { 448 exec(command); 449 } catch(Exception ex) { 450 return false; 451 } 452 return true; 453 454 } 455 456 private boolean restartServer() { 457 return stopServer() & startServer(); 458 } 459 460 private int exec(String [] commandString) throws Exception { 461 final Process subProcess; 462 if (commandString.length == 1) { 463 subProcess = Runtime.getRuntime().exec(commandString[0]); 464 } else { 465 subProcess = Runtime.getRuntime().exec(commandString); 466 } 467 468 new Thread (){ 469 public void run(){ 470 try{ 471 BufferedReader br = new BufferedReader (new InputStreamReader (subProcess.getInputStream())); 472 String str; 473 while ((str = br.readLine()) != null) { 474 AdminTasks.debug(str); 475 } 476 } catch (IOException ioe) { 477 ioe.printStackTrace(); 478 } 479 } 480 }.start(); 481 482 new Thread (){ 483 public void run(){ 484 try{ 485 BufferedReader br = new BufferedReader (new InputStreamReader (subProcess.getErrorStream())); 486 String str; 487 while ((str = br.readLine()) != null) { 488 AdminTasks.debug(str); 489 } 490 } catch (IOException ioe) { 491 ioe.printStackTrace(); 492 } 493 } }.start(); 495 496 return subProcess.waitFor(); 497 } 498 499 510 511 private void printInstanaceProperties() { 512 debug("Current instace properties : \n"); 513 Enumeration e = instProps.propertyNames(); 514 while(e.hasMoreElements()) { 515 String key = (String )e.nextElement(); 516 debug("key = " + key + ", value = " + instProps.getProperty(key)); 517 } 518 } 519 520 public static void debug(String msg) { 521 } 523 } 524 | Popular Tags |