KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.enterprise.deploy.model.DDBean JavaDoc;
23 import org.apache.geronimo.deployment.plugin.XmlBeanSupport;
24 import org.apache.geronimo.xbeans.geronimo.GerResourceadapterType;
25 import org.apache.geronimo.xbeans.geronimo.GerOutboundResourceadapterType;
26 import org.apache.geronimo.xbeans.geronimo.GerConnectionDefinitionType;
27 import org.apache.xmlbeans.SchemaTypeLoader;
28
29 /**
30  * Represents /connector/resourceadapter in the Geronimo Connector deployment plan.
31  * Note: is not a DConfigBean, because there may be more than one ResourceAdapter
32  * in the Geronimo plan per ResourceAdapter in the J2EE deployment descriptor.
33  *
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  */

36 public class ResourceAdapter extends XmlBeanSupport {
37     private DDBean JavaDoc resourceAdapter;
38     private ConnectionDefinition[] instances = new ConnectionDefinition[0];
39     private ResourceAdapterInstance resourceAdapterInstance;
40
41     public ResourceAdapter() {
42         super(null);
43     }
44
45     public ResourceAdapter(DDBean JavaDoc resourceAdapter, GerResourceadapterType ra) {
46         super(null);
47         configure(resourceAdapter, ra);
48     }
49
50     protected GerResourceadapterType getResourceAdapter() {
51         return (GerResourceadapterType)getXmlObject();
52     }
53
54     void configure(DDBean JavaDoc resourceAdapter, GerResourceadapterType ra) {
55         this.resourceAdapter = resourceAdapter;
56         setXmlObject(ra);
57         if(ra.isSetResourceadapterInstance()) {
58             resourceAdapterInstance = new ResourceAdapterInstance(resourceAdapter, ra.getResourceadapterInstance());
59         } else {
60             resourceAdapterInstance = null;
61         }
62         if(ra.isSetOutboundResourceadapter()) {
63             DDBean JavaDoc[] test = resourceAdapter.getChildBean("outbound-resourceadapter");
64             if(test != null && test.length > 0) {
65                 GerOutboundResourceadapterType outbound = ra.getOutboundResourceadapter();
66                 GerConnectionDefinitionType[] defs = outbound.getConnectionDefinitionArray();
67                 if(defs != null) {
68                     instances = new ConnectionDefinition[defs.length];
69                     for (int i = 0; i < defs.length; i++) {
70                         GerConnectionDefinitionType def = defs[i];
71                         instances[i] = new ConnectionDefinition(resourceAdapter, def);
72                     }
73                 }
74             } else {
75                 //todo: clean up the Geronimo deployment info since there's no J2EE outbound RA
76
}
77         }
78     }
79
80     // ----------------------- JavaBean Properties for /connector/resourceadapter ----------------------
81

82     public ConnectionDefinition[] getConnectionDefinition() {
83         return instances;
84     }
85
86     public ConnectionDefinition getConnectionDefinition(int index) {
87         return instances[index];
88     }
89
90     public void setConnectionDefinition(ConnectionDefinition[] definitions) {
91         ConnectionDefinition[] old = getConnectionDefinition();
92
93         if(definitions != null && definitions.length > 0) {
94             if(!getResourceAdapter().isSetOutboundResourceadapter()) {
95                 getResourceAdapter().addNewOutboundResourceadapter();
96             }
97         } else {
98             if(getResourceAdapter().isSetOutboundResourceadapter()) {
99                 getResourceAdapter().unsetOutboundResourceadapter();
100             }
101         }
102         Set JavaDoc before = new HashSet JavaDoc();
103         for (int i = 0; i < old.length; i++) {
104             before.add(old[i]);
105         }
106         instances = definitions;
107         // Handle current or new definitions
108
for (int i = 0; i < definitions.length; i++) {
109             ConnectionDefinition definition = definitions[i];
110             if(definition.getConnectionDefinition() == null) {
111                 definition.configure(resourceAdapter, getResourceAdapter().getOutboundResourceadapter().addNewConnectionDefinition());
112             } else {
113                 before.remove(definition);
114             }
115         }
116         // Handle removed definitions
117
for (Iterator JavaDoc it = before.iterator(); it.hasNext();) {
118             ConnectionDefinition definition = (ConnectionDefinition) it.next();
119             GerConnectionDefinitionType all[] = getResourceAdapter().isSetOutboundResourceadapter() ? getResourceAdapter().getOutboundResourceadapter().getConnectionDefinitionArray() : new GerConnectionDefinitionType[0];
120             for (int i = 0; i < all.length; i++) {
121                 if(all[i] == definition) {
122                     getResourceAdapter().getOutboundResourceadapter().removeConnectionDefinition(i);
123                     break;
124                 }
125             }
126         }
127         pcs.firePropertyChange("connectionDefinition", old, instances);
128     }
129
130     public void setConnectionDefinition(int index, ConnectionDefinition definition) {
131         ConnectionDefinition[] old = instances;
132         instances[index] = definition;
133         if(definition.getConnectionDefinition() == null) {
134             definition.configure(resourceAdapter, getResourceAdapter().getOutboundResourceadapter().addNewConnectionDefinition());
135         }
136         pcs.firePropertyChange("connectionDefinition", old, instances);
137     }
138
139     public ResourceAdapterInstance getResourceAdapterInstance() {
140         return resourceAdapterInstance;
141     }
142
143     public void setResourceAdapterInstance(ResourceAdapterInstance resourceAdapterInstance) {
144         ResourceAdapterInstance old = this.resourceAdapterInstance;
145         this.resourceAdapterInstance = resourceAdapterInstance;
146         if(resourceAdapterInstance.getResourceAdapterInstance() == null) {
147             if(getResourceAdapter().isSetResourceadapterInstance()) {
148                 resourceAdapterInstance.configure(resourceAdapter, getResourceAdapter().getResourceadapterInstance());
149             } else {
150                 resourceAdapterInstance.configure(resourceAdapter, getResourceAdapter().addNewResourceadapterInstance());
151             }
152         }
153         pcs.firePropertyChange("resourceAdapterInstance", old, instances);
154     }
155
156     // ----------------------- End of JavaBean Properties ----------------------
157

158     protected SchemaTypeLoader getSchemaTypeLoader() {
159         return Connector15DCBRoot.SCHEMA_TYPE_LOADER;
160     }
161 }
162
Popular Tags