1 11 12 package org.eclipse.osgi.framework.launcher; 13 14 import java.lang.reflect.Constructor ; 15 import java.util.*; 16 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor; 17 import org.eclipse.osgi.framework.internal.core.*; 18 import org.eclipse.osgi.util.NLS; 19 20 79 public class Launcher { 80 81 82 protected String consolePort = ""; 84 85 protected boolean console = false; 86 87 88 protected String adaptorClassName = "org.eclipse.osgi.baseadaptor.BaseAdaptor"; 90 protected final String osgiConsoleClazz = "org.eclipse.osgi.framework.internal.core.FrameworkConsole"; 92 93 protected String [] adaptorArgs = null; 94 95 96 private static final String OSGI_CONSOLE_COMPONENT_NAME = "OSGi Console"; private static final String OSGI_CONSOLE_COMPONENT = "console.jar"; 99 105 public static void main(String args[]) { 106 107 new Launcher().doIt(args); 108 109 } 110 111 114 public Launcher() { 115 } 117 118 123 protected void doIt(String [] args) { 124 if (FrameworkProperties.getProperty(Constants.OSGI_COMPATIBILITY_BOOTDELEGATION) == null) 126 FrameworkProperties.setProperty(Constants.OSGI_COMPATIBILITY_BOOTDELEGATION, "false"); String [] consoleArgs = parseArgs(args); 128 129 FrameworkAdaptor adaptor = null; 130 try { 131 adaptor = doAdaptor(); 132 } catch (Exception e) { 133 System.out.println(Msg.LAUNCHER_ADAPTOR_ERROR); 134 e.printStackTrace(); 135 return; 136 } 137 138 OSGi osgi = doOSGi(adaptor); 139 if (osgi != null) { 140 if (console) { 141 doConsole(osgi, consoleArgs); 142 } else { 143 osgi.launch(); 144 } 145 } 146 } 147 148 154 protected String [] parseArgs(String [] args) { 155 Vector consoleArgsVector = new Vector(); 156 for (int i = 0; i < args.length; i++) { 157 boolean match = false; 158 159 String fullarg = args[i]; 163 int quoteidx = fullarg.indexOf("\""); if (quoteidx > 0) { 165 if (quoteidx == fullarg.lastIndexOf("\"")) { boolean stillparsing = true; 167 i++; 168 while (i < args.length && stillparsing) { 169 fullarg = fullarg + " " + args[i]; i++; 171 if (quoteidx < fullarg.lastIndexOf("\"")) { stillparsing = false; 173 } 174 } 175 } 176 } else { 177 quoteidx = fullarg.indexOf("'"); if (quoteidx > 0) { 180 if (quoteidx == fullarg.lastIndexOf("'")) { boolean stillparsing = true; 182 i++; 183 while (i < args.length && stillparsing) { 184 fullarg = fullarg + " " + args[i]; i++; 186 if (quoteidx < fullarg.lastIndexOf("'")) { stillparsing = false; 188 } 189 } 190 } 191 fullarg = fullarg.replace('\'', '\"'); 192 } 193 } 194 195 Tokenizer tok = new Tokenizer(fullarg); 196 if (tok.hasMoreTokens()) { 197 String command = tok.getString(" "); StringTokenizer subtok = new StringTokenizer(command, ":"); String subcommand = subtok.nextToken().toLowerCase(); 200 201 if (matchCommand("-console", subcommand, 4)) { _console(command); 203 match = true; 204 } 205 if (matchCommand("-adaptor", subcommand, 2)) { _adaptor(command); 207 match = true; 208 } 209 if (match == false) { 210 consoleArgsVector.addElement(fullarg); 213 } 214 } 215 } 216 String [] consoleArgsArray = new String [consoleArgsVector.size()]; 218 Enumeration e = consoleArgsVector.elements(); 219 for (int i = 0; i < consoleArgsArray.length; i++) { 220 consoleArgsArray[i] = (String ) e.nextElement(); 221 } 222 return consoleArgsArray; 223 } 224 225 public static boolean matchCommand(String command, String input, int minLength) { 226 if (minLength <= 0) { 227 minLength = command.length(); 228 } 229 230 int length = input.length(); 231 232 if (minLength > length) { 233 length = minLength; 234 } 235 236 return (command.regionMatches(0, input, 0, length)); 237 } 238 239 242 protected void _console(String command) { 243 console = true; 244 StringTokenizer tok = new StringTokenizer(command, ":"); tok.nextToken(); 247 if (tok.hasMoreTokens()) { 248 consolePort = tok.nextToken(); 249 } 250 } 251 252 259 protected void _adaptor(String command) { 260 Tokenizer tok = new Tokenizer(command); 261 tok.getToken(":"); tok.getChar(); String adp = tok.getToken(":"); if (adp != null && adp.length() > 0) { 267 adaptorClassName = adp; 268 } 269 270 Vector v = new Vector(); 274 parseloop: while (true) { 275 tok.getChar(); String arg = tok.getString(":"); if (arg == null) 278 break parseloop; 279 v.addElement(arg); 280 } 281 if (v != null) { 283 int numArgs = v.size(); 284 adaptorArgs = new String [numArgs]; 285 Enumeration e = v.elements(); 286 for (int i = 0; i < numArgs; i++) { 287 adaptorArgs[i] = (String ) e.nextElement(); 288 } 289 } 290 } 291 292 300 protected FrameworkAdaptor doAdaptor() throws Exception { 301 302 Class adaptorClass = Class.forName(adaptorClassName); 303 Class [] constructorArgs = new Class [] {String [].class}; 304 Constructor constructor = adaptorClass.getConstructor(constructorArgs); 305 return (FrameworkAdaptor) constructor.newInstance(new Object [] {adaptorArgs}); 306 } 307 308 313 private OSGi doOSGi(FrameworkAdaptor adaptor) { 314 return new OSGi(adaptor); 315 } 316 317 324 private void doConsole(OSGi osgi, String [] consoleArgs) { 325 326 Constructor consoleConstructor; 327 Object osgiconsole; 328 Class [] parameterTypes; 329 Object [] parameters; 330 331 try { 332 Class osgiconsoleClass = Class.forName(osgiConsoleClazz); 333 if (consolePort.length() == 0) { 334 parameterTypes = new Class [] {OSGi.class, String [].class}; 335 parameters = new Object [] {osgi, consoleArgs}; 336 } else { 337 parameterTypes = new Class [] {OSGi.class, int.class, String [].class}; 338 parameters = new Object [] {osgi, new Integer (consolePort), consoleArgs}; 339 } 340 consoleConstructor = osgiconsoleClass.getConstructor(parameterTypes); 341 osgiconsole = consoleConstructor.newInstance(parameters); 342 343 Thread t = new Thread (((Runnable ) osgiconsole), OSGI_CONSOLE_COMPONENT_NAME); 344 t.start(); 345 } catch (NumberFormatException nfe) { 346 System.err.println(NLS.bind(Msg.LAUNCHER_INVALID_PORT, consolePort)); 347 } catch (Exception ex) { 348 informAboutMissingComponent(OSGI_CONSOLE_COMPONENT_NAME, OSGI_CONSOLE_COMPONENT); 349 } 350 351 } 352 353 359 void informAboutMissingComponent(String component, String jar) { 360 System.out.println(); 361 System.out.print(NLS.bind(Msg.LAUNCHER_COMPONENT_MISSING, component)); 362 System.out.println(NLS.bind(Msg.LAUNCHER_COMPONENT_JAR, jar)); 363 System.out.println(); 364 } 365 } 366 | Popular Tags |