KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > configuration > ServerOverride


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.system.configuration;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.geronimo.kernel.InvalidGBeanException;
27 import org.apache.geronimo.kernel.repository.Artifact;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.NodeList JavaDoc;
30 import org.w3c.dom.Document JavaDoc;
31
32 /**
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  */

35 class ServerOverride {
36     private final Map JavaDoc configurations = new LinkedHashMap JavaDoc();
37
38     public ServerOverride() {
39     }
40
41     public ServerOverride(Element JavaDoc element) throws InvalidGBeanException {
42         NodeList JavaDoc configs = element.getElementsByTagName("module");
43         for (int i = 0; i < configs.getLength(); i++) {
44             Element JavaDoc configurationElement = (Element JavaDoc) configs.item(i);
45             ConfigurationOverride configuration = new ConfigurationOverride(configurationElement);
46             addConfiguration(configuration);
47         }
48
49         // The config.xml file in 1.0 use configuration instead of module
50
configs = element.getElementsByTagName("configuration");
51         for (int i = 0; i < configs.getLength(); i++) {
52             Element JavaDoc configurationElement = (Element JavaDoc) configs.item(i);
53             ConfigurationOverride configuration = new ConfigurationOverride(configurationElement);
54             addConfiguration(configuration);
55         }
56     }
57
58     public ConfigurationOverride getConfiguration(Artifact configurationName) {
59         return getConfiguration(configurationName, false);
60     }
61
62     public ConfigurationOverride getConfiguration(Artifact configurationName, boolean create) {
63         ConfigurationOverride configuration = (ConfigurationOverride) configurations.get(configurationName);
64         if (create && configuration == null) {
65             configuration = new ConfigurationOverride(configurationName, true);
66             configurations.put(configurationName, configuration);
67         }
68         return configuration;
69     }
70
71     public void addConfiguration(ConfigurationOverride configuration) {
72         configurations.put(configuration.getName(), configuration);
73     }
74
75     public void removeConfiguration(Artifact configurationName) {
76         configurations.remove(configurationName);
77     }
78
79     public Map JavaDoc getConfigurations() {
80         return configurations;
81     }
82
83     public Artifact[] queryConfigurations(Artifact query) {
84         List JavaDoc list = new ArrayList JavaDoc();
85         for (Iterator JavaDoc it = configurations.keySet().iterator(); it.hasNext();) {
86             Artifact test = (Artifact) it.next();
87             if(query.matches(test)) {
88                 list.add(test);
89             }
90         }
91         return (Artifact[]) list.toArray(new Artifact[list.size()]);
92     }
93
94     public Element JavaDoc writeXml(Document JavaDoc doc) {
95         Element JavaDoc root = doc.createElement("attributes");
96         root.setAttribute("xmlns", "http://geronimo.apache.org/xml/ns/attributes-1.1");
97         doc.appendChild(doc.createComment(" ======================================================== "));
98         doc.appendChild(doc.createComment(" Warning - This XML file is re-generated by Geronimo when "));
99         doc.appendChild(doc.createComment(" changes are made to Geronimo's configuration, therefore "));
100         doc.appendChild(doc.createComment(" any comments added to this file will be lost. "));
101         doc.appendChild(doc.createComment(" Do not edit this file while Geronimo is running. "));
102         doc.appendChild(doc.createComment(" ======================================================== "));
103         doc.appendChild(root);
104         for (Iterator JavaDoc it = configurations.entrySet().iterator(); it.hasNext();) {
105             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
106             ConfigurationOverride configurationOverride = (ConfigurationOverride) entry.getValue();
107             configurationOverride.writeXml(doc, root);
108         }
109         return root;
110     }
111 }
112
Popular Tags