1 8 9 package mx4j.tools.config; 10 11 import java.io.Reader ; 12 import java.util.ArrayList ; 13 import java.util.List ; 14 import javax.management.MBeanRegistration ; 15 import javax.management.MBeanServer ; 16 import javax.management.ObjectName ; 17 import javax.xml.parsers.DocumentBuilder ; 18 import javax.xml.parsers.DocumentBuilderFactory ; 19 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.NamedNodeMap ; 23 import org.w3c.dom.Node ; 24 import org.w3c.dom.NodeList ; 25 import org.xml.sax.InputSource ; 26 27 30 public class ConfigurationLoader implements ConfigurationLoaderMBean, MBeanRegistration 31 { 32 private MBeanServer server; 33 private ConfigurationBuilder builder; 34 private ConfigurationBuilder.Node root; 35 36 public ConfigurationLoader() 37 { 38 this(null, new DefaultConfigurationBuilder()); 39 } 40 41 public ConfigurationLoader(ConfigurationBuilder builder) 42 { 43 this(null, builder); 44 } 45 46 public ConfigurationLoader(MBeanServer server) 47 { 48 this(server, new DefaultConfigurationBuilder()); 49 } 50 51 public ConfigurationLoader(MBeanServer server, ConfigurationBuilder builder) 52 { 53 this.server = server; 54 if (builder == null) throw new IllegalArgumentException ("ConfigurationBuilder cannot be null"); 55 this.builder = builder; 56 } 57 58 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception 59 { 60 this.server = server; 61 return name; 62 } 63 64 public void postRegister(Boolean registered) 65 { 66 } 67 68 public void preDeregister() throws Exception 69 { 70 } 71 72 public void postDeregister() 73 { 74 } 75 76 public void startup(Reader source) throws ConfigurationException 77 { 78 if (server == null) throw new ConfigurationException("Cannot startup the configuration, MBeanServer is not specified"); 79 80 try 81 { 82 DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); 83 DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder(); 84 InputSource src = new InputSource (source); 85 Document document = documentBuilder.parse(src); 86 87 Element xmlRoot = document.getDocumentElement(); 88 root = builder.createConfigurationNode(xmlRoot); 89 parse(xmlRoot, root); 90 root.configure(server); 91 } 92 catch (ConfigurationException x) 93 { 94 throw x; 95 } 96 catch (Exception x) 97 { 98 throw new ConfigurationException(x); 99 } 100 } 101 102 public void shutdown() throws ConfigurationException 103 { 104 root.configure(null); 105 } 106 107 private void parse(Element xmlNode, ConfigurationBuilder.Node node) throws ConfigurationException 108 { 109 NamedNodeMap attributes = xmlNode.getAttributes(); 110 if (attributes != null && attributes.getLength() > 0) 111 { 112 node.setAttributes(attributes); 113 } 114 115 List elements = getChildrenElements(xmlNode); 116 if (elements != null) 117 { 118 for (int i = 0; i < elements.size(); ++i) 119 { 120 Element xmlChild = (Element )elements.get(i); 121 ConfigurationBuilder.Node child = builder.createConfigurationNode(xmlChild); 122 node.addChild(child); 123 parse(xmlChild, child); 124 } 125 } 126 127 String value = getNodeValue(xmlNode); 128 node.setText(value); 129 } 130 131 private List getChildrenElements(Element xmlNode) 132 { 133 NodeList xmlChildren = xmlNode.getChildNodes(); 134 if (xmlChildren == null) return null; 135 136 ArrayList children = new ArrayList (); 137 for (int i = 0; i < xmlChildren.getLength(); ++i) 138 { 139 Node xmlChild = xmlChildren.item(i); 140 if (xmlChild.getNodeType() == Node.ELEMENT_NODE) children.add(xmlChild); 141 } 142 return children; 143 } 144 145 private String getNodeValue(Element xmlNode) 146 { 147 NodeList xmlChildren = xmlNode.getChildNodes(); 148 if (xmlChildren == null) return null; 149 150 for (int i = 0; i < xmlChildren.getLength(); ++i) 151 { 152 Node xmlChild = xmlChildren.item(i); 153 if (xmlChild.getNodeType() == Node.TEXT_NODE) 154 { 155 return xmlChild.getNodeValue(); 156 } 157 } 158 return null; 159 } 160 } 161 | Popular Tags |