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 */
017
018 package org.apache.geronimo.connector.deployment.dconfigbean;
019
020 import java.util.Arrays;
021 import java.util.HashSet;
022 import java.util.Iterator;
023 import java.util.Map;
024 import java.util.Set;
025
026 import javax.enterprise.deploy.model.DDBean;
027 import javax.enterprise.deploy.model.XpathEvent;
028 import javax.enterprise.deploy.model.XpathListener;
029
030 import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType;
031
032 /**
033 *
034 *
035 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
036 *
037 * */
038 public class ConfigPropertiesHelper {
039
040 public static void initializeConfigSettings(DDBean ddBean, ConfigPropertiesSource configPropertiesSource, Map configPropertiesMap, String configPropertyXPath, String configPropertyNameXPath) {
041 DDBean[] configProperties = ddBean.getChildBean(configPropertyXPath);
042 GerConfigPropertySettingType[] configPropertySettings = configPropertiesSource.getConfigPropertySettingArray();
043
044 if (configPropertySettings.length == 0) {
045 //we are new
046 for (int i = 0; i < configProperties.length; i++) {
047 DDBean configProperty = configProperties[i];
048 GerConfigPropertySettingType configPropertySetting = configPropertiesSource.addNewConfigPropertySetting();
049 String name = configProperty.getText(configPropertyNameXPath)[0];
050 ConfigPropertySettingDConfigBean configPropertySettingDConfigBean = new ConfigPropertySettingDConfigBean(configProperty, configPropertySetting);
051 configPropertiesMap.put(name, configPropertySettingDConfigBean);
052 }
053 } else {
054 //we are read in from xml. Check correct length
055 assert configProperties.length == configPropertySettings.length;
056 for (int i = 0; i < configProperties.length; i++) {
057 DDBean configProperty = configProperties[i];
058 GerConfigPropertySettingType configPropertySetting = configPropertySettings[i];
059 String name = configProperty.getText(configPropertyNameXPath)[0];
060 assert name.equals(configPropertySetting.getName());
061 ConfigPropertySettingDConfigBean configPropertySettingDConfigBean = new ConfigPropertySettingDConfigBean(configProperty, configPropertySetting);
062 configPropertiesMap.put(name, configPropertySettingDConfigBean);
063 }
064 }
065 }
066
067 public static XpathListener initialize(DDBean parentDDBean, final ConfigPropertiesHelper.ConfigPropertiesSource configPropertiesSource, String configPropertyXPath, String configPropertyNameXPath) {
068 DDBean[] beans = parentDDBean.getChildBean(configPropertyXPath);
069 ConfigPropertySettings[] configs = new ConfigPropertySettings[beans.length];
070 Set xmlBeans = new HashSet(Arrays.asList(configPropertiesSource.getConfigPropertySettingArray()));
071 for (int i = 0; i < beans.length; i++) {
072 DDBean bean = beans[i];
073 String[] names = bean.getText(configPropertyNameXPath);
074 String name = names.length == 1 ? names[0] : "";
075 GerConfigPropertySettingType target = null;
076 for (Iterator it = xmlBeans.iterator(); it.hasNext();) {
077 GerConfigPropertySettingType setting = (GerConfigPropertySettingType) it.next();
078 if (setting.getName().equals(name)) {
079 target = setting;
080 xmlBeans.remove(target);
081 break;
082 }
083 }
084 if (target == null) {
085 target = configPropertiesSource.addNewConfigPropertySetting();
086 }
087 configs[i] = new ConfigPropertySettings();
088 configs[i].initialize(target, bean);
089 }
090 for (Iterator it = xmlBeans.iterator(); it.hasNext();) { // used to be in XmlBeans, no longer anything matching in J2EE DD
091 GerConfigPropertySettingType target = (GerConfigPropertySettingType) it.next();
092 GerConfigPropertySettingType[] xmlConfigs = configPropertiesSource.getConfigPropertySettingArray();
093 for (int i = 0; i < xmlConfigs.length; i++) {
094 if (xmlConfigs[i] == target) {
095 configPropertiesSource.removeConfigPropertySetting(i);
096 break;
097 }
098 }
099 }
100 configPropertiesSource.setConfigPropertySettings(configs);
101 XpathListener configListener = new XpathListener() {
102 public void fireXpathEvent(XpathEvent 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 }