1 16 17 package org.apache.commons.launcher; 18 19 import java.awt.Frame ; 20 import java.awt.Image ; 21 import java.awt.Rectangle ; 22 import java.awt.Toolkit ; 23 import java.awt.event.WindowAdapter ; 24 import java.awt.event.WindowEvent ; 25 import java.io.FileOutputStream ; 26 import java.io.PrintStream ; 27 import java.lang.reflect.Method ; 28 29 42 public class ChildMain extends Thread { 43 44 46 49 public final static String APPEND_OUTPUT_PROP_NAME = 50 "org.apache.commons.launcher.appendOutput"; 51 52 55 public final static String DISPLAY_MINIMIZED_WINDOW_PROP_NAME = 56 "org.apache.commons.launcher.displayMinimizedWindow"; 57 58 61 public final static String DISPOSE_MINIMIZED_WINDOW_PROP_NAME = 62 "org.apache.commons.launcher.disposeMinimizedWindow"; 63 64 67 public final static String EXECUTABLE_PROP_NAME = 68 "org.apache.commons.launcher.executableName"; 69 70 73 public final static String HEARTBEAT_FILE_PROP_NAME = 74 "org.apache.commons.launcher.heartbeatFile"; 75 76 79 public final static String MINIMIZED_WINDOW_TITLE_PROP_NAME = 80 "org.apache.commons.launcher.minimizedWindowTitle"; 81 82 85 public final static String MINIMIZED_WINDOW_ICON_PROP_NAME= 86 "org.apache.commons.launcher.minimizedWindowIcon"; 87 88 91 public final static String OUTPUT_FILE_PROP_NAME = 92 "org.apache.commons.launcher.outputFile"; 93 94 97 public final static String WAIT_FOR_CHILD_PROP_NAME = 98 "org.apache.commons.launcher.waitForChild"; 99 100 102 105 private String [] args = null; 106 107 109 116 private ChildMain(ThreadGroup group, String [] args) { 117 118 super(group, ChildMain.class.getName()); 119 this.args = args; 120 121 } 122 123 125 131 public static void main(String [] args) { 132 133 Thread mainThread = new ChildMain(new ExitOnErrorThreadGroup(ChildMain.class.getName()), args); 136 mainThread.start(); 137 138 } 139 140 142 147 public void run() { 148 149 if (args == null || args.length == 0) 151 return; 152 153 try { 155 156 boolean waitForChild = false; 158 if (System.getProperty(ChildMain.WAIT_FOR_CHILD_PROP_NAME) != null) { 159 waitForChild = true; 160 String heartbeatFile = System.getProperty(ChildMain.HEARTBEAT_FILE_PROP_NAME); 161 ParentListener heartbeat = new ParentListener(heartbeatFile); 162 heartbeat.setDaemon(true); 166 heartbeat.start(); 167 } 168 169 String outputPath = System.getProperty(ChildMain.OUTPUT_FILE_PROP_NAME); 171 if (outputPath != null) { 172 boolean appendOutput = false; 173 if (System.getProperty(ChildMain.APPEND_OUTPUT_PROP_NAME) != null) 174 appendOutput = true; 175 PrintStream ps = new PrintStream (new FileOutputStream (outputPath, appendOutput), true); 176 System.setOut(ps); 177 System.setErr(ps); 178 } 179 180 Class mainClass = Class.forName(args[0]); 184 Class [] paramTypes = new Class [1]; 185 Object [] paramValues = new Object [1]; 186 String [] params = new String [args.length - 1]; 187 for (int i = 0; i < params.length; i++) 189 params[i] = args[i + 1]; 190 paramTypes[0] = params.getClass(); 191 paramValues[0] = params; 192 193 Frame frame = null; 195 boolean displayMinimizedWindow = false; 196 if (System.getProperty(ChildMain.DISPLAY_MINIMIZED_WINDOW_PROP_NAME) != null) 197 displayMinimizedWindow = true; 198 String osname = System.getProperty("os.name").toLowerCase(); 199 if (displayMinimizedWindow && osname.indexOf("windows") >= 0) { 200 try { 201 frame = new Frame (); 202 String title = System.getProperty(ChildMain.MINIMIZED_WINDOW_TITLE_PROP_NAME); 203 if (title != null) 204 frame.setTitle(title); 205 frame.setState(Frame.ICONIFIED); 206 String icon = System.getProperty(ChildMain.MINIMIZED_WINDOW_TITLE_PROP_NAME); 207 if (icon != null) { 208 Image iconImage = Toolkit.getDefaultToolkit().createImage(icon); 209 if (iconImage != null) 210 frame.setIconImage(iconImage); 211 } 212 213 frame.addWindowListener(new ChildWindowAdapter()); 215 Rectangle bounds = frame.getGraphicsConfiguration().getBounds(); 216 int width = (int)frame.getBounds().getWidth(); 217 int height = frame.getInsets().top + frame.getInsets().bottom; 218 int x = (int)bounds.getWidth() - width; 219 int y = (int)bounds.getHeight() - height; 220 frame.setBounds(x, y, width, height); 221 frame.setResizable(false); 222 frame.setVisible(true); 223 } catch(Exception fe) {} 224 } 225 226 Method mainMethod = mainClass.getDeclaredMethod("main", paramTypes); 228 mainMethod.invoke(null, paramValues); 229 230 if (frame != null && System.getProperty(ChildMain.DISPOSE_MINIMIZED_WINDOW_PROP_NAME) != null) { 232 System.exit(0); 235 } 236 237 } catch (Throwable t) { 238 String message = t.getMessage(); 239 t.printStackTrace(); 240 System.exit(1); 241 } 242 243 } 244 245 249 private static class ChildWindowAdapter extends WindowAdapter { 250 251 256 public void windowClosing(WindowEvent e) { 257 258 System.exit(0); 259 260 } 261 262 } 263 264 } 265 | Popular Tags |