1 | |
package com.aurea.maven.plugins.sonic.utils; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.IOException; |
5 | |
import java.util.ArrayList; |
6 | |
import java.util.Iterator; |
7 | |
import java.util.List; |
8 | |
import java.util.Properties; |
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
public class JavaInvoker { |
14 | |
|
15 | |
|
16 | 0 | private String mainClass = null; |
17 | 0 | private String workDir = null; |
18 | 0 | private Properties jvmProps = null; |
19 | 0 | private List<String> classPath = null; |
20 | 0 | private List<String> bootClassPath = null; |
21 | 0 | private List<String> arguments = null; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | 0 | public JavaInvoker(final String _mainClass) { |
28 | |
|
29 | 0 | setMainClass(_mainClass); |
30 | 0 | } |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public final void setMainClass(final String _mainClass) { |
37 | |
|
38 | 0 | mainClass = _mainClass; |
39 | 0 | } |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public final void addToClassPath(final String _path) { |
46 | |
|
47 | 0 | if (classPath == null) { |
48 | 0 | classPath = new ArrayList<String>(); |
49 | |
} |
50 | 0 | classPath.add(_path); |
51 | 0 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public final void addToBootClassPath(final String _path) { |
58 | |
|
59 | 0 | if (bootClassPath == null) { |
60 | 0 | bootClassPath = new ArrayList<String>(); |
61 | |
} |
62 | 0 | bootClassPath.add(_path); |
63 | 0 | } |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public final void setProperty(final String _key, final String _value) { |
72 | |
|
73 | 0 | if (jvmProps == null) { |
74 | 0 | jvmProps = new Properties(); |
75 | |
} |
76 | 0 | jvmProps.setProperty(_key, _value); |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public final void addArgument(final String _arg) { |
84 | |
|
85 | 0 | if (arguments == null) { |
86 | 0 | arguments = new ArrayList<String>(); |
87 | |
} |
88 | 0 | arguments.add(_arg); |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public final void setWorkingDir(final String _dir) { |
96 | |
|
97 | 0 | workDir = _dir; |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
protected final String getWorkdir() { |
104 | |
|
105 | 0 | if (workDir != null) { |
106 | 0 | return workDir; |
107 | |
} else { |
108 | 0 | return System.getProperty("user.dir"); |
109 | |
} |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
@SuppressWarnings("unchecked") |
117 | |
public final void invoke() throws IOException { |
118 | |
|
119 | 0 | List<String> cmd = new ArrayList<String>(); |
120 | |
|
121 | 0 | boolean isWindows = (System.getProperty("os.name").startsWith("Windows")); |
122 | 0 | String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; |
123 | 0 | if (isWindows) { |
124 | 0 | java += ".exe"; |
125 | |
} |
126 | |
|
127 | 0 | cmd.add(java); |
128 | |
|
129 | 0 | if (jvmProps != null) { |
130 | 0 | for (Iterator it = jvmProps.keySet().iterator(); it.hasNext();) { |
131 | 0 | String key = (String) it.next(); |
132 | 0 | String value = jvmProps.getProperty(key); |
133 | 0 | cmd.add("-D" + key + "=" + value); |
134 | 0 | } |
135 | |
} |
136 | |
|
137 | 0 | String cp = assembleClasspath(bootClassPath); |
138 | 0 | if (cp != null) { |
139 | 0 | cmd.add("-Xbootclasspath:" + cp); |
140 | |
} |
141 | |
|
142 | 0 | cp = assembleClasspath(classPath); |
143 | 0 | if (cp != null) { |
144 | 0 | cmd.add("-classpath"); |
145 | 0 | cmd.add(cp); |
146 | |
} |
147 | |
|
148 | 0 | cmd.add(mainClass); |
149 | |
|
150 | 0 | if (arguments != null) { |
151 | 0 | cmd.addAll(arguments); |
152 | |
} |
153 | |
|
154 | 0 | ProcessBuilder process = new ProcessBuilder(cmd); |
155 | 0 | process.directory(new File(getWorkdir())); |
156 | 0 | process.redirectErrorStream(true); |
157 | |
|
158 | |
|
159 | 0 | process.start(); |
160 | 0 | } |
161 | |
|
162 | |
private String assembleClasspath(final List<String> _cpElements) { |
163 | |
|
164 | 0 | StringBuffer result = new StringBuffer(); |
165 | |
|
166 | 0 | if (_cpElements != null) { |
167 | 0 | for (Iterator<String> it = _cpElements.iterator(); it.hasNext();) { |
168 | 0 | String cpElem = it.next(); |
169 | 0 | if (result.length() > 0) { |
170 | 0 | result.append(File.pathSeparator); |
171 | |
} |
172 | 0 | result.append(cpElem); |
173 | 0 | } |
174 | |
} |
175 | |
|
176 | 0 | if (result.length() > 0) { |
177 | 0 | return result.toString(); |
178 | |
} else { |
179 | 0 | return null; |
180 | |
} |
181 | |
} |
182 | |
} |