1 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 ; 24 import javax.enterprise.deploy.model.XpathEvent ; 25 import javax.enterprise.deploy.model.DDBean ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.Set ; 29 import java.util.HashSet ; 30 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 34 39 public abstract class ConfigHolder extends XmlBeanSupport { 40 final XpathListener xpathListener = new XpathListener () { 41 public void fireXpathEvent(XpathEvent event) { 42 if(event.isAddEvent()) { 43 } else if(event.isRemoveEvent()) { 45 } else if(event.isChangeEvent()) { 47 if(event.getChangeEvent().getPropertyName().equals("config-property-name")) { 48 String old = (String ) 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 ) event.getChangeEvent().getNewValue()); 53 break; 54 } 55 } 56 } 57 } 58 } 59 }; 60 private DDBean ddBean; 61 private ConfigPropertySetting[] settings = new ConfigPropertySetting[0]; 62 63 public ConfigHolder() { 64 super(null); 65 } 66 67 public void clearNullSettings() { 68 List list = new ArrayList (); 69 Set saved = new HashSet (); 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 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 List list = new ArrayList (); 98 DDBean [] all = ddBean == null ? new DDBean [0] : ddBean.getChildBean("config-property"); 99 if(all == null) { 100 all = new DDBean [0]; 101 } 102 Map byName = new HashMap (); 103 for (int i = 0; i < all.length; i++) { 104 DDBean 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 item = (DDBean ) 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 } 117 } 118 for (Iterator it = byName.keySet().iterator(); it.hasNext();) { 119 String name = (String ) it.next(); 120 DDBean bean = (DDBean ) 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 |