001: package com.xoetrope.carousel.testpilot;
002:
003: import java.io.File;
004: import net.xoetrope.debug.DebugLogger;
005: import java.io.BufferedReader;
006: import java.io.InputStreamReader;
007: import java.util.StringTokenizer;
008: import java.util.Vector;
009: import java.lang.reflect.Method;
010: import net.xoetrope.xui.XProjectManager;
011: import java.io.*;
012:
013: /**
014: * Support for launching a new application
015: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
016: * the GNU Public License (GPL), please see license.txt for more details. If
017: * you make commercial use of this software you must purchase a commercial
018: * license from Xoetrope.</p>
019: * <p> $Revision: 1.2 $</p>
020: */
021: class ApplicationLauncher extends Thread {
022: private boolean useNewJVM;
023: private String mainClass, classPath, arguments, homeDir;
024:
025: /**
026: * Launch a new application
027: * @param newJVM true for a new/separate JVM
028: * @param mainClazz the class to invoke
029: * @param clazzPath the supporting classpath for teh target application
030: * @param args the application arguments
031: * @param homePath the startup directory
032: */
033: ApplicationLauncher(boolean newJVM, String mainClazz,
034: String clazzPath, String args, String homePath) {
035: useNewJVM = newJVM;
036: mainClass = mainClazz;
037: classPath = clazzPath;
038: arguments = args;
039: homeDir = homePath;
040: }
041:
042: /**
043: * Launch Excel and open a file
044: * @param fileName the file to open.
045: */
046: public void launchExcel(String fileName) {
047: try {
048: String line;
049:
050: // Locate the launch batch file, it should be on the classpath
051: String cmdLine = getClass().getClassLoader().getResource(
052: "launchexcel.bat").toString();
053:
054: // String the protocol prefix
055: cmdLine = cmdLine.substring(6);
056: cmdLine += " " + fileName;
057: DebugLogger.log("Running: " + cmdLine);
058:
059: Process p = Runtime.getRuntime().exec(cmdLine);
060: BufferedReader input = new BufferedReader(
061: new InputStreamReader(p.getInputStream()));
062: while ((line = input.readLine()) != null)
063: System.out.println(line);
064:
065: input.close();
066: } catch (IOException ex) {
067: }
068: }
069:
070: public void run() {
071: try {
072: if (useNewJVM) {
073: String line;
074: String cmdLine = "c://jdk1.5.0//bin//java -classpath "
075: + classPath + " " + mainClass + " " + arguments;
076: String[] envp = new String[0];
077: File startDir = new File(homeDir);
078: DebugLogger.log("Running: " + cmdLine);
079: Process p = Runtime.getRuntime().exec(cmdLine, envp,
080: startDir);
081: BufferedReader input = new BufferedReader(
082: new InputStreamReader(p.getInputStream()));
083: while ((line = input.readLine()) != null) {
084: System.out.println(line);
085: }
086: input.close();
087: } else {
088: try {
089: StringTokenizer st = new StringTokenizer(classPath,
090: File.pathSeparator);
091: int i = 0;
092: int pos;
093: Vector files = new Vector();
094: while (st.hasMoreTokens()) {
095: String subPath = st.nextToken();
096: if ((pos = subPath.indexOf("..")) == 0)
097: subPath = subPath.substring(0, pos)
098: + homeDir
099: + subPath.substring(pos + 1)
100: + File.separator + "..";
101: else if ((pos = subPath.indexOf(".")) == 0)
102: subPath = subPath.substring(0, pos)
103: + homeDir
104: + subPath.substring(pos + 1);
105: files.add(new File(subPath));
106: i++;
107: }
108:
109: // Handle the classpath of the target application.
110: TestPilotAdaptiveClassLoader classLoader = new TestPilotAdaptiveClassLoader(
111: files, true);
112: Class clazz = Class.forName(mainClass.trim(),
113: false, classLoader);
114:
115: Class[] argTypes = new Class[1];
116: argTypes[0] = String[].class;
117: Method method = clazz.getMethod("main", argTypes);
118:
119: Object[] args = new Object[1];
120: String actualArgs[] = new String[1];
121: actualArgs[0] = arguments;
122: args[0] = actualArgs;
123: method.invoke(null, args);
124: } catch (Exception ex) {
125: ex.getCause().printStackTrace();
126: }
127: }
128:
129: } catch (Exception ex) {
130: ex.printStackTrace();
131: }
132: }
133: }
|