001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.util;
022:
023: import java.io.*;
024:
025: import com.db4o.foundation.*;
026: import com.db4o.foundation.io.*;
027:
028: /**
029: * @sharpen.ignore
030: */
031: public class JavaServices {
032:
033: public static String java(String className) throws IOException,
034: InterruptedException {
035: return IOServices.exec(javaExecutable(),
036: javaRunArguments(className));
037: }
038:
039: public static String startAndKillJavaProcess(String className,
040: String expectedOutput, long timeout) throws IOException {
041: return IOServices.execAndDestroy(javaExecutable(),
042: javaRunArguments(className), expectedOutput, timeout);
043: }
044:
045: private static String javaExecutable() {
046: for (int i = 0; i < vmTypes.length; i++) {
047: if (vmTypes[i].identified()) {
048: return vmTypes[i].executable();
049: }
050: }
051: throw new NotImplementedException(
052: "VM "
053: + vmName()
054: + " not known. Please add as JavaVM class to end of JavaServices class.");
055: }
056:
057: private static String[] javaRunArguments(String className) {
058: return new String[] { "-cp", currentClassPath(), className };
059: }
060:
061: private static String currentClassPath() {
062: return property("java.class.path");
063: }
064:
065: static String javaHome() {
066: return property("java.home");
067: }
068:
069: static String vmName() {
070: return property("java.vm.name");
071: }
072:
073: static String property(String propertyName) {
074: return System.getProperty(propertyName);
075: }
076:
077: private static final JavaVM[] vmTypes = new JavaVM[] { new J9(),
078: new SunWindows(), };
079:
080: static interface JavaVM {
081: boolean identified();
082:
083: String executable();
084: }
085:
086: static class SunWindows implements JavaVM {
087: public String executable() {
088: return Path4.combine(Path4.combine(javaHome(), "bin"),
089: "java");
090: }
091:
092: public boolean identified() {
093: return true;
094: }
095: }
096:
097: static class J9 implements JavaVM {
098: public String executable() {
099:
100: // The following does start J9, but it produces an error:
101: // JVMJ9VM011W Unable to load jclfoun10_23: The specified module could not be found.
102:
103: return property("com.ibm.oti.vm.exe");
104: }
105:
106: public boolean identified() {
107: return false;
108: // return vmName().equals("J9");
109: }
110:
111: }
112:
113: }
|