1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.naming.deployment;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import javax.enterprise.deploy.model.DDBean;
24 import javax.enterprise.deploy.spi.DConfigBean;
25 import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException;
26 import javax.enterprise.deploy.spi.exceptions.ConfigurationException;
27
28 import org.apache.geronimo.xbeans.geronimo.naming.GerEjbLocalRefType;
29 import org.apache.geronimo.xbeans.geronimo.naming.GerEjbRefType;
30 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceEnvRefType;
31 import org.apache.geronimo.xbeans.geronimo.naming.GerResourceRefType;
32 import org.apache.xmlbeans.SchemaTypeLoader;
33 import org.apache.xmlbeans.XmlBeans;
34
35 /**
36 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
37 */
38 public class ENCHelper {
39 static final SchemaTypeLoader SCHEMA_TYPE_LOADER = XmlBeans.typeLoaderUnion(new SchemaTypeLoader[]{
40 XmlBeans.typeLoaderForClassLoader(org.apache.geronimo.xbeans.j2ee.String.class.getClassLoader()),
41 });
42
43
44 public static final String[][] ENC_XPATHS = {
45 {"ejb-ref"},
46 {"ejb-local-ref"},
47 {"resource-env-ref"},
48 {"resource-ref"},
49 };
50
51 public static final String[][] NAME_XPATHS = {
52 {"ejb-ref-name"},
53 {"ejb-ref-name"},
54 {"resource-env-ref-name"},
55 {"res-ref-name"}
56 };
57
58
59 private final String[] xpaths;
60 private final String[] namePaths;
61
62 private RemoteRefDConfigBean[] ejbRefs;
63
64 private LocalRefDConfigBean[] ejbLocalRefs;
65 private LocalRefDConfigBean[] resourceEnvRefs;
66 private LocalRefDConfigBean[] resourceRefs;
67
68 public ENCHelper(DDBean ddBean, XmlEnvRefs envRefs, String[] xpaths, String[] namePaths) {
69 this.xpaths = xpaths;
70 this.namePaths = namePaths;
71 DDBean[] ddEjbRefs = ddBean.getChildBean(xpaths[0]);
72 if (ddEjbRefs == null) {
73 ddEjbRefs = new DDBean[0];
74 }
75 ejbRefs = new RemoteRefDConfigBean[ddEjbRefs.length];
76 GerEjbRefType[] xmlEjbRefs = envRefs.getEjbRefs();
77 Map ejbRefMap = new HashMap();
78 for (int i = 0; i < xmlEjbRefs.length; i++) {
79 GerEjbRefType refAdapter = xmlEjbRefs[i];
80 ejbRefMap.put(refAdapter.getRefName(), refAdapter);
81
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 }
177
178 public DConfigBean getDConfigBean(DDBean ddBean) throws ConfigurationException {
179 String xpath = ddBean.getXpath();
180 if (xpath.equals(xpaths[0])) {
181 String name = ddBean.getText(namePaths[0])[0];
182 for (int i = 0; i < ejbRefs.length; i++) {
183 RemoteRefDConfigBean ejbRef = ejbRefs[i];
184 if (ejbRef.getRefName().equals(name)) {
185 return ejbRef;
186 }
187 }
188 throw new ConfigurationException("no such ejb-ref-name" + name);
189 } else if (xpath.equals(xpaths[1])) {
190 String name = ddBean.getText(namePaths[1])[0];
191 for (int i = 0; i < ejbLocalRefs.length; i++) {
192 LocalRefDConfigBean ejbLocalRef = ejbLocalRefs[i];
193 if (ejbLocalRef.getRefName().equals(name)) {
194 return ejbLocalRef;
195 }
196 }
197 throw new ConfigurationException("no such ejb-ref-name" + name);
198 } else if (xpath.equals(xpaths[2])) {
199 String name = ddBean.getText(namePaths[2])[0];
200 for (int i = 0; i < resourceEnvRefs.length; i++) {
201 LocalRefDConfigBean resourceEnvRef = resourceEnvRefs[i];
202 if (resourceEnvRef.getRefName().equals(name)) {
203 return resourceEnvRef;
204 }
205 }
206 throw new ConfigurationException("no such ejb-ref-name" + name);
207 } else if (xpath.equals(xpaths[3])) {
208 String name = ddBean.getText(namePaths[3])[0];
209 for (int i = 0; i < resourceRefs.length; i++) {
210 LocalRefDConfigBean resourceRef = resourceRefs[i];
211 if (resourceRef.getRefName().equals(name)) {
212 return resourceRef;
213 }
214 }
215 throw new ConfigurationException("no such res-ref-name" + name);
216 } else {
217 throw new ConfigurationException("Unrecognized XPath: " + ddBean.getXpath());
218 }
219 }
220
221 public void removeDConfigBean(DConfigBean dcBean) throws BeanNotFoundException {
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 }
247
248
249 public static class XmlEnvRefs {
250 private final GerEjbRefType[] ejbRefs;
251 private final GerEjbLocalRefType[] ejbLocalRefs;
252 private final GerResourceRefType[] resourceRefs;
253 private final GerResourceEnvRefType[] resourceEnvRefs;
254
255 public XmlEnvRefs(GerEjbRefType[] ejbRefs, GerEjbLocalRefType[] ejbLocalRefs, GerResourceRefType[] resourceRefs, GerResourceEnvRefType[] resourceEnvRefs) {
256 this.ejbRefs = ejbRefs;
257 this.ejbLocalRefs = ejbLocalRefs;
258 this.resourceRefs = resourceRefs;
259 this.resourceEnvRefs = resourceEnvRefs;
260 }
261
262 public GerEjbRefType[] getEjbRefs() {
263 return ejbRefs;
264 }
265
266 public GerEjbLocalRefType[] getEjbLocalRefs() {
267 return ejbLocalRefs;
268 }
269
270 public GerResourceRefType[] getResourceRefs() {
271 return resourceRefs;
272 }
273
274 public GerResourceEnvRefType[] getResourceEnvRefs() {
275 return resourceEnvRefs;
276 }
277
278 }
279 }