KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jdbc > local > LocalManagedConnectionFactory


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.local;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.Driver JavaDoc;
29 import java.sql.DriverManager JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import javax.resource.ResourceException JavaDoc;
35 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
36 import javax.resource.spi.ManagedConnection JavaDoc;
37 import javax.security.auth.Subject JavaDoc;
38
39 import org.jboss.resource.JBossResourceException;
40 import org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnectionFactory;
41 import org.jboss.util.NestedRuntimeException;
42
43 /**
44  * LocalManagedConnectionFactory
45  *
46  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
47  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
48  * @version $Revision: 45415 $
49  */

50 public class LocalManagedConnectionFactory extends BaseWrapperManagedConnectionFactory
51 {
52    static final long serialVersionUID = 4698955390505160469L;
53
54    private String JavaDoc driverClass;
55
56    private transient Driver JavaDoc driver;
57
58    private String JavaDoc connectionURL;
59
60    protected String JavaDoc connectionProperties;
61
62    public LocalManagedConnectionFactory()
63    {
64
65    }
66
67    /**
68     * Get the value of ConnectionURL.
69     *
70     * @return value of ConnectionURL.
71     */

72    public String JavaDoc getConnectionURL()
73    {
74       return connectionURL;
75    }
76
77    /**
78     * Set the value of ConnectionURL.
79     *
80     * @param connectionURL Value to assign to ConnectionURL.
81     */

82    public void setConnectionURL(final String JavaDoc connectionURL)
83    {
84       this.connectionURL = connectionURL;
85    }
86
87    /**
88     * Get the DriverClass value.
89     *
90     * @return the DriverClass value.
91     */

92    public String JavaDoc getDriverClass()
93    {
94       return driverClass;
95    }
96
97    /**
98     * Set the DriverClass value.
99     *
100     * @param driverClass The new DriverClass value.
101     */

102    public synchronized void setDriverClass(final String JavaDoc driverClass)
103    {
104       this.driverClass = driverClass;
105       driver = null;
106    }
107
108    /**
109     * Get the value of connectionProperties.
110     *
111     * @return value of connectionProperties.
112     */

113    public String JavaDoc getConnectionProperties()
114    {
115       return connectionProperties;
116    }
117
118    /**
119     * Set the value of connectionProperties.
120     *
121     * @param connectionProperties Value to assign to connectionProperties.
122     */

123    public void setConnectionProperties(String JavaDoc connectionProperties)
124    {
125       this.connectionProperties = connectionProperties;
126       connectionProps.clear();
127       if (connectionProperties != null)
128       {
129          // Map any \ to \\
130
connectionProperties = connectionProperties.replaceAll("\\\\", "\\\\\\\\");
131
132          InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(connectionProperties.getBytes());
133          try
134          {
135             connectionProps.load(is);
136          }
137          catch (IOException JavaDoc ioe)
138          {
139             throw new NestedRuntimeException("Could not load connection properties", ioe);
140          }
141       }
142    }
143
144    public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cri)
145          throws javax.resource.ResourceException JavaDoc
146    {
147       Properties JavaDoc props = getConnectionProperties(subject, cri);
148       // Some friendly drivers (Oracle, you guessed right) modify the props you supply.
149
// Since we use our copy to identify compatibility in matchManagedConnection, we need
150
// a pristine copy for our own use. So give the friendly driver a copy.
151
Properties JavaDoc copy = (Properties JavaDoc) props.clone();
152       boolean trace = log.isTraceEnabled();
153       if (trace)
154       {
155          // Make yet another copy to mask the password
156
Properties JavaDoc logCopy = copy;
157          if (copy.getProperty("password") != null)
158          {
159             logCopy = (Properties JavaDoc) props.clone();
160             logCopy.setProperty("password", "--hidden--");
161          }
162          log.trace("Using properties: " + logCopy);
163       }
164
165       try
166       {
167          String JavaDoc url = getConnectionURL();
168          Driver JavaDoc d = getDriver(url);
169          Connection JavaDoc con = d.connect(url, copy);
170          if (con == null)
171             throw new JBossResourceException("Wrong driver class for this connection URL");
172
173          return new LocalManagedConnection(this, con, props, transactionIsolation, preparedStatementCacheSize);
174       }
175       catch (Exception JavaDoc e)
176       {
177          throw new JBossResourceException("Could not create connection", e);
178       }
179    }
180
181    public ManagedConnection JavaDoc matchManagedConnections(final Set JavaDoc mcs, final Subject JavaDoc subject,
182          final ConnectionRequestInfo JavaDoc cri) throws ResourceException JavaDoc
183    {
184       Properties JavaDoc newProps = getConnectionProperties(subject, cri);
185
186       for (Iterator JavaDoc i = mcs.iterator(); i.hasNext();)
187       {
188          Object JavaDoc o = i.next();
189
190          if (o instanceof LocalManagedConnection)
191          {
192             LocalManagedConnection mc = (LocalManagedConnection) o;
193
194             //First check the properties
195
if (mc.getProperties().equals(newProps))
196             {
197                //Next check to see if we are validating on matchManagedConnections
198
if ((getValidateOnMatch() && mc.checkValid()) || !getValidateOnMatch())
199                {
200
201                   return mc;
202
203                }
204
205             }
206          }
207       }
208
209       return null;
210    }
211
212    public int hashCode()
213    {
214       int result = 17;
215       result = result * 37 + ((connectionURL == null) ? 0 : connectionURL.hashCode());
216       result = result * 37 + ((driverClass == null) ? 0 : driverClass.hashCode());
217       result = result * 37 + ((userName == null) ? 0 : userName.hashCode());
218       result = result * 37 + ((password == null) ? 0 : password.hashCode());
219       result = result * 37 + transactionIsolation;
220       return result;
221    }
222
223    public boolean equals(Object JavaDoc other)
224    {
225       if (this == other)
226          return true;
227       if (getClass() != other.getClass())
228          return false;
229       LocalManagedConnectionFactory otherMcf = (LocalManagedConnectionFactory) other;
230       return this.connectionURL.equals(otherMcf.connectionURL) && this.driverClass.equals(otherMcf.driverClass)
231             && ((this.userName == null) ? otherMcf.userName == null : this.userName.equals(otherMcf.userName))
232             && ((this.password == null) ? otherMcf.password == null : this.password.equals(otherMcf.password))
233             && this.transactionIsolation == otherMcf.transactionIsolation;
234
235    }
236
237    /**
238     * Check the driver for the given URL. If it is not registered already
239     * then register it.
240     *
241     * @param url The JDBC URL which we need a driver for.
242     */

