incubator-ooo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liu...@apache.org
Subject svn commit: r1360555 - in /incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test: OpenOffice.java common/SystemUtil.java
Date Thu, 12 Jul 2012 07:17:27 GMT
Author: liuzhe
Date: Thu Jul 12 07:17:27 2012
New Revision: 1360555

URL: http://svn.apache.org/viewvc?rev=1360555&view=rev
Log:
Fixed "OpenOffice can't be killed on Windows".

Modified:
    incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/OpenOffice.java
    incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/SystemUtil.java

Modified: incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/OpenOffice.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/OpenOffice.java?rev=1360555&r1=1360554&r2=1360555&view=diff
==============================================================================
--- incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/OpenOffice.java (original)
+++ incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/OpenOffice.java Thu
Jul 12 07:17:27 2012
@@ -25,6 +25,7 @@ import java.io.File;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Properties;
+import java.util.UUID;
 import java.util.logging.Logger;
 
 import org.openoffice.test.common.FileUtil;
@@ -59,6 +60,8 @@ public class OpenOffice {
 	
 	private Properties versionProps = null;
 	
+	private String id = "-"+UUID.randomUUID().toString().replace("-", "");
+	
 	public OpenOffice() {
 		this(null);
 	}
@@ -117,6 +120,7 @@ public class OpenOffice {
 					"Use system property openoffice.home to specify the correct location of OpenOffice.");
 		
 		versionProps = FileUtil.loadProperties(versionFile);
+		addArgs(id);
 		addArgs("-nofirststartwizard", "-norestore", "-quickstart=no");
 		addRegistryModifications("<item oor:path=\"/org.openoffice.Office.Common/Misc\"><prop
oor:name=\"UseSystemFileDialog\" oor:op=\"fuse\"><value>false</value></prop></item>",
 				"<item oor:path=\"/org.openoffice.Office.Common/Security/Scripting\"><prop oor:name=\"MacroSecurityLevel\"
oor:op=\"fuse\"><value>0</value></prop></item>");
@@ -224,25 +228,15 @@ public class OpenOffice {
 	 * destroy OpenOffice
 	 */
 	public void kill() {
-		if (process != null) {
-			process.destroy();
-			try {
-				process.waitFor();
-			} catch (InterruptedException e) {
-			}
-			process = null;
-		}
+		SystemUtil.killProcess(".*soffice.*" + id + ".*");
+		process = null;
 	}
 
 	/**
 	 * Kill all openoffice instances
 	 */
 	public static void killAll() {
-		if (SystemUtil.isWindows()) {
-			SystemUtil.execScript("taskkill /F /IM soffice.bin /IM soffice.exe");
-		} else {
-			SystemUtil.execScript("killall -9 soffice soffice.bin");
-		}
+		SystemUtil.killProcess(".*soffice.*");
 	}
 	
 	
@@ -263,13 +257,7 @@ public class OpenOffice {
 			return;
 		}
 		
-		String bin = home.getAbsolutePath() + File.separatorChar + "soffice.bin";
-//		if (SystemUtil.isWindows()) {
-//			bin = "\"" + home + "\\soffice.exe\"";
-//		} else {
-//			bin = "cd \"" + home + "\" ; ./soffice.bin";
-//		}
-		
+		String bin = home.getAbsolutePath() + File.separatorChar + (SystemUtil.isWindows() ? "soffice.exe"
: "soffice.bin");
 		ArrayList<String> cmds = new ArrayList<String>();
 		cmds.add(bin);
 		if (automationPort > 0) {

Modified: incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/SystemUtil.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/SystemUtil.java?rev=1360555&r1=1360554&r2=1360555&view=diff
==============================================================================
--- incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/SystemUtil.java
(original)
+++ incubator/ooo/trunk/main/test/testcommon/source/org/openoffice/test/common/SystemUtil.java
Thu Jul 12 07:17:27 2012
@@ -241,27 +241,38 @@ public class SystemUtil {
 	/**
 	 * Get the commands of all running processes
 	 * 
-	 * @return
+	 * @return List of <PID COMMAND>
 	 */
-	public static List<String> getProcesses() {
-		List<String> ret = new ArrayList<String>();
+	public static List<String[]> getProcesses() {
+		List<String[]> ret = new ArrayList<String[]>();
 		try {
 			StringBuffer output = new StringBuffer();
 			if (isWindows()) {
-				File file = File.createTempFile("ssssss", ".js");
-				String contents = "var e=new Enumerator(GetObject(\"winmgmts:\").InstancesOf(\"Win32_process\"));\n\r";
-				contents += "for (;!e.atEnd();e.moveNext()) {\n\r";
-				contents += "WScript.Echo(e.item ().CommandLine);}";
+				File file = FileUtil.getUniqueFile(SCRIPT_TEMP_DIR, "listp", ".js");
+				String contents = "var e=new Enumerator(GetObject(\"winmgmts:\").InstancesOf(\"Win32_process\"));\n\r"
+						+ "WScript.Echo(\"PID\" + \" \"  + \"COMMAND\");\n\r"
+						+ "for (;!e.atEnd();e.moveNext()) {\n\r"
+						+ "WScript.Echo(e.item().ProcessId + \" \"  + e.item().CommandLine);}";
 				FileUtil.writeStringToFile(file.getAbsolutePath(), contents);
-				// exec("cscript //Nologo \"" + file.getAbsolutePath() + "\"",
-				// output, output);
+				exec(new String[] { "cscript", "//Nologo", file.getAbsolutePath()}, null, null, output,
output);
 			} else {
-				// exec("ps -x -eo command", output, output);
+				exec(new String[] {"ps", "-eo", "pid,command"}, null, null, output, output);
 			}
 			BufferedReader reader = new BufferedReader(new StringReader(output.toString()));
 			String line = null;
+			reader.readLine();
 			while ((line = reader.readLine()) != null) {
-				ret.add(line);
+				line = line.trim();
+				String[] row = new String[2];
+				int i = line.indexOf(" ");
+				if (i > 0) {
+					row[0] = line.substring(0, i);
+					row[1] = line.substring(++i);
+				} else {
+					row[0] = line;
+					row[1] = null;
+				}
+				ret.add(row);
 			}
 		} catch (IOException e) {
 
@@ -270,6 +281,30 @@ public class SystemUtil {
 		return ret;
 	}
 
+	public static void killProcess(String pattern) {
+		List<String[]> processes = SystemUtil.getProcesses();
+		for (String[] p : processes) {
+			if (p[1] != null && p[1].matches(pattern)) {
+				if (isWindows()) {
+					exec(new String[] { "taskkill", "/F", "/PID", p[0] }, null, null, null, null);
+				} else {
+					exec(new String[] { "kill", "-9", p[0] }, null, null, null, null);
+				}
+			}
+		}
+	}
+	
+	public static boolean hasProcess(String pattern) {
+		List<String[]> processes = SystemUtil.getProcesses();
+		for (String[] p : processes) {
+			if (p[1] != null && p[1].matches(pattern)) {
+				return true;
+			}
+		}
+		
+		return false;
+	}
+	
 	/**
 	 * parse a string to arguments array.
 	 * 



Mime
View raw message