001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Vasily Zakharov
021: * @version $Revision: 1.1.2.5 $
022: */package org.apache.harmony.rmi;
023:
024: import java.io.File;
025: import java.io.IOException;
026:
027: import java.util.Arrays;
028: import java.util.Vector;
029:
030: import org.apache.harmony.rmi.common.SubProcess;
031:
032: /**
033: * Invoker for Virtual Machine.
034: *
035: * More methods running VM in various configurations are welcome.
036: *
037: * @author Vasily Zakharov
038: * @version $Revision: 1.1.2.5 $
039: *
040: * @todo Probably in future should be replaced by VM-specific calls
041: * allowing to create VM without using heavy and system-dependent
042: * <code>exec()</code> calls.
043: */
044: public final class JavaInvoker {
045:
046: /**
047: * Home path.
048: */
049: private static final String javaHome = System
050: .getProperty("java.home");
051:
052: /**
053: * Class path.
054: */
055: private static final String javaClassPath = System
056: .getProperty("java.class.path");
057:
058: /**
059: * Security policy file.
060: */
061: private static final String policy = System
062: .getProperty("java.security.policy");
063:
064: /**
065: * Endorsed directories.
066: */
067: private static final String javaEndorsedDirs = System
068: .getProperty("java.endorsed.dirs");
069:
070: /**
071: * Boot class path, initialized in static block.
072: */
073: private static final String bootClassPath;
074:
075: /**
076: * Executable location, initialized in static block.
077: */
078: private static final String executable;
079:
080: /**
081: * Static block initializing {@link #bootClassPath} and {@link #executable}.
082: */
083: static {
084: String vmName = System.getProperty("java.vm.name");
085:
086: bootClassPath = System
087: .getProperty(vmName.equals("J9") ? "org.apache.harmony.boot.class.path"
088: : "sun.boot.class.path");
089:
090: executable = new File(new File(javaHome, "bin"), "java")
091: .getPath();
092: }
093:
094: /**
095: * Creates args array for Java invocation.
096: *
097: * @param options
098: * Java options.
099: *
100: * @param className
101: * Name of the class to run.
102: *
103: * @param params
104: * Parameters to pass to the class.
105: *
106: * @param useEndorsedDirs
107: * If endorsed dirs (<code>-Djava.endorsed.dirs</code>)
108: * from this VM should be applied.
109: *
110: * @param useBootClassPath
111: * If bootclasspath (<code>-Xbootclasspath</code>)
112: * from this VM should be applied.
113: *
114: * @return Generated args array.
115: */
116: private static String[] createArgsArray(String[] options,
117: String className, String[] params, boolean useEndorsedDirs,
118: boolean useBootClassPath) {
119: Vector args = new Vector();
120: String useJavaClassPath;
121:
122: // Add name of Java executable to run.
123: args.add(executable);
124:
125: if (useEndorsedDirs) {
126: if (javaEndorsedDirs != null) {
127: // Apply endorsed dirs.
128: args.add("-Djava.endorsed.dirs=" + javaEndorsedDirs);
129: }
130: }
131:
132: if (bootClassPath != null) {
133: if (useBootClassPath) {
134: // Apply bootclasspath.
135: args.add("-Xbootclasspath:" + bootClassPath);
136: useJavaClassPath = javaClassPath;
137: } else if (javaClassPath != null) { // Class path is set.
138: // Append bootclasspath to classpath.
139: useJavaClassPath = (javaClassPath + File.pathSeparator + bootClassPath);
140: } else { // Class path is not set.
141: // Set class path to bootclasspath.
142: useJavaClassPath = bootClassPath;
143: }
144: } else {
145: useJavaClassPath = javaClassPath;
146: }
147:
148: // Apply classpath.
149: if (useJavaClassPath != null) {
150: args.add("-classpath");
151: args.add(useJavaClassPath);
152: }
153:
154: if (policy != null) {
155: // Apply security policy.
156: args.add("-Djava.security.policy=" + policy);
157: }
158:
159: if (options != null) {
160: args.addAll(Arrays.asList(options));
161: }
162:
163: if (className != null) {
164: args.add(className);
165: }
166:
167: if (params != null) {
168: args.addAll(Arrays.asList(params));
169: }
170:
171: return (String[]) args.toArray(new String[args.size()]);
172: }
173:
174: /**
175: * Invokes Java machine with configuration similar to the current one.
176: * The properties of current VM are extracted from system properties
177: * and passed to new VM.
178: *
179: * @param options
180: * Java options.
181: *
182: * @param className
183: * Name of the class to run.
184: *
185: * @param params
186: * Parameters to pass to the class.
187: *
188: * @param useEndorsedDirs
189: * If endorsed dirs (<code>-Djava.endorsed.dirs</code>)
190: * from this VM should be applied.
191: *
192: * @param useBootClassPath
193: * If bootclasspath (<code>-Xbootclasspath</code>)
194: * from this VM should be applied.
195: *
196: * @return The process created.
197: *
198: * @throws IOException
199: * If some error occurs.
200: */
201: public static SubProcess invokeSimilar(String[] options,
202: String className, String[] params, boolean useEndorsedDirs,
203: boolean useBootClassPath) throws IOException {
204: // @ToDo: Rewrite with ProcessBuilder for Java 5.0.
205: return new SubProcess(createArgsArray(options, className,
206: params, useEndorsedDirs, useBootClassPath));
207: }
208: }
|