KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedHashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.enterprise.deploy.model.DDBean JavaDoc;
29 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
30 import javax.enterprise.deploy.model.XpathListener JavaDoc;
31 import javax.enterprise.deploy.spi.DConfigBean JavaDoc;
32 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
33 import org.apache.geronimo.deployment.plugin.DConfigBeanSupport;
34 import org.apache.geronimo.deployment.service.jsr88.EnvironmentData;
35 import org.apache.geronimo.xbeans.geronimo.GerAdminobjectInstanceType;
36 import org.apache.geronimo.xbeans.geronimo.GerAdminobjectType;
37 import org.apache.geronimo.xbeans.geronimo.GerConnectorType;
38 import org.apache.geronimo.xbeans.geronimo.GerResourceadapterType;
39 import org.apache.xmlbeans.SchemaTypeLoader;
40 import org.apache.xmlbeans.XmlCursor;
41
42 /**
43  * Represents /connector in a Geronimo Connector deployment plan.
44  * Corresponds to /connector in the J2EE deployment plan.
45  *
46  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
47  */

48 public class ConnectorDCB extends DConfigBeanSupport {
49     private DDBean JavaDoc resourceAdapterDDBean;
50     private ResourceAdapter[] resourceAdapter = new ResourceAdapter[0];
51     private AdminObjectDCB[] adminobjects = new AdminObjectDCB[0];
52     private EnvironmentData environment;
53
54     public ConnectorDCB(DDBean JavaDoc connectorDDBean, final GerConnectorType connector) {
55         super(connectorDDBean, connector);
56         DDBean JavaDoc[] list = connectorDDBean.getChildBean("resourceadapter");
57         if(list.length > 0) {
58             resourceAdapterDDBean = list[0];
59         }
60         //todo: do something if resourceAdapterDDBean is null
61
loadExistingData(connector);
62
63         // Make sure we're told if /connector/resourceadapter is replaced!
64
connectorDDBean.addXpathListener("resourceadapter", new XpathListener JavaDoc() {
65             public void fireXpathEvent(XpathEvent JavaDoc event) {
66                 if(event.isRemoveEvent()) {
67                     resourceAdapterDDBean = null; //todo: all our info was just invalidated
68
} else if(event.isAddEvent()) {
69                     resourceAdapterDDBean = event.getBean(); //todo: reload defaults from DDBean
70
} else {
71                     System.out.println("Detected change to J2EE DD /connector/resourceadapter property "+event.getChangeEvent().getPropertyName());
72                 }
73             }
74         });
75     }
76
77     public String JavaDoc[] getXpaths() {
78         return getXPathsForJ2ee_1_4(new String JavaDoc[][]{{"resourceadapter","adminobject",},});
79     }
80
81     public DConfigBean JavaDoc getDConfigBean(DDBean JavaDoc bean) throws ConfigurationException JavaDoc {
82         if (getXpaths()[0].equals(bean.getXpath())) { // "adminobject"
83
String JavaDoc aoInterface = bean.getText("adminobject-interface")[0];
84             String JavaDoc aoClass = bean.getText("adminobject-class")[0];
85             // Check whether we've seen this one before
86
for(int i=0; i<adminobjects.length; i++) {
87                 if(adminobjects[i].getAdminObjectClass().equals(aoClass) &&
88                    adminobjects[i].getAdminObjectInterface().equals(aoInterface)) {
89                     return adminobjects[i];
90                 }
91             }
92             // Haven't seen it; create a new DConfigBean
93
GerAdminobjectType ao = getConnector().addNewAdminobject();
94             AdminObjectDCB dcb = new AdminObjectDCB(bean, ao);
95             AdminObjectDCB[] list = new AdminObjectDCB[adminobjects.length+1];
96             System.arraycopy(adminobjects, 0, list, 0, adminobjects.length);
97             list[adminobjects.length] = dcb;
98             return dcb;
99         } else {
100             throw new ConfigurationException JavaDoc("No DConfigBean matching DDBean "+bean.getXpath());
101         }
102     }
103
104     private void loadExistingData(GerConnectorType connector) {
105         //todo: Handle the import children
106
//todo: Handle the hidden-classes children
107
//todo: Handle the non-overridable-classes children
108
// Handle the dependency children
109
// ArtifactType[] deps = connector.getDependencyArray();
110
// if(deps != null && deps.length > 0) {
111
// dependency = new Artifact[deps.length];
112
// for (int i = 0; i < deps.length; i++) {
113
// dependency[i] = new Artifact(deps[i]);
114
// }
115
// }
116
// Handle the resource adapter children
117
GerResourceadapterType[] adapters = connector.getResourceadapterArray();
118         if(adapters == null || adapters.length == 0) {
119             // Make sure there's at least one connector/resourceadapter element
120
if(resourceAdapterDDBean != null) {
121                 resourceAdapter = new ResourceAdapter[1];
122                 resourceAdapter[0] = new ResourceAdapter(resourceAdapterDDBean, connector.addNewResourceadapter());
123             }
124         } else {
125             resourceAdapter = new ResourceAdapter[adapters.length];
126             for (int i = 0; i < adapters.length; i++) {
127                 GerResourceadapterType adapter = adapters[i];
128                 resourceAdapter[i] = new ResourceAdapter(resourceAdapterDDBean, adapter);
129             }
130         }
131         // Handle the AdminObject children
132
GerAdminobjectType[] admins = connector.getAdminobjectArray();
133         DDBean JavaDoc[] data = getDDBean().getChildBean(getXpaths()[0]);
134         List JavaDoc ddBeans = data == null ? Collections.EMPTY_LIST : new ArrayList JavaDoc(Arrays.asList(data)); // resourceadapter/adminobject
135

136         Map JavaDoc dcbs = new LinkedHashMap JavaDoc();
137         if(admins == null) {
138             adminobjects = new AdminObjectDCB[0];
139         } else {
140             // Match up each Geronimo adminobject with a ra.xml adminobject and create DConfigBeans accordingly
141
for (int i = 0; i < admins.length; i++) {
142                 GerAdminobjectType admin = admins[i];
143                 String JavaDoc aoClass = admin.getAdminobjectClass();
144                 String JavaDoc aoIface = admin.getAdminobjectInterface();
145                 AdminObjectDCB dcb = (AdminObjectDCB) dcbs.get("class "+aoClass+" iface "+aoIface);
146                 if(dcb != null) {
147                     // this is a second Geronimo adminobject block of the same type; there will not be a matching DDBean any more
148
// merge the adminobject-instance entries instead!!!
149
if(admin.getAdminobjectInstanceArray().length > 0) {
150                         GerAdminobjectType old = dcb.getAdminObject();
151                         GerAdminobjectInstanceType[] array = admin.getAdminobjectInstanceArray();
152                         int oldCount = dcb.getAdminObjectInstance().length;
153                         for (int j = 0; j < array.length; j++) {
154                             GerAdminobjectInstanceType instance = array[j];
155                             XmlCursor source = instance.newCursor();
156                             XmlCursor dest = old.newCursor();
157                             dest.toEndToken();
158                             if(!source.moveXml(dest)) {
159                                 throw new RuntimeException JavaDoc("Unable to move admin object instance");
160                             }
161                             source.dispose();
162                             dest.dispose();
163                             dcb.addAdminObjectInstance(old.getAdminobjectInstanceArray(oldCount+j));
164                         }
165                     }
166                     continue;
167                 }
168                 DDBean JavaDoc target = null;
169                 for (int j = 0; j < ddBeans.size(); j++) {
170                     DDBean JavaDoc ddBean = (DDBean JavaDoc) ddBeans.get(j);
171                     String JavaDoc ddClass = ddBean.getText("adminobject-class")[0];
172                     String JavaDoc ddIface = ddBean.getText("adminobject-interface")[0];
173                     if(ddClass.equals(aoClass) && ddIface.equals(aoIface)) {
174                         target = ddBean;
175                         ddBeans.remove(j);
176                         break;
177                     }
178                 }
179                 if(target == null) {
180                     System.out.println("Geronimo connector deployment plan has admin object with interface '"+aoIface+"' and class '"+aoClass+"' but the ra.xml does not have a matching adminobject declared. Deleting this adminobject from the Geronimo plan.");
181                     continue;
182                 }
183                 dcb = new AdminObjectDCB(target, admin);
184                 dcbs.put("class "+aoClass+" iface "+aoIface, dcb);
185             }
186         }
187         // There are some admin object types in ra.xml with no matching instances; create DConfigBeans for those
188
for (int i = 0; i < ddBeans.size(); i++) {
189             DDBean JavaDoc ddBean = (DDBean JavaDoc) ddBeans.get(i);
190             String JavaDoc ddClass = ddBean.getText("adminobject-class")[0];
191             String JavaDoc ddIface = ddBean.getText("adminobject-interface")[0];
192             GerAdminobjectType admin = connector.addNewAdminobject();
193             dcbs.put("class "+ddClass+" iface "+ddIface, new AdminObjectDCB(ddBean, admin));
194         }
195         List JavaDoc adminResults = new ArrayList JavaDoc();
196         for (Iterator JavaDoc it = dcbs.keySet().iterator(); it.hasNext();) {
197             String JavaDoc key = (String JavaDoc) it.next();
198             DConfigBean JavaDoc value = (DConfigBean JavaDoc) dcbs.get(key);
199             adminResults.add(value);
200         }
201         adminobjects = (AdminObjectDCB[]) adminResults.toArray(new AdminObjectDCB[adminResults.size()]);
202
203         //todo: Handle the GBean children
204
}
205
206     GerConnectorType getConnector() {
207         return (GerConnectorType) getXmlObject();
208     }
209
210
211     // ----------------------- JavaBean Properties for /connector ----------------------
212

213     //todo: the following child elements
214
// gbean*
215

216     public EnvironmentData getEnvironment() {
217         return environment;
218     }
219
220     public void setEnvironment(EnvironmentData environment) {
221         EnvironmentData old = this.environment;
222         this.environment = environment;
223         if((old == null && environment == null) || (old != null&& old == environment)) {
224             return;
225         }
226         if(old != null) {
227             getConnector().unsetEnvironment();
228         }
229         if(environment != null) {
230             environment.configure(getConnector().addNewEnvironment());
231         }
232         pcs.firePropertyChange("environment", old, environment);
233     }
234
235     public ResourceAdapter[] getResourceAdapter() {
236         return resourceAdapter;
237     }
238
239     public void setResourceAdapter(ResourceAdapter[] resourceAdapter) {
240         ResourceAdapter[] old = this.resourceAdapter;
241         Set JavaDoc before = new HashSet JavaDoc();
242         for (int i = 0; i < old.length; i++) {
243             before.add(old[i]);
244         }
245         this.resourceAdapter = resourceAdapter;
246         // Handle current or new resource adapters
247
for (int i = 0; i < resourceAdapter.length; i++) {
248             ResourceAdapter adapter = resourceAdapter[i];
249             if(adapter.getResourceAdapter() == null) {
250                 adapter.configure(resourceAdapterDDBean, getConnector().addNewResourceadapter());
251             } else {
252                 before.remove(adapter);
253             }
254         }
255         // Handle removed resource adapters
256
for (Iterator JavaDoc it = before.iterator(); it.hasNext();) {
257             ResourceAdapter adapter = (ResourceAdapter) it.next();
258             GerResourceadapterType all[] = getConnector().getResourceadapterArray();
259             for (int i = 0; i < all.length; i++) {
260                 if(all[i] == adapter) {
261                     getConnector().removeResourceadapter(i);
262                     break;
263                 }
264             }
265         }
266         pcs.firePropertyChange("resourceAdapter", old, resourceAdapter);
267     }
268
269     public ResourceAdapter getResourceAdapter(int index) {
270         return resourceAdapter[index];
271     }
272
273     public void setResourceAdapter(int index, ResourceAdapter ra) {
274         ResourceAdapter[] old = this.resourceAdapter;
275         resourceAdapter[index] = ra;
276         if(ra.getResourceAdapter() == null) {
277             ra.configure(resourceAdapterDDBean, getConnector().addNewResourceadapter());
278         }
279         pcs.firePropertyChange("resourceAdapter", old, resourceAdapter);
280     }
281
282
283     // ----------------------- End of JavaBean Properties ----------------------
284

285
286
287     protected SchemaTypeLoader getSchemaTypeLoader() {
288         return Connector15DCBRoot.SCHEMA_TYPE_LOADER;
289     }
290 }
291
Popular Tags