KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jdbc > xa > XAManagedConnectionFactory


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.resource.adapter.jdbc.xa;
23
24 import org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnectionFactory;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.beans.PropertyEditorManager JavaDoc;
28 import java.beans.PropertyEditor JavaDoc;
29 import javax.sql.XADataSource JavaDoc;
30 import javax.sql.XAConnection JavaDoc;
31 import java.io.ByteArrayInputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.sql.SQLException JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Properties JavaDoc;
37 import java.util.Set JavaDoc;
38 import javax.resource.ResourceException JavaDoc;
39 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
40 import javax.resource.spi.ManagedConnection JavaDoc;
41 import javax.security.auth.Subject JavaDoc;
42 import org.jboss.resource.JBossResourceException;
43
44 /**
45  * XAManagedConnectionFactory
46  *
47  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
48  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
49  * @version $Revision: 45415 $
50  */

51 public class XAManagedConnectionFactory extends BaseWrapperManagedConnectionFactory
52 {
53    private static final long serialVersionUID = 1647927657609573729L;
54
55    private String JavaDoc xaDataSourceClass;
56
57    private String JavaDoc xaDataSourceProperties;
58
59    protected final Properties JavaDoc xaProps = new Properties JavaDoc();
60
61    private Boolean JavaDoc isSameRMOverrideValue;
62
63    private XADataSource JavaDoc xads;
64
65    public XAManagedConnectionFactory()
66    {
67    }
68
69    /**
70     * Get the XaDataSourceClass value.
71     *
72     * @return the XaDataSourceClass value.
73     */

74    public String JavaDoc getXADataSourceClass()
75    {
76       return xaDataSourceClass;
77    }
78
79    /**
80     * Set the XaDataSourceClass value.
81     *
82     * @param xaDataSourceClass The new XaDataSourceClass value.
83     */

84    public void setXADataSourceClass(String JavaDoc xaDataSourceClass)
85    {
86       this.xaDataSourceClass = xaDataSourceClass;
87    }
88
89    /**
90     * Get the XADataSourceProperties value.
91     *
92     * @return the XADataSourceProperties value.
93     */

94    public String JavaDoc getXADataSourceProperties()
95    {
96       return xaDataSourceProperties;
97    }
98
99    /**
100     * Set the XADataSourceProperties value.
101     *
102     * @param xaDataSourceProperties The new XADataSourceProperties value.
103     */

104    public void setXADataSourceProperties(String JavaDoc xaDataSourceProperties) throws ResourceException JavaDoc
105    {
106       this.xaDataSourceProperties = xaDataSourceProperties;
107       xaProps.clear();
108       if (xaDataSourceProperties != null)
109       {
110          // Map any \ to \\
111
xaDataSourceProperties = xaDataSourceProperties.replaceAll("\\\\", "\\\\\\\\");
112
113          InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(xaDataSourceProperties.getBytes());
114          try
115          {
116             xaProps.load(is);
117          }
118          catch (IOException JavaDoc ioe)
119          {
120             throw new JBossResourceException("Could not load connection properties", ioe);
121          }
122       }
123    }
124
125    /**
126     * Get the IsSameRMOverrideValue value.
127     *
128     * @return the IsSameRMOverrideValue value.
129     */

130    public Boolean JavaDoc getIsSameRMOverrideValue()
131    {
132       return isSameRMOverrideValue;
133    }
134
135    /**
136     * Set the IsSameRMOverrideValue value.
137     *
138     * @param isSameRMOverrideValue The new IsSameRMOverrideValue value.
139     */

140    public void setIsSameRMOverrideValue(Boolean JavaDoc isSameRMOverrideValue)
141    {
142       this.isSameRMOverrideValue = isSameRMOverrideValue;
143    }
144
145    public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cri)
146          throws javax.resource.ResourceException JavaDoc
147    {
148       Properties JavaDoc props = getConnectionProperties(subject, cri);
149       try
150       {
151          final String JavaDoc user = props.getProperty("user");
152          final String JavaDoc password = props.getProperty("password");
153
154          XAConnection JavaDoc xaConnection = (user != null)
155                ? getXADataSource().getXAConnection(user, password)
156                : getXADataSource().getXAConnection();
157
158          return newXAManagedConnection(props, xaConnection);
159       }
160       catch (Exception JavaDoc e)
161       {
162          throw new JBossResourceException("Could not create connection", e);
163       }
164    }
165
166    /**
167     * This method can be overwritten by sublcasses to provide rm specific
168     * implementation of XAManagedConnection
169     */

