1 17 18 package org.apache.geronimo.connector.deployment.dconfigbean; 19 20 import java.util.Arrays ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import javax.enterprise.deploy.model.DDBean ; 27 import javax.enterprise.deploy.model.XpathEvent ; 28 import javax.enterprise.deploy.model.XpathListener ; 29 30 import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType; 31 32 38 public class ConfigPropertiesHelper { 39 40 public static void initializeConfigSettings(DDBean ddBean, ConfigPropertiesSource configPropertiesSource, Map configPropertiesMap, String configPropertyXPath, String configPropertyNameXPath) { 41 DDBean [] configProperties = ddBean.getChildBean(configPropertyXPath); 42 GerConfigPropertySettingType[] configPropertySettings = configPropertiesSource.getConfigPropertySettingArray(); 43 44 if (configPropertySettings.length == 0) { 45 for (int i = 0; i < configProperties.length; i++) { 47 DDBean configProperty = configProperties[i]; 48 GerConfigPropertySettingType configPropertySetting = configPropertiesSource.addNewConfigPropertySetting(); 49 String name = configProperty.getText(configPropertyNameXPath)[0]; 50 ConfigPropertySettingDConfigBean configPropertySettingDConfigBean = new ConfigPropertySettingDConfigBean(configProperty, configPropertySetting); 51 configPropertiesMap.put(name, configPropertySettingDConfigBean); 52 } 53 } else { 54 assert configProperties.length == configPropertySettings.length; 56 for (int i = 0; i < configProperties.length; i++) { 57 DDBean configProperty = configProperties[i]; 58 GerConfigPropertySettingType configPropertySetting = configPropertySettings[i]; 59 String 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 initialize(DDBean parentDDBean, final ConfigPropertiesHelper.ConfigPropertiesSource configPropertiesSource, String configPropertyXPath, String configPropertyNameXPath) { 68 DDBean [] beans = parentDDBean.getChildBean(configPropertyXPath); 69 ConfigPropertySettings[] configs = new ConfigPropertySettings[beans.length]; 70 Set xmlBeans = new HashSet (Arrays.asList(configPropertiesSource.getConfigPropertySettingArray())); 71 for (int i = 0; i < beans.length; i++) { 72 DDBean bean = beans[i]; 73 String [] names = bean.getText(configPropertyNameXPath); 74 String name = names.length == 1 ? names[0] : ""; 75 GerConfigPropertySettingType target = null; 76 for (Iterator 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 it = xmlBeans.iterator(); it.hasNext();) { 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 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 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 configs[i].dispose(); 125 index = i; 126 break; 127 } 128 } 129 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 } 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 |