1 19 20 package org.netbeans.modules.tomcat5; 21 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.io.OutputStreamWriter ; 32 import java.util.Locale ; 33 import org.netbeans.modules.tomcat5.config.WebappConfiguration; 34 import org.netbeans.modules.tomcat5.config.gen.Server; 35 import org.openide.filesystems.*; 36 import javax.enterprise.deploy.model.DeployableObject ; 37 import javax.enterprise.deploy.shared.DConfigBeanVersionType ; 38 import javax.enterprise.deploy.shared.ModuleType ; 39 import javax.enterprise.deploy.spi.DeploymentConfiguration ; 40 import javax.enterprise.deploy.spi.DeploymentManager ; 41 import javax.enterprise.deploy.spi.Target ; 42 import javax.enterprise.deploy.spi.TargetModuleID ; 43 import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException ; 44 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException ; 45 import javax.enterprise.deploy.spi.exceptions.TargetException ; 46 import javax.enterprise.deploy.spi.status.ProgressObject ; 47 import org.openide.ErrorManager; 48 import org.netbeans.modules.j2ee.deployment.plugins.api.*; 49 import org.netbeans.modules.tomcat5.ide.StartTomcat; 50 import org.netbeans.modules.tomcat5.util.TomcatInstallUtil; 51 import org.netbeans.api.debugger.*; 52 import org.netbeans.api.debugger.jpda.*; 53 import org.netbeans.modules.tomcat5.progress.MultiProgressObjectWrapper; 54 import org.netbeans.modules.tomcat5.util.*; 55 import org.openide.util.NbBundle; 56 57 58 63 public class TomcatManager implements DeploymentManager { 64 65 public enum TomcatVersion {TOMCAT_50, TOMCAT_55, TOMCAT_60}; 66 67 public static ErrorManager ERR = ErrorManager.getDefault().getInstance("org.netbeans.modules.tomcat5"); 69 70 static final int ENUM_AVAILABLE = 0; 71 72 73 static final int ENUM_RUNNING = 1; 74 75 76 static final int ENUM_NONRUNNING = 2; 77 78 private static final String PROP_BUNDLED_TOMCAT = "is_it_bundled_tomcat"; 80 81 private boolean connected; 82 83 84 private String uri; 85 86 private StartTomcat startTomcat; 87 88 89 private Process process; 90 91 92 private TomcatManagerConfig tomcatManagerConfig; 93 94 95 private LogManager logManager = new LogManager(this); 96 97 private TomcatPlatformImpl tomcatPlatform; 98 99 private TomcatProperties tp; 100 101 private TomcatVersion tomcatVersion; 102 103 private InstanceProperties ip; 104 105 111 public TomcatManager(boolean conn, String uri, TomcatVersion tomcatVersion) 112 throws IllegalArgumentException { 113 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 114 TomcatFactory.getEM ().log ("Creating connected TomcatManager uri="+uri); } 116 this.connected = conn; 117 this.tomcatVersion = tomcatVersion; 118 this.uri = uri; 119 ip = InstanceProperties.getInstanceProperties(getUri()); 120 if (isBundledTomcat()) { 121 String displayName = NbBundle.getMessage(TomcatManager.class, "LBL_BundledTomcat"); 123 if (!displayName.equals(ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR))) { 124 ip.setProperty(InstanceProperties.DISPLAY_NAME_ATTR, displayName); 125 } 126 } 127 if (ip != null) { 128 tp = new TomcatProperties(this); 129 } 130 } 131 132 public InstanceProperties getInstanceProperties() { 133 return ip; 134 } 135 136 public boolean isBundledTomcat() { 137 if (ip == null) { 138 return false; 139 } 140 String val = ip.getProperty(PROP_BUNDLED_TOMCAT); 141 return val != null ? Boolean.valueOf(val).booleanValue() 142 : false; 143 } 144 145 public TomcatProperties getTomcatProperties() { 146 return tp; 147 } 148 149 155 public boolean isRunning(boolean checkResponse) { 156 return isRunning(tp.getRunningCheckTimeout(), checkResponse); 157 } 158 159 166 public boolean isRunning(int timeout, boolean checkResponse) { 167 Process proc = getTomcatProcess(); 168 if (proc != null) { 169 try { 170 proc.exitValue(); 172 return false; 173 } catch (IllegalThreadStateException e) { 174 if (!checkResponse) { 176 return true; 177 } 178 } 179 } 180 if (checkResponse) { 181 return Utils.pingTomcat(getServerPort(), timeout); } else { 183 return false; } 185 } 186 187 190 public String getUri () { 191 switch (tomcatVersion) { 192 case TOMCAT_60: 193 return TomcatFactory.TOMCAT_URI_PREFIX_60 + uri; 194 case TOMCAT_55: 195 return TomcatFactory.TOMCAT_URI_PREFIX_55 + uri; 196 case TOMCAT_50: 197 default: 198 return TomcatFactory.TOMCAT_URI_PREFIX_50 + uri; 199 } 200 } 201 202 205 public String getPlainUri () { 206 return "http://" + tp.getHost() + ":" + getCurrentServerPort() + "/manager/"; } 208 209 212 public String getServerUri () { 213 return "http://" + tp.getHost() + ":" + getCurrentServerPort(); } 215 216 222 public String getCatalinaWork() { 223 TomcatManagerConfig tmConfig = getTomcatManagerConfig(); 224 String engineName = tmConfig.getEngineElement().getAttributeValue("name"); String hostName = tmConfig.getHostElement().getAttributeValue("name"); StringBuffer catWork = new StringBuffer (tp.getCatalinaDir().toString()); 227 catWork.append("/work/").append(engineName).append("/").append(hostName); return catWork.toString(); 229 } 230 231 232 public void ensureCatalinaBaseReady() { 233 File baseDir = tp.getCatalinaBase(); 234 if (baseDir != null) { 235 String [] files = baseDir.list(); 236 if (files == null || files.length == 0) { 238 createBaseDir(baseDir, tp.getCatalinaHome()); 240 if (FileUtil.toFileObject(baseDir) == null) { 242 File parentDir = baseDir.getParentFile(); 244 if (parentDir != null) { 245 FileObject parentFileObject = FileUtil.toFileObject(parentDir); 246 if (parentFileObject != null) { 247 parentFileObject.refresh(); 248 } 249 } 250 } 251 } 252 } 253 } 254 255 public StartTomcat getStartTomcat(){ 256 return startTomcat; 257 } 258 259 public void setStartTomcat (StartTomcat st){ 260 startTomcat = st; 261 } 262 263 267 public boolean isDebugged() { 268 269 ServerDebugInfo sdi = null; 270 271 Session[] sessions = DebuggerManager.getDebuggerManager().getSessions(); 272 273 sdi = getStartTomcat().getDebugInfo(null); 274 if (sdi == null) { 275 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "DebuggerInfo cannot be found for: " + this.toString()); 276 } 277 278 for (int i=0; i < sessions.length; i++) { 279 Session s = sessions[i]; 280 if (s != null) { 281 Object o = s.lookupFirst(null, AttachingDICookie.class); 282 if (o != null) { 283 AttachingDICookie attCookie = (AttachingDICookie)o; 284 if (sdi.getTransport().equals(ServerDebugInfo.TRANSPORT_SHMEM)) { 285 if (attCookie.getSharedMemoryName().equalsIgnoreCase(sdi.getShmemName())) { 286 return true; 287 } 288 } else { 289 if (attCookie.getHostName().equalsIgnoreCase(sdi.getHost())) { 290 if (attCookie.getPortNumber() == sdi.getPort()) { 291 return true; 292 } 293 } 294 } 295 } 296 } 297 } 298 299 return false; 300 } 301 302 306 public boolean isSuspended() { 307 308 Session[] sessions = DebuggerManager.getDebuggerManager().getSessions(); 309 ServerDebugInfo sdi = getStartTomcat().getDebugInfo(null); 310 if (sdi == null) { 311 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "DebuggerInfo cannot be found for: " + this.toString()); 312 } 313 314 for (int i=0; i < sessions.length; i++) { 315 Session s = sessions[i]; 316 if (s != null) { 317 Object o = s.lookupFirst(null, AttachingDICookie.class); 318 if (o != null) { 319 AttachingDICookie attCookie = (AttachingDICookie)o; 320 if (sdi.getTransport().equals(ServerDebugInfo.TRANSPORT_SHMEM)) { 321 String shmem = attCookie.getSharedMemoryName(); 322 if (shmem == null) continue; 323 if (shmem.equalsIgnoreCase(sdi.getShmemName())) { 324 Object d = s.lookupFirst(null, JPDADebugger.class); 325 if (d != null) { 326 JPDADebugger jpda = (JPDADebugger)d; 327 if (jpda.getState() == JPDADebugger.STATE_STOPPED) { 328 return true; 329 } 330 } 331 } 332 } else { 333 String host = attCookie.getHostName(); 334 if (host == null) continue; 335 if (host.equalsIgnoreCase(sdi.getHost())) { 336 if (attCookie.getPortNumber() == sdi.getPort()) { 337 Object d = s.lookupFirst(null, JPDADebugger.class); 338 if (d != null) { 339 JPDADebugger jpda = (JPDADebugger)d; 340 if (jpda.getState() == JPDADebugger.STATE_STOPPED) { 341 return true; 342 } 343 } 344 } 345 } 346 } 347 } 348 } 349 } 350 351 return false; 352 } 353 354 public boolean isTomcat60() { 355 return tomcatVersion == TomcatVersion.TOMCAT_60; 356 } 357 358 public boolean isTomcat55() { 359 return tomcatVersion == TomcatVersion.TOMCAT_55; 360 } 361 362 public boolean isTomcat50() { 363 return tomcatVersion == TomcatVersion.TOMCAT_50; 364 } 365 366 367 public String libFolder() { 368 return isTomcat60() ? "lib" : "common/lib"; } 371 372 public TomcatVersion getTomcatVersion() { 373 return tomcatVersion; 374 } 375 376 378 public DeploymentConfiguration createConfiguration (DeployableObject deplObj) 379 throws InvalidModuleException { 380 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 381 TomcatFactory.getEM ().log ("TomcatManager.createConfiguration "+deplObj); 382 } 383 if (!ModuleType.WAR.equals (deplObj.getType ())) { 384 throw new InvalidModuleException ("Only WAR modules are supported for TomcatManager"); } 386 387 return new WebappConfiguration (deplObj, tomcatVersion); 388 } 389 390 public Locale getCurrentLocale () { 391 return Locale.getDefault (); 392 } 393 394 public Locale getDefaultLocale () { 395 return Locale.getDefault (); 396 } 397 398 public Locale [] getSupportedLocales () { 399 return Locale.getAvailableLocales (); 400 } 401 402 public boolean isLocaleSupported (Locale locale) { 403 if (locale == null) { 404 return false; 405 } 406 407 Locale [] supLocales = getSupportedLocales (); 408 for (int i =0; i<supLocales.length; i++) { 409 if (locale.equals (supLocales[i])) { 410 return true; 411 } 412 } 413 return false; 414 } 415 416 public TargetModuleID [] getAvailableModules (ModuleType moduleType, Target [] targetList) 417 throws TargetException , IllegalStateException { 418 return modules (ENUM_AVAILABLE, moduleType, targetList); 419 } 420 421 public TargetModuleID [] getNonRunningModules (ModuleType moduleType, Target [] targetList) 422 throws TargetException , IllegalStateException { 423 return modules (ENUM_NONRUNNING, moduleType, targetList); 424 } 425 426 public TargetModuleID [] getRunningModules (ModuleType moduleType, Target [] targetList) 427 throws TargetException , IllegalStateException { 428 return modules (ENUM_RUNNING, moduleType, targetList); 429 } 430 431 public Target [] getTargets () throws IllegalStateException { 432 if (!isConnected ()) { 433 throw new IllegalStateException ("TomcatManager.getTargets called on disconnected instance"); } 435 436 return new TomcatTarget [] { 438 new TomcatTarget (uri, "Tomcat at "+uri, getServerUri ()) 439 }; 440 } 441 442 public DConfigBeanVersionType getDConfigBeanVersion () { 443 return null; 445 } 446 447 public void setDConfigBeanVersion (DConfigBeanVersionType version) 448 throws DConfigBeanVersionUnsupportedException { 449 if (!DConfigBeanVersionType.V1_3_1.equals (version)) { 450 throw new DConfigBeanVersionUnsupportedException ("unsupported version"); 451 } 452 } 453 454 public boolean isDConfigBeanVersionSupported (DConfigBeanVersionType version) { 455 return DConfigBeanVersionType.V1_3_1.equals (version); 456 } 457 458 public boolean isRedeploySupported () { 459 return false; 461 } 462 463 public ProgressObject redeploy (TargetModuleID [] targetModuleID, InputStream inputStream, InputStream inputStream2) 464 throws UnsupportedOperationException , IllegalStateException { 465 throw new UnsupportedOperationException ("TomcatManager.redeploy not supported yet."); 467 } 468 469 public ProgressObject redeploy (TargetModuleID [] tmID, File file, File file2) 470 throws UnsupportedOperationException , IllegalStateException { 471 throw new UnsupportedOperationException ("TomcatManager.redeploy not supported yet."); 473 } 474 475 public void release () { 476 } 477 478 public void setLocale (Locale locale) throws UnsupportedOperationException { 479 } 480 481 public ProgressObject start (TargetModuleID [] tmID) throws IllegalStateException { 482 if (!isConnected ()) { 483 throw new IllegalStateException ("TomcatManager.start called on disconnected instance"); } 485 if (tmID.length != 1 || !(tmID[0] instanceof TomcatModule)) { 486 throw new IllegalStateException ("TomcatManager.start invalid TargetModuleID passed"); } 488 489 TomcatManagerImpl impl = new TomcatManagerImpl (this); 490 impl.start ((TomcatModule)tmID[0]); 491 return impl; 492 } 493 494 public ProgressObject stop (TargetModuleID [] tmID) throws IllegalStateException { 495 if (!isConnected ()) { 496 throw new IllegalStateException ("TomcatManager.stop called on disconnected instance"); } 498 if (tmID.length != 1 || !(tmID[0] instanceof TomcatModule)) { 499 throw new IllegalStateException ("TomcatManager.stop invalid TargetModuleID passed"); } 501 502 TomcatManagerImpl impl = new TomcatManagerImpl (this); 503 impl.stop ((TomcatModule)tmID[0]); 504 return impl; 505 } 506 507 public ProgressObject undeploy (TargetModuleID [] tmID) throws IllegalStateException { 508 if (!isConnected ()) { 509 throw new IllegalStateException ("TomcatManager.undeploy called on disconnected instance"); } 511 512 if (tmID == null) { 513 throw new NullPointerException ("TomcatManager.undeploy the tmID argument must not be null."); } 515 516 if (tmID.length == 0) { 517 throw new IllegalArgumentException ("TomcatManager.undeploy at least one TargetModuleID object must be passed."); } 519 520 for (int i = 0; i < tmID.length; i++) { 521 if (!(tmID[i] instanceof TomcatModule)) { 522 throw new IllegalStateException ("TomcatManager.undeploy invalid TargetModuleID passed: " + tmID[i].getClass().getName()); } 524 } 525 526 TomcatManagerImpl[] tmImpls = new TomcatManagerImpl[tmID.length]; 527 for (int i = 0; i < tmID.length; i++) { 528 tmImpls[i] = new TomcatManagerImpl (this); 529 } 530 ProgressObject po = new MultiProgressObjectWrapper(tmImpls); 532 533 for (int i = 0; i < tmID.length; i++) { 534 TomcatModule tm = (TomcatModule) tmID[i]; 535 if ("/manager".equals(tm.getPath())) { String msg = NbBundle.getMessage(TomcatModule.class, "MSG_CannotUndeployManager"); 538 throw new IllegalStateException (msg); 539 } 540 tmImpls[i].remove(tm); 541 } 542 return po; 543 } 544 545 552 public ProgressObject distribute (Target [] targets, InputStream is, InputStream deplPlan) 553 throws IllegalStateException { 554 if (!isConnected ()) { 555 throw new IllegalStateException ("TomcatManager.distribute called on disconnected instance"); } 557 558 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 559 TomcatFactory.getEM ().log ("TomcatManager.distribute streams"); 560 } 561 TomcatManagerImpl impl = new TomcatManagerImpl (this); 562 impl.deploy (targets[0], is, deplPlan); 563 return impl; 564 } 565 566 573 public ProgressObject distribute (Target [] targets, File moduleArchive, File deplPlan) 574 throws IllegalStateException { 575 if (!isConnected ()) { 576 throw new IllegalStateException ("TomcatManager.distribute called on disconnected instance"); } 578 579 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 580 TomcatFactory.getEM ().log ("TomcatManager.distribute archive="+moduleArchive.getPath ()+", plan="+deplPlan.getPath ()); } 582 TomcatManagerImpl impl = new TomcatManagerImpl (this); 583 impl.install (targets[0], moduleArchive, deplPlan); 584 return impl; 585 } 586 587 public ProgressObject distribute(Target [] target, ModuleType moduleType, InputStream inputStream, InputStream inputStream0) throws IllegalStateException { 588 return distribute(target, inputStream, inputStream0); 589 } 590 591 593 599 private TargetModuleID [] modules (int state, ModuleType moduleType, Target [] targetList) 600 throws TargetException , IllegalStateException { 601 if (!isConnected ()) { 602 throw new IllegalStateException ("TomcatManager.modules called on disconnected instance"); } 604 if (targetList.length != 1) { 605 throw new TargetException ("TomcatManager.modules supports only one target"); } 607 608 if (!ModuleType.WAR.equals (moduleType)) { 609 return new TargetModuleID [0]; 610 } 611 612 TomcatManagerImpl impl = new TomcatManagerImpl (this); 613 return impl.list (targetList[0], state); 614 } 615 616 619 public boolean isConnected () { 620 return connected; 621 } 622 623 public String toString () { 624 return "Tomcat manager ["+uri+", home "+tp.getCatalinaHome()+", base "+tp.getCatalinaBase()+(connected?"conneceted":"disconnected")+"]"; } 626 627 public void setServerPort(int port) { 628 ensureCatalinaBaseReady(); if (TomcatInstallUtil.setServerPort(port, tp.getServerXml())) { 630 tp.setServerPort(port); 631 } 632 } 633 634 public void setShutdownPort(int port) { 635 ensureCatalinaBaseReady(); if (TomcatInstallUtil.setShutdownPort(port, tp.getServerXml())) { 637 tp.setShutdownPort(port); 638 } 639 } 640 641 643 public int getCurrentServerPort() { 644 if (startTomcat != null && isRunning(false)) { 645 return startTomcat.getCurrentServerPort(); 646 } else { 647 return getServerPort(); 648 } 649 } 650 651 653 public int getServerPort() { 654 ensurePortsUptodate(); 655 return tp.getServerPort(); 656 } 657 658 public int getShutdownPort() { 659 ensurePortsUptodate(); 660 return tp.getShutdownPort(); 661 } 662 663 private void ensurePortsUptodate() { 664 File serverXml = tp.getServerXml(); 665 long timestamp = -1; 666 if (serverXml.exists()) { 667 timestamp = serverXml.lastModified(); 668 if (timestamp > tp.getTimestamp()) { 669 try { 670 if (isBundledTomcat() && !new File (tp.getCatalinaBase(), "conf/server.xml").exists()) { tp.setTimestamp(timestamp); 675 tp.setServerPort(TomcatProperties.DEF_VALUE_BUNDLED_SERVER_PORT); 676 tp.setShutdownPort(TomcatProperties.DEF_VALUE_BUNDLED_SHUTDOWN_PORT); 677 return; 678 } 679 Server server = Server.createGraph(serverXml); 680 tp.setTimestamp(timestamp); 681 tp.setServerPort(Integer.parseInt(TomcatInstallUtil.getPort(server))); 682 tp.setShutdownPort(Integer.parseInt(TomcatInstallUtil.getShutdownPort(server))); 683 } catch (IOException ioe) { 684 TomcatFactory.getEM().notify(ErrorManager.INFORMATIONAL, ioe); 685 } catch (NumberFormatException nfe) { 686 TomcatFactory.getEM().notify(ErrorManager.INFORMATIONAL, nfe); 687 } catch (RuntimeException e) { 688 TomcatFactory.getEM().notify(ErrorManager.INFORMATIONAL, e); 689 } 690 } 691 } 692 } 693 694 public Server getRoot() { 695 try { 696 return Server.createGraph(tp.getServerXml()); 697 } catch (IOException e) { 698 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 699 TomcatFactory.getEM ().log (e.toString()); 700 } 701 return null; 702 } catch (RuntimeException e) { 703 TomcatFactory.getEM().notify(ErrorManager.INFORMATIONAL, e); 704 return null; 705 } 706 } 707 708 713 public File createBaseDir(File baseDir, File homeDir) { 714 File targetFolder; 715 if (!baseDir.isAbsolute ()) { 716 baseDir = new File (System.getProperty("netbeans.user")+System.getProperty("file.separator")+baseDir); 717 targetFolder = new File (System.getProperty("netbeans.user")); 718 719 } else { 720 targetFolder = baseDir.getParentFile (); 721 } 722 723 try { 724 725 if (targetFolder == null) { 726 TomcatFactory.getEM ().log (ErrorManager.INFORMATIONAL, "Cannot find parent folder for base dir "+baseDir.getPath ()); 727 return null; 728 } 729 File baseDirFO = new File (targetFolder, baseDir.getName ()); 730 baseDirFO.mkdir (); 731 732 String [] subdirs = new String [] { 734 "conf", "conf/Catalina", "conf/Catalina/localhost", "logs", "work", "temp", "webapps" }; 742 for (int i = 0; i<subdirs.length; i++) { 743 File dest = new File (baseDirFO, subdirs [i]); 744 dest.mkdirs (); 745 } 746 final String ADMIN_XML = "conf/Catalina/localhost/admin.xml"; 748 String [] files = new String [] { 749 "conf/catalina.policy", "conf/catalina.properties", "conf/logging.properties", "conf/server.xml", "conf/tomcat-users.xml", "conf/web.xml", ADMIN_XML, "conf/Catalina/localhost/manager.xml", }; 758 String [] patternFrom = new String [] { 759 null, 760 null, 761 null, 762 null, 763 "</tomcat-users>", null, 765 "docBase=\"../server/webapps/admin\"", isTomcat50() || isTomcat55() ? "docBase=\"../server/webapps/manager\"" : null, }; 768 String passwd = null; 769 if (isBundledTomcat()) { 770 passwd = TomcatInstallUtil.generatePassword(8); 771 tp.setPassword(passwd); 772 } 773 String [] patternTo = new String [] { 774 null, 775 null, 776 null, 777 null, 778 passwd != null ? "<user username=\"ide\" password=\"" + passwd + "\" roles=\"manager,admin\"/>\n</tomcat-users>" : null, null, 780 "docBase=\"${catalina.home}/server/webapps/admin\"", isTomcat50() || isTomcat55() ? "docBase=\"${catalina.home}/server/webapps/manager\"" : null, }; 783 for (int i = 0; i<files.length; i++) { 784 int slash = files[i].lastIndexOf ('/'); 786 String sfolder = files[i].substring (0, slash); 787 File fromDir = new File (homeDir, sfolder); File toDir = new File (baseDir, sfolder); 790 if (patternTo[i] == null) { 791 File fileToCopy = new File (homeDir, files[i]); 792 if (!fileToCopy.exists()) { 793 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Cannot copy file " 794 + fileToCopy.getAbsolutePath() + " to the Tomcat base dir, since it does not exist."); continue; 796 } 797 FileInputStream is = new FileInputStream (fileToCopy); 798 FileOutputStream os = new FileOutputStream (new File (toDir, files[i].substring (slash+1))); 799 try { 800 final byte[] BUFFER = new byte[4096]; 801 int len; 802 803 for (;;) { 804 len = is.read (BUFFER); 805 if (len == -1) break; 806 os.write (BUFFER, 0, len); 807 } 808 } catch (java.io.IOException ioe) { 809 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, ioe); 810 } finally { 811 try { if (os != null) os.close (); } catch (java.io.IOException ioe) { } 813 try { if (is != null) is.close (); } catch (java.io.IOException ioe) { } 815 } 816 } 817 else { 818 if (!copyAndPatch ( 820 new File (fromDir, files[i].substring (slash+1)), 821 new File (toDir, files[i].substring (slash+1)), 822 patternFrom[i], 823 patternTo[i] 824 )) { 825 if (!(ADMIN_XML.equals(files[i]) && !(new File (fromDir, files[i].substring (slash+1))).exists()) ){ 826 ErrorManager.getDefault ().log (ErrorManager.INFORMATIONAL, "Cannot create config file "+files[i]); 827 } 828 } 829 } 830 } 831 if (new File (homeDir, "webapps/ROOT").exists()) { writeToFile(new File (baseDir, "conf/Catalina/localhost/ROOT.xml"), "<Context path=\"\" docBase=\"${catalina.home}/webapps/ROOT\"/>\n"); } 836 if (!isTomcat50() && !isTomcat55() && new File (homeDir, "webapps/manager").exists()) { writeToFile(new File (baseDir, "conf/Catalina/localhost/manager.xml"), "<Context docBase=\"${catalina.home}/webapps/manager\" antiResourceLocking=\"false\" privileged=\"true\"/>\n"); } 841 } catch (java.io.IOException ioe) { 842 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, ioe); 843 return null; 844 } 845 if (isBundledTomcat()) { 846 TomcatInstallUtil.patchBundledServerXml(new File (baseDir, "conf/server.xml")); } 848 return baseDir; 849 } 850 851 854 private void writeToFile(File file, String data) throws IOException { 855 BufferedWriter bw = null; 856 try { 857 bw = new BufferedWriter (new FileWriter (file)); 858 bw.write(data); 859 } finally { 860 if (bw != null) bw.close(); 861 } 862 } 863 864 868 private boolean copyAndPatch (File src, File dst, String from, String to) { 869 java.io.Reader r = null; 870 java.io.Writer out = null; 871 if (!src.exists()) 872 return false; 873 try { 874 r = new BufferedReader (new InputStreamReader (new FileInputStream (src), "utf-8")); StringBuffer sb = new StringBuffer (); 876 final char[] BUFFER = new char[4096]; 877 int len; 878 879 for (;;) { 880 len = r.read (BUFFER); 881 if (len == -1) break; 882 sb.append (BUFFER, 0, len); 883 } 884 int idx = sb.toString ().indexOf (from); 885 if (idx >= 0) { 886 sb.replace (idx, idx+from.length (), to); } 888 else { 889 TomcatFactory.getEM ().log(ErrorManager.INFORMATIONAL, "Pattern "+from+" not found in "+src.getPath ()); 891 } 892 out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (dst), "utf-8")); out.write (sb.toString ()); 894 895 } catch (java.io.IOException ioe) { 896 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, ioe); 897 return false; 898 } finally { 899 try { if (out != null) out.close (); } catch (java.io.IOException ioe) { } 901 try { if (r != null) r.close (); } catch (java.io.IOException ioe) { } 903 } 904 return true; 905 } 906 907 913 public void openLog(TargetModuleID module) { 914 TomcatModule tomcatModule = null; 915 if (module instanceof TomcatModule) { 916 tomcatModule = (TomcatModule)module; 917 } else { 918 try { 919 TargetModuleID [] tomMod = getRunningModules(ModuleType.WAR, new Target []{module.getTarget()}); 920 for (int i = 0; i < tomMod.length; i++) { 921 if (module.getModuleID().equals(tomMod[i].getModuleID())) { 922 tomcatModule = (TomcatModule)tomMod[i]; 923 break; 924 } 925 } 926 } catch (TargetException te) { 927 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, te); 928 } 929 } 930 if (tomcatModule != null && logManager.hasContextLogger(tomcatModule)) { 931 logManager.openContextLog(tomcatModule); 932 } else { 933 logManager.openSharedContextLog(); 934 } 935 } 936 937 944 public synchronized TomcatManagerConfig getTomcatManagerConfig() { 945 if (tomcatManagerConfig == null) { 946 tomcatManagerConfig = new TomcatManagerConfig(tp.getServerXml()); 947 } 948 return tomcatManagerConfig; 949 } 950 951 958 public LogManager logManager() { 959 return logManager; 960 } 961 962 967 public synchronized void setTomcatProcess(Process p) { 968 process = p; 969 } 970 971 977 public synchronized Process getTomcatProcess() { 978 return process; 979 } 980 981 982 public void terminate() { 983 Process proc = getTomcatProcess(); 984 if (proc != null) { 985 proc.destroy(); 986 } 987 } 988 989 public synchronized TomcatPlatformImpl getTomcatPlatform() { 990 if (tomcatPlatform == null) { 991 tomcatPlatform = new TomcatPlatformImpl(this); 992 } 993 return tomcatPlatform; 994 } 995 996 } 997 | Popular Tags |