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.connector.deployment.dconfigbean;
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.ConfigurationException;
26  
27  import org.apache.geronimo.deployment.plugin.DConfigBeanSupport;
28  import org.apache.geronimo.xbeans.geronimo.GerConfigPropertySettingType;
29  import org.apache.geronimo.xbeans.geronimo.GerConnectionDefinitionType;
30  import org.apache.geronimo.xbeans.geronimo.GerOutboundResourceadapterType;
31  import org.apache.geronimo.xbeans.geronimo.GerResourceadapterInstanceType;
32  import org.apache.geronimo.xbeans.geronimo.GerResourceadapterType;
33  import org.apache.xmlbeans.SchemaTypeLoader;
34  
35  /**
36   *
37   *
38   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
39   *
40   * */
41  public class ResourceAdapterDConfigBean extends DConfigBeanSupport {
42  
43      private final static String[][] RESOURCE_ADAPTER_XPATHS = {
44          {"config-property"},
45          {"outbound-resourceadapter", "connection-definition"},
46          {"adminobject"}};
47      private Map configPropertiesMap = new HashMap();
48      private Map connectionDefinitionsMap = new HashMap();
49      private Map adminObjectsMap = new HashMap();
50  
51      public ResourceAdapterDConfigBean(DDBean ddBean, final GerResourceadapterType resourceadapter) {
52          super(ddBean, resourceadapter);
53          if (getResourceadapterInstance() == null) {
54              resourceadapter.addNewResourceadapterInstance();
55          }
56          ConfigPropertiesHelper.initializeConfigSettings(ddBean, new ConfigPropertiesHelper.ConfigPropertiesSource() {
57              public GerConfigPropertySettingType[] getConfigPropertySettingArray() {
58                  return getResourceadapterInstance().getConfigPropertySettingArray();
59              }
60  
61              public GerConfigPropertySettingType addNewConfigPropertySetting() {
62                  return getResourceadapterInstance().addNewConfigPropertySetting();
63              }
64  
65              public void removeConfigPropertySetting(int j) {
66              }
67  
68              public ConfigPropertySettings[] getConfigPropertySettings() {
69                  return new ConfigPropertySettings[0];
70              }
71  
72              public void setConfigPropertySettings(ConfigPropertySettings[] configs) {
73              }
74  
75          }, configPropertiesMap, "config-property", "config-property-name");
76          //initialize connection definitions
77          GerOutboundResourceadapterType outboundResourceadapter = resourceadapter.getOutboundResourceadapter();
78          if (outboundResourceadapter == null) {
79              outboundResourceadapter = resourceadapter.addNewOutboundResourceadapter();
80          }
81          DDBean[] connectionDefinitionDDBeans = ddBean.getChildBean(getXpaths()[1]);
82          GerConnectionDefinitionType[] connectionDefinitions = outboundResourceadapter.getConnectionDefinitionArray();
83  
84          if (connectionDefinitions.length == 0) {
85              //we are new
86              for (int i = 0; i < connectionDefinitionDDBeans.length; i++) {
87                  DDBean connectionDefinitionDdBean = connectionDefinitionDDBeans[i];
88                  GerConnectionDefinitionType connectionDefinition = outboundResourceadapter.addNewConnectionDefinition();
89                  String connectionfactoryInterface = connectionDefinitionDdBean.getText("connectionfactory-interface")[0];
90                  ConnectionDefinitionDConfigBean connectionDefinitionDConfigBean = new ConnectionDefinitionDConfigBean(connectionDefinitionDdBean, connectionDefinition);
91                  connectionDefinitionsMap.put(connectionfactoryInterface, connectionDefinitionDConfigBean);
92              }
93          } else {
94              //we are read in from xml.  Check correct length
95              assert connectionDefinitionDDBeans.length == connectionDefinitions.length;
96              for (int i = 0; i < connectionDefinitionDDBeans.length; i++) {
97                  DDBean connectionDefinitionDdBean = connectionDefinitionDDBeans[i];
98                  GerConnectionDefinitionType connectionDefinition = connectionDefinitions[i];
99                  String connectionfactoryInterface = connectionDefinitionDdBean.getText("connectionfactory-interface")[0];
100                 assert connectionfactoryInterface.equals(connectionDefinition.getConnectionfactoryInterface());
101                 ConnectionDefinitionDConfigBean connectionDefinitionDConfigBean = new ConnectionDefinitionDConfigBean(connectionDefinitionDdBean, connectionDefinition);
102                 connectionDefinitionsMap.put(connectionfactoryInterface, connectionDefinitionDConfigBean);
103             }
104         }
105 
106         //admin objects
107 //        DDBean[] adminObjecDdBeans = ddBean.getChildBean(getXpaths()[2]);
108 //        GerAdminobjectType[] adminobjectTypes = getResourceadapter().getAdminobjectArray();
109 //
110 //        if (adminobjectTypes.length == 0) {
111 //            //we are new
112 //            for (int i = 0; i < adminObjecDdBeans.length; i++) {
113 //                DDBean adminObjectDdBean = adminObjecDdBeans[i];
114 //                GerAdminobjectType adminobjectType = getResourceadapter().addNewAdminobject();
115 //                String adminObjectInterface = adminObjectDdBean.getText("adminobject-interface")[0];
116 //                String adminObjectClass = adminObjectDdBean.getText("adminobject-class")[0];
117 //                AdminObjectDConfigBean adminObjectDConfigBean = new AdminObjectDConfigBean(adminObjectDdBean, adminobjectType);
118 //                adminObjectsMap.put(new Key(adminObjectInterface, adminObjectClass), adminObjectDConfigBean);
119 //            }
120 //        } else {
121 //            //we are read in from xml.  Check correct length
122 //            assert adminObjecDdBeans.length == adminobjectTypes.length;
123 //            for (int i = 0; i < adminObjecDdBeans.length; i++) {
124 //                DDBean adminObjectDdBean = adminObjecDdBeans[i];
125 //                GerAdminobjectType adminobjectType = adminobjectTypes[i];
126 //                String adminObjectInterface = adminObjectDdBean.getText("adminobject-interface")[0];
127 //                assert(adminObjectInterface.equals(adminobjectType.getAdminobjectInterface().getStringValue()));
128 //                String adminObjectClass = adminObjectDdBean.getText("adminobject-class")[0];
129 //                assert(adminObjectClass.equals(adminobjectType.getAdminobjectClass().getStringValue()));
130 //                AdminObjectDConfigBean adminObjectDConfigBean = new AdminObjectDConfigBean(adminObjectDdBean, adminobjectType);
131 //                adminObjectsMap.put(new Key(adminObjectInterface, adminObjectClass), adminObjectDConfigBean);
132 //
133 //            }
134 //        }
135 
136     }
137 
138     GerResourceadapterType getResourceadapter() {
139         return (GerResourceadapterType) getXmlObject();
140     }
141 
142     private GerResourceadapterInstanceType getResourceadapterInstance() {
143         return getResourceadapter().getResourceadapterInstance();
144     }
145 
146     public String getResourceAdapterName() {
147         return getResourceadapterInstance().getResourceadapterName();
148     }
149 
150     public void setResourceAdapterName(String resourceAdapterName) {
151         getResourceadapterInstance().setResourceadapterName(resourceAdapterName);
152     }
153 
154     public String getWorkManager() {
155         if(getResourceadapterInstance() == null || getResourceadapterInstance().getWorkmanager() == null) {
156             return null;
157         }
158         return getResourceadapterInstance().getWorkmanager().getGbeanLink();
159     }
160 
161     public void setWorkManager(String workManager) {
162         if(getResourceadapterInstance() == null) {
163             getResourceadapter().addNewResourceadapterInstance();
164         }
165         if(getResourceadapterInstance().getWorkmanager() == null) {
166             getResourceadapterInstance().addNewWorkmanager();
167         }
168         getResourceadapterInstance().getWorkmanager().setGbeanLink(workManager);
169     }
170 
171     public DConfigBean getDConfigBean(DDBean bean) throws ConfigurationException {
172         String xpath = bean.getXpath();
173         String[] xpaths = getXpaths();
174         if (xpath.equals(xpaths[0])) {
175             //resource adapter config property
176             String configPropertyName = bean.getText("config-property-name")[0];
177             ConfigPropertySettingDConfigBean configPropertySetting = (ConfigPropertySettingDConfigBean) configPropertiesMap.get(configPropertyName);
178             assert configPropertySetting != null;
179             return configPropertySetting;
180         }
181         if (xpath.equals(xpaths[1])) {
182             //connection definition
183             String connectionFactoryInterface = bean.getText("connectionfactory-interface")[0];
184             ConnectionDefinitionDConfigBean connectionDefinition = (ConnectionDefinitionDConfigBean) connectionDefinitionsMap.get(connectionFactoryInterface);
185             assert connectionDefinition != null;
186             return connectionDefinition;
187         }
188         if (xpath.equals(xpaths[2])) {
189             //admin objects
190             String adminObjectInterface = bean.getText("adminobject-interface")[0];
191             String adminObjectClass = bean.getText("adminobject-class")[0];
192             AdminObjectDConfigBean adminObject = (AdminObjectDConfigBean) adminObjectsMap.get(new Key(adminObjectInterface, adminObjectClass));
193             assert adminObject != null;
194             return adminObject;
195         }
196         return null;
197     }
198 
199 
200     public String[] getXpaths() {
201         return getXPathsForJ2ee_1_4(RESOURCE_ADAPTER_XPATHS);
202     }
203 
204     protected SchemaTypeLoader getSchemaTypeLoader() {
205         return ResourceAdapterDConfigRoot.SCHEMA_TYPE_LOADER;
206     }
207 
208 
209     //from doubleKeyedHashMap, currently in transaction module
210     private final static class Key {
211         private final Object part1;
212         private final Object part2;
213 
214         public Key(Object part1, Object part2) {
215             this.part1 = part1;
216             this.part2 = part2;
217         }
218 
219         public int hashCode() {
220             return part1.hashCode() ^ part2.hashCode();
221         }
222 
223         public boolean equals(Object obj) {
224             if (obj instanceof Key) {
225                 Key other = (Key) obj;
226                 return this.part1.equals(other.part1) && this.part2.equals(other.part2);
227             } else {
228                 return false;
229             }
230         }
231     }
232 }