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.connector.deployment.dconfigbean;
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.ConfigurationException;
025    
026    import org.apache.geronimo.deployment.plugin.DConfigBeanSupport;
027    import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType;
028    import org.apache.geronimo.xbeans.geronimo.GerConnectionDefinitionType;
029    import org.apache.geronimo.xbeans.geronimo.GerOutboundResourceadapterType;
030    import org.apache.geronimo.xbeans.geronimo.GerResourceadapterInstanceType;
031    import org.apache.geronimo.xbeans.geronimo.GerResourceadapterType;
032    import org.apache.xmlbeans.SchemaTypeLoader;
033    
034    /**
035     *
036     *
037     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
038     *
039     * */
040    public class ResourceAdapterDConfigBean extends DConfigBeanSupport {
041    
042        private final static String[][] RESOURCE_ADAPTER_XPATHS = {
043            {"config-property"},
044            {"outbound-resourceadapter", "connection-definition"},
045            {"adminobject"}};
046        private Map configPropertiesMap = new HashMap();
047        private Map connectionDefinitionsMap = new HashMap();
048        private Map adminObjectsMap = new HashMap();
049    
050        public ResourceAdapterDConfigBean(DDBean ddBean, final GerResourceadapterType resourceadapter) {
051            super(ddBean, resourceadapter);
052            if (getResourceadapterInstance() == null) {
053                resourceadapter.addNewResourceadapterInstance();
054            }
055            ConfigPropertiesHelper.initializeConfigSettings(ddBean, new ConfigPropertiesHelper.ConfigPropertiesSource() {
056                public GerConfigPropertySettingType[] getConfigPropertySettingArray() {
057                    return getResourceadapterInstance().getConfigPropertySettingArray();
058                }
059    
060                public GerConfigPropertySettingType addNewConfigPropertySetting() {
061                    return getResourceadapterInstance().addNewConfigPropertySetting();
062                }
063    
064                public void removeConfigPropertySetting(int j) {
065                }
066    
067                public ConfigPropertySettings[] getConfigPropertySettings() {
068                    return new ConfigPropertySettings[0];
069                }
070    
071                public void setConfigPropertySettings(ConfigPropertySettings[] configs) {
072                }
073    
074            }, configPropertiesMap, "config-property", "config-property-name");
075            //initialize connection definitions
076            GerOutboundResourceadapterType outboundResourceadapter = resourceadapter.getOutboundResourceadapter();
077            if (outboundResourceadapter == null) {
078                outboundResourceadapter = resourceadapter.addNewOutboundResourceadapter();
079            }
080            DDBean[] connectionDefinitionDDBeans = ddBean.getChildBean(getXpaths()[1]);
081            GerConnectionDefinitionType[] connectionDefinitions = outboundResourceadapter.getConnectionDefinitionArray();
082    
083            if (connectionDefinitions.length == 0) {
084                //we are new
085                for (int i = 0; i < connectionDefinitionDDBeans.length; i++) {
086                    DDBean connectionDefinitionDdBean = connectionDefinitionDDBeans[i];
087                    GerConnectionDefinitionType connectionDefinition = outboundResourceadapter.addNewConnectionDefinition();
088                    String connectionfactoryInterface = connectionDefinitionDdBean.getText("connectionfactory-interface")[0];
089                    ConnectionDefinitionDConfigBean connectionDefinitionDConfigBean = new ConnectionDefinitionDConfigBean(connectionDefinitionDdBean, connectionDefinition);
090                    connectionDefinitionsMap.put(connectionfactoryInterface, connectionDefinitionDConfigBean);
091                }
092            } else {
093                //we are read in from xml.  Check correct length
094                assert connectionDefinitionDDBeans.length == connectionDefinitions.length;
095                for (int i = 0; i < connectionDefinitionDDBeans.length; i++) {
096                    DDBean connectionDefinitionDdBean = connectionDefinitionDDBeans[i];
097                    GerConnectionDefinitionType connectionDefinition = connectionDefinitions[i];
098                    String connectionfactoryInterface = connectionDefinitionDdBean.getText("connectionfactory-interface")[0];
099                    assert connectionfactoryInterface.equals(connectionDefinition.getConnectionfactoryInterface());
100                    ConnectionDefinitionDConfigBean connectionDefinitionDConfigBean = new ConnectionDefinitionDConfigBean(connectionDefinitionDdBean, connectionDefinition);
101                    connectionDefinitionsMap.put(connectionfactoryInterface, connectionDefinitionDConfigBean);
102                }
103            }
104    
105            //admin objects
106    //        DDBean[] adminObjecDdBeans = ddBean.getChildBean(getXpaths()[2]);
107    //        GerAdminobjectType[] adminobjectTypes = getResourceadapter().getAdminobjectArray();
108    //
109    //        if (adminobjectTypes.length == 0) {
110    //            //we are new
111    //            for (int i = 0; i < adminObjecDdBeans.length; i++) {
112    //                DDBean adminObjectDdBean = adminObjecDdBeans[i];
113    //                GerAdminobjectType adminobjectType = getResourceadapter().addNewAdminobject();
114    //                String adminObjectInterface = adminObjectDdBean.getText("adminobject-interface")[0];
115    //                String adminObjectClass = adminObjectDdBean.getText("adminobject-class")[0];
116    //                AdminObjectDConfigBean adminObjectDConfigBean = new AdminObjectDConfigBean(adminObjectDdBean, adminobjectType);
117    //                adminObjectsMap.put(new Key(adminObjectInterface, adminObjectClass), adminObjectDConfigBean);
118    //            }
119    //        } else {
120    //            //we are read in from xml.  Check correct length
121    //            assert adminObjecDdBeans.length == adminobjectTypes.length;
122    //            for (int i = 0; i < adminObjecDdBeans.length; i++) {
123    //                DDBean adminObjectDdBean = adminObjecDdBeans[i];
124    //                GerAdminobjectType adminobjectType = adminobjectTypes[i];
125    //                String adminObjectInterface = adminObjectDdBean.getText("adminobject-interface")[0];
126    //                assert(adminObjectInterface.equals(adminobjectType.getAdminobjectInterface().getStringValue()));
127    //                String adminObjectClass = adminObjectDdBean.getText("adminobject-class")[0];
128    //                assert(adminObjectClass.equals(adminobjectType.getAdminobjectClass().getStringValue()));
129    //                AdminObjectDConfigBean adminObjectDConfigBean = new AdminObjectDConfigBean(adminObjectDdBean, adminobjectType);
130    //                adminObjectsMap.put(new Key(adminObjectInterface, adminObjectClass), adminObjectDConfigBean);
131    //
132    //            }
133    //        }
134    
135        }
136    
137        GerResourceadapterType getResourceadapter() {
138            return (GerResourceadapterType) getXmlObject();
139        }
140    
141        private GerResourceadapterInstanceType getResourceadapterInstance() {
142            return getResourceadapter().getResourceadapterInstance();
143        }
144    
145        public String getResourceAdapterName() {
146            return getResourceadapterInstance().getResourceadapterName();
147        }
148    
149        public void setResourceAdapterName(String resourceAdapterName) {
150            getResourceadapterInstance().setResourceadapterName(resourceAdapterName);
151        }
152    
153        public String getWorkManager() {
154            if(getResourceadapterInstance() == null || getResourceadapterInstance().getWorkmanager() == null) {
155                return null;
156            }
157            return getResourceadapterInstance().getWorkmanager().getGbeanLink();
158        }
159    
160        public void setWorkManager(String workManager) {
161            if(getResourceadapterInstance() == null) {
162                getResourceadapter().addNewResourceadapterInstance();
163            }
164            if(getResourceadapterInstance().getWorkmanager() == null) {
165                getResourceadapterInstance().addNewWorkmanager();
166            }
167            getResourceadapterInstance().getWorkmanager().setGbeanLink(workManager);
168        }
169    
170        public DConfigBean getDConfigBean(DDBean bean) throws ConfigurationException {
171            String xpath = bean.getXpath();
172            String[] xpaths = getXpaths();
173            if (xpath.equals(xpaths[0])) {
174                //resource adapter config property
175                String configPropertyName = bean.getText("config-property-name")[0];
176                ConfigPropertySettingDConfigBean configPropertySetting = (ConfigPropertySettingDConfigBean) configPropertiesMap.get(configPropertyName);
177                assert configPropertySetting != null;
178                return configPropertySetting;
179            }
180            if (xpath.equals(xpaths[1])) {
181                //connection definition
182                String connectionFactoryInterface = bean.getText("connectionfactory-interface")[0];
183                ConnectionDefinitionDConfigBean connectionDefinition = (ConnectionDefinitionDConfigBean) connectionDefinitionsMap.get(connectionFactoryInterface);
184                assert connectionDefinition != null;
185                return connectionDefinition;
186            }
187            if (xpath.equals(xpaths[2])) {
188                //admin objects
189                String adminObjectInterface = bean.getText("adminobject-interface")[0];
190                String adminObjectClass = bean.getText("adminobject-class")[0];
191                AdminObjectDConfigBean adminObject = (AdminObjectDConfigBean) adminObjectsMap.get(new Key(adminObjectInterface, adminObjectClass));
192                assert adminObject != null;
193                return adminObject;
194            }
195            return null;
196        }
197    
198    
199        public String[] getXpaths() {
200            return getXPathsForJ2ee_1_4(RESOURCE_ADAPTER_XPATHS);
201        }
202    
203        protected SchemaTypeLoader getSchemaTypeLoader() {
204            return ResourceAdapterDConfigRoot.SCHEMA_TYPE_LOADER;
205        }
206    
207    
208        //from doubleKeyedHashMap, currently in transaction module
209        private final static class Key {
210            private final Object part1;
211            private final Object part2;
212    
213            public Key(Object part1, Object part2) {
214                this.part1 = part1;
215                this.part2 = part2;
216            }
217    
218            public int hashCode() {
219                return part1.hashCode() ^ part2.hashCode();
220            }
221    
222            public boolean equals(Object obj) {
223                if (obj instanceof Key) {
224                    Key other = (Key) obj;
225                    return this.part1.equals(other.part1) && this.part2.equals(other.part2);
226                } else {
227                    return false;
228                }
229            }
230        }
231    }