📄 configurationmanager.java
字号:
return; } String configProperty = System.getProperty("dspace.configuration"); try { if (configFile != null) { is = new FileInputStream(configFile); loadedFile = new File(configFile); } // Has the default configuration location been overridden? else if (configProperty != null) { // Load the overriding configuration is = new FileInputStream(configProperty); loadedFile = new File(configProperty); } else { // Load configuration from default location is = ConfigurationManager.class .getResourceAsStream("/dspace.cfg"); loadedFile = new File(ConfigurationManager.class.getResource( "/dspace.cfg").getPath()); } if (is == null) { fatal("Cannot find dspace.cfg"); System.exit(1); } else { properties = new Properties(); properties.load(is); // walk values, interpolating any embedded references. for (Enumeration pe = properties.propertyNames(); pe.hasMoreElements(); ) { String key = (String)pe.nextElement(); String value = interpolate(key, 1); if (value != null) properties.setProperty(key, value); } } // Load in default license String licenseFile = getProperty("dspace.dir") + File.separator + "config" + File.separator + "default.license"; BufferedReader br = new BufferedReader(new FileReader(licenseFile)); String lineIn; license = ""; while ((lineIn = br.readLine()) != null) { license = license + lineIn + '\n'; } is.close(); // Load in log4j config // Load the log4j config, if a log4j.xml version exists use that // configuration format over the log4j.properties version String log4jConfProp = ConfigurationManager .getProperty("dspace.dir") + File.separator + "config" + File.separator + "log4j.properties"; String log4jConfXml = ConfigurationManager .getProperty("dspace.dir") + File.separator + "config" + File.separator + "log4j.xml"; File xmlFile = new File(log4jConfXml); if (xmlFile.exists()) { try { DOMConfigurator.configure(xmlFile.toURL()); initLog(); info("DSpace logging installed using log4j.xml"); } catch (MalformedURLException e) { PropertyConfigurator.configure(log4jConfProp); initLog(); error("Logger failed to load log4j.xml, defaulted to " + "log4j.properties: " + e); } } else { PropertyConfigurator.configure(log4jConfProp); initLog(); info("DSpace logging installed using log4j.properties"); } } catch (IOException e) { fatal("Can't load configuration", e); // FIXME: Maybe something more graceful here, but with the // configuration we can't do anything System.exit(1); } } /** * Recursively interpolate variable references in value of * property named "key". * @return new value if it contains interpolations, or null * if it had no variable references. */ private static String interpolate(String key, int level) { if (level > RECURSION_LIMIT) throw new IllegalArgumentException("ConfigurationManager: Too many levels of recursion in configuration property variable interpolation, property="+key); String value = (String)properties.get(key); int from = 0; StringBuffer result = null; while (from < value.length()) { int start = value.indexOf("${", from); if (start >= 0) { int end = value.indexOf("}", start); if (end < 0) break; String var = value.substring(start+2, end); if (result == null) result = new StringBuffer(value.substring(from, start)); else result.append(value.substring(from, start)); if (properties.containsKey(var)) { String ivalue = interpolate(var, level+1); if (ivalue != null) { result.append(ivalue); properties.setProperty(var, ivalue); } else result.append((String)properties.getProperty(var)); } else { log.warn("Interpolation failed in value of property \""+key+ "\", there is no property named \""+var+"\""); } from = end+1; } else break; } if (result != null && from < value.length()) result.append(value.substring(from)); return (result == null) ? null : result.toString(); } /** * Fill out of the configuration file templates in * <code>dspace.dir/config/templates</code> with appropriate values from * <code>dspace.cfg</code>, and copy to their appropriate destination. * The destinations are defined as properties in <code>dspace.cfg</code> * called <code>config.template.XXX</code> where <code>XXX</code> is the * filename of the template. If this property doesn't exist, the * configuration file template is skipped. * * @throws IOException * if there was some problem reading the templates or writing * the filled-out configuration files */ public static void installConfigurations() throws IOException { // Get the templates File templateDir = new File(getProperty("dspace.dir") + File.separator + "config" + File.separator + "templates"); File[] templateFiles = templateDir.listFiles(); for (int i = 0; i < templateFiles.length; i++) { installConfigurationFile(templateFiles[i].getName()); } } /** * Install the given configuration file template in its required location. * Configuration values in the template, specified as * <code>@@property.name@@</code> are filled out with appropriate properties from * the configuration. The filled-out configuration file is * written to the file named by the property * <code>config.template.XXX</code> where * <code>XXX</code> is the name of the template as * passed in to this method. * * @param template * the name of the configuration template. This must correspond * to the filename in <code>dspace.dir/config/templates</code> * and the property starting with <code>config.template.</code>. * * @throws IOException * if there was some problem reading the template or writing the * filled-out configuration file */ private static void installConfigurationFile(String template) throws IOException { // Get the destination: specified in property config.template.XXX String destination = getProperty("config.template." + template); if (destination == null) { // If no destination is specified info("Not processing config file template " + template + " because no destination specified (no property " + "config.template." + template + ")"); return; } info("Installing configuration file template " + template + " to " + destination); // Open the template BufferedReader in = new BufferedReader(new FileReader( getProperty("dspace.dir") + File.separator + "config" + File.separator + "templates" + File.separator + template)); // Open the destination for writing PrintWriter out = new PrintWriter(new FileWriter(destination)); // We'll keep track of line numbers for error messages etc. int lineNumber = 0; String line; // Copy the template, filling out config values while ((line = in.readLine()) != null) { lineNumber++; // Find configuration values boolean moreValues = true; while (moreValues) { // Look for "@@" int first = line.indexOf("@@"); if (first > -1) { // Look for the "@@" on the other side int second = line.indexOf("@@", first + 2); if (second > -1) { // We have a property String propName = line.substring(first + 2, second); String propValue = getProperty(propName); if (propValue == null) { warn(template + " line " + lineNumber + ": Property " + propName + " not defined in DSpace configuration - " + "using empty string"); propValue = ""; } // Fill in the value line = line.substring(0, first) + propValue + line.substring(second + 2); } else { // There's a "@@" with no second one... just leave as-is warn(template + " line " + lineNumber + ": Single @@ - leaving as-is"); moreValues = false; } } else { // No more @@'s moreValues = false; } } // Write the line out.println(line); } in.close(); out.close(); } /** * Command-line interface for running configuration tasks. Possible * arguments: * <ul> * <li><code>-installTemplates</code> processes and installs the * configuration file templates for other tools</li> * <li><code>-property name</code> prints the value of the property * <code>name</code> from <code>dspace.cfg</code> to the standard * output. If the property does not exist, nothing is written.</li> * </ul> * * @param argv * command-line arguments */ public static void main(String[] argv) { if ((argv.length == 1) && argv[0].equals("-installTemplates")) { try { info("Installing configuration files for other tools"); installConfigurations(); System.exit(0); } catch (IOException ie) { warn("Error installing configuration files", ie); } } else if ((argv.length == 2) && argv[0].equals("-property")) { String val = getProperty(argv[1]); if (val != null) { System.out.println(val); } else { System.out.println(""); } System.exit(0); } else { System.err .println("Usage: ConfigurationManager OPTION\n -installTemplates install config files for external tools\n -property prop.name get value of prop.name from dspace.cfg"); } System.exit(1); } private static void info(String string) { if (log == null) { System.out.println("INFO: " + string); } else { log.info(string); } } private static void warn(String string, Exception e) { if (log == null) { System.out.println("WARN: " + string); e.printStackTrace(); } else { log.warn(string, e); } } private static void warn(String string) { if (log == null) { System.out.println("WARN: " + string); } else { log.warn(string); } } private static void error(String string) { if (log == null) { System.err.println("ERROR: " + string); } else { log.error(string); } } private static void fatal(String string, Exception e) { if (log == null) { System.out.println("FATAL: " + string); e.printStackTrace(); } else { log.fatal(string, e); } } private static void fatal(String string) { if (log == null) { System.out.println("FATAL: " + string); } else { log.fatal(string); } } private static void initLog() { log = Logger.getLogger(ConfigurationManager.class); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -