1 23 24 25 package com.sun.enterprise.tools.launcher; 26 27 import java.io.FileWriter ; 28 import java.io.File ; 29 import java.io.FilenameFilter ; 30 import java.io.IOException ; 31 import java.io.BufferedReader ; 32 import java.io.InputStreamReader ; 33 import java.io.InputStream ; 34 import java.io.OutputStream ; 35 import java.io.ByteArrayOutputStream ; 36 import java.io.OutputStreamWriter ; 37 import java.io.PrintWriter ; 38 import java.io.PrintStream ; 39 import java.io.FileOutputStream ; 40 import java.io.FileNotFoundException ; 41 import java.io.BufferedWriter ; 42 import java.io.PushbackInputStream ; 43 import java.lang.InterruptedException ; 44 import java.util.Iterator ; 45 import java.util.StringTokenizer ; 46 import java.util.ArrayList ; 47 import java.util.Properties ; 48 import java.util.regex.Pattern ; 49 import java.util.logging.Logger ; 50 import java.util.logging.Level ; 51 import java.util.logging.FileHandler ; 52 import java.util.logging.Handler ; 53 import java.util.logging.SimpleFormatter ; 54 import java.util.List ; 55 import java.util.ArrayList ; 56 import javax.xml.parsers.DocumentBuilder ; 57 import javax.xml.parsers.DocumentBuilderFactory ; 58 import org.w3c.dom.Document ; 59 import org.w3c.dom.Node ; 60 import org.w3c.dom.Element ; 61 import org.w3c.dom.NodeList ; 62 import org.xml.sax.SAXException ; 63 import javax.xml.parsers.ParserConfigurationException ; 64 65 import com.sun.logging.LogDomains; 66 import com.sun.enterprise.util.OS; 67 import com.sun.enterprise.util.RelativePathResolver; 68 import com.sun.enterprise.util.ASenvPropertyReader; 69 import com.sun.enterprise.util.SystemPropertyConstants; 70 import com.sun.enterprise.util.ProcessExecutor; 71 import com.sun.enterprise.util.i18n.StringManager; 72 import com.sun.enterprise.config.ConfigContext; 73 import com.sun.enterprise.config.ConfigException; 74 import com.sun.enterprise.config.ConfigFactory; 75 import com.sun.enterprise.config.ConfigBean; 76 import com.sun.enterprise.config.serverbeans.Config; 77 import com.sun.enterprise.config.serverbeans.Domain; 78 import com.sun.enterprise.config.serverbeans.Server; 79 import com.sun.enterprise.config.serverbeans.NodeAgent; 80 import com.sun.enterprise.config.serverbeans.Cluster; 81 import com.sun.enterprise.config.serverbeans.JavaConfig; 82 import com.sun.enterprise.config.serverbeans.LogService; 83 import com.sun.enterprise.config.serverbeans.Profiler; 84 import com.sun.enterprise.config.serverbeans.ElementProperty; 85 import com.sun.enterprise.config.serverbeans.SystemProperty; 86 import com.sun.enterprise.config.serverbeans.ConfigAPIHelper; 87 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 88 import com.sun.enterprise.config.serverbeans.ServerHelper; 89 import com.sun.enterprise.config.serverbeans.NodeAgentHelper; 90 import com.sun.enterprise.config.serverbeans.ClusterHelper; 91 import com.sun.enterprise.cli.framework.CliUtil; 92 import com.sun.enterprise.security.store.IdentityManager; 93 import com.sun.enterprise.admin.util.JvmOptionsHelper; 94 95 112 113 public class ProcessLauncher { 114 115 protected static final String RELATIVE_LOCATION_DOMAIN_XML="/config/domain.xml"; 116 protected static final String CLASSPATH_ENV_NAME="CLASSPATH"; 117 protected static final String JAVA_HOME_PROPERTY="JAVA_HOME"; 118 protected static final String LAUNCHER_PROFILE_NAME="com.sun.aas.processName"; 119 protected static final String LAUNCHER_RETURN_FUNCTION="com.sun.aas.launcherReturn"; 120 protected static final String LAUNCHER_RETURN_FUNCTION_WAIT="hold"; 121 protected static final String LAUNCHER_SCRIPT_LOCATION="bin"; 122 protected static final String LAUNCHER_START_ACTION="start"; 123 protected static final String LAUNCHER_STOP_ACTION="stop"; 124 protected static final String INTERNAL_SERVER_PROFILE="s1as8-server"; 125 protected static final String AS9_INTERNAL_SERVER_PROFILE="as9-server"; 126 protected static final String INTERNAL_NODE_AGENT_PROFILE="s1as8-nodeagent"; 127 protected static final int SLEEP_TIME_FOR_PROCESS_START=2000; 128 protected static final String COMMAND_LINE_ARG_VERBOSE="verbose"; 129 protected static final String COMMAND_LINE_ARG_DEBUG="debug"; 130 protected static final String COMMAND_LINE_ARG_DISPLAY="display"; 131 protected static final String COMMAND_LINE_ARG_NATIVE="native"; 132 protected static final char COMMAND_DELIMITER_LIST[] = {'_','+','^','@','!','?','(',')','~','`','{','}'}; 133 134 public static final String DEBUG_OPTIONS = "com.sun.aas.jdwpOptions"; 135 public static final String VERBOSE_SYSTEM_PROPERTY = "com.sun.aas.verboseMode"; 136 public static final String LOGFILE_SYSTEM_PROPERTY = "com.sun.aas.defaultLogFile"; 137 public static final String PROPMPT_FOR_IDENTITY_SYSTEM_PROPERTY = "com.sun.aas.promptForIdentity"; 138 public static final String SPARC = "sparc"; 139 public static final String SPARCV9 = "sparcv9"; 140 public static final String X86 = "x86"; 141 public static final String AMD64 = "amd64"; 142 public static final String JVM_OPTION_FOR_64BIT = "-d64"; 143 144 private static final String CLASSPATH_PREFIX_PROPERTY = "com.sun.aas.ClassPathPrefix"; 145 private static final String CLASSPATH_SUFFIX_PROPERTY = "com.sun.aas.ClassPathSuffix"; 146 private static final String SERVER_CLASSPATH_PROPERTY = "com.sun.aas.ServerClassPath"; 147 148 149 150 private Logger _logger = null; 151 private String [] _args=null; 152 private static boolean bDebug=false; 153 154 155 public static void main(String [] args) { 156 ProcessLauncher pl=new ProcessLauncher(); 157 158 pl.process(args); 159 } 160 161 162 169 public static void bootstrap(String [] args) { 170 ProcessLauncher pl=new ProcessLauncher(); 171 172 String profile="s1as-deploytool"; 174 if (args != null && args.length >= 1) { 175 profile=args[0]; 177 178 String [] newArgs=new String [args.length - 1]; 180 for (int ii=0; ii < newArgs.length; ii++) { 181 newArgs[ii]=args[ii + 1]; 182 } 183 184 args=newArgs; 186 } 187 188 System.setProperty(LAUNCHER_PROFILE_NAME, profile); 190 191 if (bDebug) System.out.println("bootstrapping profile - " + profile); 192 193 pl.process(args); 195 } 196 197 198 202 public void process(String [] args) { 203 int iRet=1; 204 try { 205 206 setArgs(args); 208 209 if (System.getProperty("Debug") != null) { 211 bDebug=true; 215 } 216 217 if (isVerboseEnabled() && !isDisplayEnabled()) { 221 System.setProperty(VERBOSE_SYSTEM_PROPERTY, "true"); 223 } 224 225 String passArg=null; 226 if (args != null && args.length >= 1) { 227 passArg=args[0]; 228 } else { 229 passArg=LAUNCHER_START_ACTION; 230 } 231 232 if (bDebug) System.out.println("ProcessLauncher Building command .."); 234 Command command=buildCommand(passArg); 235 236 if (bDebug) System.out.println("ProcessLauncher Executing command .."); 238 executeCommand(command, passArg); 239 240 iRet=0; 242 } catch (ConfigException ce) { 243 getLogger().log(Level.SEVERE, "launcher.config_exception", ce); 245 ce.printStackTrace(); 246 } catch (Exception e) { 247 getLogger().log(Level.SEVERE, "enterprise.launcher_exception_startup",e); 249 e.printStackTrace(); 250 } 251 System.exit(iRet); 252 } 253 254 255 260 public void executeCommand(Command command, String action) throws IOException { 261 262 String [] cmd=null; 263 264 if (isDisplayEnabled()) { 269 270 271 command.removeArg(COMMAND_LINE_ARG_DISPLAY); 274 command.removeArg(COMMAND_LINE_ARG_NATIVE); 275 276 String launcherRet=System.getProperty(LAUNCHER_RETURN_FUNCTION); 279 if (launcherRet != null ) { 280 if (bDebug) System.out.println("-D" + LAUNCHER_RETURN_FUNCTION + "=" + launcherRet); 281 command.addSystemVariable("-D" + LAUNCHER_RETURN_FUNCTION + "=" + launcherRet); 282 } 283 284 cmd=command.getCommandInJNIFormatAsArray(); 291 292 boolean found=false; 294 String sxDelim="|"; 295 for(int ii=0; ii < COMMAND_DELIMITER_LIST.length; ii++) { 296 found=false; 298 for(int jj=0; jj < cmd.length; jj++) { 300 if(cmd[jj].indexOf(COMMAND_DELIMITER_LIST[ii]) < 0) { 301 found=true; 303 break; 304 } 305 } 306 307 if (!found) { 309 sxDelim=String.valueOf(COMMAND_DELIMITER_LIST[ii]); 311 break; 312 } 313 } 314 315 System.out.print("STARTOFCOMMAND" + sxDelim); 321 for(int ii=0; ii < cmd.length; ii++) { 322 System.out.print(cmd[ii] + sxDelim); 323 } 324 System.out.print("ENDOFCOMMAND" + sxDelim); 325 326 } else { executeBackgroundCommand(command, isVerboseEnabled(), action); 328 } 329 330 } 331 332 333 protected void preBuildProcessing() { 334 } 337 338 339 public void executeBackgroundCommand(Command command, boolean verbose, String action) throws IOException { 340 341 getLogger().log(Level.FINE, "ProcessLauncher: executing Runtime execute..."); 344 345 String [] cmd=command.getCommandAsArray(); 347 Process process=Runtime.getRuntime().exec(cmd); 348 349 350 if (System.getProperty(PROPMPT_FOR_IDENTITY_SYSTEM_PROPERTY) != null && action.equals(LAUNCHER_START_ACTION)) { 352 sendInputToProcessInput(System.in, process); 353 } 354 355 StreamFlusher sfErr=new StreamFlusher(process.getErrorStream(), System.err, command.getLogFile()); 357 sfErr.start(); 358 359 if (verbose || isWaitEnabled()) { 360 363 StreamFlusher sfOut=new StreamFlusher(process.getInputStream(), System.out, command.getLogFile()); 365 sfOut.start(); 366 367 try { 368 process.waitFor(); 369 sfOut.join(); 370 sfErr.join(); 371 } catch (InterruptedException ie) { 372 System.out.println("While waiting in verbose mode, an InterruptedException was thrown "); 374 } 375 } else { 376 377 StreamFlusher sfOut=new StreamFlusher(process.getInputStream(), System.out); 379 sfOut.start(); 380 381 String logFile=command.getLogFile(); 383 if (logFile != null) { 384 System.out.println(StringManager.getManager(ProcessLauncher.class).getString("launcher.redirecting.output", 385 logFile)); 386 } 387 388 try { 391 Thread.currentThread().sleep(SLEEP_TIME_FOR_PROCESS_START); 392 } catch (InterruptedException ie) {} 393 } 394 } 395 396 private void sendInputToProcessInput(InputStream in, Process subProcess) { 397 if (in == null || subProcess == null) return; 399 400 PrintWriter out=null; 401 BufferedReader br=null; 402 try { 403 out = new PrintWriter (new BufferedWriter ( 405 new OutputStreamWriter (subProcess.getOutputStream()))); 406 407 br=new BufferedReader (new InputStreamReader (System.in)); 409 String sxLine=null; 410 while ((sxLine=br.readLine()) != null) { 411 out.println(sxLine); 413 if (bDebug) System.out.println("Feeding in Line:" + sxLine); 414 } 415 out.flush(); 416 } catch (Exception e) { 417 getLogger().log(Level.INFO,"WRITE TO INPUT ERROR", e); 418 } finally { 419 try { 420 if (out != null) out.close(); 421 } catch (Throwable t) {} 422 } 423 } 424 425 public Command buildCommand(String action) throws ConfigException { 426 427 preBuildProcessing(); 429 String processName=System.getProperty(LAUNCHER_PROFILE_NAME, INTERNAL_SERVER_PROFILE); 430 431 Command command=null; 433 if (isServerProfile()) { 434 command=buildInternalCommand(action); 435 } else { 436 command=buildExternalCommand(action); 437 } 438 439 String finalCommand=command.toStringWithLines(); 441 getLogger().log(Level.INFO, finalCommand); 442 443 return command; 444 } 445 446 447 451 public Command buildInternalCommand(String action) throws ConfigException { 452 453 StringManager _strMgr=StringManager.getManager(ProcessLauncher.class); 454 455 String domainXMLLocation=System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY) 457 + RELATIVE_LOCATION_DOMAIN_XML; 458 ConfigContext configCtxt=ConfigFactory.createConfigContext(domainXMLLocation); 459 Domain domain=ConfigAPIHelper.getDomainConfigBean(configCtxt); 460 String serverName=System.getProperty(SystemPropertyConstants.SERVER_NAME); 462 Server server=ServerHelper.getServerByName(configCtxt, serverName); 463 String configRef=server.getConfigRef(); 464 465 Command command=new Command(); 467 468 474 Config config=ServerHelper.getConfigForServer(configCtxt, serverName); 476 477 String logFile=configureLogService(config); 479 if (bDebug) System.out.println("LOGFILE = " + logFile); 480 481 createFileStructure(logFile); 483 command.setLogFile(logFile); 484 485 489 command.addSystemVariable("-D" + LOGFILE_SYSTEM_PROPERTY + "=" + logFile); 491 492 getLogger().log(Level.FINE,"Retrieved domain.xml from " + domainXMLLocation); 493 getLogger().log(Level.FINE,"Start building the command the to execute."); 494 495 ASenvPropertyReader reader = new ASenvPropertyReader(System.getProperty(SystemPropertyConstants.CONFIG_ROOT_PROPERTY)); 498 reader.setSystemProperties(); 499 500 if (isVerboseEnabled()) { 502 command.addSystemVariable("-D" + VERBOSE_SYSTEM_PROPERTY + "=true"); 503 System.setProperty(VERBOSE_SYSTEM_PROPERTY, "true"); 505 } 506 507 String launcherConfigFile=System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY) 509 + File.separator + "lib" + File.separator + "processLauncher.xml"; 510 511 String processName=System.getProperty(LAUNCHER_PROFILE_NAME, INTERNAL_SERVER_PROFILE); 512 getLogger().log(Level.FINE,"Loading ProcessLauncher config from: " + launcherConfigFile 513 + " - for the process named: " + processName); 514 515 ProcessLauncherConfig plConfig=new ProcessLauncherConfig(launcherConfigFile, processName); 516 517 Properties systemProperties=plConfig.getSystemProperties(); 519 520 addSystemProperties(domain.getSystemProperty(), systemProperties); 523 524 System.setProperty(SystemPropertyConstants.CONFIG_NAME_PROPERTY, configRef); 526 systemProperties.put(SystemPropertyConstants.CONFIG_NAME_PROPERTY, configRef); 527 528 JavaConfig javaConfig=config.getJavaConfig(); 530 531 String jvmCmd=javaConfig.getJavaHome() + File.separator + "bin" 533 + File.separator + "java"; 534 535 command.setJavaCommand(jvmCmd); 536 537 538 544 Profiler profiler=javaConfig.getProfiler(); 545 String profilerClasspath=null; 546 547 if (!action.equals(LAUNCHER_STOP_ACTION)) { 548 549 555 559 if ((javaConfig.isDebugEnabled() || isDebugEnabled())) { 561 addDebugOptions(command, javaConfig.getDebugOptions()); 563 } 564 565 if (profiler != null && profiler.isEnabled()) { 567 addElementProperties(profiler.getElementProperty(), systemProperties); 569 String [] jvmOptions=profiler.getJvmOptions(); 570 addJvmOptions(command, jvmOptions); 571 profilerClasspath=profiler.getClasspath(); 572 } 573 } 574 575 String locale=domain.getLocale(); 577 if (locale == null || locale.equals("")) { 578 locale=System.getProperty(SystemPropertyConstants.DEFAULT_LOCALE_PROPERTY); 580 } 581 if (locale != null && !locale.equals("")) { 583 command.addSystemVariable("-D" + SystemPropertyConstants.DEFAULT_LOCALE_PROPERTY + "=" + locale); 584 } 585 586 String [] jvmOptions=javaConfig.getJvmOptions(); 589 590 if (LAUNCHER_STOP_ACTION.equals(action) && (jvmOptions != null)) { 595 try { 596 jvmOptions = (new JvmOptionsHelper(jvmOptions)).getJvmOptions(); 597 } catch (Exception e) { 598 if ((getLogger()) != null) { 599 getLogger().log(Level.SEVERE, "launcher.jvmoptions_exception", e); 600 } 601 e.printStackTrace(); 602 } 603 jvmOptions = removeJVMStopOptions(jvmOptions); 604 } 605 606 addJvmOptions(command, jvmOptions); 607 608 addSystemProperties(config.getSystemProperty(), systemProperties); 611 612 if (ServerHelper.isServerClustered(configCtxt, server)) { 615 Cluster cluster = ClusterHelper.getClusterForInstance(configCtxt, 616 server.getName()); 617 addSystemProperties(cluster.getSystemProperty(), systemProperties); 618 } 619 620 addSystemProperties(server.getSystemProperty(), systemProperties); 623 624 if(OS.isWindows()) { 629 jvmCmd=jvmCmd.replace('/', '\\'); 631 } 632 633 if (jvmCmd.startsWith(System.getProperty(SystemPropertyConstants.JAVA_ROOT_PROPERTY))) { 634 jvmCmd=null; 636 } 637 String classpath=deriveClasspath(plConfig, jvmCmd, javaConfig, profilerClasspath); 638 getLogger().log(Level.FINE, "Complete process classpath = " + classpath); 639 command.setClasspath(classpath); 640 641 command.setMainClass(plConfig.getMainClass()); 644 645 deriveNativeClasspath(command, javaConfig, profiler, systemProperties); 648 649 Iterator it=systemProperties.keySet().iterator(); 652 String key=null; 653 String property=null, value=null;; 654 while(it.hasNext()) { 655 key=(String )it.next(); 656 value=systemProperties.getProperty(key); 657 658 if (key.startsWith("-")) { 660 property = key; 661 if (value != null && !value.equals("")) { 662 property += "=" + value; 663 } 664 command.addJvmOption(property); 665 getLogger().log(Level.FINE, "JVM Option: " + property); 666 } else { 667 property = "-D" + key + "=" + value; 669 command.addSystemVariable(property); 670 getLogger().log(Level.FINE, "System Property: " + property); 671 } 672 } 673 674 if (getProcessLauncherProfile().equals(AS9_INTERNAL_SERVER_PROFILE)) { 676 String classpathPrefix=javaConfig.getClasspathPrefix(); 677 String classpathSuffix=javaConfig.getClasspathSuffix(); 678 String serverClasspath=javaConfig.getServerClasspath(); 679 getLogger().log(Level.FINE, " prefix :: " + classpathPrefix 680 + " suffix :: " + classpathSuffix); 681 if (classpathPrefix == null) classpathPrefix = ""; 682 command.addSystemVariable("-D" + CLASSPATH_PREFIX_PROPERTY + "=" + classpathPrefix); 683 if (classpathSuffix == null) classpathSuffix = ""; 684 command.addSystemVariable("-D" + CLASSPATH_SUFFIX_PROPERTY + "=" + classpathSuffix); 685 if (serverClasspath == null) serverClasspath = ""; 686 command.addSystemVariable("-D" + SERVER_CLASSPATH_PROPERTY + "=" + serverClasspath); 687 } 688 689 690 String [] args=getArgs(); 693 for(int ii=0; ii < args.length; ii++) { 694 command.addArg(args[ii]); 695 } 696 return command; 697 } 698 699 700 704 public Command buildExternalCommand(String action) throws ConfigException { 705 706 Command command=new Command(); 708 709 getLogger().log(Level.FINE,"Start building the command the to execute."); 710 711 String launcherConfigFile=System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY) 713 + File.separator + "lib" + File.separator + "processLauncher.xml"; 714 715 String processName=System.getProperty(LAUNCHER_PROFILE_NAME, "s1as8-nodeagent"); 716 getLogger().log(Level.FINE,"Loading ProcessLauncher config from: " + launcherConfigFile + 717 " - for the process named: " + processName); 718 719 String configRoot=System.getProperty(SystemPropertyConstants.CONFIG_ROOT_PROPERTY); 721 if (configRoot == null) { 722 configRoot=System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY) + File.separator + "config"; 724 } 725 ASenvPropertyReader reader = new ASenvPropertyReader(configRoot); 728 reader.setSystemProperties(); 729 730 if (isVerboseEnabled()) { 732 command.addSystemVariable("-D" + VERBOSE_SYSTEM_PROPERTY + "=true"); 733 System.setProperty(VERBOSE_SYSTEM_PROPERTY, "true"); 735 } 736 737 ProcessLauncherConfig plConfig=new ProcessLauncherConfig(launcherConfigFile, processName); 739 740 Properties systemProperties=plConfig.getSystemProperties(); 742 743 String logFile=systemProperties.getProperty(LOGFILE_SYSTEM_PROPERTY); 745 if(bDebug) System.out.println("Is external command nodeagent - " + isNodeAgentProfile()); 746 if (isNodeAgentProfile()) { 747 getLogger().log(Level.FINE,"BuildExternalCommand for NodeAgent"); 749 750 try { 751 String domainXMLLocation=System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY) 753 + RELATIVE_LOCATION_DOMAIN_XML; 754 ConfigContext configCtxt=ConfigFactory.createConfigContext(domainXMLLocation); 755 Domain domain=ConfigAPIHelper.getDomainConfigBean(configCtxt); 756 757 String nodeAgentName=System.getProperty(SystemPropertyConstants.SERVER_NAME); 759 NodeAgent nodeAgent=NodeAgentHelper.getNodeAgentByName(configCtxt, nodeAgentName); 760 LogService logService=nodeAgent.getLogService(); 761 762 if(logService != null) { 763 getLogger().log(Level.FINE, "LogService found for nodeagent"); 764 String logFileX=logService.getFile(); 766 767 if (logFileX !=null) { 768 logFile=logFileX; 769 systemProperties.setProperty(LOGFILE_SYSTEM_PROPERTY, logFile); 771 } 772 773 String logLevel=logService.getModuleLogLevels().getNodeAgent(); 775 getLogger().setLevel(Level.parse(logLevel)); 776 } 777 } catch (ConfigException ce) { 778 getLogger().log(Level.FINE,"domain.xml does not exist yet for the nodeagent"); 780 } 781 } 782 783 if (logFile != null) { 785 if(createFileStructure(logFile) && !isInternalLogger()) { 787 addLogFileToLogger(logFile); 789 } 790 command.setLogFile(logFile); 791 } 792 793 String javaHome=System.getProperty(JAVA_HOME_PROPERTY); 795 String javaCall="java", javaCmd=null; 797 if(OS.isWindows()) { 798 javaCall="javaw"; 799 } 800 801 if (javaHome != null) { 802 javaCmd=javaHome + File.separator + "bin" + File.separator + javaCall; 804 } else { 805 String javaInt=System.getProperty("java.home"); 807 javaCmd=javaInt + File.separator + "bin" + File.separator + javaCall; 808 } 809 810 command.setJavaCommand(javaCmd); 811 812 String classpath=deriveClasspath(plConfig, null); 815 command.setClasspath(classpath); 816 817 command.setMainClass(plConfig.getMainClass()); 820 821 Iterator it=systemProperties.keySet().iterator(); 824 String key=null; 825 826 String property=null, value=null;; 827 while(it.hasNext()) { 828 key=(String )it.next(); 829 value=systemProperties.getProperty(key); 830 831 if (key.startsWith("-")) { 833 property = key; 834 if (value != null && !value.equals("")) { 835 property += "=" + value; 836 } 837 if ( ( key.equals("-client")) || (key.equals("-server")) ) { 838 command.setMode(key); 843 } else { 844 command.addJvmOption(property); 845 } 846 } else { 847 property = "-D" + key + "=" + value; 849 command.addSystemVariable(property); 850 } 851 } 852 853 command.addSystemVariable("-Djava.library.path=" + deriveNativeClasspath(command, null, null, systemProperties)); 856 857 String [] args=getArgs(); 860 for(int ii=0; ii < args.length; ii++) { 861 command.addArg(args[ii]); 862 } 863 return command; 864 } 865 866 867 874 protected String configureLogService(Config config) { 875 String logFile=""; 876 LogService logService=config.getLogService(); 877 if(logService != null) { 878 logFile=logService.getFile(); 880 String logLevel=logService.getModuleLogLevels().getAdmin(); 881 882 getLogger().setLevel(Level.parse(logLevel)); 884 } 885 return logFile; 886 } 887 888 889 893 protected String deriveClasspath(ProcessLauncherConfig plConfig, String jvmCmd) { 894 return deriveClasspath(plConfig, jvmCmd, null, null); 895 } 896 897 898 913 protected String deriveClasspath(ProcessLauncherConfig plConfig, String jvmCmd, JavaConfig javaConfig, String profilerClasspath) { 914 String libDir=RelativePathResolver.resolvePath(plConfig.getClasspathLibDir()); 916 String classpath=Classpath.getLibClasspath(libDir, plConfig.getClasspathIncludes(), 917 plConfig.getClasspathExcludes()); 918 getLogger().log(Level.FINE, "Derived Classpath from " + libDir + " - \n" + classpath); 919 920 String javaVersion=System.getProperty("java.version"); if (jvmCmd != null) { 924 try { 925 Process process=Runtime.getRuntime().exec(jvmCmd + " -version"); 927 ByteArrayOutputStream baosOut= new ByteArrayOutputStream (); 929 ByteArrayOutputStream baosErr= new ByteArrayOutputStream (); 930 StreamFlusher sfOut=new StreamFlusher(process.getInputStream(), baosOut); 931 StreamFlusher sfErr=new StreamFlusher(process.getErrorStream(), baosErr); 932 sfOut.start(); 933 sfErr.start(); 934 process.waitFor(); 936 javaVersion=baosErr.toString(); 937 } catch (Exception e) { 938 getLogger().log(Level.FINE,"Java version retrieving error, will default to 1.5 or later!", e); 940 } 941 } 942 943 String prefix=""; 945 String jvmv=""; 946 if(javaVersion.indexOf("1.4") >= 0) { 947 jvmv="j2se 1.4"; 949 prefix=plConfig.getClasspathJ2se14Prefix(); 950 } else { 951 jvmv="j2se 1.5 or later"; 953 prefix=plConfig.getClasspathJ2se15OrLaterPrefix(); 954 } 955 getLogger().log(Level.FINE,"Java version being used is: ->" + jvmv + 957 "<- based on ->" + javaVersion + "<-"); 958 959 if(prefix != null && !prefix.equals("")) { 962 963 prefix=RelativePathResolver.resolvePath(prefix); 965 966 if(classpath.equals("")) { 968 classpath=prefix; 970 } else { 971 classpath=prefix + File.pathSeparator + classpath; 973 } 974 } 975 976 977 prefix=plConfig.getClasspathPrefix(); 979 if(prefix != null && !prefix.equals("")) { 981 982 prefix=RelativePathResolver.resolvePath(prefix); 984 985 if(classpath.equals("")) { 987 classpath=prefix; 989 } else { 990 classpath=prefix + File.pathSeparator + classpath; 992 } 993 } 994 995 996 if(javaConfig != null) { 998 String classpathPrefix=javaConfig.getClasspathPrefix(); 999 String classpathSystem=javaConfig.getSystemClasspath(); 1000 String classpathSuffix=javaConfig.getClasspathSuffix(); 1001 1002 if(classpathSystem != null) { 1003 classpath += File.pathSeparator + classpathSystem; 1004 } 1005 1006 if (getProcessLauncherProfile().equals(INTERNAL_SERVER_PROFILE)){ 1011 if(classpathPrefix != null) { 1012 classpath=classpathPrefix + File.pathSeparator + classpath; 1013 } 1014 if(classpathSuffix != null) { 1015 classpath=classpath + File.pathSeparator + classpathSuffix; 1016 } 1017 } 1018 1019 if(profilerClasspath != null) { 1021 classpath += File.pathSeparator + profilerClasspath; 1022 } 1023 1024 if(!javaConfig.isEnvClasspathIgnored()) { 1026 String envClassPath=""; 1028 String [] sxEnv = new CliUtil().getAllEnv(); 1029 for(int ii=0; ii < sxEnv.length; ii++) { 1030 if(sxEnv[ii].trim().startsWith(CLASSPATH_ENV_NAME)) { 1031 String userCp=sxEnv[ii].substring(CLASSPATH_ENV_NAME.length() + 1).trim(); 1032 classpath += (userCp.equals("") ? "" : File.pathSeparator + userCp); 1033 break; 1034 } 1035 } 1036 } 1037 } 1038 1039 getLogger().log(Level.FINE, "Final classpath - \n" + classpath); 1040 if (bDebug) System.out.println("Final classpath=" + classpath); 1041 return classpath; 1042 } 1043 1044 protected String deriveNativeClasspath(Command command, JavaConfig javaConfig, Profiler profiler, Properties systemProperties) { 1045 String javaLibPath = System.getProperty("java.library.path"); 1047 if (bDebug) System.out.println("Current java.library.path=" + javaLibPath + "\n"); 1048 if (javaLibPath == null) javaLibPath=""; 1049 String libDirFor64Bit = ""; 1050 1051 if (javaConfig != null) { 1052 String nativePrefix=javaConfig.getNativeLibraryPathPrefix(); 1053 String nativeSuffix=javaConfig.getNativeLibraryPathSuffix(); 1054 1055 String nativeProfiler=null; 1056 if(profiler != null && profiler.isEnabled()) { 1057 nativeProfiler=profiler.getNativeLibraryPath(); 1058 } 1059 1060 if ( (nativePrefix != null ) && !nativePrefix.trim().equals("")) { 1062 javaLibPath=nativePrefix + (javaLibPath.equals("") ? "" : File.pathSeparator + javaLibPath); 1063 } 1064 if ( (nativeSuffix != null ) && !nativeSuffix.trim().equals("")) { 1065 javaLibPath=(javaLibPath.equals("") ? "" : javaLibPath + File.pathSeparator) + nativeSuffix ; 1066 } 1067 if (( nativeProfiler!= null ) && !nativeProfiler.trim().equals("")) { 1068 javaLibPath=(javaLibPath.equals("") ? "" : javaLibPath + File.pathSeparator) + nativeProfiler ; 1069 } 1070 1071 1072 String [] jvmOptions=javaConfig.getJvmOptions(); 1073 for(String s:jvmOptions){ 1074 if(s.indexOf(JVM_OPTION_FOR_64BIT)!=-1){ 1075 String osArch = System.getProperty("os.arch"); 1076 if(osArch.equals(SPARC)) libDirFor64Bit = SPARCV9; 1077 else if(osArch.equals(X86)) libDirFor64Bit = AMD64; 1078 1079 String nssRoot=System.getProperty(SystemPropertyConstants.NSS_ROOT_PROPERTY); 1080 String installRoot=System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY); 1081 String imqLib=System.getProperty(SystemPropertyConstants.IMQ_LIB_PROPERTY); 1082 String icuLib=System.getProperty(SystemPropertyConstants.ICU_LIB_PROPERTY); 1083 String java64BitLibPath = ""; 1084 if (installRoot != null){ 1085 if(nssRoot != null){ 1086 java64BitLibPath= nssRoot + File.separator + libDirFor64Bit; 1087 } 1088 if(imqLib != null){ 1089 java64BitLibPath = imqLib + File.separator + libDirFor64Bit + File.pathSeparator + java64BitLibPath; 1090 } 1091 1092 if(icuLib != null){ 1093 java64BitLibPath = icuLib + File.separator + libDirFor64Bit + File.pathSeparator + java64BitLibPath; 1094 } 1095 javaLibPath = java64BitLibPath + File.pathSeparator + javaLibPath; 1096 } 1097 } 1098 } 1099 } 1100 if (OS.isWindows()) { 1103 String nssRoot=System.getProperty(SystemPropertyConstants.NSS_ROOT_PROPERTY); 1104 String installRoot=System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY); 1105 1106 if (installRoot != null && nssRoot != null) { 1107 javaLibPath= nssRoot + File.pathSeparator + 1108 installRoot + File.separator + "lib" + File.pathSeparator + javaLibPath; 1109 } 1110 } 1111 1112 if (isDisplayEnabled()) { 1113 1116 1121 String java_home = null; 1122 if ((javaConfig != null) && (javaConfig.getJavaHome() != null)) { 1123 java_home = javaConfig.getJavaHome(); 1124 } else { 1125 java_home = SystemPropertyConstants.JAVA_ROOT_PROPERTY; 1126 } 1127 String jvmLibPath = java_home + 1128 System.getProperty(SystemPropertyConstants.NATIVE_LAUNCHER_LIB_PREFIX); 1129 1130 if (command.getMode() != null) { 1132 jvmLibPath=jvmLibPath.substring(0, jvmLibPath.lastIndexOf(File.separator) + 1) + 1133 command.getMode().substring(1); 1134 } 1135 1136 if (javaLibPath != null) { 1137 javaLibPath=jvmLibPath + File.pathSeparator + javaLibPath; 1138 } else { 1139 javaLibPath=jvmLibPath; 1140 } 1141 } 1142 1143 1144 if(javaLibPath.indexOf(" ") >= 0) { 1146 String items = getPathItemsWithSpaces(javaLibPath); 1152 getLogger().log(Level.FINE,"launcher.spacesInPath", new Object [] { items }); 1153 1154 javaLibPath=javaLibPath.replaceAll("\"", ""); 1158 } 1159 1160 systemProperties.put("java.library.path" , javaLibPath); 1161 1162 1163 1164 System.setProperty("java.library.path", javaLibPath ); 1165 command.setNativeClasspath(javaLibPath); 1166 1167 1168 if (bDebug) System.out.println("Final java.library.path=" + javaLibPath + "\n"); 1169 return javaLibPath; 1170 } 1171 1172 1173 protected void addSystemProperties(SystemProperty sp[], Properties systemProperties) { 1174 if(sp != null) { 1175 for(int ii=0; ii < sp.length; ii++) { 1176 systemProperties.put(sp[ii].getName(), sp[ii].getValue()); 1177 } 1178 } 1179 } 1180 1181 protected void addElementProperties(ElementProperty ep[], Properties systemProperties) { 1182 if(ep != null) { 1183 for(int ii=0; ii < ep.length; ii++) { 1184 systemProperties.put(ep[ii].getName(), ep[ii].getValue()); 1185 } 1186 } 1187 } 1188 1189 1190 1191 1192 protected Logger getLogger() { 1193 if (_logger == null) { 1194 _logger = Logger.getLogger(LogDomains.PROCESS_LAUNCHER_LOGGER, "com.sun.logging.enterprise.system.tools.launcher.LogStrings"); 1199 1200 if (!isVerboseEnabled()) { 1202 Handler [] h=_logger.getParent().getHandlers(); 1203 for (int ii=0; ii < h.length; ii++) { 1204 if (h[ii].getClass().getName().equals("java.util.logging.ConsoleHandler")) { 1205 _logger.getParent().removeHandler(h[ii]); 1206 } 1207 } 1208 } 1209 1210 } 1216 1217 if (bDebug) _logger.setLevel(Level.FINEST); 1219 return _logger; 1220 } 1221 1222 1223 protected boolean isInternalLogger() { 1224 boolean bRet=false; 1225 String logManager=System.getProperty("java.util.logging.manager"); 1227 if (logManager != null && logManager.equals("com.sun.enterprise.server.logging.ServerLogManager")) { 1228 bRet=true; 1229 } 1230 return bRet; 1231 } 1232 1233 protected void addLogFileToLogger(String logFile) { 1234 if (logFile == null) return; 1235 1236 getLogger().log(Level.FINE, "*** Adding logFileHandler - " + logFile); 1238 try { 1240 FileHandler fh = new FileHandler (logFile, true); 1241 fh.setFormatter(new SimpleFormatter ()); 1242 fh.setLevel(Level.ALL); 1243 getLogger().addHandler(fh); 1244 } catch(IOException e) { 1245 e.printStackTrace(); 1247 } 1248 } 1249 1250 1251 1254 protected void addJvmOptions(Command command, String [] args) { 1255 String systemProperty = null; 1256 String property = null; 1257 String value = null; 1258 String [] jvmOptions = null; 1259 1260 try { 1261 jvmOptions = (new JvmOptionsHelper(args)).getJvmOptions(); 1262 } catch(Exception e) { 1263 } 1264 if(jvmOptions!=null) 1265 { 1266 for(int i=0; i<jvmOptions.length; i++) 1267 { 1268 addJvmArg(command, jvmOptions[i]); 1269 } 1270 return; 1271 } 1272 1273 if(args != null) { 1276 for(int ii=0; ii < args.length; ii++) { 1278 systemProperty=args[ii].trim(); 1280 1281 if (bDebug) System.out.println("addJvmOptions: IN Property " + systemProperty); 1282 1283 if(systemProperty.trim().equals("")) { 1285 continue; 1286 } 1287 1288 int iSpace=0, iQuote1=0, iQuote2=0; 1289 while(systemProperty.length() > 0) { 1291 1292 iSpace=systemProperty.indexOf(" -"); 1294 iQuote1=systemProperty.indexOf("\""); 1295 1296 if (iSpace >= 0) { 1298 if (iQuote1 >= 0) { 1300 if (iQuote1 > iSpace && iSpace >= 0 ) { 1302 addJvmArg(command, systemProperty.substring(0, iSpace)); 1304 systemProperty=systemProperty.substring(iSpace + 1).trim(); 1306 if (bDebug) System.out.println("*** left 1:" + systemProperty); 1307 } else { 1308 1310 int iQuoteStartPos=iQuote1 + 1; 1312 while (true) { 1313 iQuote2=systemProperty.indexOf("\"", iQuoteStartPos); 1314 if (iQuote2 < 0) { 1315 getLogger().log(Level.WARNING, "launcher.missMatchQuotesInArg", systemProperty); 1317 systemProperty=""; 1319 break; 1321 } 1322 1323 if (systemProperty.charAt(iQuote2 - 1) == '\\') { 1326 iQuoteStartPos=iQuote2 + 1; 1328 continue; 1329 } else { 1330 1332 if (systemProperty.indexOf(" -", iQuote2) < 0) { 1336 addJvmArg(command, systemProperty); 1339 1340 systemProperty=""; 1343 } else { 1344 iQuote2++; addJvmArg(command, systemProperty.substring(0, iQuote2)); 1347 if (iQuote2 < systemProperty.length()) { 1349 systemProperty=systemProperty.substring(iQuote2 + 1).trim(); 1350 if (bDebug) System.out.println("*** left 2:" + systemProperty); 1351 } else { 1352 systemProperty=""; 1355 } 1356 } 1357 break; 1359 } 1360 } 1361 } 1362 1363 } else { 1364 int iDel=systemProperty.indexOf(" -"); 1367 while(iDel >= 0) { 1368 addJvmArg(command, systemProperty.substring(0, iDel)); 1370 systemProperty=systemProperty.substring(iDel + 1).trim(); 1371 iDel=systemProperty.indexOf(" -"); 1372 } 1373 1374 if (!systemProperty.equals("")) { 1376 addJvmArg(command, systemProperty); 1377 } 1378 1379 break; 1381 } 1382 } else { 1383 addJvmArg(command, systemProperty); 1385 break; 1387 } 1388 } 1389 } 1390 } 1391 1392 } 1393 1394 1395 protected void addJvmArg(Command command, String option) { 1396 if ( option.startsWith("-D") ) { 1397 command.addSystemVariable(option); 1399 } else { 1400 if ( ( option.equals("-client")) || (option.equals("-server")) ) { 1401 command.setMode(option); 1406 } else { 1407 command.addJvmOption(option); 1409 } 1410 } 1411 1412 if (bDebug) System.out.println("addJvmOptions: OUT Property " + option); 1414 } 1415 1416 1417 1420 protected void addDebugOptions(Command command, String debug_options) { 1421 1423 command.addDebugOption("-Xdebug"); 1425 1426 debug_options=debug_options.trim(); 1430 if ( debug_options.startsWith("-Xdebug") ) { 1431 debug_options =debug_options.substring("-Xdebug".length()).trim(); 1432 } 1433 1434 1450 String transport = getDebugProperty(debug_options, "transport"); 1451 String addr = getDebugProperty(debug_options, "address"); 1452 1453 if ( transport == null || transport.equals("") ) { 1454 System.out.println("Cannot start server in debug mode: no transport specified in debug-options in domain.xml."); 1457 } 1458 1459 if ( transport.equals("dt_socket") ) { 1460 if ( addr != null && !addr.equals("") ) { 1461 } 1464 else { 1465 int port = 1467 com.sun.enterprise.util.net.NetUtils.getFreePort(); 1468 if ( port == 0 ) { 1469 System.out.println("Cannot start server in debug mode: unable to obtain a free port for transport dt_socket."); 1472 } 1473 addr = String.valueOf(port); 1474 1475 debug_options = debug_options + ",address=" + addr; 1476 } 1477 } 1478 1479 command.addDebugOption(debug_options); 1480 1481 String jdwpOptions = debug_options.substring( 1485 debug_options.indexOf("-Xrunjdwp:") + "-Xrunjdwp:".length()); 1486 1487 command.addSystemVariable("-D" + DEBUG_OPTIONS + "=" + jdwpOptions); 1488 1489 } 1490 1491 protected String getDebugProperty(String debug_options, String name) { 1492 int nameIndex; 1493 if ( (nameIndex = debug_options.indexOf(name)) != -1 ) { 1494 String value = debug_options.substring(nameIndex 1496 + name.length() + 1); 1497 int commaIndex; 1498 if ( (commaIndex = value.indexOf(",")) != -1 ) { 1499 value = value.substring(0, commaIndex); 1500 } 1501 return value; 1502 } 1503 return null; 1504 } 1505 1506 protected boolean isWaitEnabled() { 1507 boolean bRet=false; 1508 String launcherRet=System.getProperty(LAUNCHER_RETURN_FUNCTION); 1509 if(launcherRet != null && launcherRet.equals(LAUNCHER_RETURN_FUNCTION_WAIT)) { 1510 bRet=true; 1511 } 1512 return bRet; 1513 } 1514 1515 protected boolean isVerboseEnabled() { 1516 return argExists(COMMAND_LINE_ARG_VERBOSE); 1517 } 1518 1519 1520 protected boolean isDebugEnabled() { 1521 return argExists(COMMAND_LINE_ARG_DEBUG); 1522 } 1523 1524 protected boolean isDisplayEnabled() { 1525 return argExists(COMMAND_LINE_ARG_DISPLAY); 1526 } 1527 1528 protected boolean isServerProfile() { 1529 String processName=getProcessLauncherProfile(); 1530 return processName.equals(INTERNAL_SERVER_PROFILE) || 1531 processName.equals(AS9_INTERNAL_SERVER_PROFILE); 1532 } 1533 1534 protected boolean isNodeAgentProfile() { 1535 String processName=getProcessLauncherProfile(); 1536 return processName.equals(INTERNAL_NODE_AGENT_PROFILE); 1537 } 1538 1539 1540 protected String getFiletRelativeName(String action) { 1541 return System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY) + File.separator 1542 + LAUNCHER_SCRIPT_LOCATION + File.separator + action; 1543 } 1544 1545 1546 protected String getProcessLauncherProfile() { 1547 return System.getProperty(LAUNCHER_PROFILE_NAME, INTERNAL_SERVER_PROFILE); 1548 } 1549 1550 protected String getScriptRelativeName(String action) { 1551 String sxRet=getFiletRelativeName(action); 1552 if(OS.isWindows()) { 1553 sxRet+="_temp.bat"; 1554 } else { 1555 sxRet+="_temp.sh"; 1556 } 1557 return sxRet; 1558 } 1559 1560 protected void setArgs(String [] args) { 1561 this._args=args; 1562 } 1563 1564 protected String [] getArgs() { 1565 return _args; 1566 } 1567 1568 protected boolean argExists(String arg) { 1569 boolean bRet=false; 1570 String [] args=getArgs(); 1571 if(args != null) { 1572 for(int ii=0; ii < args.length; ii++) { 1573 if(args[ii].equals(arg)) { 1574 bRet=true; 1575 break; 1576 } 1577 } 1578 } 1579 return bRet; 1580 } 1581 1582 1583 1589 protected boolean createFileStructure(String logFile) { 1590 boolean bRet=false; 1591 File outputFile=new File (logFile); 1592 1593 try { 1594 File parentFile = new File (outputFile.getParent()); 1596 if ( !parentFile.exists() ) { 1598 parentFile.mkdirs(); 1600 } 1601 if (!outputFile.exists()) { 1603 outputFile.createNewFile(); 1604 } 1605 if (outputFile.canWrite()) { 1606 bRet=true; 1608 } 1609 } catch (IOException e) { 1610 e.printStackTrace(); 1612 } 1613 1614 if (!bRet) { 1615 getLogger().log(Level.WARNING,"launcher.logWriteFailure", logFile); 1617 } 1618 1619 return bRet; 1620 } 1621 1622 private String getPathItemsWithSpaces(String path) 1623 { 1624 StringTokenizer st = new StringTokenizer (path, File.pathSeparator); 1625 String ret = ""; 1626 boolean firstItem = true; 1627 1628 while (st.hasMoreTokens()) 1629 { 1630 String item = st.nextToken(); 1631 1632 if(item.indexOf(' ') >= 0) 1633 { 1634 if(!firstItem) 1635 ret += File.pathSeparator; 1636 else 1637 firstItem = false; 1638 1639 ret += item; 1640 } 1641 } 1642 1643 return ret; 1644 } 1645 1646 1647 private String [] removeJVMStopOptions(String [] jvmOptions) { 1648 for (int i=0; i<jvmOptions.length; i++) { 1649 if (jvmOptions[i].startsWith("-X")) { 1650 jvmOptions[i] = ""; 1651 } 1652 if (jvmOptions[i].startsWith("-server")) { 1653 jvmOptions[i] = "-client"; 1654 } 1655 } 1656 return jvmOptions; 1657 } 1658 1659 1672 protected class Command { 1673 1674 private ArrayList _jvmOptions=new ArrayList (); 1675 private ArrayList _systemVariables=new ArrayList (); 1676 private ArrayList _args=new ArrayList (); 1677 private ArrayList _debugOptions=new ArrayList (); 1678 private String _mainClass=null; 1679 private String _classpath=null; 1680 private String _javaCommand=null; 1681 private String _mode=null; 1682 private String _logFile=null; 1683 private String _nativeClasspath=null; 1684 1685 1686 protected void addJvmOption(String jvmOptions) { 1687 _jvmOptions.add(jvmOptions.trim()); 1689 } 1690 1691 protected void addSystemVariable(String systemVariable) { 1692 _systemVariables.add(systemVariable.trim()); 1694 } 1695 1696 protected void addArg(String arg) { 1697 _args.add(arg); 1698 } 1699 protected String [] getArgs() { 1700 return (String [])_args.toArray(new String [_args.size()]); 1701 } 1702 protected void removeArg(String arg) { 1703 _args.remove(arg); 1704 } 1705 1706 protected void addDebugOption(String debugOption) { 1707 _debugOptions.add(debugOption); 1708 } 1709 protected String [] getDebugOptions() { 1710 return (String [])_debugOptions.toArray(new String [_debugOptions.size()]); 1711 } 1712 1713 protected void setMode(String mode) { 1714 _mode=mode; 1715 } 1716 protected String getMode() { 1717 return _mode; 1718 } 1719 1720 protected void setMainClass(String mainClass) { 1721 _mainClass=mainClass; 1722 } 1723 protected String getMainClass() { 1724 return _mainClass; 1725 } 1726 1727 protected void setNativeClasspath(String classpath) { 1728 _nativeClasspath=classpath; 1729 } 1730 protected String getNativeClasspath() { 1731 return _nativeClasspath; 1732 } 1733 1734 protected void setClasspath(String classpath) { 1735 _classpath=classpath; 1736 } 1737 protected String getClasspath() { 1738 return _classpath; 1739 } 1740 1741 protected void setJavaCommand(String javaCommand) { 1742 _javaCommand=javaCommand; 1743 } 1744 protected String getJavaCommand() { 1745 return _javaCommand; 1746 } 1747 1748 protected void setLogFile(String logFile) { 1749 if (bDebug) System.out.println("Logfile set to " + logFile); 1750 _logFile=logFile; 1751 } 1752 protected String getLogFile() { 1753 return _logFile; 1754 } 1755 1756 1760 protected String [] getCommandAsArray() { 1761 ArrayList cmd=new ArrayList (); 1762 cmd.add(_javaCommand); 1763 if(_mode != null) { 1764 cmd.add(_mode); 1765 } 1766 cmd.addAll(_debugOptions); 1767 cmd.addAll(_jvmOptions); 1768 cmd.addAll(_systemVariables); 1769 cmd.add("-cp"); 1772 cmd.add(_classpath); 1773 cmd.add(_mainClass); 1774 cmd.addAll(_args); 1775 return (String [])cmd.toArray(new String [cmd.size()]); 1776 } 1777 1778 1785 protected String [] getLimitedCommandAsArray() { 1786 ArrayList cmd=new ArrayList (); 1787 cmd.add(_javaCommand); 1788 if(_mode != null) { 1789 cmd.add(_mode); 1790 } 1791 cmd.addAll(_debugOptions); 1792 cmd.addAll(_jvmOptions); 1793 cmd.addAll(_systemVariables); 1794 cmd.add(_mainClass); 1795 cmd.addAll(_args); 1796 return (String [])cmd.toArray(new String [cmd.size()]); 1797 } 1798 1799 1800 1804 protected String [] getCommandInJNIFormatAsArray() { 1805 1812 String jniMainClassName=_mainClass; 1814 int iPos=0; 1815 while((iPos=jniMainClassName.indexOf(".")) >= 0) { 1816 jniMainClassName=jniMainClassName.substring(0, iPos) + "/" + jniMainClassName.substring(iPos + 1); 1817 } 1818 1819 ArrayList cmd=new ArrayList (); 1820 if(_mode != null) { 1821 cmd.add(_mode); 1822 } 1823 cmd.addAll(_debugOptions); 1824 cmd.addAll(_jvmOptions); 1825 cmd.addAll(_systemVariables); 1826 cmd.add("-Djava.class.path=" + _classpath); 1827 cmd.add(_mainClass); 1828 cmd.addAll(_args); 1829 return (String [])cmd.toArray(new String [cmd.size()]); 1830 } 1831 1832 1833 1836 protected String [] getSystemVariablesAsArray() { 1837 ArrayList cmd=new ArrayList (); 1838 cmd.addAll(_systemVariables); 1839 return (String [])cmd.toArray(new String [cmd.size()]); 1840 } 1841 1842 1843 public String toString() { 1844 StringBuffer cmd= new StringBuffer (); 1845 1846 String [] ret=getCommandAsArray(); 1847 for(int ii=0; ii < ret.length; ii++) { 1848 cmd.append(ret[ii]); 1849 } 1850 1851 return cmd.toString(); 1852 } 1853 1854 public String toStringWithLines() { 1855 StringBuffer cmd= new StringBuffer (); 1856 1857 String [] ret=getCommandAsArray(); 1858 for(int ii=0; ii < ret.length; ii++) { 1859 cmd.append("\n" + ret[ii]); 1860 } 1861 return cmd.toString(); 1862 } 1863 } 1864 1865 1866 1873 protected static class Classpath { 1874 1879 protected static String getLibClasspath(String libDir, String includes, String excludes) { 1880 ArrayList arIncludes=new ArrayList (); 1881 ArrayList arExcludes=new ArrayList (); 1882 1883 StringTokenizer st=new StringTokenizer (includes, ","); 1885 while(st.hasMoreTokens()) { 1886 arIncludes.add(st.nextToken().trim()); 1887 } 1888 st=new StringTokenizer (excludes, ","); 1889 while(st.hasMoreTokens()) { 1890 arExcludes.add(st.nextToken().trim()); 1891 } 1892 1893 String path=""; 1894 if (libDir != null && !libDir.equals("")) { 1896 File dir=new File (libDir); 1898 1899 String [] filenames=dir.list(); 1901 for(int ii=0; ii < filenames.length; ii++) { 1902 1903 if(matchStringToList(filenames[ii], arIncludes) && !matchStringToList(filenames[ii], arExcludes)) { 1905 path += libDir + File.separator + filenames[ii] + File.pathSeparator; 1907 } 1908 } 1909 1910 if(path.endsWith(File.pathSeparator)) { 1912 path=path.substring(0, path.length()-1); 1913 } 1914 } 1915 1916 return path; 1917 } 1918 1919 1920 1924 protected static boolean matchStringToList(String filename, ArrayList list) { 1925 boolean bRet=false; 1926 1927 String criteria=null, endMatch=null; 1928 Iterator it=list.iterator(); 1929 while(it.hasNext()) { 1930 criteria=(String )it.next(); 1931 1932 if (criteria.startsWith("*")) { 1933 endMatch=criteria.substring(1); 1936 if (filename.endsWith(endMatch)) { 1938 bRet=true; 1940 break; 1941 } 1942 } else if (isRegularExpression(criteria)) { 1943 if(Pattern.matches(criteria, filename)) { 1945 bRet=true; 1947 break; 1948 } 1949 } else { 1950 if(filename.equals(criteria)) { 1952 bRet=true; 1954 break; 1955 } 1956 } 1957 } 1958 1959 return bRet; 1960 } 1961 1962 1966 protected static boolean isRegularExpression(String criteria) { 1967 boolean bRet=false; 1968 if(criteria.indexOf("^") > -1 || criteria.indexOf("$") > -1 || criteria.indexOf("[") > -1 1969 || criteria.indexOf("]") > -1 || criteria.indexOf("*") > -1) { 1970 bRet=true; 1971 } 1972 return bRet; 1973 } 1974 } 1975 1976 1977 1978 1985 protected class StreamFlusher extends Thread { 1986 1987 private InputStream _input=null; 1988 private OutputStream _output=null; 1989 private String _logFile=null; 1990 1991 1992 public StreamFlusher(InputStream input, OutputStream output) { 1993 this(input, output, null); 1994 } 1995 1996 public StreamFlusher(InputStream input, OutputStream output, String logFile) { 1997 this._input=input; 1998 this._output=output; 1999 this._logFile=logFile; 2000 } 2001 2002 public void run() { 2003 2004 if (_input == null) return; 2006 2007 PrintStream printStream=null; 2008 2009 if (_logFile != null) { 2011 try { 2012 if(createFileStructure(_logFile)) { 2013 printStream = new PrintStream (new FileOutputStream (_logFile, true), true); 2015 } else { 2016 _logFile=null; 2018 } 2019 } catch (IOException ie) { 2020 ie.printStackTrace(); 2021 _logFile=null; 2022 } 2023 } 2024 2025 try { 2027 int byteCnt=0; 2028 byte[] buffer=new byte[4096]; 2029 while ((byteCnt=_input.read(buffer)) != -1) { 2030 if (_output != null && byteCnt > 0) { 2031 _output.write(buffer, 0, byteCnt); 2032 _output.flush(); 2033 2034 if (_logFile != null) { 2036 printStream.write(buffer, 0, byteCnt); 2037 printStream.flush(); 2038 } 2039 } 2040 yield(); 2041 } 2042 } catch (IOException e) { 2043 } 2046 } 2047 2048 } 2049} 2050 | Popular Tags |