1 22 package org.enhydra.multiServer.launch; 23 24 import org.enhydra.multiServer.kernel.Kernel; 26 import org.enhydra.multiServer.bootstrap.Bootstrap; 27 28 import java.util.Properties ; 30 import java.util.Vector ; 31 import java.io.IOException ; 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.io.FileOutputStream ; 35 import java.io.PrintStream ; 36 import java.io.PrintWriter ; 37 import java.io.FileWriter ; 38 import javax.swing.JOptionPane ; 39 40 public class LaunchApp { 42 protected static boolean debug = false; 43 private final String PROPERTY_HOME = "launch.home"; 44 private final String PROPERTY_CONF_DIR = "enhydra.conf"; 45 private final String PROPERTY_CONF_FILE = "launch.bootstrap"; 46 private final String PROPERTY_POLICY = "java.security.policy"; 47 private final String PROPERTY_BROWSER = "launch.browser"; 48 private final String PROPERTY_USER_HOME = "user.home"; 49 private String [] bootArgs = new String [0]; 50 private String home = null; 51 private String browser = null; 52 private BootThread boot = null; 53 private LaunchFrame frame = null; 54 private PrintWriter logWriter = null; 55 protected static PrintStream sysOut = System.out; 56 protected static PrintStream sysErr = System.err; 57 protected static LaunchApp app = null; 58 59 public static void main(String [] args) { 60 LaunchApp.app = new LaunchApp(); 61 app.init(args); 62 } 63 64 public LaunchApp() {} 65 66 public static void shutdownAlert(Kernel k) { 67 if (LaunchApp.app != null) { 68 LaunchApp.app.runKiller(30); 69 LaunchApp.app.storeAppProperties(); 70 LaunchApp.app.closeFrame(); 71 } 72 } 73 74 protected boolean isStarted() { 75 return (boot != null); 76 } 77 78 protected void setHome(String path) { 79 home = path; 80 } 81 82 protected String getHome() { 83 return home; 84 } 85 86 protected String getConfFile() { 87 String path = null; 88 89 if (bootArgs.length > 0) { 90 path = bootArgs[0]; 91 } 92 return path; 93 } 94 95 protected void setConfFile(String path) { 96 if (bootArgs.length == 0) { 97 bootArgs = new String [1]; 98 } 99 bootArgs[0] = path; 100 } 101 102 protected String getConfDir() { 103 String path = null; 104 105 if (System.getProperty(PROPERTY_CONF_DIR) != null) { 106 File dir = new File (System.getProperty(PROPERTY_CONF_DIR)); 107 108 path = dir.getAbsolutePath(); 109 } 110 return path; 111 } 112 113 protected void setConfDir(String path) { 114 System.setProperty(PROPERTY_CONF_DIR, path); 115 } 116 117 protected String getPolicy() { 118 String path = null; 119 120 if (System.getProperty(PROPERTY_POLICY) != null) { 121 File dir = new File (System.getProperty(PROPERTY_POLICY)); 122 123 path = dir.getAbsolutePath(); 124 } 125 return path; 126 } 127 128 protected void setPolicy(String path) { 129 System.setProperty(PROPERTY_POLICY, path); 130 } 131 132 protected void setBrowser(String path) { 133 browser = path; 134 } 135 136 protected String getBrowser() { 137 return browser; 138 } 139 140 protected void startServer() { 141 if ((frame == null) || (validatePaths())) { 142 stopServer(); 143 boot = new BootThread(); 144 boot.start(); 145 } 146 } 147 148 private boolean validatePaths() { 149 boolean valid = true; 150 StringBuffer path = new StringBuffer (); 151 StringBuffer message = new StringBuffer (); 152 File file = null; 153 154 if (!isHomeValid()) { 156 valid = false; 157 path.setLength(0); 158 path.append(getHome()); 159 path.append(File.separator); 160 path.append("lib"); 161 path.append(File.separator); 162 path.append("enhydra.jar"); 163 message.append("Invalid enhydra home directory: \n"); 164 message.append(getHome()); 165 message.append("\n\n"); 166 message.append("File not found: \n"); 167 message.append(path.toString()); 168 } 169 170 if (valid) { 172 path.setLength(0); 173 path.append(getConfDir()); 174 path.append(File.separator); 175 path.append("servlet"); 176 path.append(File.separator); 177 path.append("servlet.conf"); 178 file = new File (path.toString()); 179 if (!file.isFile()) { 180 valid = false; 181 message.append("Invalid configuration directory: \n"); 182 message.append(getConfDir()); 183 message.append("\n\n"); 184 message.append("File not found: \n"); 185 message.append(path.toString()); 186 } 187 } 188 189 if (valid) { 191 path.setLength(0); 192 path.append(getConfDir()); 193 path.append(File.separator); 194 path.append("naming"); 195 path.append(File.separator); 196 path.append("jndi.properties"); 197 file = new File (path.toString()); 198 if (!file.isFile()) { 199 valid = false; 200 message.append("Invalid configuration directory: \n"); 201 message.append(getConfDir()); 202 message.append("\n\n"); 203 message.append("File not found: \n"); 204 message.append(path.toString()); 205 } 206 } 207 208 if (valid) { 210 path.setLength(0); 211 path.append(getPolicy()); 212 file = new File (path.toString()); 213 if ((!file.isFile()) || (!file.canRead())) { 214 valid = false; 215 message.append("Invalid policy file: \n"); 216 message.append(getPolicy()); 217 } 218 } 219 220 if (valid) { 222 path.setLength(0); 223 path.append(this.getConfFile()); 224 file = new File (path.toString()); 225 if ((!file.isFile()) || (!file.canRead())) { 226 valid = false; 227 message.append("Invalid configuration file: \n"); 228 message.append(getConfFile()); 229 } 230 } 231 if (!valid) { 232 JOptionPane.showMessageDialog(frame, message.toString(), 233 frame.getTitle(), 234 JOptionPane.ERROR_MESSAGE); 235 } 236 return valid; 237 } 238 239 protected void stopServer() { 240 storeAppProperties(); 241 if (boot != null) { 242 runKiller(15); 243 boot.degrade(); 244 boot = null; 245 } 246 } 247 248 protected void runKiller(int seconds) { 249 KillerThread killer = new KillerThread(seconds); 250 251 killer.setPriority(Thread.MAX_PRIORITY); 252 killer.start(); 253 } 254 255 protected boolean isServerRunning() { 256 boolean running = false; 257 258 if (Kernel.getKernel() != null) { 259 running = 260 Kernel.getKernel().getState().equalsIgnoreCase("running"); 261 } 262 return running; 263 } 264 265 private void closeFrame() { 266 if (frame != null) { 267 freeOutput(); 268 frame.clearAll(); 269 frame = null; 270 } 271 } 272 273 protected static void freeOutput() { 274 System.setOut(LaunchApp.sysOut); 275 System.setErr(LaunchApp.sysErr); 276 } 277 278 protected void exit() { 279 storeAppProperties(); 280 if (Kernel.getKernel() == null) { 281 System.setSecurityManager(null); 282 System.exit(0); 283 } else { 284 int response = JOptionPane.showConfirmDialog(frame, 285 "Enhydra may be running. Do you want to shutdown the server?", 286 frame.getTitle(), JOptionPane.YES_NO_CANCEL_OPTION, 287 JOptionPane.QUESTION_MESSAGE); 288 289 if (response == JOptionPane.CANCEL_OPTION) { 290 return; 291 } else if (response == JOptionPane.NO_OPTION) { 292 closeFrame(); 293 closeLogWriter(); 294 } else { 295 stopServer(); 296 closeLogWriter(); 297 298 } 299 } 300 } 301 302 private void closeLogWriter() { 303 304 if (logWriter != null) { 305 logWriter.flush(); 306 logWriter.close(); 307 } 308 309 } 310 311 private void init(String [] args) { 312 boolean showFrame = true; 313 Properties appProps = new Properties (); 314 315 Vector argVector = new Vector (); 317 318 for (int i = 0; i < args.length; i++) { 319 if (args[i].equalsIgnoreCase("-noframe")) { 320 showFrame = false; 321 } else if (args[i].equalsIgnoreCase("-debug")) { 322 LaunchApp.debug = true; 323 } else { 324 argVector.addElement(args[i]); 325 } 326 } 327 bootArgs = (String []) argVector.toArray(bootArgs); 328 argVector.removeAllElements(); 329 330 appProps = readAppProperties(); 332 initHome(appProps); 333 initConfFile(appProps); 334 initConfDir(appProps); 335 initPolicy(appProps); 336 initBrowser(appProps); 337 File logFile = new File (getLogFilePath()); 338 339 if (logFile.exists()) { 340 logFile.delete(); 341 try { 342 logFile.createNewFile(); 343 } catch (IOException e) { 344 345 } 347 } 348 if (showFrame) { 349 SwingUtil.setLookAndFeelToSystem(); 350 frame = new LaunchFrame(); 351 frame.initPaths(); 352 System.setSecurityManager(null); 353 frame.show(); 354 } else { 355 startServer(); 356 } 357 } 358 359 private void initHome(Properties appProps) { 360 File f = null; 361 362 f = new File ("."); f = new File (f.getAbsolutePath()); 367 setHome(f.getParentFile().getParent()); 368 369 if (isHomeValid()) { 371 return; 372 } else if (bootArgs.length > 0) { 373 f = new File (bootArgs[0]); 374 setHome(f.getParentFile().getParent()); 375 } 376 377 if (isHomeValid()) { 379 return; 380 } else if (getPolicy() != null) { 381 f = new File (getPolicy()); 382 setHome(f.getParentFile().getParent()); 383 } 384 385 if (isHomeValid()) { 387 return; 388 } else if (getConfDir() != null) { 389 f = new File (getConfDir()); 390 setHome(f.getParent()); 391 } 392 393 if (isHomeValid()) { 395 return; 396 } else if (appProps.getProperty(PROPERTY_HOME) != null) { 397 f = new File (appProps.getProperty(PROPERTY_HOME)); 398 setHome(f.getAbsolutePath()); 399 } 400 } 401 402 private boolean isHomeValid() { 403 boolean valid = false; 404 StringBuffer path = new StringBuffer (); 405 File f = null; 406 407 path.append(getHome()); 408 path.append(File.separator); 409 path.append("lib"); 410 path.append(File.separator); 411 path.append("enhydra.jar"); 412 f = new File (path.toString()); 413 valid = f.isFile(); 414 f = null; 415 return valid; 416 } 417 418 private void initConfDir(Properties appProps) { 419 if (getConfDir() == null) { 420 if (appProps.getProperty(PROPERTY_CONF_DIR) == null) { 421 File dir = null; 422 423 dir = new File (getHome() + File.separator + "conf"); 424 setConfDir(dir.getAbsolutePath()); 425 dir = null; 426 } else { 427 setConfDir(appProps.getProperty(PROPERTY_CONF_DIR)); 428 } 429 } 430 } 431 432 private void initPolicy(Properties appProps) { 433 if (getPolicy() == null) { 434 if (appProps.getProperty(PROPERTY_POLICY) == null) { 435 File policyFile = null; 436 StringBuffer path = new StringBuffer (); 437 438 path.append(getHome()); 439 path.append(File.separator); 440 path.append("bin"); 441 path.append(File.separator); 442 path.append("java.policy"); 443 policyFile = new File (path.toString()); 444 if (!policyFile.isFile()) { 445 policyFile = createPolicyFile(); 446 } 447 setPolicy(policyFile.getAbsolutePath()); 448 } else { 449 setPolicy(appProps.getProperty(PROPERTY_POLICY)); 450 } 451 } 452 } 453 454 private void initBrowser(Properties appProps) { 455 if (getBrowser() == null) { 456 if (appProps.getProperty(PROPERTY_BROWSER) != null) { 457 setBrowser(appProps.getProperty(PROPERTY_BROWSER)); 458 } 459 } 460 } 461 462 private File createPolicyFile() { 463 File file = null; 464 StringBuffer path = new StringBuffer (); 465 466 path.append(System.getProperty(PROPERTY_USER_HOME)); 467 path.append("java.policy"); 468 file = new File (path.toString()); 469 if (!file.exists()) { 470 try { 471 file = new File (path.toString()); 472 file.getParentFile().mkdirs(); 473 file.createNewFile(); 474 PrintWriter writer = new PrintWriter (new FileWriter (file)); 475 476 writer.println("grant {"); 477 writer.println("permission java.security.AllPermission;"); 478 writer.println("};"); 479 writer.flush(); 480 writer.close(); 481 file = null; 482 file = new File (path.toString()); 483 } catch (java.io.IOException e) { 484 e.printStackTrace(); 485 } 486 } 487 return file; 488 } 489 490 private void initConfFile(Properties appProps) { 491 if (bootArgs.length == 0) { 492 if (appProps.getProperty(PROPERTY_CONF_FILE) == null) { 493 StringBuffer path = new StringBuffer (); 494 495 path.append(getHome()); 496 path.append(File.separator); 497 path.append("bin"); 498 path.append(File.separator); 499 path.append("bootstrap.conf"); 500 setConfFile(path.toString()); 501 } else { 502 setConfFile(appProps.getProperty(PROPERTY_CONF_FILE)); 503 } 504 } 505 } 506 507 protected String getLogFilePath() { 508 StringBuffer logPath = null; 509 510 logPath = new StringBuffer (); 511 logPath.append(getHome()); 512 logPath.append(File.separator); 513 logPath.append("logs"); 514 logPath.append(File.separator); 515 logPath.append("launcher.txt"); 516 return logPath.toString(); 517 } 518 519 protected PrintWriter getLogWriter() throws IOException { 520 if (logWriter == null) { 521 File logFile = null; 522 logFile = new File (getLogFilePath()); 523 if (!logFile.getParentFile().exists()) { 524 logFile.getParentFile().mkdirs(); 525 } 526 logFile = null; 527 logWriter = 528 new PrintWriter (new FileWriter (getLogFilePath(), 529 true)); 530 } 531 return logWriter; 532 } 533 534 private String getAppPropertyPath() { 535 StringBuffer buf = new StringBuffer (); 536 537 buf.append(System.getProperty("user.home")); 538 buf.append(File.separator); 539 buf.append(".enhydra"); 540 buf.append(File.separator); 541 buf.append("launcher.properties"); 542 return buf.toString(); 543 } 544 545 private boolean isFile(String path) { 546 boolean is = false; 547 548 if (path != null) { 549 File file = new File (path); 550 551 is = file.isFile() && file.canRead(); 552 } 553 return is; 554 } 555 556 private boolean isDir(String path) { 557 boolean is = false; 558 559 if (path != null) { 560 File file = new File (path); 561 562 is = file.isDirectory(); 563 } 564 return is; 565 } 566 567 private Properties readAppProperties() { 568 Properties props = new Properties (); 569 File file = new File (getAppPropertyPath()); 570 571 if (file.exists()) { 572 try { 573 FileInputStream in = new FileInputStream (file); 574 575 props.load(in); 576 } catch (Exception e) { 577 e.printStackTrace(); 578 props = new Properties (); 579 } 580 } 581 return props; 582 } 583 584 protected void storeAppProperties() { 585 File file = null; 586 FileOutputStream out = null; 587 Properties prop = new Properties (); 588 589 if (isDir(getHome())) { 590 prop.setProperty(PROPERTY_HOME, getHome()); 591 } 592 if (isDir(getConfDir())) { 593 prop.setProperty(PROPERTY_CONF_DIR, getConfDir()); 594 } 595 if (isFile(getConfFile())) { 596 prop.setProperty(PROPERTY_CONF_FILE, getConfFile()); 597 } 598 if (isFile(getPolicy())) { 599 prop.setProperty(PROPERTY_POLICY, getPolicy()); 600 } 601 if (isFile(getBrowser())) { 602 prop.setProperty(PROPERTY_BROWSER, getBrowser()); 603 } 604 file = new File (getAppPropertyPath()); 605 file.getParentFile().mkdirs(); 606 try { 607 out = new FileOutputStream (file); 608 prop.store(out, "Enhydra Launch Utility Properties"); 609 } catch (IOException e) { 610 e.printStackTrace(); 611 } 612 file = null; 613 } 614 615 class BootThread extends Thread { 616 private boolean fresh = true; 617 618 public void run() { 619 try { 620 if (fresh && (frame != null)) { 621 frame.refreshStatus("Launched server", null, true); 622 } 623 Bootstrap.main(bootArgs); 624 while (fresh) { 625 sleep(5000); 626 if (Kernel.getKernel() == null) { 627 if (fresh && (frame != null)) { 628 frame.refreshStatus("Server not present", "", 629 false); 630 } 631 } else { 632 if (fresh && (frame != null)) { 633 frame.refreshStatus("Server " 634 + Kernel.getKernel().getState(), 635 "", false); 636 } 637 } 638 } 639 } catch (Exception e) { 640 if (fresh && (frame != null)) { 641 frame.refreshStatus("Failed to start server", 642 e.getMessage(), false); 643 } 644 } catch (Error e) { 645 if (fresh && (frame != null)) { 646 frame.refreshStatus("Failed to start server", 647 e.getMessage(), false); 648 } 649 } 650 } 651 652 protected void degrade() { 653 fresh = false; 654 if (frame != null) { 655 frame.refreshStatus("Shutting down server", null, false); 656 } 657 closeFrame(); 658 if (isServerRunning()) { 659 try { 660 Kernel.getKernel().shutdownHard(); 661 } catch (Exception e) { 662 e.printStackTrace(); 663 } 664 } 665 } 666 667 } 668 class KillerThread extends Thread { 669 private int seconds = 10; 670 671 public KillerThread(int s) { 672 seconds = s; 673 } 674 675 public void run() { 676 try { 677 sleep(seconds * 1000); 678 } catch (Exception e) { 679 680 } 682 closeLogWriter(); 683 System.setSecurityManager(null); 684 System.exit(0); 685 } 686 687 } 688 } 689 | Popular Tags |