001: package tide.execute;
002:
003: import tide.project.ProjectUtils;
004: import java.io.*;
005: import java.util.*;
006:
007: /** Executes a given class and gobble the output to the execution tab
008: * TODO: system.err should NOT be buffered...
009: */
010: public final class JavaExecutor {
011: public final static String JMX_ENABLE_JVM_ARG = "-Dcom.sun.management.jmxremote";
012:
013: private JavaExecutor() {
014:
015: } // Constructor
016:
017: /** Execute the given class.
018: * The class must have a static main method to be started.
019: * REMARK: the returned process must be gobbled ! and in a very tolerant manner (buffered)
020: */
021: public static JProcess executeClass(File sourceBase,
022: File javaExecutor, File classesDest, String mainClassName,
023: List<File> classPath, String jvmOptions, String appOptions,
024: File jpsTool, // use to parse pid for the lsunched java process
025: boolean enableRemoteJMXAccess, // add if not already present, but let if present.
026: String hprofCommand, boolean launchDebugger)
027: throws Exception {
028: if (!javaExecutor.exists())
029: throw new Exception("Java executor no found: "
030: + javaExecutor);
031:
032: List<String> execCommand = new ArrayList<String>();
033: execCommand.add(javaExecutor.getAbsolutePath());
034:
035: boolean isEnableRemoteJMXAccessAlreadyPresent = false;
036: if (jvmOptions != null && jvmOptions.length() > 0) {
037: if (jvmOptions.indexOf(JMX_ENABLE_JVM_ARG) >= 0) {
038: isEnableRemoteJMXAccessAlreadyPresent = true;
039: }
040: execCommand.addAll(ProjectUtils
041: .splitArgs(jvmOptions, false));
042: }
043:
044: boolean isJMXPresent = isEnableRemoteJMXAccessAlreadyPresent;
045: if (!isJMXPresent && enableRemoteJMXAccess) {
046: execCommand.add(JMX_ENABLE_JVM_ARG);
047: isJMXPresent = true;
048: }
049:
050: if (hprofCommand != null && hprofCommand.length() > 0) {
051: execCommand.add(hprofCommand);
052: }
053:
054: if (launchDebugger) {
055: execCommand
056: .add("-agentlib:jdwp=transport=dt_shmem,address=jdbconn,server=y,suspend=n");
057: }
058:
059: // Classpath
060: execCommand.add("-classpath");
061: List<File> cp = new ArrayList<File>();
062: cp.add(classesDest); // IMPORTANT: the classes are in the CP.
063:
064: if (classPath != null) {
065: cp.addAll(classPath);
066: }
067:
068: StringBuilder cpsb = new StringBuilder();
069: for (int i = 0; i < cp.size(); i++) {
070: cpsb.append(cp.get(i).getAbsolutePath());
071: if (i < cp.size() - 1)
072: cpsb.append(";");
073: }
074: execCommand.add(cpsb.toString());
075:
076: execCommand.add(mainClassName);
077:
078: if (appOptions != null && appOptions.length() > 0) {
079: execCommand.addAll(ProjectUtils
080: .splitArgs(appOptions, false));
081: }
082:
083: long startTime = System.currentTimeMillis();
084: // start the process in the sourceBase directory
085: System.out.println("Execute command " + execCommand);
086:
087: List<JProcess> jprocessesBefore = null;
088: if (jpsTool != null) {
089: jprocessesBefore = JProcess
090: .getAllRunningJavaProcesses(jpsTool);
091: }
092:
093: ProcessBuilder pb = new ProcessBuilder(execCommand);
094: pb.directory(sourceBase);
095: JProcess jp = JProcess.recognizeRunningProcess(pb.start(),
096: mainClassName, isJMXPresent, jprocessesBefore, jpsTool);
097: return jp;
098: }
099:
100: /*test
101: public static void main(String[] args)
102: {
103: try
104: {
105: File sourceBase = new File("C:/sources/other/Script/src");
106:
107: final Vector<File> classPath = new Vector<File>();
108: classPath.add(new File("C:/Java/jython-2.1/jython.jar"));
109: classPath.add(new File("C:/Java/jython-2.1/jython.jar;C:/Java/antlr/antlr-2.7.5.jar"));
110:
111: JProcess proc = executeClass(
112: sourceBase,
113: new File(System.getenv("JAVA_HOME"), "bin/java.exe"),
114: new File("C:/sources/other/Script/classes"),
115: "script.editor.ScriptEditor",
116: classPath,
117: "-Xmx256m",
118: "",
119: null, true,
120: null );
121:
122: StreamGobbler sg1 = new StreamGobbler(proc.process.getInputStream(), System.out);
123: sg1.start();
124: StreamGobbler sg2 = new StreamGobbler(proc.process.getErrorStream(), System.err);
125: sg2.start();
126:
127: sg1.join();
128: sg2.join();
129: }
130: catch(Exception e)
131: {
132: e.printStackTrace();
133: }
134: }*/
135:
136: }
|