📄 rtconfig.java
字号:
return _foundConfigFile; } public static File getLoadedConfigDir() { File cfgFile = RTConfig.getLoadedConfigFile(); if (cfgFile != null) { return cfgFile.getParentFile(); } else { return null; } } protected static synchronized void _startupInit(boolean allowChangeSystemProperties) { // Note: this method is synchronized /* check init */ if (_didStartupInit == 2) { // already initialized return; } else if (_didStartupInit == 1) { Print.logError("_startupInit' is already initializing!"); return; } _didStartupInit = 1; /* System properties */ Properties propMap = null; if (allowChangeSystemProperties) { try { propMap = System.getProperties(); } catch (SecurityException se) { // SecurityException, AccessControlException Print.sysPrintln("ERROR: Attempting to call 'System.getProperties()': " + se); } } else { propMap = new Properties(); for (Iterator i = RTKey.getRuntimeKeyIterator(); i.hasNext();) { String key = (String)i.next(); try { String val = System.getProperty(key, null); if (val != null) { propMap.setProperty(key, val); } } catch (SecurityException se) { // SecurityException, AccessControlException Print.sysPrintln("Attempting to get System property '" + key + "': " + se); } } } CFG_PROPERTIES[SYSTEM] = new RTProperties(propMap); /* verbose? */ if (hasProperty(RTKey.RT_VERBOSE, false)) { RTConfig.verbose = RTConfig.getBoolean(RTKey.RT_VERBOSE, false); } else if (hasProperty(RTKey.RT_QUIET, false)) { RTConfig.verbose = RTConfig.getBoolean(RTKey.RT_QUIET, false); } /* config file */ _foundConfigFile = null; _foundConfigURL = RTConfig.getConfigURL(); if (_foundConfigURL != null) { CFG_PROPERTIES[CONFIG_FILE] = new RTProperties(_foundConfigURL); if (RTConfig.verbose) { Print.logInfo("Found config file at " + _foundConfigURL); } } else { //String cfgDir = RTConfig.getFile(RTKey.CONFIG_FILE_DIR); //String cfgFile = RTConfig.getFile(RTKey.CONFIG_FILE); CFG_PROPERTIES[CONFIG_FILE] = new RTProperties(); // must be non-null if (RTConfig.verbose) { Print.logWarn("No config file was found"); } } CFG_PROPERTIES[CONFIG_FILE].setKeyReplacementMode(RTProperties.KEY_REPLACEMENT_GLOBAL); /* initialize http proxy */ // http.proxyHost // http.proxyPort // http.nonProxyHosts String proxyHost = RTConfig.getString(RTKey.HTTP_PROXY_HOST); int proxyPort = RTConfig.getInt (RTKey.HTTP_PROXY_PORT); if ((proxyHost != null) && (proxyPort > 1024)) { String port = String.valueOf(proxyPort); //Properties sysProp = System.getProperties(); //sysProp.put("proxySet" , "true"); // < jdk 1.3 //sysProp.put("proxyHost", proxyHost); // < jdk 1.3 //sysProp.put("proxyPort", port); // < jdk 1.3 //sysProp.put("http.proxyHost", proxyHost); // >= jdk 1.3 //sysProp.put("http.proxyPort", port); // >= jdk 1.3 //sysProp.put("firewallSet", "true"); // MS JVM //sysProp.put("firewallHost", proxyHost); // MS JVM //sysProp.put("firewallPort", port); // MS JVM System.setProperty("proxySet" , "true"); // < jdk 1.3 System.setProperty("proxyHost", proxyHost); // < jdk 1.3 System.setProperty("proxyPort", port); // < jdk 1.3 System.setProperty("http.proxyHost", proxyHost); // >= jdk 1.3 System.setProperty("http.proxyPort", port); // >= jdk 1.3 System.setProperty("firewallSet", "true"); // MS JVM System.setProperty("firewallHost", proxyHost); // MS JVM System.setProperty("firewallPort", port); // MS JVM } /* URLConnection timeouts */ // sun.net.client.defaultConnectTimeout // sun.net.client.defaultReadTimeout long urlConnectTimeout = RTConfig.getLong(RTKey.URL_CONNECT_TIMEOUT); if (urlConnectTimeout > 0) { String timeout = String.valueOf(urlConnectTimeout); //System.getProperties().put("sun.net.client.defaultConnectTimeout", timeout); System.setProperty("sun.net.client.defaultConnectTimeout", timeout); } long urlReadTimeout = RTConfig.getLong(RTKey.URL_READ_TIMEOUT); if (urlReadTimeout > 0) { String timeout = String.valueOf(urlReadTimeout); //System.getProperties().put("sun.net.client.defaultReadTimeout", timeout); System.setProperty("sun.net.client.defaultReadTimeout", timeout); } /* now initialized */ _didStartupInit = 2; /* set all of the Print configuration */ Print.initRTConfig(); } protected static URL getConfigURL() { try{ URL cfgURL = RTConfig._getConfigURL(); if (cfgURL != null) { Print.logInfo("Config URL found at " + cfgURL); return cfgURL; } else { Print.logWarn("No valid config URL was found"); } } catch (MalformedURLException mue) { Print.logError("Invalid URL: " + mue); } catch (Throwable t) { Print.logException("Invalid URL", t); } return null; } protected static URL _getConfigURL() throws MalformedURLException { // This module checks for the runtime config file in the following order: // - Explicit "-configFile=<file>" specification // - Explicit "-conf=<file>" specification (if command-line) // - Resource "[default|webapp]_<host>.conf" // - Resource "[default|webapp].conf" // - Servlet "<Context>/WEB-INF/webapp_<host>.conf" (if servlet) // - Servlet "<Context>/WEB-INF/webapp.conf" (if servlet) // - FileSystem "<CONFIG_FILE_DIR>/[default|webapp]_<host>.conf" // - FileSystem "<CONFIG_FILE_DIR>/[default|webapp].conf" /* init */ String hostName = RTConfig.getHostName(); // should not be null Class servletClass = null; RTProperties cmdLineProps = getCommandLineProperties(); boolean isCommandLine = (cmdLineProps != null); RTProperties servletProps = getServletContextProperties(); boolean isServlet = !isCommandLine && ((servletProps != null) || ((servletClass = getServletClass()) != null)); /* explicitly defined (command-line, etc) "-configFile=<file>" */ // Check for "-configFile=<file>" if (RTConfig.hasProperty(RTKey.CONFIG_FILE,false)) { File cfgFile = RTConfig.getFile(RTKey.CONFIG_FILE); if (RTConfig.hasProperty(RTKey.CONFIG_FILE_DIR,false)) { File cfgDir = RTConfig.getFile(RTKey.CONFIG_FILE_DIR); cfgFile = new File(cfgDir, cfgFile.toString()); } if (cfgFile.isFile()) { return cfgFile.toURL(); } else { Print.logError("Specified config file does not exist: " + cfgFile); return null; } } else // Check for "-conf=<file>" if (isCommandLine) { /* check for alternate command line override '-conf=<file>' */ File cfgFile = cmdLineProps.getFile(RTKey.COMMAND_LINE_CONF, null); if (cfgFile != null) { if (RTConfig.hasProperty(RTKey.CONFIG_FILE_DIR,false)) { File cfgDir = RTConfig.getFile(RTKey.CONFIG_FILE_DIR); cfgFile = new File(cfgDir, cfgFile.toString()); } if (cfgFile.isFile()) { return cfgFile.toURL(); } else { Print.logError("Specified config file does not exist: " + cfgFile); return null; } } } /* separate directory from path */ File rtCfgDir; File rtCfgFile; if (isServlet) { rtCfgDir = RTConfig.getFile(RTKey.CONFIG_FILE_DIR); rtCfgFile = RTConfig.getFile(RTKey.WEBAPP_FILE); // should be simply "webapp.conf" } else { rtCfgFile = RTConfig.getFile(RTKey.CONFIG_FILE); if (rtCfgFile.isAbsolute()) { rtCfgDir = rtCfgFile.getParentFile(); rtCfgFile = new File(rtCfgFile.getName()); } else { File cf = new File(RTConfig.getFile(RTKey.CONFIG_FILE_DIR), rtCfgFile.toString()); rtCfgDir = cf.getParentFile(); rtCfgFile = new File(cf.getName()); } } /* separate file name from extension */ String cfgFileName = rtCfgFile.toString(); int cfgExtPos = cfgFileName.lastIndexOf("."); String cfgName = (cfgExtPos >= 0)? cfgFileName.substring(0,cfgExtPos) : cfgFileName; String cfgExtn = (cfgExtPos >= 0)? cfgFileName.substring(cfgExtPos ) : ""; /* check for config file in resources */ Class mainClass = RTConfig.getMainClass(); if (mainClass != null) { try { ClassLoader mainClassLoader = mainClass.getClassLoader(); if (mainClassLoader == null) { // bootstrap classloader mainClassLoader = ClassLoader.getSystemClassLoader(); // may still be null } if (mainClassLoader != null) { // Check for resource "default_<host>.conf" URL cfgHostRes = mainClassLoader.getResource(cfgName + "_" + hostName + cfgExtn); if (cfgHostRes != null) { return cfgHostRes; } // Check for resource "default.conf" URL cfgRes = mainClassLoader.getResource(cfgFileName); if (cfgRes != null) { return cfgRes; } } else { Print.logWarn("System class loader is null"); } } catch (Throwable t) { Print.logException("Error retrieving class loader", t); } } /* special context (command-line/servlet) config files */ if (isCommandLine) { // continue below } else if (servletProps != null) { /* check "<ContextPath>/[WEB-INF/]webapp[_<host>].conf" */ String ctxPath = servletProps.getString(RTKey.WEBAPP_CONTEXT_PATH, null); if (ctxPath != null) { File web_inf = new File(ctxPath, "WEB-INF"); // Check for "<Context>/WEB-INF/webapp_<host>.conf" File webInfHostFile = new File(web_inf, cfgName + "_" + hostName + cfgExtn); if (webInfHostFile.isFile()) { return webInfHostFile.toURL(); } //Print.logInfo("Config not found: " + webInfHostFile); // Check for "<Context>/WEB-INF/webapp.conf" File webInfFile = new File(web_inf, cfgFileName); if (webInfFile.isFile()) { return webInfFile.toURL(); } //Print.logInfo("Config not found: " + webInfFile); } } else if (servletClass != null) { // If we are here, then this means that 'ContextListener' was not specified for this // context and we are initializing late (possible too late). if (RTConfig.verbose) { Print.logInfo("--------------------------------------------------------"); Print.logInfo("******* WebApp: " + StringTools.className(servletClass)); Print.logInfo("--------------------------------------------------------"); } } else { // this only occurs if we're attempting to acces RTConfig properties and we // haven't previously been properly initialized Print.logStackTrace("CommandLine/ServletContext properties not specified"); } // Check for "<CONFIG_FILE_DIR>/[default|webapp]_<host>.conf" File cfgDirHostFile = new File(rtCfgDir, cfgName + "_" + hostName + cfgExtn); if (cfgDirHostFile.isFile()) { return cfgDirHostFile.toURL(); } //Print.logInfo("Config not found: " + cfgDirHostFile); //Check for "<CONFIG_FILE_DIR>/[default|webapp].conf" File cfgDirFile = new File(rtCfgDir, rtCfgFile.toString()); if (cfgDirFile.isFile()) { return cfgDirFile.toURL(); } //Print.logInfo("Config not found: " + cfgDirFile); /* no config file */ return null; } protected static boolean _resourceExists(File file) { if (file == null) { return false; } Class mainClass = RTConfig.getMainClass(); Print.logInfo("MainClass: " + mainClass); URL cfgRes = (mainClass != null)? mainClass.getClassLoader().getResource(file.toString()) : null; if (cfgRes != null) { Print.logInfo("ConfigFile found as resource: " + file); return true; } else { Print.logInfo("ConfigFile NOT found as resource: " + file); return false; } } // ------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -