1 22 package org.jboss; 23 24 import gnu.getopt.Getopt; 25 import gnu.getopt.LongOpt; 26 27 import java.io.File ; 28 import java.io.FilenameFilter ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.net.URLDecoder ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 import java.util.Properties ; 35 36 import org.jboss.system.server.Server; 37 import org.jboss.system.server.ServerConfig; 38 import org.jboss.system.server.ServerConfigUtil; 39 import org.jboss.system.server.ServerLoader; 40 41 61 public class Main 62 { 63 64 private String concurrentLib = "concurrent.jar"; 65 66 67 private URL bootURL; 68 69 72 private List bootLibraries = new LinkedList (); 73 74 75 private List extraLibraries = new LinkedList (); 76 77 78 private List extraClasspath = new LinkedList (); 79 80 85 private Properties props = new Properties (System.getProperties()); 86 87 private Server server; 88 89 92 public Main() 93 { 94 super(); 95 } 96 97 101 public Server getServer() 102 { 103 return server; 104 } 105 106 113 public void boot(final String [] args) throws Exception 114 { 115 processCommandLine(args); 117 118 String homeDir = props.getProperty(ServerConfig.HOME_DIR); 120 if (homeDir == null) 121 { 122 String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getFile(); 123 127 path = URLDecoder.decode(path, "UTF-8"); 128 File runJar = new File (path); 129 File homeFile = runJar.getParentFile().getParentFile(); 130 homeDir = homeFile.getCanonicalPath(); 131 } 132 props.setProperty(ServerConfig.HOME_DIR, homeDir); 133 134 String homeURL = props.getProperty(ServerConfig.HOME_URL); 136 if (homeURL == null) 137 { 138 File file = new File (homeDir); 139 homeURL = file.toURL().toString(); 140 props.setProperty(ServerConfig.HOME_URL, homeURL); 141 } 142 143 ServerLoader loader = new ServerLoader(props); 145 146 150 if (bootURL != null) 151 { 152 if (bootURL.getProtocol().equals("file")) 153 { 154 File dir = new File (bootURL.getFile()); 155 if (dir.exists()) 156 { 157 loader.addURL(dir.toURL()); 159 160 File [] jars = dir.listFiles(new JarFilter()); 162 163 for (int j = 0; jars != null && j < jars.length; j++) 164 { 165 loader.addURL(jars[j].getCanonicalFile().toURL()); 166 } 167 } 168 } 169 else 170 { 171 loader.addURL(bootURL); 172 } 173 } 174 175 for (int i = 0; i < bootLibraries.size(); i++) 177 { 178 loader.addLibrary((String )bootLibraries.get(i)); 179 } 180 181 loader.addEndorsedJars(); 183 184 187 loader.addLibrary(concurrentLib); 189 190 for (int i = 0; i < extraLibraries.size(); i++) 192 { 193 loader.addLibrary((String )extraLibraries.get(i)); 194 } 195 196 for (int i = 0; i < extraClasspath.size(); i++) 198 { 199 loader.addURL((URL )extraClasspath.get(i)); 200 } 201 202 ClassLoader parentCL = Thread.currentThread().getContextClassLoader(); 204 server = loader.load(parentCL); 205 206 server.init(props); 208 209 server.start(); 211 } 212 213 217 public void shutdown() 218 { 219 server.shutdown(); 220 } 221 222 private URL makeURL(String urlspec) throws MalformedURLException 223 { 224 urlspec = urlspec.trim(); 225 226 URL url; 227 228 try 229 { 230 url = new URL (urlspec); 231 if (url.getProtocol().equals("file")) 232 { 233 File file = new File (url.getFile()).getCanonicalFile(); 235 url = file.toURL(); 236 } 237 } 238 catch (Exception e) 239 { 240 try 242 { 243 File file = new File (urlspec).getCanonicalFile(); 244 url = file.toURL(); 245 } 246 catch (Exception n) 247 { 248 throw new MalformedURLException (n.toString()); 249 } 250 } 251 252 return url; 253 } 254 255 256 private void processCommandLine(final String [] args) throws Exception 257 { 258 String programName = System.getProperty("program.name", "jboss"); 260 String sopts = "-:hD:d:p:n:c:Vj::B:L:C:P:b:g:u:l:"; 261 LongOpt[] lopts = 262 { 263 new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'), 264 new LongOpt("bootdir", LongOpt.REQUIRED_ARGUMENT, null, 'd'), 265 new LongOpt("patchdir", LongOpt.REQUIRED_ARGUMENT, null, 'p'), 266 new LongOpt("netboot", LongOpt.REQUIRED_ARGUMENT, null, 'n'), 267 new LongOpt("configuration", LongOpt.REQUIRED_ARGUMENT, null, 'c'), 268 new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V'), 269 new LongOpt("jaxp", LongOpt.REQUIRED_ARGUMENT, null, 'j'), 270 new LongOpt("bootlib", LongOpt.REQUIRED_ARGUMENT, null, 'B'), 271 new LongOpt("library", LongOpt.REQUIRED_ARGUMENT, null, 'L'), 272 new LongOpt("classpath", LongOpt.REQUIRED_ARGUMENT, null, 'C'), 273 new LongOpt("properties", LongOpt.REQUIRED_ARGUMENT, null, 'P'), 274 new LongOpt("host", LongOpt.REQUIRED_ARGUMENT, null, 'b'), 275 new LongOpt("partition", LongOpt.REQUIRED_ARGUMENT, null, 'g'), 276 new LongOpt("udp", LongOpt.REQUIRED_ARGUMENT, null, 'u'), 277 new LongOpt("log", LongOpt.REQUIRED_ARGUMENT, null, 'l'), 278 }; 279 280 Getopt getopt = new Getopt(programName, args, sopts, lopts); 281 int code; 282 String arg; 283 props.setProperty(ServerConfig.SERVER_BIND_ADDRESS, "0.0.0.0"); 284 System.setProperty(ServerConfig.SERVER_BIND_ADDRESS, "0.0.0.0"); 285 while ((code = getopt.getopt()) != -1) 286 { 287 switch (code) 288 { 289 case ':': 290 case '?': 291 System.exit(1); 293 break; 295 case 1: 296 System.err.println(programName + 299 ": unused non-option argument: " + 300 getopt.getOptarg()); 301 break; 303 case 'h': 304 System.out.println("usage: " + programName + " [options]"); 306 System.out.println(); 307 System.out.println("options:"); 308 System.out.println(" -h, --help Show this help message"); 309 System.out.println(" -V, --version Show version information"); 310 System.out.println(" -- Stop processing options"); 311 System.out.println(" -D<name>[=<value>] Set a system property"); 312 System.out.println(" -d, --bootdir=<dir> Set the boot patch directory; Must be absolute or url"); 313 System.out.println(" -p, --patchdir=<dir> Set the patch directory; Must be absolute or url"); 314 System.out.println(" -n, --netboot=<url> Boot from net with the given url as base"); 315 System.out.println(" -c, --configuration=<name> Set the server configuration name"); 316 System.out.println(" -B, --bootlib=<filename> Add an extra library to the front bootclasspath"); 317 System.out.println(" -L, --library=<filename> Add an extra library to the loaders classpath"); 318 System.out.println(" -C, --classpath=<url> Add an extra url to the loaders classpath"); 319 System.out.println(" -P, --properties=<url> Load system properties from the given url"); 320 System.out.println(" -b, --host=<host or ip> Bind address for all JBoss services"); 321 System.out.println(" -g, --partition=<name> HA Partition name (default=DefaultDomain)"); 322 System.out.println(" -u, --udp=<ip> UDP multicast address"); 323 System.out.println(" -l, --log=<log4j|jdk> Specify the logger plugin type"); 324 System.out.println(); 325 System.exit(0); 326 break; 328 case 'D': 329 { 330 arg = getopt.getOptarg(); 332 String name, value; 333 int i = arg.indexOf("="); 334 if (i == -1) 335 { 336 name = arg; 337 value = "true"; 338 } 339 else 340 { 341 name = arg.substring(0, i); 342 value = arg.substring(i + 1, arg.length()); 343 } 344 System.setProperty(name, value); 345 if ("bind.address".equals(name)) 348 { 349 System.setProperty("jgroups.bind_addr", value); 350 } 351 break; 352 } 353 354 case 'd': 355 bootURL = makeURL(getopt.getOptarg()); 357 break; 358 359 case 'p': 360 { 361 URL patchURL = makeURL(getopt.getOptarg()); 363 props.put(ServerConfig.PATCH_URL, patchURL.toString()); 364 365 break; 366 } 367 368 case 'n': 369 arg = getopt.getOptarg(); 371 372 if (!arg.endsWith("/")) arg += "/"; 374 375 props.put(ServerConfig.HOME_URL, new URL (arg).toString()); 376 break; 377 378 case 'c': 379 arg = getopt.getOptarg(); 381 props.put(ServerConfig.SERVER_NAME, arg); 382 break; 383 384 case 'V': 385 { 386 Package jbossPackage = Package.getPackage("org.jboss"); 388 389 System.out.println("JBoss " + jbossPackage.getImplementationVersion()); 391 System.out.println(); 392 System.out.println("Distributable under LGPL license."); 393 System.out.println("See terms of license at gnu.org."); 394 System.out.println(); 395 System.exit(0); 396 break; } 398 399 case 'j': 400 System.err.println(programName + ": option '-j, --jaxp' no longer supported"); 402 System.exit(1); 403 break; 405 case 'B': 406 arg = getopt.getOptarg(); 407 bootLibraries.add(arg); 408 break; 409 410 case 'L': 411 arg = getopt.getOptarg(); 412 extraLibraries.add(arg); 413 break; 414 415 case 'C': 416 { 417 URL url = makeURL(getopt.getOptarg()); 418 extraClasspath.add(url); 419 break; 420 } 421 422 case 'P': 423 { 424 URL url = makeURL(getopt.getOptarg()); 426 Properties props = System.getProperties(); 427 props.load(url.openConnection().getInputStream()); 428 break; 429 } 430 case 'b': 431 arg = getopt.getOptarg(); 432 props.put(ServerConfig.SERVER_BIND_ADDRESS, arg); 433 System.setProperty(ServerConfig.SERVER_BIND_ADDRESS, arg); 434 String bindAddress = System.getProperty("bind.address"); 439 if (bindAddress == null) 440 { 441 System.setProperty("bind.address", arg); 442 } 443 bindAddress = System.getProperty("jgroups.bind_addr"); 444 if (bindAddress == null) 445 { 446 System.setProperty("jgroups.bind_addr", arg); 447 } 448 String rmiHost = System.getProperty("java.rmi.server.hostname"); 450 if( rmiHost == null ) 451 { 452 rmiHost = ServerConfigUtil.fixRemoteAddress(arg); 453 System.setProperty("java.rmi.server.hostname", rmiHost); 454 } 455 break; 456 case 'g': 457 arg = getopt.getOptarg(); 458 props.put(ServerConfig.PARTITION_NAME_PROPERTY, arg); 459 System.setProperty(ServerConfig.PARTITION_NAME_PROPERTY, arg); 460 break; 461 case 'u': 462 arg = getopt.getOptarg(); 463 props.put(ServerConfig.PARTITION_UDP_PROPERTY, arg); 464 System.setProperty(ServerConfig.PARTITION_UDP_PROPERTY, arg); 465 System.setProperty("jgroups.udp.mcast_addr", arg); 467 break; 468 case 'l': 469 { 470 arg = getopt.getOptarg(); 471 String logPlugin = arg; 472 if( arg.equalsIgnoreCase("log4j") ) 473 logPlugin = "org.jboss.logging.Log4jLoggerPlugin"; 474 else if( arg.equalsIgnoreCase("jdk") ) 475 { 476 logPlugin = "org.jboss.logging.JDK14LoggerPlugin"; 477 System.setProperty("java.util.logging.manager", 479 "org.jboss.logging.jdk.JDKLogManager"); 480 } 481 System.setProperty("org.jboss.logging.Logger.pluginClass", logPlugin); 482 break; 483 } 484 default: 485 throw new Error ("unhandled option code: " + code); 488 } 489 } 490 } 491 492 500 public static void main(final String [] args) throws Exception 501 { 502 Runnable worker = new Runnable () { 503 public void run() 504 { 505 try 506 { 507 Main main = new Main(); 508 main.boot(args); 509 } 510 catch (Exception e) 511 { 512 System.err.println("Failed to boot JBoss:"); 513 e.printStackTrace(); 514 } 515 } 516 517 }; 518 519 ThreadGroup threads = new ThreadGroup ("jboss"); 520 new Thread (threads, worker, "main").start(); 521 } 522 523 528 public static void systemExit(String argv[]) 529 { 530 System.exit(0); 531 } 532 533 static class JarFilter implements FilenameFilter 534 { 535 public boolean accept(File dir, String name) 536 { 537 return name.endsWith(".jar"); 538 } 539 } 540 } | Popular Tags |