001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.connector.deployment.dconfigbean;
017:
018: import java.util.Arrays;
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import javax.enterprise.deploy.model.DDBean;
025: import javax.enterprise.deploy.model.XpathEvent;
026: import javax.enterprise.deploy.model.XpathListener;
027:
028: import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType;
029:
030: /**
031: *
032: *
033: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
034: *
035: * */
036: public class ConfigPropertiesHelper {
037:
038: public static void initializeConfigSettings(DDBean ddBean,
039: ConfigPropertiesSource configPropertiesSource,
040: Map configPropertiesMap, String configPropertyXPath,
041: String configPropertyNameXPath) {
042: DDBean[] configProperties = ddBean
043: .getChildBean(configPropertyXPath);
044: GerConfigPropertySettingType[] configPropertySettings = configPropertiesSource
045: .getConfigPropertySettingArray();
046:
047: if (configPropertySettings.length == 0) {
048: //we are new
049: for (int i = 0; i < configProperties.length; i++) {
050: DDBean configProperty = configProperties[i];
051: GerConfigPropertySettingType configPropertySetting = configPropertiesSource
052: .addNewConfigPropertySetting();
053: String name = configProperty
054: .getText(configPropertyNameXPath)[0];
055: ConfigPropertySettingDConfigBean configPropertySettingDConfigBean = new ConfigPropertySettingDConfigBean(
056: configProperty, configPropertySetting);
057: configPropertiesMap.put(name,
058: configPropertySettingDConfigBean);
059: }
060: } else {
061: //we are read in from xml. Check correct length
062: assert configProperties.length == configPropertySettings.length;
063: for (int i = 0; i < configProperties.length; i++) {
064: DDBean configProperty = configProperties[i];
065: GerConfigPropertySettingType configPropertySetting = configPropertySettings[i];
066: String name = configProperty
067: .getText(configPropertyNameXPath)[0];
068: assert name.equals(configPropertySetting.getName());
069: ConfigPropertySettingDConfigBean configPropertySettingDConfigBean = new ConfigPropertySettingDConfigBean(
070: configProperty, configPropertySetting);
071: configPropertiesMap.put(name,
072: configPropertySettingDConfigBean);
073: }
074: }
075: }
076:
077: public static XpathListener initialize(
078: DDBean parentDDBean,
079: final ConfigPropertiesHelper.ConfigPropertiesSource configPropertiesSource,
080: String configPropertyXPath, String configPropertyNameXPath) {
081: DDBean[] beans = parentDDBean.getChildBean(configPropertyXPath);
082: ConfigPropertySettings[] configs = new ConfigPropertySettings[beans.length];
083: Set xmlBeans = new HashSet(Arrays.asList(configPropertiesSource
084: .getConfigPropertySettingArray()));
085: for (int i = 0; i < beans.length; i++) {
086: DDBean bean = beans[i];
087: String[] names = bean.getText(configPropertyNameXPath);
088: String name = names.length == 1 ? names[0] : "";
089: GerConfigPropertySettingType target = null;
090: for (Iterator it = xmlBeans.iterator(); it.hasNext();) {
091: GerConfigPropertySettingType setting = (GerConfigPropertySettingType) it
092: .next();
093: if (setting.getName().equals(name)) {
094: target = setting;
095: xmlBeans.remove(target);
096: break;
097: }
098: }
099: if (target == null) {
100: target = configPropertiesSource
101: .addNewConfigPropertySetting();
102: }
103: configs[i] = new ConfigPropertySettings();
104: configs[i].initialize(target, bean);
105: }
106: for (Iterator it = xmlBeans.iterator(); it.hasNext();) { // used to be in XmlBeans, no longer anything matching in J2EE DD
107: GerConfigPropertySettingType target = (GerConfigPropertySettingType) it
108: .next();
109: GerConfigPropertySettingType[] xmlConfigs = configPropertiesSource
110: .getConfigPropertySettingArray();
111: for (int i = 0; i < xmlConfigs.length; i++) {
112: if (xmlConfigs[i] == target) {
113: configPropertiesSource
114: .removeConfigPropertySetting(i);
115: break;
116: }
117: }
118: }
119: configPropertiesSource.setConfigPropertySettings(configs);
120: XpathListener configListener = new XpathListener() {
121: public void fireXpathEvent(XpathEvent xpe) {
122: ConfigPropertySettings[] configs = configPropertiesSource
123: .getConfigPropertySettings();
124: if (xpe.isAddEvent()) {
125: ConfigPropertySettings[] bigger = new ConfigPropertySettings[configs.length + 1];
126: System.arraycopy(configs, 0, bigger, 0,
127: configs.length);
128: bigger[configs.length] = new ConfigPropertySettings();
129: bigger[configs.length].initialize(
130: configPropertiesSource
131: .addNewConfigPropertySetting(), xpe
132: .getBean());
133: configPropertiesSource
134: .setConfigPropertySettings(bigger);
135: } else if (xpe.isRemoveEvent()) {
136: int index = -1;
137: for (int i = 0; i < configs.length; i++) {
138: if (configs[i].matches(xpe.getBean())) {
139: // remove the XMLBean
140: GerConfigPropertySettingType[] xmlConfigs = configPropertiesSource
141: .getConfigPropertySettingArray();
142: for (int j = 0; j < xmlConfigs.length; j++) {
143: GerConfigPropertySettingType test = xmlConfigs[j];
144: if (test == configs[i]
145: .getConfigPropertySetting()) {
146: configPropertiesSource
147: .removeConfigPropertySetting(j);
148: break;
149: }
150: }
151: // clean up the JavaBean
152: configs[i].dispose();
153: index = i;
154: break;
155: }
156: }
157: // remove the JavaBean from my list
158: if (index > -1) {
159: ConfigPropertySettings[] smaller = new ConfigPropertySettings[configs.length - 1];
160: System.arraycopy(configs, 0, smaller, 0, index);
161: System.arraycopy(configs, index + 1, smaller,
162: index, smaller.length - index);
163: configPropertiesSource
164: .setConfigPropertySettings(smaller);
165: }
166: }
167: // ignore change event (no contents, no attributes)
168: }
169: };
170: parentDDBean.addXpathListener(configPropertyXPath,
171: configListener);
172: return configListener;
173: }
174:
175: public interface ConfigPropertiesSource {
176: GerConfigPropertySettingType[] getConfigPropertySettingArray();
177:
178: GerConfigPropertySettingType addNewConfigPropertySetting();
179:
180: void removeConfigPropertySetting(int j);
181:
182: ConfigPropertySettings[] getConfigPropertySettings();
183:
184: void setConfigPropertySettings(ConfigPropertySettings[] configs);
185: }
186: }
|