1 17 package org.apache.geronimo.connector.deployment.jsr88; 18 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.Collections ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.LinkedHashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import javax.enterprise.deploy.model.DDBean ; 29 import javax.enterprise.deploy.model.XpathEvent ; 30 import javax.enterprise.deploy.model.XpathListener ; 31 import javax.enterprise.deploy.spi.DConfigBean ; 32 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 33 import org.apache.geronimo.deployment.plugin.DConfigBeanSupport; 34 import org.apache.geronimo.deployment.service.jsr88.EnvironmentData; 35 import org.apache.geronimo.xbeans.geronimo.GerAdminobjectInstanceType; 36 import org.apache.geronimo.xbeans.geronimo.GerAdminobjectType; 37 import org.apache.geronimo.xbeans.geronimo.GerConnectorType; 38 import org.apache.geronimo.xbeans.geronimo.GerResourceadapterType; 39 import org.apache.xmlbeans.SchemaTypeLoader; 40 import org.apache.xmlbeans.XmlCursor; 41 42 48 public class ConnectorDCB extends DConfigBeanSupport { 49 private DDBean resourceAdapterDDBean; 50 private ResourceAdapter[] resourceAdapter = new ResourceAdapter[0]; 51 private AdminObjectDCB[] adminobjects = new AdminObjectDCB[0]; 52 private EnvironmentData environment; 53 54 public ConnectorDCB(DDBean connectorDDBean, final GerConnectorType connector) { 55 super(connectorDDBean, connector); 56 DDBean [] list = connectorDDBean.getChildBean("resourceadapter"); 57 if(list.length > 0) { 58 resourceAdapterDDBean = list[0]; 59 } 60 loadExistingData(connector); 62 63 connectorDDBean.addXpathListener("resourceadapter", new XpathListener () { 65 public void fireXpathEvent(XpathEvent event) { 66 if(event.isRemoveEvent()) { 67 resourceAdapterDDBean = null; } else if(event.isAddEvent()) { 69 resourceAdapterDDBean = event.getBean(); } else { 71 System.out.println("Detected change to J2EE DD /connector/resourceadapter property "+event.getChangeEvent().getPropertyName()); 72 } 73 } 74 }); 75 } 76 77 public String [] getXpaths() { 78 return getXPathsForJ2ee_1_4(new String [][]{{"resourceadapter","adminobject",},}); 79 } 80 81 public DConfigBean getDConfigBean(DDBean bean) throws ConfigurationException { 82 if (getXpaths()[0].equals(bean.getXpath())) { String aoInterface = bean.getText("adminobject-interface")[0]; 84 String aoClass = bean.getText("adminobject-class")[0]; 85 for(int i=0; i<adminobjects.length; i++) { 87 if(adminobjects[i].getAdminObjectClass().equals(aoClass) && 88 adminobjects[i].getAdminObjectInterface().equals(aoInterface)) { 89 return adminobjects[i]; 90 } 91 } 92 GerAdminobjectType ao = getConnector().addNewAdminobject(); 94 AdminObjectDCB dcb = new AdminObjectDCB(bean, ao); 95 AdminObjectDCB[] list = new AdminObjectDCB[adminobjects.length+1]; 96 System.arraycopy(adminobjects, 0, list, 0, adminobjects.length); 97 list[adminobjects.length] = dcb; 98 return dcb; 99 } else { 100 throw new ConfigurationException ("No DConfigBean matching DDBean "+bean.getXpath()); 101 } 102 } 103 104 private void loadExistingData(GerConnectorType connector) { 105 GerResourceadapterType[] adapters = connector.getResourceadapterArray(); 118 if(adapters == null || adapters.length == 0) { 119 if(resourceAdapterDDBean != null) { 121 resourceAdapter = new ResourceAdapter[1]; 122 resourceAdapter[0] = new ResourceAdapter(resourceAdapterDDBean, connector.addNewResourceadapter()); 123 } 124 } else { 125 resourceAdapter = new ResourceAdapter[adapters.length]; 126 for (int i = 0; i < adapters.length; i++) { 127 GerResourceadapterType adapter = adapters[i]; 128 resourceAdapter[i] = new ResourceAdapter(resourceAdapterDDBean, adapter); 129 } 130 } 131 GerAdminobjectType[] admins = connector.getAdminobjectArray(); 133 DDBean [] data = getDDBean().getChildBean(getXpaths()[0]); 134 List ddBeans = data == null ? Collections.EMPTY_LIST : new ArrayList (Arrays.asList(data)); 136 Map dcbs = new LinkedHashMap (); 137 if(admins == null) { 138 adminobjects = new AdminObjectDCB[0]; 139 } else { 140 for (int i = 0; i < admins.length; i++) { 142 GerAdminobjectType admin = admins[i]; 143 String aoClass = admin.getAdminobjectClass(); 144 String aoIface = admin.getAdminobjectInterface(); 145 AdminObjectDCB dcb = (AdminObjectDCB) dcbs.get("class "+aoClass+" iface "+aoIface); 146 if(dcb != null) { 147 if(admin.getAdminobjectInstanceArray().length > 0) { 150 GerAdminobjectType old = dcb.getAdminObject(); 151 GerAdminobjectInstanceType[] array = admin.getAdminobjectInstanceArray(); 152 int oldCount = dcb.getAdminObjectInstance().length; 153 for (int j = 0; j < array.length; j++) { 154 GerAdminobjectInstanceType instance = array[j]; 155 XmlCursor source = instance.newCursor(); 156 XmlCursor dest = old.newCursor(); 157 dest.toEndToken(); 158 if(!source.moveXml(dest)) { 159 throw new RuntimeException ("Unable to move admin object instance"); 160 } 161 source.dispose(); 162 dest.dispose(); 163 dcb.addAdminObjectInstance(old.getAdminobjectInstanceArray(oldCount+j)); 164 } 165 } 166 continue; 167 } 168 DDBean target = null; 169 for (int j = 0; j < ddBeans.size(); j++) { 170 DDBean ddBean = (DDBean ) ddBeans.get(j); 171 String ddClass = ddBean.getText("adminobject-class")[0]; 172 String ddIface = ddBean.getText("adminobject-interface")[0]; 173 if(ddClass.equals(aoClass) && ddIface.equals(aoIface)) { 174 target = ddBean; 175 ddBeans.remove(j); 176 break; 177 } 178 } 179 if(target == null) { 180 System.out.println("Geronimo connector deployment plan has admin object with interface '"+aoIface+"' and class '"+aoClass+"' but the ra.xml does not have a matching adminobject declared. Deleting this adminobject from the Geronimo plan."); 181 continue; 182 } 183 dcb = new AdminObjectDCB(target, admin); 184 dcbs.put("class "+aoClass+" iface "+aoIface, dcb); 185 } 186 } 187 for (int i = 0; i < ddBeans.size(); i++) { 189 DDBean ddBean = (DDBean ) ddBeans.get(i); 190 String ddClass = ddBean.getText("adminobject-class")[0]; 191 String ddIface = ddBean.getText("adminobject-interface")[0]; 192 GerAdminobjectType admin = connector.addNewAdminobject(); 193 dcbs.put("class "+ddClass+" iface "+ddIface, new AdminObjectDCB(ddBean, admin)); 194 } 195 List adminResults = new ArrayList (); 196 for (Iterator it = dcbs.keySet().iterator(); it.hasNext();) { 197 String key = (String ) it.next(); 198 DConfigBean value = (DConfigBean ) dcbs.get(key); 199 adminResults.add(value); 200 } 201 adminobjects = (AdminObjectDCB[]) adminResults.toArray(new AdminObjectDCB[adminResults.size()]); 202 203 } 205 206 GerConnectorType getConnector() { 207 return (GerConnectorType) getXmlObject(); 208 } 209 210 211 213 216 public EnvironmentData getEnvironment() { 217 return environment; 218 } 219 220 public void setEnvironment(EnvironmentData environment) { 221 EnvironmentData old = this.environment; 222 this.environment = environment; 223 if((old == null && environment == null) || (old != null&& old == environment)) { 224 return; 225 } 226 if(old != null) { 227 getConnector().unsetEnvironment(); 228 } 229 if(environment != null) { 230 environment.configure(getConnector().addNewEnvironment()); 231 } 232 pcs.firePropertyChange("environment", old, environment); 233 } 234 235 public ResourceAdapter[] getResourceAdapter() { 236 return resourceAdapter; 237 } 238 239 public void setResourceAdapter(ResourceAdapter[] resourceAdapter) { 240 ResourceAdapter[] old = this.resourceAdapter; 241 Set before = new HashSet (); 242 for (int i = 0; i < old.length; i++) { 243 before.add(old[i]); 244 } 245 this.resourceAdapter = resourceAdapter; 246 for (int i = 0; i < resourceAdapter.length; i++) { 248 ResourceAdapter adapter = resourceAdapter[i]; 249 if(adapter.getResourceAdapter() == null) { 250 adapter.configure(resourceAdapterDDBean, getConnector().addNewResourceadapter()); 251 } else { 252 before.remove(adapter); 253 } 254 } 255 for (Iterator it = before.iterator(); it.hasNext();) { 257 ResourceAdapter adapter = (ResourceAdapter) it.next(); 258 GerResourceadapterType all[] = getConnector().getResourceadapterArray(); 259 for (int i = 0; i < all.length; i++) { 260 if(all[i] == adapter) { 261 getConnector().removeResourceadapter(i); 262 break; 263 } 264 } 265 } 266 pcs.firePropertyChange("resourceAdapter", old, resourceAdapter); 267 } 268 269 public ResourceAdapter getResourceAdapter(int index) { 270 return resourceAdapter[index]; 271 } 272 273 public void setResourceAdapter(int index, ResourceAdapter ra) { 274 ResourceAdapter[] old = this.resourceAdapter; 275 resourceAdapter[index] = ra; 276 if(ra.getResourceAdapter() == null) { 277 ra.configure(resourceAdapterDDBean, getConnector().addNewResourceadapter()); 278 } 279 pcs.firePropertyChange("resourceAdapter", old, resourceAdapter); 280 } 281 282 283 285 286 287 protected SchemaTypeLoader getSchemaTypeLoader() { 288 return Connector15DCBRoot.SCHEMA_TYPE_LOADER; 289 } 290 } 291 | Popular Tags |