170    protected ManagedConnection JavaDoc newXAManagedConnection(Properties JavaDoc props, XAConnection JavaDoc xaConnection) throws SQLException JavaDoc
171    {
172       return new XAManagedConnection(this, xaConnection, props, transactionIsolation, preparedStatementCacheSize);
173    }
174
175    public ManagedConnection JavaDoc matchManagedConnections(Set JavaDoc mcs, Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cri)
176          throws ResourceException JavaDoc
177    {
178       Properties JavaDoc newProps = getConnectionProperties(subject, cri);
179       for (Iterator JavaDoc i = mcs.iterator(); i.hasNext();)
180       {
181          Object JavaDoc o = i.next();
182          if (o instanceof XAManagedConnection)
183          {
184             XAManagedConnection mc = (XAManagedConnection) o;
185
186             if (mc.getProperties().equals(newProps))
187             {
188                //Next check to see if we are validating on matchManagedConnections
189
if ((getValidateOnMatch() && mc.checkValid()) || !getValidateOnMatch())
190                {
191
192                   return mc;
193
194                }
195
196             }
197
198          }
199       }
200       return null;
201    }
202
203    public int hashCode()
204    {
205       int result = 17;
206       result = result * 37 + ((xaDataSourceClass == null) ? 0 : xaDataSourceClass.hashCode());
207       result = result * 37 + xaProps.hashCode();
208       result = result * 37 + ((userName == null) ? 0 : userName.hashCode());
209       result = result * 37 + ((password == null) ? 0 : password.hashCode());
210       result = result * 37 + transactionIsolation;
211       return result;
212    }
213
214    public boolean equals(Object JavaDoc other)
215    {
216       if (this == other)
217          return true;
218       if (getClass() != other.getClass())
219          return false;
220       XAManagedConnectionFactory otherMcf = (XAManagedConnectionFactory) other;
221       return this.xaDataSourceClass.equals(otherMcf.xaDataSourceClass) && this.xaProps.equals(otherMcf.xaProps)
222             && ((this.userName == null) ? otherMcf.userName == null : this.userName.equals(otherMcf.userName))
223             && ((this.password == null) ? otherMcf.password == null : this.password.equals(otherMcf.password))
224             && this.transactionIsolation == otherMcf.transactionIsolation;
225
226    }
227
228    protected synchronized XADataSource JavaDoc getXADataSource() throws ResourceException JavaDoc
229    {
230       if (xads == null)
231       {
232          if (xaDataSourceClass == null)
233             throw new JBossResourceException("No XADataSourceClass supplied!");
234          try
235          {
236             Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(xaDataSourceClass);
237             xads = (XADataSource JavaDoc) clazz.newInstance();
238             Class JavaDoc[] NOCLASSES = new Class JavaDoc[] {};
239             for (Iterator JavaDoc i = xaProps.keySet().iterator(); i.hasNext();)
240             {
241                String JavaDoc name = (String JavaDoc) i.next();
242                String JavaDoc value = xaProps.getProperty(name);
243                //This is a bad solution. On the other hand the only known example
244
// of a setter with no getter is for Oracle with password.
245
//Anyway, each xadatasource implementation should get its
246
//own subclass of this that explicitly sets the
247
//properties individually.
248
Class JavaDoc type = null;
249                try
250                {
251                   Method JavaDoc getter = clazz.getMethod("get" + name, NOCLASSES);
252                   type = getter.getReturnType();
253                }
254                catch (NoSuchMethodException JavaDoc e)
255                {
256                   type = String JavaDoc.class;
257                }
258
259                Method JavaDoc setter = clazz.getMethod("set" + name, new Class JavaDoc[] { type });
260                PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(type);
261                if (editor == null)
262                   throw new JBossResourceException("No property editor found for type: " + type);
263                editor.setAsText(value);
264                setter.invoke(xads, new Object JavaDoc[] { editor.getValue() });
265             }
266          }
267          catch (ClassNotFoundException JavaDoc cnfe)
268          {
269             throw new JBossResourceException("Class not found for XADataSource " + xaDataSourceClass, cnfe);
270          }
271          catch (InstantiationException JavaDoc ie)
272          {
273             throw new JBossResourceException("Could not create an XADataSource: ", ie);
274          }
275          catch (IllegalAccessException JavaDoc iae)
276          {
277             throw new JBossResourceException("Could not set a property: ", iae);
278          }
279          catch (IllegalArgumentException JavaDoc iae)
280          {
281             throw new JBossResourceException("Could not set a property: ", iae);
282          }
283          catch (InvocationTargetException JavaDoc ite)
284          {
285             throw new JBossResourceException("Could not invoke setter on XADataSource: ", ite);
286          }
287          catch (NoSuchMethodException JavaDoc nsme)
288          {
289             throw new JBossResourceException("Could not find accessor on XADataSource: ", nsme);
290          }
291       }
292       return xads;
293    }
294
295    protected Properties JavaDoc getXaProps()
296    {
297       return xaProps;
298    }
299 }
300
Popular Tags