001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package mx4j.tools.config;
010:
011: import java.io.Reader;
012: import java.util.ArrayList;
013: import java.util.List;
014: import javax.management.MBeanRegistration;
015: import javax.management.MBeanServer;
016: import javax.management.ObjectName;
017: import javax.xml.parsers.DocumentBuilder;
018: import javax.xml.parsers.DocumentBuilderFactory;
019:
020: import org.w3c.dom.Document;
021: import org.w3c.dom.Element;
022: import org.w3c.dom.NamedNodeMap;
023: import org.w3c.dom.Node;
024: import org.w3c.dom.NodeList;
025: import org.xml.sax.InputSource;
026:
027: /**
028: * @version $Revision: 1.3 $
029: */
030: public class ConfigurationLoader implements ConfigurationLoaderMBean,
031: MBeanRegistration {
032: private MBeanServer server;
033: private ConfigurationBuilder builder;
034: private ConfigurationBuilder.Node root;
035:
036: public ConfigurationLoader() {
037: this (null, new DefaultConfigurationBuilder());
038: }
039:
040: public ConfigurationLoader(ConfigurationBuilder builder) {
041: this (null, builder);
042: }
043:
044: public ConfigurationLoader(MBeanServer server) {
045: this (server, new DefaultConfigurationBuilder());
046: }
047:
048: public ConfigurationLoader(MBeanServer server,
049: ConfigurationBuilder builder) {
050: this .server = server;
051: if (builder == null)
052: throw new IllegalArgumentException(
053: "ConfigurationBuilder cannot be null");
054: this .builder = builder;
055: }
056:
057: public ObjectName preRegister(MBeanServer server, ObjectName name)
058: throws Exception {
059: this .server = server;
060: return name;
061: }
062:
063: public void postRegister(Boolean registered) {
064: }
065:
066: public void preDeregister() throws Exception {
067: }
068:
069: public void postDeregister() {
070: }
071:
072: public void startup(Reader source) throws ConfigurationException {
073: if (server == null)
074: throw new ConfigurationException(
075: "Cannot startup the configuration, MBeanServer is not specified");
076:
077: try {
078: DocumentBuilderFactory documentFactory = DocumentBuilderFactory
079: .newInstance();
080: DocumentBuilder documentBuilder = documentFactory
081: .newDocumentBuilder();
082: InputSource src = new InputSource(source);
083: Document document = documentBuilder.parse(src);
084:
085: Element xmlRoot = document.getDocumentElement();
086: root = builder.createConfigurationNode(xmlRoot);
087: parse(xmlRoot, root);
088: root.configure(server);
089: } catch (ConfigurationException x) {
090: throw x;
091: } catch (Exception x) {
092: throw new ConfigurationException(x);
093: }
094: }
095:
096: public void shutdown() throws ConfigurationException {
097: root.configure(null);
098: }
099:
100: private void parse(Element xmlNode, ConfigurationBuilder.Node node)
101: throws ConfigurationException {
102: NamedNodeMap attributes = xmlNode.getAttributes();
103: if (attributes != null && attributes.getLength() > 0) {
104: node.setAttributes(attributes);
105: }
106:
107: List elements = getChildrenElements(xmlNode);
108: if (elements != null) {
109: for (int i = 0; i < elements.size(); ++i) {
110: Element xmlChild = (Element) elements.get(i);
111: ConfigurationBuilder.Node child = builder
112: .createConfigurationNode(xmlChild);
113: node.addChild(child);
114: parse(xmlChild, child);
115: }
116: }
117:
118: String value = getNodeValue(xmlNode);
119: node.setText(value);
120: }
121:
122: private List getChildrenElements(Element xmlNode) {
123: NodeList xmlChildren = xmlNode.getChildNodes();
124: if (xmlChildren == null)
125: return null;
126:
127: ArrayList children = new ArrayList();
128: for (int i = 0; i < xmlChildren.getLength(); ++i) {
129: Node xmlChild = xmlChildren.item(i);
130: if (xmlChild.getNodeType() == Node.ELEMENT_NODE)
131: children.add(xmlChild);
132: }
133: return children;
134: }
135:
136: private String getNodeValue(Element xmlNode) {
137: NodeList xmlChildren = xmlNode.getChildNodes();
138: if (xmlChildren == null)
139: return null;
140:
141: for (int i = 0; i < xmlChildren.getLength(); ++i) {
142: Node xmlChild = xmlChildren.item(i);
143: if (xmlChild.getNodeType() == Node.TEXT_NODE) {
144: return xmlChild.getNodeValue();
145: }
146: }
147: return null;
148: }
149: }
|