KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > config > ConfigurationLoader


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.config;
10
11 import java.io.Reader JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14 import javax.management.MBeanRegistration JavaDoc;
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17 import javax.xml.parsers.DocumentBuilder JavaDoc;
18 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
19
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.NamedNodeMap JavaDoc;
23 import org.w3c.dom.Node JavaDoc;
24 import org.w3c.dom.NodeList JavaDoc;
25 import org.xml.sax.InputSource JavaDoc;
26
27 /**
28  * @version $Revision: 1.3 $
29  */

30 public class ConfigurationLoader implements ConfigurationLoaderMBean, MBeanRegistration JavaDoc
31 {
32    private MBeanServer JavaDoc 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 JavaDoc server)
47    {
48       this(server, new DefaultConfigurationBuilder());
49    }
50
51    public ConfigurationLoader(MBeanServer JavaDoc server, ConfigurationBuilder builder)
52    {
53       this.server = server;
54       if (builder == null) throw new IllegalArgumentException JavaDoc("ConfigurationBuilder cannot be null");
55       this.builder = builder;
56    }
57
58    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
59    {
60       this.server = server;
61       return name;
62    }
63
64    public void postRegister(Boolean JavaDoc registered)
65    {
66    }
67
68    public void preDeregister() throws Exception JavaDoc
69    {
70    }
71
72    public void postDeregister()
73    {
74    }
75
76    public void startup(Reader JavaDoc 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 JavaDoc documentFactory = DocumentBuilderFactory.newInstance();
83          DocumentBuilder JavaDoc documentBuilder = documentFactory.newDocumentBuilder();
84          InputSource JavaDoc src = new InputSource JavaDoc(source);
85          Document JavaDoc document = documentBuilder.parse(src);
86
87          Element JavaDoc 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 JavaDoc 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 JavaDoc xmlNode, ConfigurationBuilder.Node node) throws ConfigurationException
108    {
109       NamedNodeMap JavaDoc attributes = xmlNode.getAttributes();
110       if (attributes != null && attributes.getLength() > 0)
111       {
112          node.setAttributes(attributes);
113       }
114
115       List JavaDoc elements = getChildrenElements(xmlNode);
116       if (elements != null)
117       {
118          for (int i = 0; i < elements.size(); ++i)
119          {
120             Element JavaDoc xmlChild = (Element JavaDoc)elements.get(i);
121             ConfigurationBuilder.Node child = builder.createConfigurationNode(xmlChild);
122             node.addChild(child);
123             parse(xmlChild, child);
124          }
125       }
126
127       String JavaDoc value = getNodeValue(xmlNode);
128       node.setText(value);
129    }
130
131    private List JavaDoc getChildrenElements(Element JavaDoc xmlNode)
132    {
133       NodeList JavaDoc xmlChildren = xmlNode.getChildNodes();
134       if (xmlChildren == null) return null;
135
136       ArrayList JavaDoc children = new ArrayList JavaDoc();
137       for (int i = 0; i < xmlChildren.getLength(); ++i)
138       {
139          Node JavaDoc xmlChild = xmlChildren.item(i);
140          if (xmlChild.getNodeType() == Node.ELEMENT_NODE) children.add(xmlChild);
141       }
142       return children;
143    }
144
145    private String JavaDoc getNodeValue(Element JavaDoc xmlNode)
146    {
147       NodeList JavaDoc xmlChildren = xmlNode.getChildNodes();
148       if (xmlChildren == null) return null;
149
150       for (int i = 0; i < xmlChildren.getLength(); ++i)
151       {
152          Node JavaDoc 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