View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.LinkedHashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.geronimo.kernel.InvalidGBeanException;
27  import org.apache.geronimo.kernel.repository.Artifact;
28  import org.w3c.dom.Element;
29  import org.w3c.dom.NodeList;
30  import org.w3c.dom.Document;
31  
32  /**
33   * @version $Rev: 407884 $ $Date: 2006-05-19 11:57:37 -0700 (Fri, 19 May 2006) $
34   */
35  class ServerOverride {
36      private final Map configurations = new LinkedHashMap();
37  
38      public ServerOverride() {
39      }
40  
41      public ServerOverride(Element element) throws InvalidGBeanException {
42          NodeList configs = element.getElementsByTagName("module");
43          for (int i = 0; i < configs.getLength(); i++) {
44              Element configurationElement = (Element) 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 configurationElement = (Element) 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 getConfigurations() {
80          return configurations;
81      }
82  
83      public Artifact[] queryConfigurations(Artifact query) {
84          List list = new ArrayList();
85          for (Iterator 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 writeXml(Document doc) {
95          Element 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 it = configurations.entrySet().iterator(); it.hasNext();) {
105             Map.Entry entry = (Map.Entry) it.next();
106             ConfigurationOverride configurationOverride = (ConfigurationOverride) entry.getValue();
107             configurationOverride.writeXml(doc, root);
108         }
109         return root;
110     }
111 }