1 25 package org.ofbiz.base.start; 26 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.InputStreamReader ; 34 import java.io.PrintWriter ; 35 import java.lang.reflect.Method ; 36 import java.net.ConnectException ; 37 import java.net.InetAddress ; 38 import java.net.ServerSocket ; 39 import java.net.Socket ; 40 import java.util.ArrayList ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Properties ; 44 45 52 public class Start implements Runnable { 53 54 private Classpath classPath = new Classpath(System.getProperty("java.class.path")); 55 private ClassLoader classloader = null; 56 private ServerSocket serverSocket = null; 57 private Thread serverThread = null; 58 private boolean serverRunning = true; 59 private List loaders = null; 60 private Config config = null; 61 private String [] loaderArgs = null; 62 63 private static final String SHUTDOWN_COMMAND = "SHUTDOWN"; 64 private static final String STATUS_COMMAND = "STATUS"; 65 private static final double REQUIRED_JDK = 1.4; 66 67 public void init(String [] args, boolean fullInit) throws IOException { 68 String firstArg = args.length > 0 ? args[0] : ""; 69 String cfgFile = Start.getConfigFileName(firstArg); 70 71 this.loaders = new ArrayList (); 72 this.config = new Config(); 73 74 config.readConfig(cfgFile); 76 77 if (args.length > 1) { 79 this.loaderArgs = new String [args.length - 1]; 80 for (int i = 1; i < args.length; i++) { 81 this.loaderArgs[i - 1] = args[i]; 82 } 83 } 84 85 if (fullInit) { 86 initClasspath(); 88 89 initLogDirectory(); 91 92 initListenerThread(); 94 95 initStartLoaders(); 97 98 if (config.useShutdownHook) { 100 setShutdownHook(); 101 } else { 102 System.out.println("Shutdown hook disabled"); 103 } 104 } 105 } 106 107 public void init(String [] args) throws IOException { 108 init(args, true); 109 } 110 111 public void run() { 112 while (serverRunning) { 113 try { 114 Socket clientSocket = serverSocket.accept(); 115 System.out.println("Received connection from - " + clientSocket.getInetAddress() + " : " + clientSocket.getPort()); 116 processClientRequest(clientSocket); 117 clientSocket.close(); 118 } catch (IOException e) { 119 e.printStackTrace(); 120 } 121 } 122 shutdownServer(); 123 System.exit(0); 124 } 125 126 private void processClientRequest(Socket client) throws IOException { 127 BufferedReader reader = new BufferedReader (new InputStreamReader (client.getInputStream())); 128 String request = reader.readLine(); 129 130 PrintWriter writer = new PrintWriter (client.getOutputStream(), true); 131 writer.println(processRequest(request, client)); 132 writer.flush(); 133 134 writer.close(); 135 reader.close(); 136 } 137 138 private String processRequest(String request, Socket client) { 139 if (request != null) { 140 String key = request.substring(0, request.indexOf(':')); 141 String command = request.substring(request.indexOf(':') + 1); 142 if (!key.equals(config.adminKey)) { 143 return "FAIL"; 144 } else { 145 if (command.equals(Start.SHUTDOWN_COMMAND)) { 146 System.out.println("Shutdown initiated from: " + client.getInetAddress().getHostAddress() + ":" + client.getPort()); 147 serverRunning = false; 148 } else if (command.equals(Start.STATUS_COMMAND)) { 149 return serverRunning ? "Running" : "Stopped"; 150 } 151 return "OK"; 152 } 153 } else { 154 return "FAIL"; 155 } 156 } 157 158 private void initListenerThread() throws IOException { 159 if (config.adminPort > 0) { 160 this.serverSocket = new ServerSocket (config.adminPort, 1, config.adminAddress); 161 this.serverThread = new Thread (this, this.toString()); 162 this.serverThread.setDaemon(false); 163 System.out.println("Admin socket configured on - " + config.adminAddress + ":" + config.adminPort); 164 } else { 165 System.out.println("Admin socket not configured; set to port 0"); 166 } 167 } 168 169 private void startListenerThread() { 170 if (serverSocket != null && serverThread != null) { 171 this.serverThread.start(); 172 } 173 } 174 175 private void loadLibs(String path, boolean recurse) throws IOException { 176 File libDir = new File (path); 177 if (libDir.exists()) { 178 File files[] = libDir.listFiles(); 179 for (int i = 0; i < files.length; i++) { 180 String fileName = files[i].getName(); 181 if (files[i].isDirectory() && !"CVS".equals(fileName) && recurse) { 182 loadLibs(files[i].getCanonicalPath(), recurse); 183 } else if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) { 184 classPath.addComponent(files[i]); 185 } 186 } 187 } 188 } 189 190 private void initClasspath() throws IOException { 191 if (config.toolsJar != null) { 193 classPath.addComponent(config.toolsJar); 194 } 195 196 if (config.commJar != null) { 198 classPath.addComponent(config.commJar); 199 } 200 201 classPath.addClasspath(config.ofbizHome); 203 loadLibs(config.ofbizHome, false); 204 205 if (config.baseLib != null) { 207 loadLibs(config.baseLib, true); 208 } 209 210 if (config.baseJar != null) { 212 classPath.addComponent(config.baseJar); 213 } 214 215 if (config.baseDtd != null) { 217 classPath.addComponent(config.baseDtd); 218 } 219 220 if (config.baseConfig != null) { 222 classPath.addComponent(config.baseConfig); 223 } 224 225 System.setProperty("java.class.path", classPath.toString()); 227 this.classloader = classPath.getClassLoader(); 228 Thread.currentThread().setContextClassLoader(classloader); 229 if (System.getProperty("DEBUG") != null) { 230 System.out.println("Startup Classloader: " + classloader.toString()); 231 System.out.println("Startup Classpath: " + classPath.toString()); 232 } 233 } 234 235 private void initLogDirectory() { 236 boolean createdDir = false; 238 File logDir = new File (config.logDir); 239 if (!logDir.exists()) { 240 logDir.mkdir(); 241 createdDir = true; 242 } 243 244 if (createdDir) { 245 System.out.println("Created OFBiz log dir [" + logDir.getAbsolutePath() + "]"); 246 } 247 } 248 249 private void initStartLoaders() { 250 Iterator li = config.loaders.iterator(); 252 while (li.hasNext()) { 253 String loaderClassName = (String ) li.next(); 254 try { 255 Class loaderClass = classloader.loadClass(loaderClassName); 256 StartupLoader loader = (StartupLoader) loaderClass.newInstance(); 257 loader.load(config, loaderArgs); 258 loaders.add(loader); 259 } catch (Exception e) { 260 e.printStackTrace(); 261 System.exit(99); 262 } 263 } 264 } 265 266 private void startStartLoaders() { 267 Iterator i = loaders.iterator(); 269 while (i.hasNext()) { 270 StartupLoader loader = (StartupLoader) i.next(); 271 try { 272 loader.start(); 273 } catch (StartupException e) { 274 e.printStackTrace(); 275 System.exit(99); 276 } 277 } 278 } 279 280 private void setShutdownHook() { 281 try { 282 Method shutdownHook = java.lang.Runtime .class.getMethod("addShutdownHook", new Class []{java.lang.Thread .class}); 283 Thread hook = new Thread () { 284 public void run() { 285 setName("OFBiz_Shutdown_Hook"); 286 shutdownServer(); 287 try { 289 Thread.sleep(1000); 290 } catch (Exception e) { 291 e.printStackTrace(); 292 } 293 } 294 }; 295 296 shutdownHook.invoke(Runtime.getRuntime(), new Object []{hook}); 297 } catch (Exception e) { 298 e.printStackTrace(); 300 } 301 } 302 303 private void shutdownServer() { 304 if (loaders != null && loaders.size() > 0) { 305 Iterator i = loaders.iterator(); 306 while (i.hasNext()) { 307 StartupLoader loader = (StartupLoader) i.next(); 308 try { 309 loader.unload(); 310 } catch (Exception e) { 311 e.printStackTrace(); 312 } 313 } 314 } 315 serverRunning = false; 316 } 317 318 private void startServer() { 319 startListenerThread(); 321 322 startStartLoaders(); 324 } 325 326 public void start() { 327 startServer(); 328 if (config.shutdownAfterLoad) { 329 shutdownServer(); 330 System.exit(0); 331 } 332 } 333 334 public void stop() { 335 shutdownServer(); 336 } 337 338 public void destroy() { 339 this.serverSocket = null; 340 this.serverThread = null; 341 this.loaders = null; 342 this.config = null; 343 this.loaderArgs = null; 344 } 345 346 public String shutdown() throws IOException { 347 return sendSocketCommand(Start.SHUTDOWN_COMMAND); 348 } 349 350 public String status() throws IOException { 351 String status = null; 352 try { 353 status = sendSocketCommand(Start.STATUS_COMMAND); 354 } catch (ConnectException e) { 355 return "Not Running"; 356 } catch (IOException e) { 357 throw e; 358 } 359 return status; 360 } 361 362 private String sendSocketCommand(String command) throws IOException , ConnectException { 363 Socket socket = new Socket (config.adminAddress, config.adminPort); 364 365 PrintWriter writer = new PrintWriter (socket.getOutputStream(), true); 367 writer.println(config.adminKey + ":" + command); 368 writer.flush(); 369 370 BufferedReader reader = new BufferedReader (new InputStreamReader (socket.getInputStream())); 372 String response = null; 373 if (reader.ready()) { 374 response = reader.readLine(); 375 } 376 377 reader.close(); 378 379 writer.close(); 381 socket.close(); 382 383 return response; 384 } 385 386 public static void main(String [] args) throws IOException { 387 String firstArg = args.length > 0 ? args[0] : ""; 388 Start start = new Start(); 389 390 if (firstArg.equals("-help") || firstArg.equals("-?")) { 391 System.out.println(""); 392 System.out.println("Usage: java -jar ofbiz.jar [command] [arguments]"); 393 System.out.println("-help, -? ----> This screen"); 394 System.out.println("-install -----> Run install (create tables/load data)"); 395 System.out.println("-setup -------> Run external application server setup"); 396 System.out.println("-start -------> Start the server"); 397 System.out.println("-status ------> Status of the server"); 398 System.out.println("-shutdown ----> Shutdown the server"); 399 System.out.println("-test --------> Run the JUnit test script"); 400 System.out.println("[no config] --> Use default config"); 401 System.out.println("[no command] -> Start the server w/ default config"); 402 } else { 403 if (firstArg.equals("-status")) { 405 start.init(args, false); 406 System.out.println("Current Status : " + start.status()); 407 } else if (firstArg.equals("-shutdown")) { 408 start.init(args, false); 409 System.out.println("Shutting down server : " + start.shutdown()); 410 } else { 411 start.init(args, true); 413 start.start(); 414 } 415 } 416 } 417 418 private static String getConfigFileName(String command) { 419 if (command == null || command.trim().length() == 0) { 421 command = "start"; 422 } 423 424 if (command.startsWith("-")) { 426 command = command.substring(1); 427 } 428 429 if (command.equalsIgnoreCase("shutdown")) { 431 command = "start"; 432 } else if (command.equalsIgnoreCase("status")) { 433 command = "start"; 434 } 435 436 return "org/ofbiz/base/start/" + command + ".properties"; 437 } 438 439 public static class Config { 440 public String containerConfig; 441 public String testConfig; 442 public InetAddress adminAddress; 443 public int adminPort; 444 public String adminKey; 445 public String ofbizHome; 446 public String baseJar; 447 public String toolsJar; 448 public String commJar; 449 public String baseLib; 450 public String baseDtd; 451 public String baseConfig; 452 public String logDir; 453 public List loaders; 454 public String awtHeadless; 455 public String splashLogo; 456 public boolean shutdownAfterLoad = false; 457 public boolean useShutdownHook = true; 458 public boolean requireToolsJar = false; 459 public boolean requireCommJar = false; 460 461 private Properties getPropertiesFile(String config) throws IOException { 462 InputStream propsStream = null; 463 Properties props = new Properties (); 464 try { 465 propsStream = getClass().getClassLoader().getResourceAsStream(config); 467 if (propsStream != null) { 468 props.load(propsStream); 469 } else { 470 throw new IOException (); 471 } 472 } catch (IOException e) { 473 File propsFile = new File (config); 475 if (propsFile != null) { 476 FileInputStream fis = null; 477 try { 478 fis = new FileInputStream (propsFile); 479 if (fis != null) { 480 props.load(fis); 481 } else { 482 throw new FileNotFoundException (); 483 } 484 } catch (FileNotFoundException e2) { 485 } finally { 487 if (fis != null) { 488 fis.close(); 489 } 490 } 491 } 492 } finally { 493 if (propsStream != null) { 494 propsStream.close(); 495 } 496 } 497 498 if (props.isEmpty()) { 500 throw new IOException ("Cannot load configuration properties : " + config); 501 } 502 return props; 503 } 504 505 public void readConfig(String config) throws IOException { 506 String javaVersion = System.getProperty("java.version"); 508 String javaVendor = System.getProperty("java.vendor"); 509 double version = Double.parseDouble(javaVersion.substring(0, javaVersion.indexOf(".") + 2)); 510 if (REQUIRED_JDK > version) { 511 System.err.println(""); 512 System.err.println("Java Version - " + javaVendor + " " + javaVersion + " - is not supported by OFBiz."); 513 System.err.println("Please install Java2 SDK " + REQUIRED_JDK + "+"); 514 System.err.println(""); 515 System.exit(-1); 516 } 517 518 Properties props = this.getPropertiesFile(config); 519 520 if (ofbizHome == null) { 522 ofbizHome = props.getProperty("ofbiz.home", "."); 523 if (ofbizHome.equals(".")) { 525 ofbizHome = System.getProperty("user.dir"); 526 ofbizHome = ofbizHome.replace('\\', '/'); 527 System.out.println("Set OFBIZ_HOME to - " + ofbizHome); 528 } 529 } 530 System.setProperty("ofbiz.home", ofbizHome); 531 532 baseConfig = System.getProperty("ofbiz.base.config"); 534 if (baseConfig == null) { 535 baseConfig = ofbizHome + "/" + props.getProperty("ofbiz.base.config", "base/config"); 536 } 537 538 baseDtd = System.getProperty("ofbiz.base.schema"); 540 if (baseDtd == null) { 541 baseDtd = ofbizHome + "/" + props.getProperty("ofbiz.base.schema", "base/dtd"); 542 } 543 544 baseLib = System.getProperty("ofbiz.base.lib"); 546 if (baseLib == null) { 547 baseLib = ofbizHome + "/" + props.getProperty("ofbiz.base.lib", "base/lib"); 548 } 549 550 baseJar = System.getProperty("ofbiz.base.jar"); 552 if (baseJar == null) { 553 baseJar = ofbizHome + "/" + props.getProperty("ofbiz.base.jar", "base/build/lib/ofbiz-base.jar"); 554 } 555 556 String reqTJ = System.getProperty("java.tools.jar.required"); 558 if (reqTJ == null) { 559 reqTJ = props.getProperty("java.tools.jar.required", "false"); 560 } 561 requireToolsJar = "true".equalsIgnoreCase(reqTJ); 562 toolsJar = this.findSystemJar(props, javaVendor, javaVersion, "tools.jar", requireToolsJar); 563 564 String reqCJ = System.getProperty("java.comm.jar.required"); 566 if (reqTJ == null) { 567 reqTJ = props.getProperty("java.comm.jar.required", "false"); 568 } 569 requireCommJar = "true".equalsIgnoreCase(reqCJ); 570 commJar = this.findSystemJar(props, javaVendor, javaVersion, "comm.jar", requireCommJar); 571 572 logDir = System.getProperty("ofbiz.log.dir"); 574 if (logDir == null) { 575 logDir = ofbizHome + "/" + props.getProperty("ofbiz.log.dir", "logs"); 576 } 577 578 containerConfig = System.getProperty("ofbiz.container.config"); 580 if (containerConfig == null) { 581 containerConfig = ofbizHome + "/" + props.getProperty("ofbiz.container.config", "base/config/ofbiz-containers.xml"); 582 } 583 584 String serverHost = System.getProperty("ofbiz.admin.host"); 586 if (serverHost == null) { 587 serverHost = props.getProperty("ofbiz.admin.host", "127.0.0.1"); 588 } 589 590 String adminPortStr = System.getProperty("ofbiz.admin.port"); 591 if (adminPortStr == null) { 592 adminPortStr = props.getProperty("ofbiz.admin.port", "0"); 593 } 594 595 adminKey = System.getProperty("ofbiz.admin.key"); 597 if (adminKey == null) { 598 adminKey = props.getProperty("ofbiz.admin.key", "NA"); 599 } 600 601 adminAddress = InetAddress.getByName(serverHost); 603 604 try { 606 adminPort = Integer.parseInt(adminPortStr); 607 } catch (Exception e) { 608 adminPort = 0; 609 } 610 611 String derbyPath = System.getProperty("derby.system.home"); 613 if (derbyPath == null) { 614 derbyPath = props.getProperty("derby.system.home", "data/derby"); 615 } 616 System.setProperty("derby.system.home", derbyPath); 617 618 String log4jConfig = System.getProperty("log4j.configuration"); 620 if (log4jConfig == null) { 621 log4jConfig = props.getProperty("log4j.configuration"); 622 } 623 624 if (log4jConfig == null) { 626 log4jConfig = ofbizHome + "/base/config/debug.properties"; 627 } 628 629 System.setProperty("log4j.configuration", log4jConfig); 631 632 if (System.getProperty("ofbiz.enable.hook") != null && System.getProperty("ofbiz.enable.hook").length() > 0) { 634 useShutdownHook = "true".equalsIgnoreCase(System.getProperty("ofbiz.enable.hook")); 635 } else if (props.getProperty("ofbiz.enable.hook") != null && props.getProperty("ofbiz.enable.hook").length() > 0) { 636 useShutdownHook = "true".equalsIgnoreCase(props.getProperty("ofbiz.enable.hook")); 637 } 638 639 if (System.getProperty("ofbiz.auto.shutdown") != null && System.getProperty("ofbiz.auto.shutdown").length() > 0) { 641 shutdownAfterLoad = "true".equalsIgnoreCase(System.getProperty("ofbiz.auto.shutdown")); 642 } else if (props.getProperty("ofbiz.auto.shutdown") != null && props.getProperty("ofbiz.auto.shutdown").length() > 0) { 643 shutdownAfterLoad = "true".equalsIgnoreCase(props.getProperty("ofbiz.auto.shutdown")); 644 } 645 646 awtHeadless = System.getProperty("java.awt.headless"); 648 if (awtHeadless == null) { 649 awtHeadless = props.getProperty("java.awt.headless"); 650 } 651 if (awtHeadless != null) { 652 System.setProperty("java.awt.headless", awtHeadless); 653 } 654 655 splashLogo = props.getProperty("ofbiz.start.splash.logo", null); 657 658 System.setProperty("org.mortbay.jetty.servlet.AbstractSessionManager.24SessionDestroyed", "true"); 660 661 loaders = new ArrayList (); 663 int currentPosition = 1; 664 while (true) { 665 String loaderClass = props.getProperty("ofbiz.start.loader" + currentPosition); 666 if (loaderClass == null || loaderClass.length() == 0) { 667 break; 668 } else { 669 loaders.add(loaderClass); 670 currentPosition++; 671 } 672 } 673 } 674 675 private String findSystemJar(Properties props, String javaVendor, String javaVersion, String jarName, boolean required) { 676 String fileSep = System.getProperty("file.separator"); 677 String javaHome = System.getProperty("java.home"); 678 String errorMsg = "Unable to locate " + jarName + " - "; 679 String jarLoc = "lib" + fileSep + jarName; 681 File tj = null; 682 683 if ("tools.jar".equals(jarName) && javaVendor.startsWith("Apple")) { 684 return null; 686 } 687 688 tj = new File (ofbizHome + fileSep + jarName); 690 if (tj.exists()) { 691 return null; 692 } 693 694 tj = new File (baseLib + fileSep + jarName); 696 if (tj.exists()) { 697 return null; 698 } 699 700 String jarProps = props.getProperty("java." + jarName, null); 702 if (jarProps != null) { 703 tj = new File (jarProps); 704 if (!tj.exists()) { 705 if (required) { 706 System.err.println(errorMsg + tj.getAbsolutePath()); 707 } 708 } else { 709 return jarProps; 711 } 712 } 713 714 tj = new File (javaHome + fileSep + jarLoc); 716 if (!tj.exists()) { 717 if (required) { 718 System.err.println(errorMsg + tj.getAbsolutePath()); 719 } 720 } else { 721 return tj.getAbsolutePath(); 723 } 724 725 String jreExt = fileSep + "jre"; 727 if (javaHome.toLowerCase().endsWith(jreExt)) { 728 javaHome = javaHome.substring(0, javaHome.lastIndexOf(fileSep)); 729 tj = new File (javaHome + fileSep + jarLoc); 730 if (!tj.exists()) { 731 if (required) { 732 System.err.println(errorMsg + tj.getAbsolutePath()); 733 } 734 } else { 735 return tj.getAbsolutePath(); 737 } 738 } 739 740 if (javaHome.toLowerCase().charAt(1) == ':') { 742 String driveLetter = javaHome.substring(0, 2); 743 String windowsPath = driveLetter + fileSep + "j2sdk" + javaVersion; 744 tj = new File (windowsPath + fileSep + jarLoc); 745 if (!tj.exists()) { 746 if (required) { 747 System.err.println(errorMsg + tj.getAbsolutePath()); 748 } 749 } else { 750 return tj.getAbsolutePath(); 752 } 753 } 754 755 if (required) { 756 System.err.println(""); 757 System.err.println("Required library " + jarName + " could not be located."); 758 System.err.println("Make sure you using Java2 SDK " + REQUIRED_JDK + "+ and NOT the JRE."); 759 System.err.println("You may need to copy " + jarName + " into a loadable lib directory"); 760 System.err.println("(i.e. OFBIZ_HOME or OFBIZ_HOME/base/lib)"); 761 System.err.println(""); 762 System.exit(-1); 763 } 764 765 return null; 766 } 767 } 768 } 769 770 | Popular Tags |