KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > deployment > jsr88 > ConfigHolder


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.connector.deployment.jsr88;
18
19 import org.apache.geronimo.deployment.plugin.XmlBeanSupport;
20 import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType;
21 import org.apache.xmlbeans.XmlObject;
22
23 import javax.enterprise.deploy.model.XpathListener JavaDoc;
24 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
25 import javax.enterprise.deploy.model.DDBean JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * Base class for beans that hold an array of config property settings.
36  *
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  */

39 public abstract class ConfigHolder extends XmlBeanSupport {
40     final XpathListener JavaDoc xpathListener = new XpathListener JavaDoc() {
41                     public void fireXpathEvent(XpathEvent JavaDoc event) {
42                         if(event.isAddEvent()) {
43                             //todo: add new config-property-setting, fire change event
44
} else if(event.isRemoveEvent()) {
45                             //todo: remove config-property-setting, fire change event
46
} else if(event.isChangeEvent()) {
47                             if(event.getChangeEvent().getPropertyName().equals("config-property-name")) {
48                                 String JavaDoc old = (String JavaDoc) event.getChangeEvent().getOldValue();
49                                 for (int i = 0; i < settings.length; i++) {
50                                     ConfigPropertySetting setting = settings[i];
51                                     if(setting.getName().equals(old)) {
52                                         setting.setName((String JavaDoc) event.getChangeEvent().getNewValue());
53                                         break;
54                                     }
55                                 }
56                             }
57                         }
58                     }
59                 };
60     private DDBean JavaDoc ddBean;
61     private ConfigPropertySetting[] settings = new ConfigPropertySetting[0];
62
63     public ConfigHolder() {
64         super(null);
65     }
66
67     public void clearNullSettings() {
68         List JavaDoc list = new ArrayList JavaDoc();
69         Set JavaDoc saved = new HashSet JavaDoc();
70         for (int i = 0; i < settings.length; i++) {
71             ConfigPropertySetting setting = settings[i];
72             if(setting.getValue() != null && !setting.isSetToDefault()) {
73                 list.add(setting);
74                 saved.add(setting.getName());
75             }
76         }
77         settings = (ConfigPropertySetting[]) list.toArray(new ConfigPropertySetting[list.size()]);
78         GerConfigPropertySettingType[] configs = getConfigProperties();
79         for (int i = configs.length-1; i>=0; --i) {
80             GerConfigPropertySettingType type = configs[i];
81             if(!saved.contains(type.getName())) {
82                 removeConfigProperty(i);
83             }
84         }
85     }
86
87     protected void configure(DDBean JavaDoc ddBean, XmlObject xml) {
88         ConfigPropertySetting[] old = null;
89         if(this.ddBean != null) {
90             this.ddBean.removeXpathListener("config-property", xpathListener);
91             old = settings;
92         }
93         this.ddBean = ddBean;
94         setXmlObject(xml);
95
96         // Prepare the ConfigPropertySetting array
97
List JavaDoc list = new ArrayList JavaDoc();
98         DDBean JavaDoc[] all = ddBean == null ? new DDBean JavaDoc[0] : ddBean.getChildBean("config-property");
99         if(all == null) {
100             all = new DDBean JavaDoc[0];
101         }
102         Map JavaDoc byName = new HashMap JavaDoc();
103         for (int i = 0; i < all.length; i++) {
104             DDBean JavaDoc item = all[i];
105             byName.put(item.getText("config-property-name")[0], item);
106         }
107         GerConfigPropertySettingType[] previous = getConfigProperties();
108         for (int i = 0; i < previous.length; i++) {
109             GerConfigPropertySettingType setting = previous[i];
110             DDBean JavaDoc item = (DDBean JavaDoc) byName.remove(setting.getName());
111             if(item != null) {
112                 list.add(new ConfigPropertySetting(item, setting, false));
113             } else {
114                 System.out.println("Ignoring connectiondefinition-instance/config-setting "+setting.getName()+" (no matching config-property in J2EE DD)");
115                 //todo: delete it from the XMLBeans tree
116
}
117         }
118         for (Iterator JavaDoc it = byName.keySet().iterator(); it.hasNext();) {
119             String JavaDoc name = (String JavaDoc) it.next();
120             DDBean JavaDoc bean = (DDBean JavaDoc) byName.get(name);
121             list.add(new ConfigPropertySetting(bean, createConfigProperty(), true));
122         }
123         settings = (ConfigPropertySetting[]) list.toArray(new ConfigPropertySetting[list.size()]);
124         if(old != null) {
125             pcs.firePropertyChange("configPropertySetting", old, settings);
126         }
127         if(ddBean != null) {
128             ddBean.addXpathListener("config-property", xpathListener);
129         }
130     }
131
132     public ConfigPropertySetting[] getConfigPropertySetting() {
133         return settings;
134     }
135
136     public ConfigPropertySetting getConfigPropertySetting(int index) {
137         return settings[index];
138     }
139
140     protected abstract GerConfigPropertySettingType createConfigProperty();
141     protected abstract GerConfigPropertySettingType[] getConfigProperties();
142     protected abstract void removeConfigProperty(int index);
143     public abstract void reconfigure();
144 }
145
Popular Tags