KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 package org.apache.geronimo.system.configuration;
19
20 import org.apache.geronimo.system.configuration.condition.ConditionParser;
21 import org.apache.geronimo.system.configuration.condition.JexlConditionParser;
22
23 import org.apache.geronimo.gbean.AbstractName;
24 import org.apache.geronimo.kernel.InvalidGBeanException;
25 import org.apache.geronimo.kernel.repository.Artifact;
26
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29 import org.w3c.dom.Document JavaDoc;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedHashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 /**
36  * @version $Rev: 470597 $ $Date: 2006-11-02 18:30:55 -0500 (Thu, 02 Nov 2006) $
37  */

38 class ConfigurationOverride {
39     private final Artifact name;
40     private boolean load;
41     private String JavaDoc condition;
42     private final Map JavaDoc gbeans = new LinkedHashMap JavaDoc();
43
44     /**
45      * Cached condition parser; lazy init on the first call to {@link #parseCondition()}
46      * whne {@link #condition} is non-null.
47      */

48     private static ConditionParser parser;
49
50     public ConfigurationOverride(Artifact name, boolean load) {
51         this.name = name;
52         this.load = load;
53     }
54
55     public ConfigurationOverride(ConfigurationOverride base, Artifact name) {
56         this.name = name;
57         this.load = base.load;
58         this.condition = base.condition;
59         this.gbeans.putAll(base.gbeans);
60     }
61
62     public ConfigurationOverride(Element JavaDoc element) throws InvalidGBeanException {
63         name = Artifact.create(element.getAttribute("name"));
64         
65         condition = element.getAttribute("condition");
66         
67         String JavaDoc loadConfigString = element.getAttribute("load");
68         load = !"false".equals(loadConfigString);
69
70         NodeList JavaDoc gbeans = element.getElementsByTagName("gbean");
71         for (int g = 0; g < gbeans.getLength(); g++) {
72             Element JavaDoc gbeanElement = (Element JavaDoc) gbeans.item(g);
73             GBeanOverride gbean = new GBeanOverride(gbeanElement);
74             addGBean(gbean);
75         }
76     }
77
78     public Artifact getName() {
79         return name;
80     }
81
82     public String JavaDoc getCondition() {
83         return condition;
84     }
85     
86     public void setCondition(final String JavaDoc condition) {
87         this.condition = condition;
88     }
89
90     private boolean parseCondition() {
91         if (condition == null) {
92             // no condition means true
93
return true;
94         }
95
96         // Create a parser if one does not already exist
97
if (parser == null) {
98             parser = new JexlConditionParser();
99         }
100         
101         return parser.evaluate(condition);
102     }
103     
104     public boolean isLoad() {
105         return load && parseCondition();
106     }
107
108     public void setLoad(boolean load) {
109         this.load = load;
110     }
111     
112     public GBeanOverride getGBean(String JavaDoc gbeanName) {
113         return (GBeanOverride) gbeans.get(gbeanName);
114     }
115
116     public void addGBean(GBeanOverride gbean) {
117         gbeans.put(gbean.getName(), gbean);
118     }
119
120     public void addGBean(String JavaDoc gbeanName, GBeanOverride gbean) {
121         gbeans.put(gbeanName, gbean);
122     }
123
124     public Map JavaDoc getGBeans() {
125         return gbeans;
126     }
127
128     public GBeanOverride getGBean(AbstractName gbeanName) {
129         return (GBeanOverride) gbeans.get(gbeanName);
130     }
131
132     public void addGBean(AbstractName gbeanName, GBeanOverride gbean) {
133         gbeans.put(gbeanName, gbean);
134     }
135
136     public Element JavaDoc writeXml(Document JavaDoc doc, Element JavaDoc root) {
137         Element JavaDoc module = doc.createElement("module");
138         root.appendChild(module);
139         module.setAttribute("name", name.toString());
140         if (!load) {
141             module.setAttribute("load", "false");
142         }
143         if (condition != null && condition.trim().length() != 0) {
144             module.setAttribute("condition", condition);
145         }
146
147         // GBeans
148
for (Iterator JavaDoc gb = gbeans.entrySet().iterator(); gb.hasNext();) {
149             Map.Entry JavaDoc gbean = (Map.Entry JavaDoc) gb.next();
150             GBeanOverride gbeanOverride = (GBeanOverride) gbean.getValue();
151             gbeanOverride.writeXml(doc, module);
152         }
153         return module;
154     }
155 }
156
Popular Tags