KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > deployment > dconfigbean > ConfigPropertiesHelper


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.connector.deployment.dconfigbean;
19
20 import java.util.Arrays JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.enterprise.deploy.model.DDBean JavaDoc;
27 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
28 import javax.enterprise.deploy.model.XpathListener JavaDoc;
29
30 import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType;
31
32 /**
33  *
34  *
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  *
37  * */

38 public class ConfigPropertiesHelper {
39
40     public static void initializeConfigSettings(DDBean JavaDoc ddBean, ConfigPropertiesSource configPropertiesSource, Map JavaDoc configPropertiesMap, String JavaDoc configPropertyXPath, String JavaDoc configPropertyNameXPath) {
41         DDBean JavaDoc[] configProperties = ddBean.getChildBean(configPropertyXPath);
42         GerConfigPropertySettingType[] configPropertySettings = configPropertiesSource.getConfigPropertySettingArray();
43
44         if (configPropertySettings.length == 0) {
45             //we are new
46
for (int i = 0; i < configProperties.length; i++) {
47                 DDBean JavaDoc configProperty = configProperties[i];
48                 GerConfigPropertySettingType configPropertySetting = configPropertiesSource.addNewConfigPropertySetting();
49                 String JavaDoc name = configProperty.getText(configPropertyNameXPath)[0];
50                 ConfigPropertySettingDConfigBean configPropertySettingDConfigBean = new ConfigPropertySettingDConfigBean(configProperty, configPropertySetting);
51                 configPropertiesMap.put(name, configPropertySettingDConfigBean);
52             }
53         } else {
54             //we are read in from xml. Check correct length
55
assert configProperties.length == configPropertySettings.length;
56             for (int i = 0; i < configProperties.length; i++) {
57                 DDBean JavaDoc configProperty = configProperties[i];
58                 GerConfigPropertySettingType configPropertySetting = configPropertySettings[i];
59                 String JavaDoc name = configProperty.getText(configPropertyNameXPath)[0];
60                 assert name.equals(configPropertySetting.getName());
61                 ConfigPropertySettingDConfigBean configPropertySettingDConfigBean = new ConfigPropertySettingDConfigBean(configProperty, configPropertySetting);
62                 configPropertiesMap.put(name, configPropertySettingDConfigBean);
63             }
64         }
65     }
66
67     public static XpathListener JavaDoc initialize(DDBean JavaDoc parentDDBean, final ConfigPropertiesHelper.ConfigPropertiesSource configPropertiesSource, String JavaDoc configPropertyXPath, String JavaDoc configPropertyNameXPath) {
68         DDBean JavaDoc[] beans = parentDDBean.getChildBean(configPropertyXPath);
69         ConfigPropertySettings[] configs = new ConfigPropertySettings[beans.length];
70         Set JavaDoc xmlBeans = new HashSet JavaDoc(Arrays.asList(configPropertiesSource.getConfigPropertySettingArray()));
71         for (int i = 0; i < beans.length; i++) {
72             DDBean JavaDoc bean = beans[i];
73             String JavaDoc[] names = bean.getText(configPropertyNameXPath);
74             String JavaDoc name = names.length == 1 ? names[0] : "";
75             GerConfigPropertySettingType target = null;
76             for (Iterator JavaDoc it = xmlBeans.iterator(); it.hasNext();) {
77                 GerConfigPropertySettingType setting = (GerConfigPropertySettingType) it.next();
78                 if (setting.getName().equals(name)) {
79                     target = setting;
80                     xmlBeans.remove(target);
81                     break;
82                 }
83             }
84             if (target == null) {
85                 target = configPropertiesSource.addNewConfigPropertySetting();
86             }
87             configs[i] = new ConfigPropertySettings();
88             configs[i].initialize(target, bean);
89         }
90         for (Iterator JavaDoc it = xmlBeans.iterator(); it.hasNext();) { // used to be in XmlBeans, no longer anything matching in J2EE DD
91
GerConfigPropertySettingType target = (GerConfigPropertySettingType) it.next();
92             GerConfigPropertySettingType[] xmlConfigs = configPropertiesSource.getConfigPropertySettingArray();
93             for (int i = 0; i < xmlConfigs.length; i++) {
94                 if (xmlConfigs[i] == target) {
95                     configPropertiesSource.removeConfigPropertySetting(i);
96                     break;
97                 }
98             }
99         }
100         configPropertiesSource.setConfigPropertySettings(configs);
101         XpathListener JavaDoc configListener = new XpathListener JavaDoc() {
102             public void fireXpathEvent(XpathEvent JavaDoc xpe) {
103                 ConfigPropertySettings[] configs = configPropertiesSource.getConfigPropertySettings();
104                 if (xpe.isAddEvent()) {
105                     ConfigPropertySettings[] bigger = new ConfigPropertySettings[configs.length + 1];
106                     System.arraycopy(configs, 0, bigger, 0, configs.length);
107                     bigger[configs.length] = new ConfigPropertySettings();
108                     bigger[configs.length].initialize(configPropertiesSource.addNewConfigPropertySetting(), xpe.getBean());
109                     configPropertiesSource.setConfigPropertySettings(bigger);
110                 } else if (xpe.isRemoveEvent()) {
111                     int index = -1;
112                     for (int i = 0; i < configs.length; i++) {
113                         if (configs[i].matches(xpe.getBean())) {
114                             // remove the XMLBean
115
GerConfigPropertySettingType[] xmlConfigs = configPropertiesSource.getConfigPropertySettingArray();
116                             for (int j = 0; j < xmlConfigs.length; j++) {
117                                 GerConfigPropertySettingType test = xmlConfigs[j];
118                                 if (test == configs[i].getConfigPropertySetting()) {
119                                     configPropertiesSource.removeConfigPropertySetting(j);
120                                     break;
121                                 }
122                             }
123                             // clean up the JavaBean
124
configs[i].dispose();
125                             index = i;
126                             break;
127                         }
128                     }
129                     // remove the JavaBean from my list
130
if (index > -1) {
131                         ConfigPropertySettings[] smaller = new ConfigPropertySettings[configs.length - 1];
132                         System.arraycopy(configs, 0, smaller, 0, index);
133                         System.arraycopy(configs, index + 1, smaller, index, smaller.length - index);
134                         configPropertiesSource.setConfigPropertySettings(smaller);
135                     }
136                 }
137                 // ignore change event (no contents, no attributes)
138
}
139         };
140         parentDDBean.addXpathListener(configPropertyXPath, configListener);
141         return configListener;
142     }
143
144     public interface ConfigPropertiesSource {
145         GerConfigPropertySettingType[] getConfigPropertySettingArray();
146
147         GerConfigPropertySettingType addNewConfigPropertySetting();
148
149         void removeConfigPropertySetting(int j);
150
151         ConfigPropertySettings[] getConfigPropertySettings();
152
153         void setConfigPropertySettings(ConfigPropertySettings[] configs);
154     }
155 }
156
Popular Tags