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