001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.naming.deployment;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    import javax.enterprise.deploy.model.DDBean;
024    import javax.enterprise.deploy.spi.DConfigBean;
025    import javax.enterprise.deploy.spi.exceptions.BeanNotFoundException;
026    import javax.enterprise.deploy.spi.exceptions.ConfigurationException;
027    
028    import org.apache.geronimo.xbeans.geronimo.naming.GerEjbLocalRefType;
029    import org.apache.geronimo.xbeans.geronimo.naming.GerEjbRefType;
030    import org.apache.geronimo.xbeans.geronimo.naming.GerResourceEnvRefType;
031    import org.apache.geronimo.xbeans.geronimo.naming.GerResourceRefType;
032    import org.apache.xmlbeans.SchemaTypeLoader;
033    import org.apache.xmlbeans.XmlBeans;
034    
035    /**
036     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
037     */
038    public class ENCHelper {
039        static final SchemaTypeLoader SCHEMA_TYPE_LOADER = XmlBeans.typeLoaderUnion(new SchemaTypeLoader[]{
040            XmlBeans.typeLoaderForClassLoader(org.apache.geronimo.xbeans.j2ee.String.class.getClassLoader()),
041        });
042    
043    
044        public static final String[][] ENC_XPATHS = {
045            {"ejb-ref"},
046            {"ejb-local-ref"},
047            {"resource-env-ref"},
048            {"resource-ref"},
049        };
050    
051        public static final String[][] NAME_XPATHS = {
052            {"ejb-ref-name"},
053            {"ejb-ref-name"},
054            {"resource-env-ref-name"},
055            {"res-ref-name"}
056        };
057    
058    
059        private final String[] xpaths;
060        private final String[] namePaths;
061    
062        private RemoteRefDConfigBean[] ejbRefs;
063    
064        private LocalRefDConfigBean[] ejbLocalRefs;
065        private LocalRefDConfigBean[] resourceEnvRefs;
066        private LocalRefDConfigBean[] resourceRefs;
067    
068        public ENCHelper(DDBean ddBean, XmlEnvRefs envRefs, String[] xpaths, String[] namePaths) {
069            this.xpaths = xpaths;
070            this.namePaths = namePaths;
071            DDBean[] ddEjbRefs = ddBean.getChildBean(xpaths[0]);
072            if (ddEjbRefs == null) {
073                ddEjbRefs = new DDBean[0];
074            }
075            ejbRefs = new RemoteRefDConfigBean[ddEjbRefs.length];
076            GerEjbRefType[] xmlEjbRefs = envRefs.getEjbRefs();
077            Map ejbRefMap = new HashMap();
078            for (int i = 0; i < xmlEjbRefs.length; i++) {
079                GerEjbRefType refAdapter = xmlEjbRefs[i];
080                ejbRefMap.put(refAdapter.getRefName(), refAdapter);
081    //            envRefs.removeEjbRef(0);
082            }
083    //        for (int i = 0; i < ddEjbRefs.length; i++) {
084    //            DDBean ddRef = ddEjbRefs[i];
085    //            String name = ddRef.getText(namePaths[0])[0];
086    //            GerRemoteRefType refAdapter;
087    //            if (ejbRefMap.get(name) == null) {
088    //                refAdapter = envRefs.addNewEjbRef();
089    //                refAdapter.setRefName(name);
090    //            } else {
091    //                refAdapter = (GerRemoteRefType) ejbRefMap.get(name);
092    //                envRefs.setEjbRef(i, refAdapter);
093    //            }
094    //            ejbRefs[i] = new RemoteRefDConfigBean(ddRef, refAdapter, namePaths[0]);
095    //        }
096    //
097    //        DDBean[] ddEjbLocalRefs = ddBean.getChildBean(xpaths[1]);
098    //        if (ddEjbLocalRefs == null) {
099    //            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    }