KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > deployment > jsr88 > Connector15DCBRoot


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.connector.deployment.jsr88;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
29 import javax.enterprise.deploy.model.DDBean JavaDoc;
30 import javax.enterprise.deploy.spi.DConfigBean JavaDoc;
31 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
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 /**
45  * Represents "/" in a Geronimo Connector deployment plan (geronimo-ra.xml).
46  * The only function here is to navigate to an appropriate "Connector"
47  * DConfigBean.
48  *
49  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
50  */

51 public class Connector15DCBRoot extends DConfigBeanRootSupport {
52     // This may be overcomplicated -- if we don't refer to J2EE types in our schemas
53
// then we should only need to use the GerConnectorDocument loader
54
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 JavaDoc ddBean) {
62         super(ddBean, null);
63         setXmlObject(loadDefaultData(ddBean));
64     }
65
66     private XmlObject loadDefaultData(DDBeanRoot JavaDoc root) {
67         InputStream JavaDoc in = root.getDeployableObject().getEntry("META-INF/geronimo-ra.xml");
68         if(in == null) {
69             GerConnectorDocument doc = GerConnectorDocument.Factory.newInstance();
70             DDBean JavaDoc[] 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 JavaDoc[] 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 JavaDoc("Unable to load default Geronimo RA data", e);
86             } catch (IOException JavaDoc e) {
87                 throw new RuntimeException JavaDoc("Unable to load default Geronimo RA data", e);
88             }
89         }
90     }
91
92     GerConnectorDocument getConnectorDocument() {
93         return (GerConnectorDocument) getXmlObject();
94     }
95
96     public String JavaDoc[] getXpaths() {
97         return getXPathsForJ2ee_1_4(new String JavaDoc[][]{{"connector",},});
98     }
99
100     public DConfigBean JavaDoc getDConfigBean(DDBean JavaDoc bean) throws ConfigurationException JavaDoc {
101         if (getXpaths()[0].equals(bean.getXpath())) { // "connector"
102
return connector;
103         } else {
104             throw new ConfigurationException JavaDoc("No DConfigBean matching DDBean "+bean.getXpath());
105         }
106     }
107
108     protected SchemaTypeLoader getSchemaTypeLoader() {
109         return SCHEMA_TYPE_LOADER;
110     }
111
112     /**
113      * When loaded, reset the cached "connector" child
114      */

115     public void fromXML(InputStream JavaDoc inputStream) throws XmlException, IOException JavaDoc {
116         DDBean JavaDoc 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         //todo: fire some kind of notification for the DDBeans to catch?
124
}
125
126     /**
127      * A little trickery -- on a save event, temporarily remove any config-property-setting
128      * elements with a null value, and then immediately replace them again. This is because
129      * we don't want to write them out as null, but we also want to keep the objects in
130      * sync 1:1 with the config params declared in the J2EE deployment descriptor.
131      */

132     public void toXML(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
133         List JavaDoc parents = new ArrayList JavaDoc();
134         clearNulls(parents);
135         try {
136             super.toXML(outputStream);
137         } finally {
138             for (int i = 0; i < parents.size(); i++) {
139                 Object JavaDoc parent = parents.get(i);
140                 ConfigHolder instance = (ConfigHolder) parent;
141                 instance.reconfigure();
142             }
143         }
144     }
145
146     private void clearNulls(List JavaDoc 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 JavaDoc[] adminDDBs = connector.getDDBean().getChildBean(connector.getXpaths()[0]);
167             if(adminDDBs == null) adminDDBs = new DDBean JavaDoc[0];
168             for (int i = 0; i < adminDDBs.length; i++) {
169                 DDBean JavaDoc 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 JavaDoc e) {
179             e.printStackTrace();
180         }
181     }
182 }
183
Popular Tags