1 23 package com.sun.enterprise.launcher; 24 25 import java.util.HashMap ; 26 import java.util.ArrayList ; 27 import java.util.StringTokenizer ; 28 import org.w3c.dom.*; 29 import java.io.File ; 30 import javax.xml.parsers.*; 31 import com.sun.org.apache.commons.launcher.LaunchFilter; 32 import com.sun.org.apache.commons.launcher.LaunchCommand; 33 import org.apache.tools.ant.BuildException; 34 35 import com.sun.enterprise.util.RelativePathResolver; 39 40 import com.sun.enterprise.util.ASenvPropertyReader; 41 import com.sun.enterprise.util.SystemPropertyConstants; 42 import com.sun.enterprise.util.OS; 43 44 45 52 53 public class PELaunchFilter implements LaunchFilter { 54 55 public static final String DEBUG_OPTIONS = "com.sun.aas.jdwpOptions"; 56 public static final String LOCALE = "locale"; 57 58 ArrayList jvmArgsList = null; 59 HashMap sysProps = null; 60 String serverModeOrClientMode = null; 61 62 63 public static String getDebugProperty(String debug_options, String name) { 64 int nameIndex; 65 if ( (nameIndex = debug_options.indexOf(name)) != -1 ) { 66 String value = debug_options.substring(nameIndex 68 + name.length() + 1); 69 int commaIndex; 70 if ( (commaIndex = value.indexOf(",")) != -1 ) { 71 value = value.substring(0, commaIndex); 72 } 73 return value; 74 } 75 return null; 76 } 77 78 79 public void filter(LaunchCommand command) throws BuildException { 80 81 82 try { 83 ASenvPropertyReader reader = new ASenvPropertyReader( 86 System.getProperty(SystemPropertyConstants.CONFIG_ROOT_PROPERTY)); 87 reader.setSystemProperties(); 88 89 serverModeOrClientMode = null; 91 92 boolean stopOperation = false; 93 boolean waitForChild = command.getWaitforchild(); 94 boolean debugOption = false; 95 96 ArrayList argsList = command.getArgs(); 97 String [] args =(String [])argsList.toArray(new String [argsList.size()]); 98 99 for ( int ar=0; ar < args.length; ar++ ) { 100 if ( "stop".equals( args[ar] ) ) { 101 stopOperation = true; 103 waitForChild = true; 104 debugOption = false; 105 break; 106 } 107 else if ( "debug".equals(args[ar]) ) { 108 debugOption = true; } 110 else if ( "verbose".equals(args[ar]) ) { 111 waitForChild = true; } 113 } 114 115 116 jvmArgsList = command.getJvmargs( ); 118 if ( jvmArgsList == null ) { 119 jvmArgsList = new ArrayList (); 120 } 121 122 if ( waitForChild ) { 123 jvmArgsList.add("-Dcom.sun.aas.verboseMode=true"); 125 } 126 127 sysProps = command.getSysproperties(); 129 if ( sysProps == null ) { 130 System.out.println("[FATAL ERROR] System properties with S1AS"+ 135 " installation values doesn't present" ); 136 System.out.println("Please check <installtionDir>/lib/launcher.xml" ); 137 System.out.println( "Exiting out"); 138 System.exit(1); 139 } 140 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 141 DocumentBuilder db = dbf.newDocumentBuilder(); 142 143 db.setEntityResolver((org.xml.sax.helpers.DefaultHandler )Class.forName 147 ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance()); 148 149 String instanceRoot =(String )sysProps.get("com.sun.aas.instanceRoot"); 150 String serverName = (String )sysProps.get("com.sun.aas.instanceName"); 151 if ( serverName == null ) { 154 serverName="server"; 155 } 156 157 String domainConfigFilePath = instanceRoot + File.separator + 158 "config" + File.separator + 159 "domain.xml"; 160 161 Document doc = db.parse(domainConfigFilePath ); 162 163 Element root = doc.getDocumentElement(); 164 165 handleSystemProperties(root); 168 169 if(root.hasAttribute(LOCALE)) { 171 String locale = root.getAttribute(LOCALE); 172 if(locale != null && !"".equals(locale)) { 173 sysProps.put(SystemPropertyConstants.DEFAULT_LOCALE_PROPERTY, locale); 174 } 175 } else { 176 sysProps.put(SystemPropertyConstants.DEFAULT_LOCALE_PROPERTY, 177 System.getProperty(SystemPropertyConstants.DEFAULT_LOCALE_PROPERTY)); 178 } 179 180 184 NodeList servers = root.getElementsByTagName("servers"); 185 186 if (servers.getLength() < 1) { 187 System.out.println("[FATAL ERROR] " + 188 " domain.xml doesn't have servers element"); 189 System.exit(1); 190 } 191 192 Element serversElement = (Element) servers.item(0); 193 194 NodeList server = serversElement.getElementsByTagName("server"); 195 196 if (server.getLength() < 1) { 197 System.out.println("[FATAL ERROR] " + 198 " domain.xml doesn't have server element"); 199 System.exit(1); 200 } 201 202 Element serverElement = null; 203 204 boolean foundMatchingServer = false; 205 for (int i=0; i<server.getLength(); i++) { 206 serverElement = (Element) server.item(i); 207 if ((serverElement.getAttribute("name")).equals(serverName)) { 208 foundMatchingServer = true; 209 break; 210 } 211 } 212 213 if (! foundMatchingServer) { 214 System.out.println("[FATAL ERROR] " + 215 " domain.xml doesn't have the server element named: " + serverName); 216 System.exit(1); 217 } 218 219 String configRef = serverElement.getAttribute("config-ref"); 224 225 sysProps.put(SystemPropertyConstants.CONFIG_NAME_PROPERTY , 227 configRef); 228 System.setProperty(SystemPropertyConstants.CONFIG_NAME_PROPERTY, 229 configRef); 230 231 NodeList logServices = getNodeList(root, configRef, "log-service"); 232 233 Element logServiceElement = null; 234 String logFileName = null; 235 String logToConsole = null; 236 237 for ( int ls=0; ls<logServices.getLength(); ls++ ) { 241 logServiceElement = (Element)logServices.item(ls); 242 logFileName = RelativePathResolver.resolvePath( 243 logServiceElement.getAttribute("file")); 244 logToConsole = logServiceElement.getAttribute("log-to-console"); 245 } 246 247 if( (logToConsole != null ) 248 &&(logToConsole.equals( "true" ) ) ) 249 { 250 waitForChild = true; 251 jvmArgsList.add("-Dcom.sun.aas.verboseMode=true"); 252 } else if ( logFileName != null ) { 253 command.setOutput( new File (logFileName) ); 254 } 255 command.setWaitforchild ( waitForChild ); 256 257 262 NodeList javaConfigs = getNodeList(root, configRef, "java-config"); 263 264 Element javaConfigElement = null; 266 String debug_enabled = null; 267 String debug_options = null; 268 String rmic_options = null; 269 String javac_options = null; 270 String java_home = null; 271 String classpath_prefix = null; 272 String server_classpath = null; 273 String classpath_suffix = null; 274 String native_library_path_prefix = null; 275 String native_library_path_suffix = null; 276 String bytecode_preprocessors = null; 277 String env_classpath_ignored = "true"; 278 279 for ( int jc=0; jc<javaConfigs.getLength(); jc++ ) { 283 javaConfigElement = (Element)javaConfigs.item(jc); 284 285 debug_enabled = javaConfigElement.getAttribute 286 ("debug-enabled"); 287 debug_options = javaConfigElement.getAttribute 288 ("debug-options"); 289 rmic_options = javaConfigElement.getAttribute 290 ("rmic-options"); 291 javac_options = javaConfigElement.getAttribute 292 ("javac-options"); 293 if (OS.isDarwin()) { 294 java_home = RelativePathResolver.resolvePath( 295 javaConfigElement.getAttribute("java-home")); 296 } else { 297 java_home = RelativePathResolver.resolvePath( 298 javaConfigElement.getAttribute("java-home") + 299 File.separator + "jre"); 300 } 301 classpath_prefix = RelativePathResolver.resolvePath( 302 javaConfigElement.getAttribute("classpath-prefix")); 303 server_classpath = RelativePathResolver.resolvePath( 304 javaConfigElement.getAttribute("server-classpath")); 305 classpath_suffix = RelativePathResolver.resolvePath( 306 javaConfigElement.getAttribute("classpath-suffix")); 307 native_library_path_prefix = javaConfigElement.getAttribute 308 ("native-library-path-prefix"); 309 native_library_path_suffix = javaConfigElement.getAttribute 310 ("native-library-path-suffix"); 311 bytecode_preprocessors = javaConfigElement.getAttribute 312 ("bytecode-preprocessors"); 313 env_classpath_ignored= javaConfigElement.getAttribute 314 ("env-classpath-ignored"); 315 } 316 if(java_home != null) 317 System.setProperty("java.home", java_home ); 318 String profilerClasspath = null; 319 String profilerNativeLibraryPath = null; 320 322 327 NodeList profilers = javaConfigElement.getElementsByTagName("profiler"); 328 329 int numberOfProfilerElements = profilers.getLength(); 330 if ( numberOfProfilerElements > 0 ) { 331 Element profilerElement = (Element)(profilers.item(0)); 334 String profilerEnabled=profilerElement.getAttribute("enabled"); 335 336 if ( (profilerEnabled != null ) && 337 ( profilerEnabled.equals("true") ) ) { 338 String profilerName=profilerElement.getAttribute("name"); 340 if ( profilerName == null ) { 341 System.err.println("ERROR : Profiler is enabled. But Name is null"); 343 System.err.println("Profiler settings are ignored"); 344 } else { 345 profilerClasspath =profilerElement.getAttribute( 346 "classpath"); 347 profilerNativeLibraryPath = 348 profilerElement.getAttribute("native-library-path"); 349 350 handleJvmOptions ( profilerElement ); 351 handleProperties ( profilerElement ); 352 353 } 354 355 356 } 357 358 359 } 360 361 String requestedClasspath = server_classpath; 362 363 String originalClasspath = command.getClasspath(); 365 366 if ( originalClasspath != null ) { 367 requestedClasspath = originalClasspath + File.pathSeparator + 368 requestedClasspath; 369 } 370 371 if (( classpath_prefix != null ) && 372 (!classpath_prefix.trim().equals("")) ) { 373 requestedClasspath = classpath_prefix + File.pathSeparator + 374 requestedClasspath; 375 } 376 if (( classpath_suffix != null ) && 377 (!classpath_suffix.trim().equals("")) ) { 378 requestedClasspath = requestedClasspath + File.pathSeparator + 379 classpath_suffix ; 380 } 381 382 if (( profilerClasspath != null)&& 383 (!profilerClasspath.trim().equals("")) ) { 384 requestedClasspath = requestedClasspath + File.pathSeparator + 385 profilerClasspath; 386 } 387 388 389 String envClasspath = (String )sysProps.get("user.classpath"); 390 sysProps.remove("user.classpath"); 391 392 if ( env_classpath_ignored != null && 393 env_classpath_ignored.equals("false") && 394 (!envClasspath.equals("${env.CLASSPATH}") )) { 395 requestedClasspath = requestedClasspath + File.pathSeparator + 396 envClasspath; 397 } 398 399 command.setClasspath ( requestedClasspath ); 400 401 handleJvmOptions ( javaConfigElement ); 402 403 if ( debug_enabled != null && debug_enabled.equals("true") ) { 406 debugOption = true; 407 } 408 if ( debugOption && !stopOperation ) { 409 410 jvmArgsList.add("-Xdebug"); 412 413 debug_options=debug_options.trim(); 417 if ( debug_options.startsWith("-Xdebug") ) { 418 debug_options =debug_options.substring("-Xdebug".length()).trim(); 419 } 420 421 437 String transport = getDebugProperty(debug_options, "transport"); 438 String addr = getDebugProperty(debug_options, "address"); 439 440 if ( transport == null || transport.equals("") ) { 441 throw new BuildException("Cannot start server in debug mode: no transport specified in debug-options in domain.xml."); 443 } 444 445 if ( transport.equals("dt_socket") ) { 446 if ( addr != null && !addr.equals("") ) { 447 } 450 else { 451 int port = 453 com.sun.enterprise.util.net.NetUtils.getFreePort(); 454 if ( port == 0 ) { 455 throw new BuildException("Cannot start server in debug mode: unable to obtain a free port for transport dt_socket."); 457 } 458 addr = String.valueOf(port); 459 460 debug_options = debug_options + ",address=" + addr; 461 } 462 } 463 464 jvmArgsList.add(debug_options); 465 466 String jdwpOptions = debug_options.substring( 470 debug_options.indexOf("-Xrunjdwp:") + "-Xrunjdwp:".length()); 471 472 jvmArgsList.add("-D" + DEBUG_OPTIONS + "=" + jdwpOptions); 473 } 474 475 477 String javaLibPath = System.getProperty("java.library.path"); 478 479 if ( (native_library_path_prefix != null ) && 480 (! native_library_path_prefix.trim().equals("") ) ) { 481 if (javaLibPath != null ) { 482 javaLibPath =native_library_path_prefix + 483 File.pathSeparator + javaLibPath; 484 } else { 485 javaLibPath = native_library_path_prefix; 486 } 487 } 488 if ( (native_library_path_suffix != null ) && 489 (! native_library_path_suffix.trim().equals("") ) ) { 490 if (javaLibPath != null ) { 491 javaLibPath =javaLibPath + File.pathSeparator + 492 native_library_path_suffix ; 493 } else { 494 javaLibPath = native_library_path_suffix; 495 } 496 } 497 if (( profilerNativeLibraryPath != null ) && 498 ( !profilerNativeLibraryPath.trim().equals("") ) ) { 499 if (javaLibPath != null ) { 500 javaLibPath =javaLibPath + File.pathSeparator + 501 profilerNativeLibraryPath ; 502 } else { 503 javaLibPath = profilerNativeLibraryPath; 504 } 505 } 506 507 512 javaLibPath = normalize(javaLibPath); 513 sysProps.put("java.library.path" , javaLibPath); 514 System.setProperty("java.library.path", javaLibPath ); 515 516 520 if ( serverModeOrClientMode != null ) { 521 jvmArgsList.add( 0, "-" + serverModeOrClientMode ); 522 } 523 524 handleSystemProperties(getConfigElement(root, configRef)); 527 handleSystemProperties(serverElement); 534 540 541 command.setSysproperties(sysProps); 542 } catch(Exception e) { 543 e.printStackTrace(); 544 throw new BuildException(); 545 } 546 } 547 548 553 private String normalize(String path) { 554 path = path.replaceAll("\"",""); 555 path = path.replaceAll("\'",""); 556 return path; 557 } 558 559 private void handleJvmOptions ( Element element ) { 560 NodeList jvmoptions = element.getChildNodes(); 561 Element tmpElement = null; 562 Node tmpNode = null; 563 String systemProperty = null; 564 String property = null; 565 String value = null; 566 for (int i = 0; i < jvmoptions.getLength(); i++) { 567 Node varNode = jvmoptions.item(i); 568 if ( ( varNode.getNodeType()!= Node.ELEMENT_NODE ) || 569 (!varNode.getNodeName().equals("jvm-options") ) ) { 570 continue; 571 } 572 tmpElement = (Element) varNode; 573 tmpNode = tmpElement.getFirstChild(); 574 if ( tmpNode == null ) { 576 continue; 577 } 578 579 if(tmpNode instanceof Text) { 582 if(tmpNode.getNodeValue().equals("")) { 583 continue; 584 } 585 } 586 systemProperty = (tmpNode.getNodeValue()).trim(); 587 588 StringTokenizer stk = new StringTokenizer (systemProperty,"-"); 592 String currentToken = null; 593 String option = null; 594 595 int tokenCount = 0; 596 int numOfTokens = stk.countTokens(); 597 598 while ( stk.hasMoreTokens() ) { 599 if ( currentToken != null ) { 600 currentToken = currentToken + "-" + stk.nextToken(); 601 } else { 602 currentToken = stk.nextToken(); 603 } 604 tokenCount++; 605 606 if ( ! currentToken.endsWith(" " ) ) { 607 if ( tokenCount < numOfTokens ) { 610 continue; 611 } 612 } 613 614 option = currentToken.trim(); 615 currentToken = null; 616 617 if ( option.startsWith("D") ) { 618 option = option.substring(1, option.length() ); 619 620 int indexOfEqual= option.indexOf("="); 621 if ( indexOfEqual > 0 ) { 622 property = option.substring(0,indexOfEqual ); 623 value = option.substring(indexOfEqual+1 ); 624 sysProps.put(property, 625 RelativePathResolver.resolvePath(value)); 626 } else { 627 property = option; 629 jvmArgsList.add( "-D" + property ); 630 } 631 } else { 632 if ( ( option.equals("client")) || 633 (option.equals("server")) ) { 634 serverModeOrClientMode = option; 639 } else { 640 jvmArgsList.add( "-" + option ); 641 } 642 } 643 } 644 645 } 646 647 } 648 649 private void handleSystemProperties ( Element element ) { 650 handleProperties(element, "system-property"); 651 } 652 653 private void handleProperties ( Element element ) { 654 handleProperties(element, "property"); 655 } 656 657 private void handleProperties ( Element element, String elementName ) { 658 NodeList properties = element.getChildNodes(); 659 Element propertyElement = null; 660 Node tmpNode = null; 661 String systemProperty = null; 662 String property = null; 663 String value = null; 664 for (int i = 0; i < properties.getLength(); i++) { 665 Node varNode = properties.item(i); 666 if ( ( varNode.getNodeType()!= Node.ELEMENT_NODE ) || 667 (!varNode.getNodeName().equals(elementName) ) ) { 668 continue; 669 } 670 propertyElement = (Element)varNode; 671 672 String propertyName = propertyElement.getAttribute("name"); 673 String propertyValue = propertyElement.getAttribute("value"); 674 675 sysProps.put(propertyName, propertyValue); 676 } 677 } 678 679 private Element getConfigElement(Element root, String configRef) 680 { 681 Element configElement = null; 682 NodeList configs = root.getElementsByTagName("configs"); 683 if (configs.getLength() < 1) { 685 System.out.println("[FATAL ERROR] " + 686 " domain.xml doesn't have configs element"); 687 System.exit(1); 688 } 689 690 Element configsElement = (Element) configs.item(0); 691 NodeList config = configsElement.getElementsByTagName("config"); 692 if (config.getLength() < 1) { 693 System.out.println("[FATAL ERROR] " + 694 " domain.xml doesn't have config element"); 695 System.exit(1); 696 } 697 698 for (int j=0; j < config.getLength(); j++) { 699 configElement = (Element) config.item(j); 700 if ((configElement.getAttribute("name")).equals(configRef)) { 701 return configElement; 702 } 703 } 704 705 System.out.println("[FATAL ERROR] " + 708 " domain.xml doesn't have config element matching " + configRef); 709 System.exit(1); 710 return null; 711 } 712 713 private NodeList getNodeList(Element root, String configRef, String nlName) { 714 Element configElement = getConfigElement(root, configRef); 715 return configElement.getElementsByTagName(nlName); 716 } 717 718 } 719 | Popular Tags |