View Javadoc

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  //            envRefs.removeEjbRef(0);
82          }
83  //        for (int i = 0; i < ddEjbRefs.length; i++) {
84  //            DDBean ddRef = ddEjbRefs[i];
85  //            String name = ddRef.getText(namePaths[0])[0];
86  //            GerRemoteRefType refAdapter;
87  //            if (ejbRefMap.get(name) == null) {
88  //                refAdapter = envRefs.addNewEjbRef();
89  //                refAdapter.setRefName(name);
90  //            } else {
91  //                refAdapter = (GerRemoteRefType) ejbRefMap.get(name);
92  //                envRefs.setEjbRef(i, refAdapter);
93  //            }
94  //            ejbRefs[i] = new RemoteRefDConfigBean(ddRef, refAdapter, namePaths[0]);
95  //        }
96  //
97  //        DDBean[] ddEjbLocalRefs = ddBean.getChildBean(xpaths[1]);
98  //        if (ddEjbLocalRefs == null) {
99  //            ddEjbLocalRefs = new DDBean[0];
100 //        }
101 //        ejbLocalRefs = new LocalRefDConfigBean[ddEjbLocalRefs.length];
102 //        GerLocalRefType[] xmlEjbLocalRefs = envRefs.getEjbLocalRefs();
103 //        Map ejbLocalRefMap = new HashMap();
104 //        for (int i = 0; i < xmlEjbLocalRefs.length; i++) {
105 //            GerLocalRefType refAdapter = xmlEjbLocalRefs[i];
106 //            ejbLocalRefMap.put(refAdapter.getRefName(), refAdapter);
107 //            envRefs.removeEjbLocalRef(0);
108 //        }
109 //        for (int i = 0; i < ddEjbLocalRefs.length; i++) {
110 //            DDBean ddRef = ddEjbLocalRefs[i];
111 //            String name = ddRef.getText(namePaths[1])[0];
112 //            GerLocalRefType refAdapter;
113 //            if (ejbLocalRefMap.get(name) == null) {
114 //                refAdapter = envRefs.addNewEjbLocalRef();
115 //                refAdapter.setRefName(name);
116 //            } else {
117 //                refAdapter = (GerLocalRefType) ejbLocalRefMap.get(name);
118 //                envRefs.setEjbLocalRef(i, refAdapter);
119 //            }
120 //            ejbLocalRefs[i] = new LocalRefDConfigBean(ddRef, refAdapter, namePaths[1]);
121 //        }
122 //
123 //
124 //        DDBean[] ddResourceEnvRefs = ddBean.getChildBean(xpaths[2]);
125 //        if (ddResourceEnvRefs == null) {
126 //            ddResourceEnvRefs = new DDBean[0];
127 //        }
128 //        resourceEnvRefs = new LocalRefDConfigBean[ddResourceEnvRefs.length];
129 //        GerLocalRefType[] xmlResourceEnvRefs = envRefs.getResourceEnvRefs();
130 //        Map resourceEnvRefMap = new HashMap();
131 //        for (int i = 0; i < xmlResourceEnvRefs.length; i++) {
132 //            GerLocalRefType refAdapter = xmlResourceEnvRefs[i];
133 //            resourceEnvRefMap.put(refAdapter.getRefName(), refAdapter);
134 //            envRefs.removeResourceEnvRef(0);
135 //        }
136 //        for (int i = 0; i < ddResourceEnvRefs.length; i++) {
137 //            DDBean ddRef = ddResourceEnvRefs[i];
138 //            String name = ddRef.getText(namePaths[2])[0];
139 //            GerLocalRefType refAdapter;
140 //            if (resourceEnvRefMap.get(name) == null) {
141 //                refAdapter = envRefs.addNewResourceEnvRef();
142 //                refAdapter.setRefName(name);
143 //            } else {
144 //                refAdapter = (GerLocalRefType) resourceEnvRefMap.get(name);
145 //                envRefs.setResourceEnvRef(i, refAdapter);
146 //            }
147 //            resourceEnvRefs[i] = new LocalRefDConfigBean(ddRef, refAdapter, namePaths[2]);
148 //        }
149 //
150 //        DDBean[] ddResourceRefs = ddBean.getChildBean(xpaths[3]);
151 //        if (ddResourceRefs == null) {
152 //            ddResourceRefs = new DDBean[0];
153 //        }
154 //        resourceRefs = new LocalRefDConfigBean[ddResourceRefs.length];
155 //        GerLocalRefType[] xmlResourceRefs = envRefs.getResourceRefs();
156 //        Map resourceRefMap = new HashMap();
157 //        for (int i = 0; i < xmlResourceRefs.length; i++) {
158 //            GerLocalRefType refAdapter = xmlResourceRefs[i];
159 //            resourceRefMap.put(refAdapter.getRefName(), refAdapter);
160 //            envRefs.removeResourceRef(0);
161 //        }
162 //        for (int i = 0; i < ddResourceRefs.length; i++) {
163 //            DDBean ddRef = ddResourceRefs[i];
164 //            String name = ddRef.getText(namePaths[3])[0];
165 //            GerLocalRefType refAdapter;
166 //            if (resourceRefMap.get(name) == null) {
167 //                refAdapter = envRefs.addNewResourceRef();
168 //                refAdapter.setRefName(name);
169 //            } else {
170 //                refAdapter = (GerLocalRefType) resourceRefMap.get(name);
171 //                envRefs.setResourceRef(i, refAdapter);
172 //            }
173 //            resourceRefs[i] = new LocalRefDConfigBean(ddRef, refAdapter, namePaths[3]);
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 //          DDBean ddBean = dcBean.getDDBean();
223 //          String xpath = ddBean.getXpath();
224 //          String name = ddBean.getText();
225         /*
226               if (xpath.endsWith("ejb-ref/ejb-ref-name")) {
227                   if (ejbRefs.remove(name) == null) {
228                       throw new BeanNotFoundException("No DConfigBean found with name: " + name);
229                   }
230               } else if (xpath.endsWith("ejb-local-ref/ejb-ref-name")) {
231                   if (ejbLocalRefs.remove(name) == null) {
232                       throw new BeanNotFoundException("No DConfigBean found with name: " + name);
233                   }
234               } else if (xpath.endsWith("service-ref/service-ref-name")) {
235                   if (serviceRefs.remove(name) == null) {
236                       throw new BeanNotFoundException("No DConfigBean found with name: " + name);
237                   }
238               } else if (xpath.endsWith("resource-ref/res-ref-name")) {
239                   if (resourceRefs.remove(name) == null) {
240                       throw new BeanNotFoundException("No DConfigBean found with name: " + name);
241                   }
242               } else {
243                   throw new BeanNotFoundException("Unrecognized XPath: " + xpath);
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 }