KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > deployment > ENCHelper


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
18 package org.apache.geronimo.naming.deployment;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import javax.enterprise.deploy.model.DDBean JavaDoc;
23 import javax.enterprise.deploy.spi.DConfigBean JavaDoc;
24 import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException JavaDoc;
25 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
26
27 import org.apache.geronimo.xbeans.geronimo.naming.GerEjbLocalRefType;
28 import org.apache.geronimo.xbeans.geronimo.naming.GerEjbRefType;
29 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceEnvRefType;
30 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceRefType;
31 import org.apache.xmlbeans.SchemaTypeLoader;
32 import org.apache.xmlbeans.XmlBeans;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class ENCHelper {
38     static final SchemaTypeLoader SCHEMA_TYPE_LOADER = XmlBeans.typeLoaderUnion(new SchemaTypeLoader[]{
39         XmlBeans.typeLoaderForClassLoader(org.apache.geronimo.xbeans.j2ee.String.class.getClassLoader()),
40     });
41
42
43     public static final String JavaDoc[][] ENC_XPATHS = {
44         {"ejb-ref"},
45         {"ejb-local-ref"},
46         {"resource-env-ref"},
47         {"resource-ref"},
48     };
49
50     public static final String JavaDoc[][] NAME_XPATHS = {
51         {"ejb-ref-name"},
52         {"ejb-ref-name"},
53         {"resource-env-ref-name"},
54         {"res-ref-name"}
55     };
56
57
58     private final String JavaDoc[] xpaths;
59     private final String JavaDoc[] namePaths;
60
61     private RemoteRefDConfigBean[] ejbRefs;
62
63     private LocalRefDConfigBean[] ejbLocalRefs;
64     private LocalRefDConfigBean[] resourceEnvRefs;
65     private LocalRefDConfigBean[] resourceRefs;
66
67     public ENCHelper(DDBean JavaDoc ddBean, XmlEnvRefs envRefs, String JavaDoc[] xpaths, String JavaDoc[] namePaths) {
68         this.xpaths = xpaths;
69         this.namePaths = namePaths;
70         DDBean JavaDoc[] ddEjbRefs = ddBean.getChildBean(xpaths[0]);
71         if (ddEjbRefs == null) {
72             ddEjbRefs = new DDBean JavaDoc[0];
73         }
74         ejbRefs = new RemoteRefDConfigBean[ddEjbRefs.length];
75         GerEjbRefType[] xmlEjbRefs = envRefs.getEjbRefs();
76         Map JavaDoc ejbRefMap = new HashMap JavaDoc();
77         for (int i = 0; i < xmlEjbRefs.length; i++) {
78             GerEjbRefType refAdapter = xmlEjbRefs[i];
79             ejbRefMap.put(refAdapter.getRefName(), refAdapter);
80 // envRefs.removeEjbRef(0);
81
}
82 // for (int i = 0; i < ddEjbRefs.length; i++) {
83
// DDBean ddRef = ddEjbRefs[i];
84
// String name = ddRef.getText(namePaths[0])[0];
85
// GerRemoteRefType refAdapter;
86
// if (ejbRefMap.get(name) == null) {
87
// refAdapter = envRefs.addNewEjbRef();
88
// refAdapter.setRefName(name);
89
// } else {
90
// refAdapter = (GerRemoteRefType) ejbRefMap.get(name);
91
// envRefs.setEjbRef(i, refAdapter);
92
// }
93
// ejbRefs[i] = new RemoteRefDConfigBean(ddRef, refAdapter, namePaths[0]);
94
// }
95
//
96
// DDBean[] ddEjbLocalRefs = ddBean.getChildBean(xpaths[1]);
97
// if (ddEjbLocalRefs == null) {
98
// ddEjbLocalRefs = new DDBean[0];
99
// }
100
// ejbLocalRefs = new LocalRefDConfigBean[ddEjbLocalRefs.length];
101
// GerLocalRefType[] xmlEjbLocalRefs = envRefs.getEjbLocalRefs();
102
// Map ejbLocalRefMap = new HashMap();
103
// for (int i = 0; i < xmlEjbLocalRefs.length; i++) {
104
// GerLocalRefType refAdapter = xmlEjbLocalRefs[i];
105
// ejbLocalRefMap.put(refAdapter.getRefName(), refAdapter);
106
// envRefs.removeEjbLocalRef(0);
107
// }
108
// for (int i = 0; i < ddEjbLocalRefs.length; i++) {
109
// DDBean ddRef = ddEjbLocalRefs[i];
110
// String name = ddRef.getText(namePaths[1])[0];
111
// GerLocalRefType refAdapter;
112
// if (ejbLocalRefMap.get(name) == null) {
113
// refAdapter = envRefs.addNewEjbLocalRef();
114
// refAdapter.setRefName(name);
115
// } else {
116
// refAdapter = (GerLocalRefType) ejbLocalRefMap.get(name);
117
// envRefs.setEjbLocalRef(i, refAdapter);
118
// }
119
// ejbLocalRefs[i] = new LocalRefDConfigBean(ddRef, refAdapter, namePaths[1]);
120
// }
121
//
122
//
123
// DDBean[] ddResourceEnvRefs = ddBean.getChildBean(xpaths[2]);
124
// if (ddResourceEnvRefs == null) {
125
// ddResourceEnvRefs = new DDBean[0];
126
// }
127
// resourceEnvRefs = new LocalRefDConfigBean[ddResourceEnvRefs.length];
128
// GerLocalRefType[] xmlResourceEnvRefs = envRefs.getResourceEnvRefs();
129
// Map resourceEnvRefMap = new HashMap();
130
// for (int i = 0; i < xmlResourceEnvRefs.length; i++) {
131
// GerLocalRefType refAdapter = xmlResourceEnvRefs[i];
132
// resourceEnvRefMap.put(refAdapter.getRefName(), refAdapter);
133
// envRefs.removeResourceEnvRef(0);
134
// }
135
// for (int i = 0; i < ddResourceEnvRefs.length; i++) {
136
// DDBean ddRef = ddResourceEnvRefs[i];
137
// String name = ddRef.getText(namePaths[2])[0];
138
// GerLocalRefType refAdapter;
139
// if (resourceEnvRefMap.get(name) == null) {
140
// refAdapter = envRefs.addNewResourceEnvRef();
141
// refAdapter.setRefName(name);
142
// } else {
143
// refAdapter = (GerLocalRefType) resourceEnvRefMap.get(name);
144
// envRefs.setResourceEnvRef(i, refAdapter);
145
// }
146
// resourceEnvRefs[i] = new LocalRefDConfigBean(ddRef, refAdapter, namePaths[2]);
147
// }
148
//
149
// DDBean[] ddResourceRefs = ddBean.getChildBean(xpaths[3]);
150
// if (ddResourceRefs == null) {
151
// ddResourceRefs = new DDBean[0];
152
// }
153
// resourceRefs = new LocalRefDConfigBean[ddResourceRefs.length];
154
// GerLocalRefType[] xmlResourceRefs = envRefs.getResourceRefs();
155
// Map resourceRefMap = new HashMap();
156
// for (int i = 0; i < xmlResourceRefs.length; i++) {
157
// GerLocalRefType refAdapter = xmlResourceRefs[i];
158
// resourceRefMap.put(refAdapter.getRefName(), refAdapter);
159
// envRefs.removeResourceRef(0);
160
// }
161
// for (int i = 0; i < ddResourceRefs.length; i++) {
162
// DDBean ddRef = ddResourceRefs[i];
163
// String name = ddRef.getText(namePaths[3])[0];
164
// GerLocalRefType refAdapter;
165
// if (resourceRefMap.get(name) == null) {
166
// refAdapter = envRefs.addNewResourceRef();
167
// refAdapter.setRefName(name);
168
// } else {
169
// refAdapter = (GerLocalRefType) resourceRefMap.get(name);
170
// envRefs.setResourceRef(i, refAdapter);
171
// }
172
// resourceRefs[i] = new LocalRefDConfigBean(ddRef, refAdapter, namePaths[3]);
173
// }
174

175     }
176
177     public DConfigBean JavaDoc getDConfigBean(DDBean JavaDoc ddBean) throws ConfigurationException JavaDoc {
178         String JavaDoc xpath = ddBean.getXpath();
179         if (xpath.equals(xpaths[0])) {
180             String JavaDoc name = ddBean.getText(namePaths[0])[0];
181             for (int i = 0; i < ejbRefs.length; i++) {
182                 RemoteRefDConfigBean ejbRef = ejbRefs[i];
183                 if (ejbRef.getRefName().equals(name)) {
184                     return ejbRef;
185                 }
186             }
187             throw new ConfigurationException JavaDoc("no such ejb-ref-name" + name);
188         } else if (xpath.equals(xpaths[1])) {
189             String JavaDoc name = ddBean.getText(namePaths[1])[0];
190             for (int i = 0; i < ejbLocalRefs.length; i++) {
191                 LocalRefDConfigBean ejbLocalRef = ejbLocalRefs[i];
192                 if (ejbLocalRef.getRefName().equals(name)) {
193                     return ejbLocalRef;
194                 }
195             }
196             throw new ConfigurationException JavaDoc("no such ejb-ref-name" + name);
197         } else if (xpath.equals(xpaths[2])) {
198             String JavaDoc name = ddBean.getText(namePaths[2])[0];
199             for (int i = 0; i < resourceEnvRefs.length; i++) {
200                 LocalRefDConfigBean resourceEnvRef = resourceEnvRefs[i];
201                 if (resourceEnvRef.getRefName().equals(name)) {
202                     return resourceEnvRef;
203                 }
204             }
205             throw new ConfigurationException JavaDoc("no such ejb-ref-name" + name);
206         } else if (xpath.equals(xpaths[3])) {
207             String JavaDoc name = ddBean.getText(namePaths[3])[0];
208             for (int i = 0; i < resourceRefs.length; i++) {
209                 LocalRefDConfigBean resourceRef = resourceRefs[i];
210                 if (resourceRef.getRefName().equals(name)) {
211                     return resourceRef;
212                 }
213             }
214             throw new ConfigurationException JavaDoc("no such res-ref-name" + name);
215         } else {
216             throw new ConfigurationException JavaDoc("Unrecognized XPath: " + ddBean.getXpath());
217         }
218     }
219
220     public void removeDConfigBean(DConfigBean JavaDoc dcBean) throws BeanNotFoundException JavaDoc {
221 // DDBean ddBean = dcBean.getDDBean();
222
// String xpath = ddBean.getXpath();
223
// String name = ddBean.getText();
224
/*
225               if (xpath.endsWith("ejb-ref/ejb-ref-name")) {
226                   if (ejbRefs.remove(name) == null) {
227                       throw new BeanNotFoundException("No DConfigBean found with name: " + name);
228                   }
229               } else if (xpath.endsWith("ejb-local-ref/ejb-ref-name")) {
230                   if (ejbLocalRefs.remove(name) == null) {
231                       throw new BeanNotFoundException("No DConfigBean found with name: " + name);
232                   }
233               } else if (xpath.endsWith("service-ref/service-ref-name")) {
234                   if (serviceRefs.remove(name) == null) {
235                       throw new BeanNotFoundException("No DConfigBean found with name: " + name);
236                   }
237               } else if (xpath.endsWith("resource-ref/res-ref-name")) {
238                   if (resourceRefs.remove(name) == null) {
239                       throw new BeanNotFoundException("No DConfigBean found with name: " + name);
240                   }
241               } else {
242                   throw new BeanNotFoundException("Unrecognized XPath: " + xpath);
243               }
244               */

245     }
246
247
248     public static class XmlEnvRefs {
249         private final GerEjbRefType[] ejbRefs;
250         private final GerEjbLocalRefType[] ejbLocalRefs;
251         private final GerResourceRefType[] resourceRefs;
252         private final GerResourceEnvRefType[] resourceEnvRefs;
253
254         public XmlEnvRefs(GerEjbRefType[] ejbRefs, GerEjbLocalRefType[] ejbLocalRefs, GerResourceRefType[] resourceRefs, GerResourceEnvRefType[] resourceEnvRefs) {
255             this.ejbRefs = ejbRefs;
256             this.ejbLocalRefs = ejbLocalRefs;
257             this.resourceRefs = resourceRefs;
258             this.resourceEnvRefs = resourceEnvRefs;
259         }
260
261         public GerEjbRefType[] getEjbRefs() {
262             return ejbRefs;
263         }
264
265         public GerEjbLocalRefType[] getEjbLocalRefs() {
266             return ejbLocalRefs;
267         }
268
269         public GerResourceRefType[] getResourceRefs() {
270             return resourceRefs;
271         }
272
273         public GerResourceEnvRefType[] getResourceEnvRefs() {
274             return resourceEnvRefs;
275         }
276
277     }
278 }
279
Popular Tags