1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.launcher;
19
20 import java.awt.Frame;
21 import java.awt.Image;
22 import java.awt.Rectangle;
23 import java.awt.Toolkit;
24 import java.awt.event.WindowAdapter;
25 import java.awt.event.WindowEvent;
26 import java.io.FileOutputStream;
27 import java.io.PrintStream;
28 import java.lang.reflect.Method;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class ChildMain extends Thread {
44
45
46
47
48
49
50 public final static String APPEND_OUTPUT_PROP_NAME =
51 "org.apache.commons.launcher.appendOutput";
52
53
54
55
56 public final static String DISPLAY_MINIMIZED_WINDOW_PROP_NAME =
57 "org.apache.commons.launcher.displayMinimizedWindow";
58
59
60
61
62 public final static String DISPOSE_MINIMIZED_WINDOW_PROP_NAME =
63 "org.apache.commons.launcher.disposeMinimizedWindow";
64
65
66
67
68 public final static String EXECUTABLE_PROP_NAME =
69 "org.apache.commons.launcher.executableName";
70
71
72
73
74 public final static String HEARTBEAT_FILE_PROP_NAME =
75 "org.apache.commons.launcher.heartbeatFile";
76
77
78
79
80 public final static String MINIMIZED_WINDOW_TITLE_PROP_NAME =
81 "org.apache.commons.launcher.minimizedWindowTitle";
82
83
84
85
86 public final static String MINIMIZED_WINDOW_ICON_PROP_NAME=
87 "org.apache.commons.launcher.minimizedWindowIcon";
88
89
90
91
92 public final static String OUTPUT_FILE_PROP_NAME =
93 "org.apache.commons.launcher.outputFile";
94
95
96
97
98 public final static String WAIT_FOR_CHILD_PROP_NAME =
99 "org.apache.commons.launcher.waitForChild";
100
101
102
103
104
105
106 private String[] args = null;
107
108
109
110
111
112
113
114
115
116
117 private ChildMain(ThreadGroup group, String[] args) {
118
119 super(group, ChildMain.class.getName());
120 this.args = args;
121
122 }
123
124
125
126
127
128
129
130
131
132 public static void main(String[] args) {
133
134
135
136 Thread mainThread = new ChildMain(new ExitOnErrorThreadGroup(ChildMain.class.getName()), args);
137 mainThread.start();
138
139 }
140
141
142
143
144
145
146 public void run() {
147
148
149 if (args == null || args.length == 0)
150 return;
151
152
153 try {
154
155
156 boolean waitForChild = false;
157 if (System.getProperty(ChildMain.WAIT_FOR_CHILD_PROP_NAME) != null) {
158 waitForChild = true;
159 String heartbeatFile = System.getProperty(ChildMain.HEARTBEAT_FILE_PROP_NAME);
160 ParentListener heartbeat = new ParentListener(heartbeatFile);
161
162
163
164 heartbeat.setDaemon(true);
165 heartbeat.start();
166 }
167
168
169 String outputPath = System.getProperty(ChildMain.OUTPUT_FILE_PROP_NAME);
170 if (outputPath != null) {
171 boolean appendOutput = false;
172 if (System.getProperty(ChildMain.APPEND_OUTPUT_PROP_NAME) != null)
173 appendOutput = true;
174 PrintStream ps = new PrintStream(new FileOutputStream(outputPath, appendOutput), true);
175 System.setOut(ps);
176 System.setErr(ps);
177 }
178
179
180
181
182 Class mainClass = Class.forName(args[0]);
183 Class[] paramTypes = new Class[1];
184 Object[] paramValues = new Object[1];
185 String[] params = new String[args.length - 1];
186
187 for (int i = 0; i < params.length; i++)
188 params[i] = args[i + 1];
189 paramTypes[0] = params.getClass();
190 paramValues[0] = params;
191
192
193 Frame frame = null;
194 boolean displayMinimizedWindow = false;
195 if (System.getProperty(ChildMain.DISPLAY_MINIMIZED_WINDOW_PROP_NAME) != null)
196 displayMinimizedWindow = true;
197 String osname = System.getProperty("os.name").toLowerCase();
198 if (displayMinimizedWindow && osname.indexOf("windows") >= 0) {
199 try {
200 frame = new Frame();
201 String title = System.getProperty(ChildMain.MINIMIZED_WINDOW_TITLE_PROP_NAME);
202 if (title != null)
203 frame.setTitle(title);
204 frame.setState(Frame.ICONIFIED);
205 String icon = System.getProperty(ChildMain.MINIMIZED_WINDOW_ICON_PROP_NAME);
206 if (icon != null) {
207 Image iconImage = Toolkit.getDefaultToolkit().createImage(icon);
208 if (iconImage != null)
209 frame.setIconImage(iconImage);
210 }
211
212
213 frame.addWindowListener(new ChildWindowAdapter());
214 Rectangle bounds = frame.getGraphicsConfiguration().getBounds();
215 int width = (int)frame.getBounds().getWidth();
216 int height = frame.getInsets().top + frame.getInsets().bottom;
217 int x = (int)bounds.getWidth() - width;
218 int y = (int)bounds.getHeight() - height;
219 frame.setBounds(x, y, width, height);
220 frame.setResizable(false);
221 frame.setVisible(true);
222 } catch(Exception fe) {}
223 }
224
225
226 Method mainMethod = mainClass.getDeclaredMethod("main", paramTypes);
227 mainMethod.invoke(null, paramValues);
228
229
230 if (frame != null && System.getProperty(ChildMain.DISPOSE_MINIMIZED_WINDOW_PROP_NAME) != null) {
231
232
233 System.exit(0);
234 }
235
236 } catch (Throwable t) {
237 String message = t.getMessage();
238 t.printStackTrace();
239 System.exit(1);
240 }
241
242 }
243
244
245
246
247
248 private static class ChildWindowAdapter extends WindowAdapter {
249
250
251
252
253
254
255 public void windowClosing(WindowEvent e) {
256
257 System.exit(0);
258
259 }
260
261 }
262
263 }