1 17 package org.apache.geronimo.connector.deployment.jsr88; 18 19 import java.io.InputStream ; 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.util.Map ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.Arrays ; 28 import javax.enterprise.deploy.model.DDBeanRoot ; 29 import javax.enterprise.deploy.model.DDBean ; 30 import javax.enterprise.deploy.spi.DConfigBean ; 31 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 32 import org.apache.geronimo.deployment.plugin.DConfigBeanRootSupport; 33 import org.apache.geronimo.xbeans.geronimo.GerConnectorDocument; 34 import org.apache.geronimo.xbeans.geronimo.GerAdminobjectInstanceType; 35 import org.apache.geronimo.xbeans.geronimo.GerConnectiondefinitionInstanceType; 36 import org.apache.geronimo.xbeans.geronimo.GerResourceadapterInstanceType; 37 import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType; 38 import org.apache.xmlbeans.XmlObject; 39 import org.apache.xmlbeans.XmlException; 40 import org.apache.xmlbeans.SchemaTypeLoader; 41 import org.apache.xmlbeans.XmlBeans; 42 import org.apache.xmlbeans.XmlCursor; 43 44 51 public class Connector15DCBRoot extends DConfigBeanRootSupport { 52 static final SchemaTypeLoader SCHEMA_TYPE_LOADER = XmlBeans.typeLoaderUnion(new SchemaTypeLoader[] { 55 XmlBeans.typeLoaderForClassLoader(org.apache.geronimo.xbeans.j2ee.String.class.getClassLoader()), 56 XmlBeans.typeLoaderForClassLoader(GerConnectorDocument.class.getClassLoader()) 57 }); 58 59 private ConnectorDCB connector; 60 61 public Connector15DCBRoot(DDBeanRoot ddBean) { 62 super(ddBean, null); 63 setXmlObject(loadDefaultData(ddBean)); 64 } 65 66 private XmlObject loadDefaultData(DDBeanRoot root) { 67 InputStream in = root.getDeployableObject().getEntry("META-INF/geronimo-ra.xml"); 68 if(in == null) { 69 GerConnectorDocument doc = GerConnectorDocument.Factory.newInstance(); 70 DDBean [] list = root.getChildBean("connector"); 71 if(list.length > 0) { 72 connector = new ConnectorDCB(list[0], doc.addNewConnector()); 73 } 74 return doc; 75 } else { 76 try { 77 GerConnectorDocument result = GerConnectorDocument.Factory.parse(in); 78 in.close(); 79 DDBean [] list = root.getChildBean("connector"); 80 if(list.length > 0) { 81 connector = new ConnectorDCB(list[0], result.getConnector()); 82 } 83 return result; 84 } catch (XmlException e) { 85 throw new RuntimeException ("Unable to load default Geronimo RA data", e); 86 } catch (IOException e) { 87 throw new RuntimeException ("Unable to load default Geronimo RA data", e); 88 } 89 } 90 } 91 92 GerConnectorDocument getConnectorDocument() { 93 return (GerConnectorDocument) getXmlObject(); 94 } 95 96 public String [] getXpaths() { 97 return getXPathsForJ2ee_1_4(new String [][]{{"connector",},}); 98 } 99 100 public DConfigBean getDConfigBean(DDBean bean) throws ConfigurationException { 101 if (getXpaths()[0].equals(bean.getXpath())) { return connector; 103 } else { 104 throw new ConfigurationException ("No DConfigBean matching DDBean "+bean.getXpath()); 105 } 106 } 107 108 protected SchemaTypeLoader getSchemaTypeLoader() { 109 return SCHEMA_TYPE_LOADER; 110 } 111 112 115 public void fromXML(InputStream inputStream) throws XmlException, IOException { 116 DDBean ddb = connector.getDDBean(); 117 super.fromXML(inputStream); 118 if(getConnectorDocument().getConnector() != null) { 119 connector = new ConnectorDCB(ddb, getConnectorDocument().getConnector()); 120 } else { 121 connector = new ConnectorDCB(ddb, getConnectorDocument().addNewConnector()); 122 } 123 } 125 126 132 public void toXML(OutputStream outputStream) throws IOException { 133 List parents = new ArrayList (); 134 clearNulls(parents); 135 try { 136 super.toXML(outputStream); 137 } finally { 138 for (int i = 0; i < parents.size(); i++) { 139 Object parent = parents.get(i); 140 ConfigHolder instance = (ConfigHolder) parent; 141 instance.reconfigure(); 142 } 143 } 144 } 145 146 private void clearNulls(List parents) { 147 ResourceAdapter[] adapters = connector.getResourceAdapter(); 148 for (int i = 0; i < adapters.length; i++) { 149 ResourceAdapter adapter = adapters[i]; 150 if(adapter.getResourceAdapterInstance() != null) { 151 parents.add(adapter.getResourceAdapterInstance()); 152 adapter.getResourceAdapterInstance().clearNullSettings(); 153 } 154 ConnectionDefinition defs[] = adapter.getConnectionDefinition(); 155 for (int j = 0; j < defs.length; j++) { 156 ConnectionDefinition def = defs[j]; 157 ConnectionDefinitionInstance instances[] = def.getConnectionInstances(); 158 for (int k = 0; k < instances.length; k++) { 159 ConnectionDefinitionInstance instance = instances[k]; 160 parents.add(instance); 161 instance.clearNullSettings(); 162 } 163 } 164 } 165 try { 166 DDBean [] adminDDBs = connector.getDDBean().getChildBean(connector.getXpaths()[0]); 167 if(adminDDBs == null) adminDDBs = new DDBean [0]; 168 for (int i = 0; i < adminDDBs.length; i++) { 169 DDBean ddb = adminDDBs[i]; 170 AdminObjectDCB dcb = (AdminObjectDCB) connector.getDConfigBean(ddb); 171 AdminObjectInstance[] instances = dcb.getAdminObjectInstance(); 172 for (int j = 0; j < instances.length; j++) { 173 AdminObjectInstance instance = instances[j]; 174 parents.add(instance); 175 instance.clearNullSettings(); 176 } 177 } 178 } catch (ConfigurationException e) { 179 e.printStackTrace(); 180 } 181 } 182 } 183 | Popular Tags |