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.util.Set;
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.HashMap;
023: import javax.enterprise.deploy.model.DDBean;
024: import org.apache.geronimo.deployment.plugin.XmlBeanSupport;
025: import org.apache.geronimo.xbeans.geronimo.GerConnectionDefinitionType;
026: import org.apache.geronimo.xbeans.geronimo.GerConnectiondefinitionInstanceType;
027: import org.apache.xmlbeans.SchemaTypeLoader;
028:
029: /**
030: * Represents /connector/resourceadapter/outbound-resourceadapter/connection-definition
031: * in the Geronimo Connector deployment plan. A Geronimo connection definition
032: * corresponds to a ra.xml connection definition (though there may be several
033: * Geronimo CDs for each ra.xml CD so this cannot be a DConfigBean [which would
034: * require a 1:1 mapping]). Each Geronimo connection definition may have one
035: * or more instances with different config property settings, etc.
036: *
037: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
038: */
039: public class ConnectionDefinition extends XmlBeanSupport {
040: private DDBean resourceAdapter;
041: private ConnectionDefinitionInstance[] instances = new ConnectionDefinitionInstance[0];
042:
043: public ConnectionDefinition() {
044: super (null);
045: }
046:
047: public ConnectionDefinition(DDBean resourceAdapter,
048: GerConnectionDefinitionType definition) {
049: super (null);
050: configure(resourceAdapter, definition);
051: }
052:
053: protected GerConnectionDefinitionType getConnectionDefinition() {
054: return (GerConnectionDefinitionType) getXmlObject();
055: }
056:
057: void configure(DDBean resourceAdapter,
058: GerConnectionDefinitionType definition) {
059: this .resourceAdapter = resourceAdapter;
060: setXmlObject(definition);
061: //todo: handle unmatched interfaces below
062: instances = new ConnectionDefinitionInstance[definition
063: .getConnectiondefinitionInstanceArray().length];
064: DDBean[] beans = resourceAdapter
065: .getChildBean("outbound-resourceadapter/connection-definition");
066: DDBean match = null;
067: for (int i = 0; i < beans.length; i++) {
068: DDBean bean = beans[i];
069: if (bean.getText("connectionfactory-interface")[0]
070: .equals(definition.getConnectionfactoryInterface())) {
071: match = bean;
072: break;
073: }
074: }
075: for (int i = 0; i < instances.length; i++) {
076: GerConnectiondefinitionInstanceType gerInstance = definition
077: .getConnectiondefinitionInstanceArray()[i];
078: instances[i] = new ConnectionDefinitionInstance(match,
079: gerInstance);
080: }
081: }
082:
083: // ----------------------- JavaBean Properties for connection-definition ----------------------
084:
085: //todo: instead of String, make this an Enum type aware of the interfaces available in the J2EE DD
086: public String getConnectionFactoryInterface() {
087: return getConnectionDefinition()
088: .getConnectionfactoryInterface();
089: }
090:
091: public void setConnectionFactoryInterface(String iface) {
092: String old = getConnectionFactoryInterface();
093: getConnectionDefinition().setConnectionfactoryInterface(iface);
094: DDBean match = getConnectionDefinitionDDBean();
095: for (int i = 0; i < instances.length; i++) {
096: ConnectionDefinitionInstance instance = instances[i];
097: if (instance.getDDBean() != match) {
098: instance.configure(match, instance
099: .getConnectionInstance());
100: }
101: }
102: pcs
103: .firePropertyChange("connectionFactoryInterface", old,
104: iface);
105: }
106:
107: public ConnectionDefinitionInstance[] getConnectionInstances() {
108: return instances;
109: }
110:
111: public void setConnectionInstance(
112: ConnectionDefinitionInstance[] instances) {
113: ConnectionDefinitionInstance[] old = this .instances;
114: Set before = new HashSet();
115: for (int i = 0; i < old.length; i++) {
116: before.add(old[i]);
117: }
118: this .instances = instances;
119: // Handle current or new resource adapters
120: for (int i = 0; i < instances.length; i++) {
121: ConnectionDefinitionInstance instance = instances[i];
122: if (instance.getConnectionInstance() == null) {
123: instance.configure(getConnectionDefinitionDDBean(),
124: getConnectionDefinition()
125: .addNewConnectiondefinitionInstance());
126: } else {
127: before.remove(instance);
128: }
129: }
130: // Handle removed resource adapters
131: for (Iterator it = before.iterator(); it.hasNext();) {
132: ConnectionDefinitionInstance instance = (ConnectionDefinitionInstance) it
133: .next();
134: GerConnectiondefinitionInstanceType all[] = getConnectionDefinition()
135: .getConnectiondefinitionInstanceArray();
136: for (int i = 0; i < all.length; i++) {
137: if (all[i] == instance) {
138: getConnectionDefinition()
139: .removeConnectiondefinitionInstance(i);
140: break;
141: }
142: }
143: }
144: pcs.firePropertyChange("connectionInstance", old, instances);
145:
146: }
147:
148: // ----------------------- End of JavaBean Properties ----------------------
149:
150: /**
151: * Look up the J2EE connection definition corresponding to this one (based on connectionfactory-interface)
152: */
153: private DDBean getConnectionDefinitionDDBean() {
154: String iface = getConnectionFactoryInterface();
155: if (iface == null || iface.equals("")) {
156: return null;
157: }
158: DDBean list[] = resourceAdapter
159: .getChildBean("outbound-resourceadapter/connection-definition");
160: for (int i = 0; i < list.length; i++) {
161: DDBean bean = list[i];
162: String[] test = bean.getText("connectionfactory-interface");
163: if (test.length > 0) {
164: String myface = test[0];
165: if (myface.equals(iface)) {
166: return bean;
167: }
168: }
169: }
170: return null;
171: }
172:
173: protected SchemaTypeLoader getSchemaTypeLoader() {
174: return Connector15DCBRoot.SCHEMA_TYPE_LOADER;
175: }
176: }
|