org.apache.oodt.commons.Configuration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oodt-commons Show documentation
Show all versions of oodt-commons Show documentation
Apache OODT Common Utilities Project
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.oodt.commons;
import org.apache.oodt.commons.util.DOMParser;
import org.apache.oodt.commons.util.XML;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.NoInitialContextException;
/** EDA Configuration.
*
* An object of this class represents the configuration information for the EDA software.
*
* @author Kelly
*/
public class Configuration {
public static final int INT = 7577;
public static final int INT1 = 6000000;
/** The singleton configuration. */
static Configuration configuration = null;
/** Name of property that specifies the direcotries that contains XML entities. */
public static final String ENTITY_DIRS_PROP = "entity.dirs";
/** Name of the default config file. */
public static final String DEFAULT_CONFIG_FILE = ".edarc.xml";
/** Alternate config file. */
public static final String ALT_CONFIG_FILE = ".oodtrc.xml";
/** Library-location config file. */
public static final File LIB_CONFIG_FILE = new File(System.getProperty("java.home", "/") + File.separator + "lib"
+ File.separator + "edarc.xml");
/** Non-JRE library location of config file. */
public static final File ALT_LIB_CONFIG_FILE = new File(System.getProperty("java.home", "/") + File.separator + ".."
+ File.separator + "lib" + File.separator + "edarc.xml");
/** Get the singleton configuration.
*
* This method returns the singleton configuration object, or creates it if it
* doesn't yet exist. To create it, it reads the configuration file specified by
* the system property org.apache.oodt.commons.Configuration.url
or the file in the
* user's home directory called .edarc.xml if the system property isn't
* specified. It parses the file and returns a Configuration
object
* initialized with the data specified therein.
*
* @throws IOException If reading the configuration file fails.
* @throws SAXException If parsing the configuration file fails.
* @throws MalformedURLException If the URL specification is invalid.
* @return An initialized configuration object.
*/
public static Configuration getConfiguration() throws IOException, SAXException {
// Got one? Use it.
if (configuration != null) {
return configuration;
}
URL url;
// First preference: URL via the org.apache.oodt.commons.Configuration.url prop.
String urlString = System.getProperty("org.apache.oodt.commons.Configuration.url");
if (urlString != null) {
url = new URL(urlString);
} else {
File file = null;
// Second preference: file via the org.apache.oodt.commons.Configuration.file prop.
String filename = System.getProperty("org.apache.oodt.commons.Configuration.file");
if (filename != null) {
file = new File(filename);
if (!file.exists()) {
throw new IOException("File " + file + " not found");
}
} else {
List candidates = new ArrayList();
// Third preference: ~/.edarc.xml
File homedir = new File(System.getProperty("user.home", "/"));
File homedirfile = new File(homedir, DEFAULT_CONFIG_FILE);
candidates.add(homedirfile);
// Fourth preference: ~/.oodtrc.xml
File alt = new File(homedir, ALT_CONFIG_FILE);
candidates.add(alt);
// Fifth and sixth preferences: $EDA_HOME/conf/edarc.xml and $EDA_HOME/etc/edarc.xml
String edaHome = System.getProperty("eda.home");
if (edaHome != null) {
File edaHomeDir = new File(edaHome);
candidates.add(new File(new File(edaHomeDir, "conf"), "edarc.xml"));
candidates.add(new File(new File(edaHomeDir, "etc"), "edarc.xml"));
}
// Seventh preference: JAVA_HOME/lib/edarc.xml
candidates.add(LIB_CONFIG_FILE);
// Final preference: JAVA_HOME/../lib/edarc.xml (to get out of JRE)
candidates.add(ALT_LIB_CONFIG_FILE);
// Now find one.
boolean found = false;
for (Object candidate : candidates) {
file = (File) candidate;
if (file.exists()) {
found = true;
break;
}
}
if (found && file == alt) {
System.err.println("WARNING: Using older config file " + alt + "; rename to "
+ homedirfile + " as soon as possible.");
}
if (!found) {
return getEmptyConfiguration();
}
}
url = file.toURL();
}
return getConfiguration(url);
}
/** Get the singleton configuration.
*
* This method returns the singleton configuration object from a
* specified file url. It parses the file and returns a Configuration
object
* initialized with the data specified therein. Added by Chris Mattmann 12/05/03.
*
* @throws IOException If an I/O error occurs.
* @return An initialized configuration object.
*/
public static Configuration getConfiguration(URL configFileUrl) throws SAXException, IOException {
synchronized (Configuration.class) {
if (configuration == null) {
configuration = new Configuration(configFileUrl);
}
}
return configuration;
}
private static Configuration getEmptyConfiguration() {
synchronized (Configuration.class) {
if (configuration == null) {
configuration = new Configuration();
}
}
return configuration;
}
/** Get the singleton configuration without exception.
*
* This method is identical to {@link #getConfiguration} but traps all checked
* exceptions. If the configuration can't be read, it returns null.
*
* @return An initialized configuration object, or null if an error occurred.
*/
public static Configuration getConfigurationWithoutException() {
// Got one? Use it. Do this out of a try block for performance.
if (configuration != null) {
return configuration;
}
// Try to get it.
try {
return getConfiguration();
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
System.err.println("Exception " + ex.getClass().getName() + " while getting configuration: "
+ ex.getMessage());
ex.printStackTrace();
return null;
}
}
Configuration() {
serverMgrPort = INT;
nameServerStateFrequency = INT1;
nameServerObjectKey = "StandardNS%20POA";
nameServerPort = "10000";
nameServerHost = "localhost";
nameServerVersion = "1.0";
nameServerUsingRIRProtocol = false;
webServerDocumentDirectory = new File(System.getProperty("user.home", "/") + "tomcat/webapps/ROOT");
webPort = "8080";
webHost = "localhost";
System.setProperty(WEB_PROTOCOL_PROPERTY, "http");
initializeContext();
}
/** Construct a configuration.
*
* @param url The location of the configuration.
* @throws IOException If reading the configuration file fails.
* @throws SAXParseException If parsing the configuration file fails.
*/
Configuration(URL url) throws IOException, SAXException {
this(new InputSource(url.toString()));
}
Configuration(InputSource inputSource) throws IOException, SAXException {
String systemID = inputSource.getSystemId();
if (systemID == null) {
inputSource.setSystemId("file:/unknown");
}
// Get the document
DOMParser parser = XML.createDOMParser();
parser.setEntityResolver(new ConfigurationEntityResolver());
parser.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException ex) throws SAXException {
throw ex;
}
public void warning(SAXParseException ex) {
System.err.println("Warning: " + ex.getMessage());
}
public void fatalError(SAXParseException ex) throws SAXException {
System.err.println("Fatal parse error: " + ex.getMessage());
throw ex;
}
});
parser.parse(inputSource);
Document document = parser.getDocument();
XML.removeComments(document);
document.normalize();
// See if this really is a document.
if (!document.getDocumentElement().getNodeName().equals("configuration")) {
throw new SAXException("Configuration " + inputSource.getSystemId() + " is not a document");
}
NodeList list = document.getDocumentElement().getChildNodes();
for (int eachChild = 0; eachChild < list.getLength(); ++eachChild) {
Node childNode = list.item(eachChild);
if (childNode.getNodeName().equals("webServer")) {
NodeList children = childNode.getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
Node node = children.item(i);
if ("host".equals(node.getNodeName())) {
webHost = XML.unwrappedText(node);
} else if ("port".equals(node.getNodeName())) {
webPort = XML.unwrappedText(node);
} else if ("dir".equals(node.getNodeName())) {
webServerDocumentDirectory = new File(XML.unwrappedText(node));
}
}
properties.setProperty("org.apache.oodt.commons.Configuration.webServer.baseURL", getWebServerBaseURL());
if (webServerDocumentDirectory == null) {
webServerDocumentDirectory = new File(System.getProperty("user.home", "/")
+ "/dev/htdocs");
}
} else if (childNode.getNodeName().equals("nameServer")) {
Element nameServerNode = (Element) childNode;
String nameServerStateFrequencyString = nameServerNode.getAttribute("stateFrequency");
if (nameServerStateFrequencyString == null || nameServerStateFrequencyString.length() == 0) {
nameServerStateFrequency = 0;
} else {
try {
nameServerStateFrequency = Integer.parseInt(nameServerStateFrequencyString);
} catch (NumberFormatException ex) {
throw new SAXException("Illegal nun-numeric value \"" + nameServerStateFrequencyString
+ "\" for stateFrequency attribute");
}
}
if (childNode.getFirstChild().getNodeName().equals("rir")) {
nameServerUsingRIRProtocol = true;
NodeList children = childNode.getFirstChild().getChildNodes();
nameServerObjectKey = children.getLength() == 1? XML.unwrappedText(children.item(0)):null;
} else {
nameServerUsingRIRProtocol = false;
nameServerVersion = null;
nameServerPort = null;
// Must be same as CORBAMgr.NS_OBJECT_KEY:
nameServerObjectKey = "StandardNS/NameServer%2DPOA/_root";
NodeList children = childNode.getFirstChild().getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
Node node = children.item(i);
if (node.getNodeName().equals("version")) {
nameServerVersion = XML.unwrappedText(node);
} else if (node.getNodeName().equals("host")) {
nameServerHost = XML.unwrappedText(node);
} else if (node.getNodeName().equals("port")) {
nameServerPort = XML.unwrappedText(node);
} else if (node.getNodeName().equals("objectKey")) {
nameServerObjectKey = XML.unwrappedText(node);
}
}
}
} else if (childNode.getNodeName().equals("xml")) {
NodeList children = childNode.getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
Node xmlNode = children.item(i);
if ("entityRef".equals(xmlNode.getNodeName())) {
NodeList dirNodes = xmlNode.getChildNodes();
StringBuilder refDirs = new StringBuilder(System.getProperty(ENTITY_DIRS_PROP, ""));
for (int j = 0; j < dirNodes.getLength(); ++j) {
refDirs.append(',').append(XML.unwrappedText(dirNodes.item(j)));
}
if (refDirs.length() > 0) {
System.setProperty(ENTITY_DIRS_PROP, refDirs.charAt(0) == ',' ?
refDirs.substring(1) : refDirs.toString());
}
}
}
} else if ("serverMgr".equals(childNode.getNodeName())) {
serverMgrPort = Integer.parseInt(XML.unwrappedText(childNode.getFirstChild()));
} else if (childNode.getNodeName().equals("properties")) {
loadProperties(childNode, properties);
} else if (childNode.getNodeName().equals("programs")) {
NodeList children = childNode.getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
// They're all of type execServer---for now.
ExecServerConfig esc = new ExecServerConfig(children.item(i));
esc.getProperties().setProperty("org.apache.oodt.commons.Configuration.url", inputSource.getSystemId());
execServers.add(esc);
}
}
}
initializeContext();
}
private void initializeContext() {
contextEnvironment.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.oodt.commons.object.jndi.ObjectCtxFactory");
String registryList = System.getProperty("org.apache.oodt.commons.rmiregistries", System.getProperty("rmiregistries"));
if (registryList == null) {
String host = System.getProperty("rmiregistry.host", "localhost");
int port = Integer.getInteger("rmiregistry.port", Registry.REGISTRY_PORT);
registryList = "rmi://" + host + ":" + port;
}
contextEnvironment.put("rmiregistries", registryList);
}
/** Serialize this configuration into a serialized XML document.
*
* @return Serialized XML version of this configuration.
* @throws DOMException If an error occurs constructing the XML structure.
*/
public String toXML() throws DOMException {
Document doc = createDocument("configuration");
doc.replaceChild(toXML(doc), doc.getDocumentElement());
return XML.serialize(doc);
}
/**
*
* @param document The document to which the XML structure will belong.
* @return The root node representing this configuration.
* @throws DOMException If an error occurs constructing the XML structure.
*/
public Node toXML(Document document) throws DOMException {
//
Element configurationNode = document.createElement("configuration");
//
Element webServerNode = document.createElement("webServer");
configurationNode.appendChild(webServerNode);
//
// ... ... ...
XML.add(webServerNode, "host", webHost);
XML.add(webServerNode, "port", webPort);
XML.add(webServerNode, "dir", webServerDocumentDirectory.toString());
//
Element nameServerNode = document.createElement("nameServer");
nameServerNode.setAttribute("stateFrequency", String.valueOf(nameServerStateFrequency));
configurationNode.appendChild(nameServerNode);
//
// or
if (nameServerUsingRIRProtocol) {
Element rirNode = document.createElement("rir");
nameServerNode.appendChild(rirNode);
if (nameServerObjectKey != null) {
XML.add(rirNode, "objectKey", nameServerObjectKey);
}
} else {
Element iiopNode = document.createElement("iiop");
nameServerNode.appendChild(iiopNode);
if (nameServerVersion != null) {
XML.add(iiopNode, "version", nameServerVersion);
}
XML.add(iiopNode, "host", nameServerHost);
if (nameServerPort != null) {
XML.add(iiopNode, "port", nameServerPort);
}
if (nameServerObjectKey != null) {
XML.add(iiopNode, "objectKey", nameServerObjectKey);
}
}
// ...
if (!getEntityRefDirs().isEmpty()) {
Element xmlNode = document.createElement("xml");
configurationNode.appendChild(xmlNode);
Element entityRefNode = document.createElement("entityRef");
xmlNode.appendChild(entityRefNode);
XML.add(entityRefNode, "dir", getEntityRefDirs());
}
// ...
if (getServerMgrPort() != 0) {
Element serverMgrNode = document.createElement("serverMgr");
configurationNode.appendChild(serverMgrNode);
XML.add(serverMgrNode, "port", String.valueOf(getServerMgrPort()));
}
// Global ...
if (properties.size() > 0) {
dumpProperties(properties, configurationNode);
}
// ...
if (execServers.size() > 0) {
Element programsNode = document.createElement("programs");
configurationNode.appendChild(programsNode);
for (Object execServer : execServers) {
ExecServerConfig esc = (ExecServerConfig) execServer;
Element execServerNode = document.createElement("execServer");
programsNode.appendChild(execServerNode);
XML.add(execServerNode, "class", esc.getClassName());
XML.add(execServerNode, "objectKey", esc.getObjectKey());
XML.add(execServerNode, "host", esc.getPreferredHost().toString());
if (esc.getProperties().size() > 0) {
dumpProperties(esc.getProperties(), execServerNode);
}
}
}
return configurationNode;
}
/** Merge the properties in the configuration into the given properties.
*
* Properties that already exist in the targetProps won't be
* overwritten.
*
* @param targetProps The target properties.
*/
public void mergeProperties(Properties targetProps) {
for (Map.Entry