243    protected synchronized Driver JavaDoc getDriver(final String JavaDoc url) throws ResourceException JavaDoc
244    {
245       boolean trace = log.isTraceEnabled();
246       
247       // don't bother if it is loaded already
248
if (driver != null)
249       {
250          return driver;
251       }
252       if (trace)
253          log.trace("Checking driver for URL: " + url);
254
255       if (driverClass == null)
256       {
257          throw new JBossResourceException("No Driver class specified!");
258       }
259
260       // Check if the driver is already loaded, if not then try to load it
261

262       if (isDriverLoadedForURL(url))
263       {
264          return driver;
265       } // end of if ()
266

267       try
268       {
269          //try to load the class... this should register with DriverManager.
270
Class JavaDoc clazz = Class.forName(driverClass, true, Thread.currentThread().getContextClassLoader());
271          if (isDriverLoadedForURL(url))
272             //return immediately, some drivers (Cloudscape) do not let you create an instance.
273
return driver;
274
275          //We loaded the class, but either it didn't register
276
//and is not spec compliant, or is the wrong class.
277
driver = (Driver JavaDoc) clazz.newInstance();
278          DriverManager.registerDriver(driver);
279          if (isDriverLoadedForURL(url))
280             return driver;
281          //We can even instantiate one, it must be the wrong class for the URL.
282
}
283       catch (Exception JavaDoc e)
284       {
285          throw new JBossResourceException("Failed to register driver for: " + driverClass, e);
286       }
287
288       throw new JBossResourceException("Apparently wrong driver class specified for URL: class: " + driverClass
289             + ", url: " + url);
290    }
291
292    private boolean isDriverLoadedForURL(String JavaDoc url)
293    {
294       boolean trace = log.isTraceEnabled();
295       
296       try
297       {
298          driver = DriverManager.getDriver(url);
299          if (trace)
300             log.trace("Driver already registered for url: " + url);
301          return true;
302       }
303       catch (Exception JavaDoc e)
304       {
305          if (trace)
306             log.trace("Driver not yet registered for url: " + url);
307          return false;
308       }
309    }
310
311    protected String JavaDoc internalGetConnectionURL()
312    {
313       return connectionURL;
314    }
315 }
316
Popular Tags