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