001: package tide.execute;
002:
003: import tide.utils.SyntaxUtils;
004: import tide.utils.StreamReader;
005: import snow.utils.ProcessUtils;
006: import tide.project.ProjectUtils;
007: import tide.editor.TideUtils;
008: import javax.swing.*;
009: import java.io.*;
010: import tide.project.ProjectSettings;
011: import tide.editor.MainEditorFrame;
012: import javaparser.Parameter;
013: import java.util.*;
014: import tide.sources.FileItem;
015:
016: /** Launch the StaticStarter for some static method of the project.
017: * Starts the project as if it would run !
018: * With the same classpath as the project.
019: *
020: * @see StaticStarter
021: *
022: */
023: public final class StaticLauncher {
024: public StaticLauncher(FileItem fileToLaunch, String className,
025: String metName, List<Parameter> params) {
026: String fullName = className + "." + metName;
027: System.out.println("StaticLauncher:: " + className + " . "
028: + metName + " (" + params + ")");
029:
030: File tideJar = TideUtils.getTideJarCurrentlyRunning();
031: if (tideJar == null) {
032: JOptionPane.showMessageDialog(null,
033: "Sorry, tide.jar not found, can't launch "
034: + className + "." + metName, "Error",
035: JOptionPane.ERROR_MESSAGE);
036: return;
037: }
038:
039: // Use the project java.exe (?)
040: final ProjectSettings projectSettings = MainEditorFrame.instance
041: .getActualProject();
042: File javaToolProj = projectSettings.getJava_TOOL();
043: String verProj = TideUtils.readJavaVersion(javaToolProj);
044: System.out.println("proj: " + verProj);
045: File javaToolTide = TideUtils.getJavaToolUsedToLaunchTide();
046: String verTide = TideUtils.readJavaVersion(javaToolTide);
047: System.out.println("tide: " + verTide);
048:
049: File latestJavaTool = javaToolTide;
050: System.out.println("comp : t comp proj = "
051: + verTide.compareTo(verProj));
052:
053: List<String> cmd = new ArrayList<String>();
054: cmd.add(latestJavaTool.getAbsolutePath()); // java
055: String jvmOptions = projectSettings.getRuntimeArgs();
056: if (jvmOptions != null && jvmOptions.length() > 0) {
057: cmd.addAll(ProjectUtils.splitArgs(jvmOptions, false)); // -server -Xmx ...
058: }
059:
060: // Classpath
061: cmd.add("-classpath");
062: List<File> cp = projectSettings.getClassPath(false, true); // user CP
063: cp.add(0, projectSettings.getClasses_Home()); // IMPORTANT: the classes are in the CP.
064: cp.add(1, tideJar); // NEEDED !
065:
066: StringBuilder cpsb = new StringBuilder();
067: for (int i = 0; i < cp.size(); i++) {
068: cpsb.append(cp.get(i).getAbsolutePath());
069: if (i < cp.size() - 1)
070: cpsb.append(";");
071: }
072: cmd.add(cpsb.toString());
073:
074: cmd.add("tide.execute.StaticStarter");
075:
076: cmd.add(className);
077:
078: cmd.add(metName);
079:
080: // types and names of params !
081: StringBuilder pt = new StringBuilder();
082: StringBuilder pn = new StringBuilder();
083: for (Parameter pi : params) {
084: pt.append(pi.type + ";");
085: pn.append(pi.name + ";");
086: }
087: if (pt.length() > 0) {
088: pt.setLength(pt.length() - 1);
089: }
090: if (pn.length() > 0) {
091: pn.setLength(pn.length() - 1);
092: }
093: cmd.add(pt.toString());
094: cmd.add(pn.toString());
095:
096: MainEditorFrame.debugOut("Static launch: " + cmd);
097: //cmd.add();
098: try {
099: List<JProcess> jprocessesBefore = null;
100: File jpsTool = projectSettings.getJPS_TOOL();
101: if (jpsTool != null) {
102: jprocessesBefore = JProcess
103: .getAllRunningJavaProcesses(jpsTool);
104: }
105:
106: ProcessBuilder pb = new ProcessBuilder(cmd);
107: pb.directory(projectSettings.getSources_Home());
108: JProcess jp = JProcess.recognizeRunningProcess(pb.start(),
109: "tide.execute.StaticStarter", true,
110: jprocessesBefore, jpsTool);
111:
112: //Process proc = Runtime.getRuntime().exec(cmd);
113: MainEditorFrame.instance.outputPanels.processesManager
114: .addProcess("Static started: " + fullName, jp,
115: false);
116: MainEditorFrame.instance.outputPanels.selectOutputTab(true);
117:
118: String commentName = fullName; //SyntaxUtils.makeSingleJavaNameSimple(mainClassName);
119: StreamReader inReader = new StreamReader(
120: commentName + "> ", jp.process.getInputStream(),
121: MainEditorFrame.instance.outputPanels
122: .getExecutionOutputDoc(), false,
123: "Stat run " + commentName + " (in)");
124: inReader.start();
125:
126: // errors appears red. [Nov2007]: was black, not red.
127: // TODO: read err immediately, to see print before line break...
128: StreamReader errReader = new StreamReader(commentName
129: + "> ", jp.process.getErrorStream(),
130: MainEditorFrame.instance.outputPanels
131: .getExecutionOutputDoc(), true, "Stat run "
132: + commentName + " (err)");
133: errReader.start();
134:
135: } catch (Exception e) {
136: //throw e;
137: e.printStackTrace();
138: }
139:
140: }
141:
142: }
|