KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Set JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import javax.enterprise.deploy.model.DDBean JavaDoc;
25 import org.apache.geronimo.deployment.plugin.XmlBeanSupport;
26 import org.apache.geronimo.xbeans.geronimo.GerConnectionDefinitionType;
27 import org.apache.geronimo.xbeans.geronimo.GerConnectiondefinitionInstanceType;
28 import org.apache.xmlbeans.SchemaTypeLoader;
29
30 /**
31  * Represents /connector/resourceadapter/outbound-resourceadapter/connection-definition
32  * in the Geronimo Connector deployment plan. A Geronimo connection definition
33  * corresponds to a ra.xml connection definition (though there may be several
34  * Geronimo CDs for each ra.xml CD so this cannot be a DConfigBean [which would
35  * require a 1:1 mapping]). Each Geronimo connection definition may have one
36  * or more instances with different config property settings, etc.
37  *
38  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
39  */

40 public class ConnectionDefinition extends XmlBeanSupport {
41     private DDBean JavaDoc resourceAdapter;
42     private ConnectionDefinitionInstance[] instances = new ConnectionDefinitionInstance[0];
43
44     public ConnectionDefinition() {
45         super(null);
46     }
47
48     public ConnectionDefinition(DDBean JavaDoc resourceAdapter, GerConnectionDefinitionType definition) {
49         super(null);
50         configure(resourceAdapter, definition);
51     }
52
53     protected GerConnectionDefinitionType getConnectionDefinition() {
54         return (GerConnectionDefinitionType) getXmlObject();
55     }
56
57     void configure(DDBean JavaDoc resourceAdapter, GerConnectionDefinitionType definition) {
58         this.resourceAdapter = resourceAdapter;
59         setXmlObject(definition);
60         //todo: handle unmatched interfaces below
61
instances = new ConnectionDefinitionInstance[definition.getConnectiondefinitionInstanceArray().length];
62         DDBean JavaDoc[] beans = resourceAdapter.getChildBean("outbound-resourceadapter/connection-definition");
63         DDBean JavaDoc match = null;
64         for (int i = 0; i < beans.length; i++) {
65             DDBean JavaDoc bean = beans[i];
66             if(bean.getText("connectionfactory-interface")[0].equals(definition.getConnectionfactoryInterface())) {
67                 match = bean;
68                 break;
69             }
70         }
71         for (int i = 0; i < instances.length; i++) {
72             GerConnectiondefinitionInstanceType gerInstance = definition.getConnectiondefinitionInstanceArray()[i];
73             instances[i] = new ConnectionDefinitionInstance(match, gerInstance);
74         }
75     }
76
77     // ----------------------- JavaBean Properties for connection-definition ----------------------
78

79     //todo: instead of String, make this an Enum type aware of the interfaces available in the J2EE DD
80
public String JavaDoc getConnectionFactoryInterface() {
81         return getConnectionDefinition().getConnectionfactoryInterface();
82     }
83
84     public void setConnectionFactoryInterface(String JavaDoc iface) {
85         String JavaDoc old = getConnectionFactoryInterface();
86         getConnectionDefinition().setConnectionfactoryInterface(iface);
87         DDBean JavaDoc match = getConnectionDefinitionDDBean();
88         for (int i = 0; i < instances.length; i++) {
89             ConnectionDefinitionInstance instance = instances[i];
90             if(instance.getDDBean() != match) {
91                 instance.configure(match, instance.getConnectionInstance());
92             }
93         }
94         pcs.firePropertyChange("connectionFactoryInterface", old, iface);
95     }
96
97     public ConnectionDefinitionInstance[] getConnectionInstances() {
98         return instances;
99     }
100
101     public void setConnectionInstance(ConnectionDefinitionInstance[] instances) {
102         ConnectionDefinitionInstance[] old = this.instances;
103         Set JavaDoc before = new HashSet JavaDoc();
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 JavaDoc 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 JavaDoc getConnectionDefinitionDDBean() {
139         String JavaDoc iface = getConnectionFactoryInterface();
140         if(iface == null || iface.equals("")) {
141             return null;
142         }
143         DDBean JavaDoc list[] = resourceAdapter.getChildBean("outbound-resourceadapter/connection-definition");
144         for (int i = 0; i < list.length; i++) {
145             DDBean JavaDoc bean = list[i];
146             String JavaDoc[] test = bean.getText("connectionfactory-interface");
147             if(test.length > 0) {
148                 String JavaDoc 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 }
161
Popular Tags