blob: 869c958893acb9e40b2d76b1333069bff9f24952 [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.File;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.InterruptedIOException;
24import java.io.OutputStream;
25import java.io.PrintStream;
26import java.util.Map;
27import java.util.Scanner;
28
29public class ScriptUtils {
30
31 public static int execute(String name, Map<String, String> props) throws IOException {
32 File script = File.createTempFile("jpm.", ".script");
33 try {
34 if (isWindows()) {
35 String res = "windows/" + name + ".vbs";
36 ScriptUtils.copyFilteredResource(res, script, props);
37 return executeProcess(new java.lang.ProcessBuilder("cscript",
38 "/NOLOGO",
39 "//E:vbs",
40 script.getCanonicalPath()));
41 } else {
42 String res = "unix/" + name + ".sh";
43 ScriptUtils.copyFilteredResource(res, script, props);
44 return executeProcess(new java.lang.ProcessBuilder("/bin/sh",
45 script.getCanonicalPath()));
46 }
47 } finally {
48 script.delete();
49 }
50 }
51
52 public static int executeProcess(java.lang.ProcessBuilder builder) throws IOException {
53 try {
54 java.lang.Process process = builder.start();
55 return process.waitFor();
56 } catch (InterruptedException e) {
57 throw new InterruptedIOException();
58 }
59 }
60
61 public static void copyFilteredResource(String resource, File outFile, Map<String, String> props) throws IOException {
62 InputStream is = null;
63 try {
64 is = ScriptUtils.class.getResourceAsStream(resource);
65 // Read it line at a time so that we can use the platform line ending when we write it out.
66 PrintStream out = new PrintStream(new FileOutputStream(outFile));
67 try {
68 Scanner scanner = new Scanner(is);
69 while (scanner.hasNextLine() ) {
70 String line = scanner.nextLine();
71 line = filter(line, props);
72 out.println(line);
73 }
74 } finally {
75 safeClose(out);
76 }
77 } finally {
78 safeClose(is);
79 }
80 }
81
82 private static void safeClose(InputStream is) throws IOException {
83 if (is == null) {
84 return;
85 }
86 try {
87 is.close();
88 } catch (Throwable ignore) {
89 }
90 }
91
92 private static void safeClose(OutputStream is) throws IOException {
93 if (is == null) {
94 return;
95 }
96 try {
97 is.close();
98 } catch (Throwable ignore) {
99 }
100 }
101
102 private static String filter(String line, Map<String, String> props) {
103 for (Map.Entry<String, String> i : props.entrySet()) {
104 int p1 = line.indexOf(i.getKey());
105 if( p1 >= 0 ) {
106 String l1 = line.substring(0, p1);
107 String l2 = line.substring(p1+i.getKey().length());
108 line = l1+i.getValue()+l2;
109 }
110 }
111 return line;
112 }
113
114 private static final boolean windows;
115
116 static {
117 windows = System.getProperty("os.name").toLowerCase().indexOf("windows") != -1;
118 }
119
120 public static boolean isWindows() {
121 return windows;
122 }
123
124}