blob: 71146fa7b4e6343b1fba2fb8c5f1f6938f64c5a2 [file] [log] [blame]
Guillaume Nodet05fac962009-04-27 10:01:58 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package org.apache.servicemix.jpm.impl;
18
19import java.io.BufferedReader;
20import java.io.File;
21import java.io.FileInputStream;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25import java.io.InterruptedIOException;
26import java.util.HashMap;
27import java.util.Map;
28
29import org.apache.servicemix.jpm.Process;
30
31public class ProcessImpl implements Process {
32
33 private int pid;
34 //private File input;
35 //private File output;
36 //private File error;
37
38 public ProcessImpl(int pid/*, File input, File output, File error*/) {
39 this.pid = pid;
40 //this.input = input;
41 //this.output = output;
42 //this.error = error;
43 }
44
45 public int getPid() {
46 return pid;
47 }
48
49 public boolean isRunning() throws IOException {
50 if (ScriptUtils.isWindows()) {
51 Map<String, String> props = new HashMap<String, String>();
52 props.put("${pid}", Integer.toString(pid));
53 int ret = ScriptUtils.execute("running", props);
54 return ret == 0;
55 } else {
56 try {
57 java.lang.Process process = new java.lang.ProcessBuilder("ps", "-p", Integer.toString(pid)).start();
58 BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));
59 r.readLine(); // skip headers
60 String s = r.readLine();
61 boolean running = s != null && s.length() > 0;
62 int ret = process.waitFor();
63 return running;
64 } catch (InterruptedException e) {
65 throw new InterruptedIOException();
66 }
67 }
68 }
69
70 public void destroy() throws IOException {
71 int ret;
72 if (ScriptUtils.isWindows()) {
73 Map<String, String> props = new HashMap<String, String>();
74 props.put("${pid}", Integer.toString(pid));
75 ret = ScriptUtils.execute("destroy", props);
76 } else {
77 ret = ScriptUtils.executeProcess(new java.lang.ProcessBuilder("kill", "-9", Integer.toString(pid)));
78 }
79 if (ret != 0) {
80 throw new IOException("Unable to destroy proces, it may be already terminated");
81 }
82 }
83
84 /*
85 public OutputStream getInputStream() throws FileNotFoundException {
86 return new FileOutputStream(input);
87 }
88
89 public InputStream getOutputStream() throws FileNotFoundException {
90 return new FileInputStream(output);
91 }
92
93 public InputStream getErrorStream() throws FileNotFoundException {
94 return new FileInputStream(error);
95 }
96 */
97
98 public int waitFor() throws InterruptedException {
99 return 0;
100 }
101
102 public int exitValue() {
103 return 0;
104 }
105
106 public static Process create(File dir, String command) throws IOException {
107 //File input = File.createTempFile("jpm.", ".input");
108 //File output = File.createTempFile("jpm.", ".output");
109 //File error = File.createTempFile("jpm.", ".error");
110 File pidFile = File.createTempFile("jpm.", ".pid");
111 try {
112 Map<String, String> props = new HashMap<String, String>();
113 //props.put("${in.file}", input.getCanonicalPath());
114 //props.put("${out.file}", output.getCanonicalPath());
115 //props.put("${err.file}", error.getCanonicalPath());
116 props.put("${pid.file}", pidFile.getCanonicalPath());
117 props.put("${dir}", dir != null ? dir.getCanonicalPath() : "");
118 if (ScriptUtils.isWindows()) {
119 command = command.replaceAll("\"", "\"\"");
120 }
121 props.put("${command}", command);
122 int ret = ScriptUtils.execute("start", props);
123 if (ret != 0) {
124 throw new IOException("Unable to create process (error code: " + ret + ")");
125 }
126 int pid = readPid(pidFile);
127 return new ProcessImpl(pid/*, input, output, error*/);
128 } finally {
129 pidFile.delete();
130 }
131 }
132
133 public static Process attach(int pid) throws IOException {
134 return new ProcessImpl(pid);
135 }
136
137 private static int readPid(File pidFile) throws IOException {
138 InputStream is = new FileInputStream(pidFile);
139 try {
140 BufferedReader r = new BufferedReader(new InputStreamReader(is));
141 String pidString = r.readLine();
142 return Integer.valueOf(pidString);
143 } finally {
144 try {
145 is.close();
146 } catch (IOException e) {}
147 }
148 }
149
150}