001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.connector.deployment.jsr88;
017:
018: import java.io.InputStream;
019: import java.io.IOException;
020: import java.io.OutputStream;
021: import java.util.Map;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.Arrays;
027: import javax.enterprise.deploy.model.DDBeanRoot;
028: import javax.enterprise.deploy.model.DDBean;
029: import javax.enterprise.deploy.spi.DConfigBean;
030: import javax.enterprise.deploy.spi.exceptions.ConfigurationException;
031: import org.apache.geronimo.deployment.plugin.DConfigBeanRootSupport;
032: import org.apache.geronimo.xbeans.geronimo.GerConnectorDocument;
033: import org.apache.geronimo.xbeans.geronimo.GerAdminobjectInstanceType;
034: import org.apache.geronimo.xbeans.geronimo.GerConnectiondefinitionInstanceType;
035: import org.apache.geronimo.xbeans.geronimo.GerResourceadapterInstanceType;
036: import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType;
037: import org.apache.xmlbeans.XmlObject;
038: import org.apache.xmlbeans.XmlException;
039: import org.apache.xmlbeans.SchemaTypeLoader;
040: import org.apache.xmlbeans.XmlBeans;
041: import org.apache.xmlbeans.XmlCursor;
042:
043: /**
044: * Represents "/" in a Geronimo Connector deployment plan (geronimo-ra.xml).
045: * The only function here is to navigate to an appropriate "Connector"
046: * DConfigBean.
047: *
048: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
049: */
050: public class Connector15DCBRoot extends DConfigBeanRootSupport {
051: // This may be overcomplicated -- if we don't refer to J2EE types in our schemas
052: // then we should only need to use the GerConnectorDocument loader
053: static final SchemaTypeLoader SCHEMA_TYPE_LOADER = XmlBeans
054: .typeLoaderUnion(new SchemaTypeLoader[] {
055: XmlBeans
056: .typeLoaderForClassLoader(org.apache.geronimo.xbeans.j2ee.String.class
057: .getClassLoader()),
058: XmlBeans
059: .typeLoaderForClassLoader(GerConnectorDocument.class
060: .getClassLoader()) });
061:
062: private ConnectorDCB connector;
063:
064: public Connector15DCBRoot(DDBeanRoot ddBean) {
065: super (ddBean, null);
066: setXmlObject(loadDefaultData(ddBean));
067: }
068:
069: private XmlObject loadDefaultData(DDBeanRoot root) {
070: InputStream in = root.getDeployableObject().getEntry(
071: "META-INF/geronimo-ra.xml");
072: if (in == null) {
073: GerConnectorDocument doc = GerConnectorDocument.Factory
074: .newInstance();
075: DDBean[] list = root.getChildBean("connector");
076: if (list.length > 0) {
077: connector = new ConnectorDCB(list[0], doc
078: .addNewConnector());
079: }
080: return doc;
081: } else {
082: try {
083: GerConnectorDocument result = GerConnectorDocument.Factory
084: .parse(in);
085: in.close();
086: DDBean[] list = root.getChildBean("connector");
087: if (list.length > 0) {
088: connector = new ConnectorDCB(list[0], result
089: .getConnector());
090: }
091: return result;
092: } catch (XmlException e) {
093: throw new RuntimeException(
094: "Unable to load default Geronimo RA data", e);
095: } catch (IOException e) {
096: throw new RuntimeException(
097: "Unable to load default Geronimo RA data", e);
098: }
099: }
100: }
101:
102: GerConnectorDocument getConnectorDocument() {
103: return (GerConnectorDocument) getXmlObject();
104: }
105:
106: public String[] getXpaths() {
107: return getXPathsForJ2ee_1_4(new String[][] { { "connector", }, });
108: }
109:
110: public DConfigBean getDConfigBean(DDBean bean)
111: throws ConfigurationException {
112: if (getXpaths()[0].equals(bean.getXpath())) { // "connector"
113: return connector;
114: } else {
115: throw new ConfigurationException(
116: "No DConfigBean matching DDBean " + bean.getXpath());
117: }
118: }
119:
120: protected SchemaTypeLoader getSchemaTypeLoader() {
121: return SCHEMA_TYPE_LOADER;
122: }
123:
124: /**
125: * When loaded, reset the cached "connector" child
126: */
127: public void fromXML(InputStream inputStream) throws XmlException,
128: IOException {
129: DDBean ddb = connector.getDDBean();
130: super .fromXML(inputStream);
131: if (getConnectorDocument().getConnector() != null) {
132: connector = new ConnectorDCB(ddb, getConnectorDocument()
133: .getConnector());
134: } else {
135: connector = new ConnectorDCB(ddb, getConnectorDocument()
136: .addNewConnector());
137: }
138: //todo: fire some kind of notification for the DDBeans to catch?
139: }
140:
141: /**
142: * A little trickery -- on a save event, temporarily remove any config-property-setting
143: * elements with a null value, and then immediately replace them again. This is because
144: * we don't want to write them out as null, but we also want to keep the objects in
145: * sync 1:1 with the config params declared in the J2EE deployment descriptor.
146: */
147: public void toXML(OutputStream outputStream) throws IOException {
148: List parents = new ArrayList();
149: clearNulls(parents);
150: try {
151: super .toXML(outputStream);
152: } finally {
153: for (int i = 0; i < parents.size(); i++) {
154: Object parent = parents.get(i);
155: ConfigHolder instance = (ConfigHolder) parent;
156: instance.reconfigure();
157: }
158: }
159: }
160:
161: private void clearNulls(List parents) {
162: ResourceAdapter[] adapters = connector.getResourceAdapter();
163: for (int i = 0; i < adapters.length; i++) {
164: ResourceAdapter adapter = adapters[i];
165: if (adapter.getResourceAdapterInstance() != null) {
166: parents.add(adapter.getResourceAdapterInstance());
167: adapter.getResourceAdapterInstance()
168: .clearNullSettings();
169: }
170: ConnectionDefinition defs[] = adapter
171: .getConnectionDefinition();
172: for (int j = 0; j < defs.length; j++) {
173: ConnectionDefinition def = defs[j];
174: ConnectionDefinitionInstance instances[] = def
175: .getConnectionInstances();
176: for (int k = 0; k < instances.length; k++) {
177: ConnectionDefinitionInstance instance = instances[k];
178: parents.add(instance);
179: instance.clearNullSettings();
180: }
181: }
182: }
183: try {
184: DDBean[] adminDDBs = connector.getDDBean().getChildBean(
185: connector.getXpaths()[0]);
186: if (adminDDBs == null)
187: adminDDBs = new DDBean[0];
188: for (int i = 0; i < adminDDBs.length; i++) {
189: DDBean ddb = adminDDBs[i];
190: AdminObjectDCB dcb = (AdminObjectDCB) connector
191: .getDConfigBean(ddb);
192: AdminObjectInstance[] instances = dcb
193: .getAdminObjectInstance();
194: for (int j = 0; j < instances.length; j++) {
195: AdminObjectInstance instance = instances[j];
196: parents.add(instance);
197: instance.clearNullSettings();
198: }
199: }
200: } catch (ConfigurationException e) {
201: e.printStackTrace();
202: }
203: }
204: }
|