1 19 20 package org.netbeans.modules.tomcat5.util; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.FilenameFilter ; 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import org.netbeans.api.java.platform.JavaPlatform; 35 import org.netbeans.api.java.platform.JavaPlatformManager; 36 import org.netbeans.api.java.platform.Specification; 37 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 38 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 39 import org.netbeans.modules.tomcat5.TomcatFactory; 40 import org.netbeans.modules.tomcat5.TomcatManager; 41 import org.netbeans.modules.tomcat5.TomcatManager.TomcatVersion; 42 import org.netbeans.modules.tomcat5.customizer.CustomizerSupport; 43 import org.openide.ErrorManager; 44 import org.openide.filesystems.FileLock; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileSystem; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.filesystems.Repository; 49 import org.openide.modules.InstalledFileLocator; 50 import org.openide.util.NbBundle; 51 import org.openide.util.Utilities; 52 57 public class TomcatProperties { 58 59 private static final Logger LOGGER = Logger.getLogger(TomcatProperties.class.getName()); 60 61 62 public static final String PLAT_PROP_ANT_NAME = "platform.ant.name"; 64 public static final String DEBUG_TYPE_SOCKET = "SEL_debuggingType_socket"; public static final String DEBUG_TYPE_SHARED = "SEL_debuggingType_shared"; 67 public static final String BUNDLED_TOMCAT_SETTING = "J2EE/BundledTomcat/Setting"; 69 private static final String PROP_URL = InstanceProperties.URL_ATTR; 71 private static final String PROP_USERNAME = InstanceProperties.USERNAME_ATTR; 72 private static final String PROP_PASSWORD = InstanceProperties.PASSWORD_ATTR; 73 public static final String PROP_SERVER_PORT = InstanceProperties.HTTP_PORT_NUMBER; 74 private static final String PROP_DISPLAY_NAME = InstanceProperties.DISPLAY_NAME_ATTR; 75 public static final String PROP_SHUTDOWN = "admin_port"; public static final String PROP_MONITOR = "monitor_enabled"; public static final String PROP_PROXY_ENABLED = "proxy_enabled"; private static final String PROP_CUSTOM_SCRIPT = "custom_script_enabled"; private static final String PROP_SCRIPT_PATH = "script_path"; private static final String PROP_FORCE_STOP = "forceStopOption"; private static final String PROP_DEBUG_TYPE = "debug_type"; private static final String PROP_DEBUG_PORT = "debugger_port"; private static final String PROP_SHARED_MEM = "shared_memory"; private static final String PROP_JAVA_PLATFORM = "java_platform"; private static final String PROP_JAVA_OPTS = "java_opts"; private static final String PROP_SEC_MANAGER = "securityStartupOption"; private static final String PROP_SOURCES = "sources"; private static final String PROP_JAVADOCS = "javadocs"; private static final String PROP_OPEN_LOG = "openContextLogOnRun"; 91 private static final String PROP_TIMESTAMP = "timestamp"; private static final String PROP_HOST = "host"; public static final String PROP_RUNNING_CHECK_TIMEOUT = "runningCheckTimeout"; private static final String PROP_INSTANCE_ID = "instance_id"; 96 97 private static final boolean DEF_VALUE_SEC_MANAGER = false; 99 private static final boolean DEF_VALUE_CUSTOM_SCRIPT = false; 100 private static final String DEF_VALUE_SCRIPT_PATH = ""; private static final boolean DEF_VALUE_FORCE_STOP = false; 102 private static final String DEF_VALUE_JAVA_OPTS = ""; private static final String DEF_VALUE_DEBUG_TYPE = Utilities.isWindows() ? DEBUG_TYPE_SHARED 104 : DEBUG_TYPE_SOCKET; 105 private static final boolean DEF_VALUE_MONITOR = true; 106 private static final boolean DEF_VALUE_PROXY_ENABLED = true; 107 private static final int DEF_VALUE_DEBUG_PORT = 11555; 108 private static final int DEF_VALUE_SERVER_PORT = 8080; 109 public static final int DEF_VALUE_SHUTDOWN_PORT = 8005; 110 111 public static final int DEF_VALUE_BUNDLED_SERVER_PORT = 8084; 112 public static final int DEF_VALUE_BUNDLED_SHUTDOWN_PORT = 8025; 113 114 private static final String DEF_VALUE_SHARED_MEM = "tomcat_shared_memory_id"; private static final boolean DEF_VALUE_OPEN_LOG = true; 116 private static final String DEF_VALUE_HOST = "localhost"; public static final int DEF_VALUE_RUNNING_CHECK_TIMEOUT = 2000; 118 private static final String DEF_VALUE_DISPLAY_NAME = 119 NbBundle.getMessage(TomcatProperties.class, "LBL_DefaultDisplayName"); 120 121 private TomcatManager tm; 122 private InstanceProperties ip; 123 private File homeDir; 124 private File baseDir; 125 126 127 public TomcatProperties(TomcatManager tm) throws IllegalArgumentException { 128 this.tm = tm; 129 this.ip = tm.getInstanceProperties(); 130 String catalinaHome = null; 131 String catalinaBase = null; 132 String uri = ip.getProperty(PROP_URL); final String home = "home="; final String base = ":base="; final String uriString = "http://"; int uriOffset = uri.indexOf (uriString); 137 int homeOffset = uri.indexOf (home) + home.length (); 138 int baseOffset = uri.indexOf (base, homeOffset); 139 if (homeOffset >= home.length ()) { 140 int homeEnd = baseOffset > 0 ? baseOffset : (uriOffset > 0 ? uriOffset - 1 : uri.length ()); 141 int baseEnd = uriOffset > 0 ? uriOffset - 1 : uri.length (); 142 catalinaHome= uri.substring (homeOffset, homeEnd); 143 if (baseOffset > 0) { 144 catalinaBase = uri.substring (baseOffset + base.length (), baseEnd); 145 } 146 if (catalinaHome.length() > 0 && catalinaHome.charAt(0) == '$') { 151 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 152 FileObject fo = fs.findResource(BUNDLED_TOMCAT_SETTING); 153 if (fo != null) { 154 catalinaHome = fo.getAttribute(catalinaHome.substring(1)).toString(); 155 if (catalinaBase != null && catalinaBase.length() > 0 156 && catalinaBase.charAt(0) == '$') { 157 catalinaBase = fo.getAttribute(catalinaBase.substring(1)).toString(); 158 } 159 } 160 } 161 } 162 if (catalinaHome == null) { 163 throw new IllegalArgumentException ("CATALINA_HOME must not be null."); } 165 homeDir = new File (catalinaHome); 166 if (!homeDir.isAbsolute ()) { 167 InstalledFileLocator ifl = InstalledFileLocator.getDefault(); 168 homeDir = ifl.locate(catalinaHome, null, false); 169 } 170 if (!homeDir.exists()) { 171 throw new IllegalArgumentException ("CATALINA_HOME directory does not exist."); } 173 if (catalinaBase != null) { 174 baseDir = new File (catalinaBase); 175 if (!baseDir.isAbsolute ()) { 176 InstalledFileLocator ifl = InstalledFileLocator.getDefault(); 177 baseDir = ifl.locate(catalinaBase, null, false); 178 if (baseDir == null) { 179 baseDir = new File (System.getProperty("netbeans.user"), catalinaBase); } 181 } 182 } 183 184 ip.addPropertyChangeListener(new PropertyChangeListener () { 202 public void propertyChange(PropertyChangeEvent evt) { 203 String name = evt.getPropertyName(); 204 if (PROP_SERVER_PORT.equals(name) || PROP_USERNAME.equals(name) 205 || PROP_PASSWORD.equals(name)) { 206 try { 208 storeAntDeploymentProperties(getAntDeploymentPropertiesFile(), false); 209 } catch(IOException ioe) { 210 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 211 } 212 } 213 } 214 }); 215 } 216 217 223 public void storeAntDeploymentProperties(File file, boolean create) throws IOException { 224 if (!create && !file.exists()) { 225 return; 226 } 227 EditableProperties antProps = new EditableProperties(); 228 antProps.setProperty("tomcat.home", homeDir.getAbsolutePath()); antProps.setProperty("tomcat.url", getWebUrl()); antProps.setProperty("tomcat.username", getUsername()); antProps.setProperty("tomcat.password", getPassword()); file.createNewFile(); 233 FileObject fo = FileUtil.toFileObject(file); 234 FileLock lock = fo.lock(); 235 try { 236 OutputStream os = fo.getOutputStream(lock); 237 try { 238 antProps.store(os); 239 } finally { 240 os.close(); 241 } 242 } finally { 243 lock.releaseLock(); 244 } 245 } 246 247 248 public File getAntDeploymentPropertiesFile() { 249 return new File (System.getProperty("netbeans.user"), getInstanceID() + ".properties"); } 251 252 256 private String getInstanceID() { 257 String name = ip.getProperty(PROP_INSTANCE_ID); 258 if (name != null) { 259 return name; 260 } 261 String prefix; 264 String serverID; 265 switch (tm.getTomcatVersion()) { 266 case TOMCAT_60: 267 prefix = "tomcat60"; serverID = TomcatFactory.SERVER_ID_60; 269 break; 270 case TOMCAT_55: 271 prefix = "tomcat55"; serverID = TomcatFactory.SERVER_ID_55; 273 break; 274 case TOMCAT_50: 275 default: 276 prefix = "tomcat50"; serverID = TomcatFactory.SERVER_ID_50; 278 } 279 String [] instanceURLs = Deployment.getDefault().getInstancesOfServer(serverID); 280 for (int i = 0; name == null; i++) { 281 if (i == 0) { 282 name = prefix; 283 } else { 284 name = prefix + "_" + i; } 286 for (String url: instanceURLs) { 287 if (!tm.getUri().equals(url)) { 288 InstanceProperties ip = InstanceProperties.getInstanceProperties(url); 289 if (ip != null) { 290 String anotherName = ip.getProperty(PROP_INSTANCE_ID); 291 if (name.equals(anotherName)) { 292 name = null; 293 break; 294 } 295 } 296 } 297 } 298 } 299 ip.setProperty(PROP_INSTANCE_ID, name); 300 return name; 301 } 302 303 304 public String getWebUrl() { 305 return "http://" + getHost() + ":" + getServerPort(); } 307 308 309 public File getCatalinaHome() { 310 return homeDir; 311 } 312 313 314 public File getCatalinaBase() { 315 return baseDir; 316 } 317 318 319 public File getCatalinaDir() { 320 return baseDir == null ? homeDir : baseDir; 321 } 322 323 public String getUsername() { 324 String val = ip.getProperty(PROP_USERNAME); 325 return val != null ? val : ""; } 327 328 public void setUsername(String value) { 329 ip.setProperty(PROP_USERNAME, value); 330 } 331 332 public String getPassword() { 333 String val = ip.getProperty(PROP_PASSWORD); 334 return val != null ? val : ""; } 336 337 public void setPassword(String value) { 338 ip.setProperty(PROP_PASSWORD, value); 339 } 340 341 public JavaPlatform getJavaPlatform() { 342 String currentJvm = ip.getProperty(PROP_JAVA_PLATFORM); 343 JavaPlatformManager jpm = JavaPlatformManager.getDefault(); 344 JavaPlatform[] installedPlatforms = jpm.getPlatforms(null, new Specification("J2SE", null)); for (int i = 0; i < installedPlatforms.length; i++) { 346 String platformName = (String )installedPlatforms[i].getProperties().get(PLAT_PROP_ANT_NAME); 347 if (platformName != null && platformName.equals(currentJvm)) { 348 return installedPlatforms[i]; 349 } 350 } 351 return jpm.getDefaultPlatform(); 353 } 354 355 public void setJavaPlatform(JavaPlatform javaPlatform) { 356 ip.setProperty(PROP_JAVA_PLATFORM, (String )javaPlatform.getProperties().get(PLAT_PROP_ANT_NAME)); 357 } 358 359 public String getJavaOpts() { 360 String val = ip.getProperty(PROP_JAVA_OPTS); 361 return val != null ? val 362 : DEF_VALUE_JAVA_OPTS; 363 } 364 365 public void setJavaOpts(String javaOpts) { 366 ip.setProperty(PROP_JAVA_OPTS, javaOpts); 367 } 368 369 public boolean getSecManager() { 370 String val = ip.getProperty(PROP_SEC_MANAGER); 371 return val != null ? Boolean.valueOf(val).booleanValue() 372 : DEF_VALUE_SEC_MANAGER; 373 } 374 375 public void setSecManager(boolean enabled) { 376 ip.setProperty(PROP_SEC_MANAGER, Boolean.toString(enabled)); 377 } 378 379 380 public boolean getCustomScript() { 381 String val = ip.getProperty(PROP_CUSTOM_SCRIPT); 382 return val != null ? Boolean.valueOf(val).booleanValue() 383 : DEF_VALUE_CUSTOM_SCRIPT; 384 } 385 386 public void setCustomScript(boolean enabled) { 387 ip.setProperty(PROP_CUSTOM_SCRIPT, Boolean.toString(enabled)); 388 } 389 390 public String getScriptPath() { 391 String val = ip.getProperty(PROP_SCRIPT_PATH); 392 return val != null ? val 393 : DEF_VALUE_SCRIPT_PATH; 394 } 395 396 public void setScriptPath(String path) { 397 ip.setProperty(PROP_SCRIPT_PATH, path); 398 } 399 400 public boolean getForceStop() { 401 if (Utilities.isWindows()) { 402 return false; 403 } 404 String val = ip.getProperty(PROP_FORCE_STOP); 405 return val != null ? Boolean.valueOf(val).booleanValue() 406 : DEF_VALUE_FORCE_STOP; 407 } 408 409 public void setForceStop(boolean enabled) { 410 ip.setProperty(PROP_FORCE_STOP, Boolean.toString(enabled)); 411 } 412 413 public String getDebugType() { 414 String val = ip.getProperty(PROP_DEBUG_TYPE); 415 if ((DEBUG_TYPE_SHARED.equalsIgnoreCase(val) && Utilities.isWindows()) 416 || DEBUG_TYPE_SOCKET.equalsIgnoreCase(val)) { 417 return val; 418 } 419 return DEF_VALUE_DEBUG_TYPE; 420 } 421 422 public void setDebugType(String type) { 423 ip.setProperty(PROP_DEBUG_TYPE, type); 424 } 425 426 public boolean getMonitor() { 427 String val = ip.getProperty(PROP_MONITOR); 428 return val != null ? Boolean.valueOf(val).booleanValue() 429 : DEF_VALUE_MONITOR; 430 } 431 432 public void setMonitor(boolean enabled) { 433 ip.setProperty(PROP_MONITOR, Boolean.toString(enabled)); 434 } 435 436 public boolean getProxyEnabled() { 437 String val = ip.getProperty(PROP_PROXY_ENABLED); 438 return val != null ? Boolean.valueOf(val).booleanValue() 439 : DEF_VALUE_PROXY_ENABLED; 440 } 441 442 public void setProxyEnabled(boolean enabled) { 443 ip.setProperty(PROP_PROXY_ENABLED, Boolean.toString(enabled)); 444 } 445 446 public int getDebugPort() { 447 String val = ip.getProperty(PROP_DEBUG_PORT); 448 449 if (val != null) { 450 try { 451 return Integer.parseInt(val); 452 } catch (NumberFormatException nfe) { 453 TomcatManager.ERR.notify(ErrorManager.INFORMATIONAL, nfe); 454 } 455 } 456 return DEF_VALUE_DEBUG_PORT; 457 } 458 459 public void setDebugPort(int port) { 460 ip.setProperty(PROP_DEBUG_PORT, Integer.toString(port)); 461 } 462 463 464 public int getServerPort() { 465 String val = ip.getProperty(PROP_SERVER_PORT); 466 if (val != null) { 467 try { 468 int port = Integer.parseInt(val); 469 if (port >= 0 && port <= 65535) { 470 return port; 471 } 472 } catch (NumberFormatException nfe) { 473 TomcatManager.ERR.notify(ErrorManager.INFORMATIONAL, nfe); 474 } 475 } 476 return tm.isBundledTomcat() ? DEF_VALUE_BUNDLED_SERVER_PORT : DEF_VALUE_SERVER_PORT; 477 } 478 479 480 public void setServerPort(int port) { 481 ip.setProperty(PROP_SERVER_PORT, Integer.toString(port)); 482 } 483 484 public int getShutdownPort() { 485 String val = ip.getProperty(PROP_SHUTDOWN); 486 if (val != null) { 487 try { 488 int port = Integer.parseInt(val); 489 if (port >= 0 && port <= 65535) { 490 return port; 491 } 492 } catch (NumberFormatException nfe) { 493 TomcatManager.ERR.notify(ErrorManager.INFORMATIONAL, nfe); 494 } 495 } 496 return tm.isBundledTomcat() ? DEF_VALUE_BUNDLED_SHUTDOWN_PORT : DEF_VALUE_SHUTDOWN_PORT; 497 } 498 499 500 public void setShutdownPort(int port) { 501 ip.setProperty(PROP_SHUTDOWN, Integer.toString(port)); 502 } 503 504 public String getSharedMem() { 505 String val = ip.getProperty(PROP_SHARED_MEM); 506 return val != null && val.length() > 0 ? val 507 : DEF_VALUE_SHARED_MEM; 508 } 509 510 public void setSharedMem(String val) { 511 ip.setProperty(PROP_SHARED_MEM, val); 512 } 513 514 public List getClasses() { 515 String [] nbFilter = new String [] { 516 "httpmonitor", "schema2beans" 517 }; 518 519 String [] implFilter = new String [] { 520 "-impl.jar" 521 }; 522 523 List retValue = listUrls(new File (homeDir, tm.libFolder()), nbFilter); 526 if (tm.isTomcat60()) { 527 try { 528 retValue.add(new File (homeDir, "bin/tomcat-juli.jar").toURI().toURL()); } catch (MalformedURLException e) { 530 LOGGER.log(Level.WARNING, "$CATALINA_HOME/bin/tomcat-juli.jar not found", e); } 532 } 533 534 retValue.addAll(listUrls(new File (homeDir, "jaxws/lib"), implFilter)); retValue.addAll(listUrls(new File (homeDir, "jaxb/lib"), implFilter)); retValue.addAll(listUrls(new File (homeDir, "jwsdp-shared/lib"), implFilter)); retValue.addAll(listUrls(new File (homeDir, "jaxp/lib"), implFilter)); retValue.addAll(listUrls(new File (homeDir, "jaxrpc/lib"), implFilter)); retValue.addAll(listUrls(new File (homeDir, "jaxr/lib"), implFilter)); retValue.addAll(listUrls(new File (homeDir, "saaj/lib"), implFilter)); retValue.addAll(listUrls(new File (homeDir, "sjsxp/lib"), implFilter)); 544 retValue.addAll(listUrls(new File (homeDir, "shared/lib"), implFilter)); 547 retValue.addAll(listUrls(new File (homeDir, "jstl/lib"), implFilter)); retValue.addAll(listUrls(new File (baseDir, "shared/lib"), nbFilter)); return retValue; 551 } 552 553 public List getSources() { 554 String path = ip.getProperty(PROP_SOURCES); 555 if (path == null) { 556 return new ArrayList (); 557 } 558 return CustomizerSupport.tokenizePath(path); 559 } 560 561 public void setSources(List path) { 562 ip.setProperty(PROP_SOURCES, CustomizerSupport.buildPath(path)); 563 tm.getTomcatPlatform().notifyLibrariesChanged(); 564 } 565 566 public List getJavadocs() { 567 String path = ip.getProperty(PROP_JAVADOCS); 568 if (path == null) { 569 ArrayList list = new ArrayList (); 570 try { 571 File jspApiDoc = new File (homeDir, "webapps/tomcat-docs/jspapi"); File servletApiDoc = new File (homeDir, "webapps/tomcat-docs/servletapi"); if (jspApiDoc.exists() && servletApiDoc.exists()) { 575 list.add(Utils.fileToUrl(jspApiDoc)); 576 list.add(Utils.fileToUrl(servletApiDoc)); 577 } else { 578 File j2eeDoc = InstalledFileLocator.getDefault().locate("docs/javaee5-doc-api.zip", null, false); if (j2eeDoc != null) { 580 list.add(Utils.fileToUrl(j2eeDoc)); 581 } 582 } 583 File docs = new File (homeDir, "docs/api"); if (docs.exists()) { 586 list.add(Utils.fileToUrl(docs)); 587 } 588 } catch (MalformedURLException e) { 589 ErrorManager.getDefault().notify(e); 590 } 591 return list; 592 } 593 return CustomizerSupport.tokenizePath(path); 594 } 595 596 public void setJavadocs(List path) { 597 ip.setProperty(PROP_JAVADOCS, CustomizerSupport.buildPath(path)); 598 tm.getTomcatPlatform().notifyLibrariesChanged(); 599 } 600 601 public void setOpenContextLogOnRun(boolean val) { 602 ip.setProperty(PROP_OPEN_LOG, Boolean.valueOf(val).toString()); 603 } 604 605 public boolean getOpenContextLogOnRun() { 606 Object val = ip.getProperty(PROP_OPEN_LOG); 607 if (val != null) { 608 return Boolean.valueOf(val.toString()).booleanValue(); 609 } 610 return DEF_VALUE_OPEN_LOG; 611 } 612 613 614 public void setTimestamp(long timestamp) { 615 ip.setProperty(PROP_TIMESTAMP, Long.toString(timestamp)); 616 } 617 618 619 public long getTimestamp() { 620 String val = ip.getProperty(PROP_TIMESTAMP); 621 if (val != null) { 622 try { 623 return Long.parseLong(val); 624 } catch (NumberFormatException nfe) { 625 TomcatManager.ERR.notify(ErrorManager.INFORMATIONAL, nfe); 626 } 627 } 628 return -1; 629 } 630 631 639 public File getServerXml() { 640 String confServerXml = "conf/server.xml"; File serverXml = null; 642 if (baseDir != null) { 643 serverXml = new File (baseDir, confServerXml); 644 } 645 if (serverXml == null || !serverXml.exists()) { 646 serverXml = new File (getCatalinaHome(), confServerXml); 647 } 648 return serverXml; 649 } 650 651 public String getHost () { 652 String val = ip.getProperty(PROP_HOST); 653 return val != null ? val : DEF_VALUE_HOST; 654 } 655 656 public int getRunningCheckTimeout() { 657 String val = ip.getProperty(PROP_RUNNING_CHECK_TIMEOUT); 658 if (val != null) { 659 try { 660 return Integer.parseInt(val); 661 } catch (NumberFormatException nfe) { 662 TomcatManager.ERR.notify(ErrorManager.INFORMATIONAL, nfe); 663 } 664 } 665 return DEF_VALUE_RUNNING_CHECK_TIMEOUT; 666 } 667 668 public String getDisplayName() { 669 String val = ip.getProperty(PROP_DISPLAY_NAME); 670 return val != null && val.length() > 0 ? val 671 : DEF_VALUE_DISPLAY_NAME; 672 } 673 674 676 private static List listUrls(final File folder, final String [] filter) { 677 File [] jars = folder.listFiles(new FilenameFilter () { 678 public boolean accept(File dir, String name) { 679 if (!name.endsWith(".jar") || !dir.equals(folder)) { 680 return false; 681 } 682 for (int i = 0; i < filter.length; i++) { 683 if (name.indexOf(filter[i]) != -1) { 684 return false; 685 } 686 } 687 return true; 688 } 689 }); 690 if (jars == null) { 691 return new ArrayList (); 692 } 693 List urls = new ArrayList (jars.length); 694 for (int i = 0; i < jars.length; i++) { 695 try { 696 urls.add(Utils.fileToUrl(jars[i])); 697 } catch (MalformedURLException e) { 698 ErrorManager.getDefault().notify(e); 699 } 700 } 701 return urls; 702 } 703 } 704 | Popular Tags |