KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > sql > DriverDatabase


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.sql;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.Driver JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import net.sf.hajdbc.ActiveDatabaseMBean;
29 import net.sf.hajdbc.InactiveDatabaseMBean;
30 import net.sf.hajdbc.Messages;
31
32 /**
33  * @author Paul Ferraro
34  * @version $Revision: 1049 $
35  * @since 1.0
36  */

37 public class DriverDatabase extends AbstractDatabase<Driver> implements InactiveDriverDatabaseMBean
38 {
39     private static final String JavaDoc USER = "user";
40     private static final String JavaDoc PASSWORD = "password";
41     
42     private String JavaDoc url;
43     private Class JavaDoc<? extends Driver> driverClass;
44     
45     /**
46      * @see net.sf.hajdbc.sql.ActiveDriverDatabaseMBean#getUrl()
47      */

48     public String JavaDoc getUrl()
49     {
50         return this.url;
51     }
52     
53     /**
54      * @see net.sf.hajdbc.sql.InactiveDriverDatabaseMBean#setUrl(java.lang.String)
55      */

56     public void setUrl(String JavaDoc url)
57     {
58         this.getDriver(url);
59         this.checkDirty(this.url, url);
60         this.url = url;
61     }
62     
63     /**
64      * @see net.sf.hajdbc.sql.ActiveDriverDatabaseMBean#getDriver()
65      */

66     public String JavaDoc getDriver()
67     {
68         return (this.driverClass != null) ? this.driverClass.getName() : null;
69     }
70     
71     /**
72      * Set the driver class for this database.
73      * @param driver the driver class name
74      * @throws IllegalArgumentException if driver class could not be found or does not implement <code>java.sql.Driver</code>
75      */

76     public void setDriver(String JavaDoc driver)
77     {
78         try
79         {
80             Class JavaDoc<? extends Driver> driverClass = null;
81             
82             if ((driver != null) && (driver.length() > 0))
83             {
84                 driverClass = Class.forName(driver).asSubclass(Driver.class);
85             }
86             
87             this.checkDirty(this.driverClass, driverClass);
88             this.driverClass = driverClass;
89         }
90         catch (ClassNotFoundException JavaDoc e)
91         {
92             throw new IllegalArgumentException JavaDoc(e);
93         }
94         catch (ClassCastException JavaDoc e)
95         {
96             throw new IllegalArgumentException JavaDoc(e);
97         }
98     }
99     
100     /**
101      * @param driver a JDBC driver
102      * @return a database connection
103      * @throws java.sql.SQLException if a database connection could not be made
104      * @see net.sf.hajdbc.Database#connect(Object)
105      */

106     public Connection connect(Driver driver) throws java.sql.SQLException JavaDoc
107     {
108         Properties JavaDoc properties = new Properties JavaDoc(this.getProperties());
109         
110         if (this.user != null)
111         {
112             properties.setProperty(USER, this.user);
113         }
114
115         if (this.password != null)
116         {
117             properties.setProperty(PASSWORD, this.password);
118         }
119         
120         return driver.connect(this.url, properties);
121     }
122
123     /**
124      * @see net.sf.hajdbc.Database#createConnectionFactory()
125      */

126     public Driver createConnectionFactory()
127     {
128         return this.getDriver(this.url);
129     }
130
131     private Driver getDriver(String JavaDoc url)
132     {
133         try
134         {
135             return DriverManager.getDriver(url);
136         }
137         catch (java.sql.SQLException JavaDoc e)
138         {
139             throw new IllegalArgumentException JavaDoc(Messages.getMessage(Messages.JDBC_URL_REJECTED, url), e);
140         }
141     }
142     
143     /**
144      * @see net.sf.hajdbc.Database#getConnectionFactoryClass()
145      */

146     public Class JavaDoc<Driver> getConnectionFactoryClass()
147     {
148         return Driver.class;
149     }
150
151     /**
152      * @see net.sf.hajdbc.Database#getActiveMBeanClass()
153      */

154     public Class JavaDoc<? extends ActiveDatabaseMBean> getActiveMBeanClass()
155     {
156         return ActiveDriverDatabaseMBean.class;
157     }
158
159     /**
160      * @see net.sf.hajdbc.Database#getInactiveMBeanClass()
161      */

162     public Class JavaDoc<? extends InactiveDatabaseMBean> getInactiveMBeanClass()
163     {
164         return InactiveDriverDatabaseMBean.class;
165     }
166     
167     static String JavaDoc getClassName(Class JavaDoc<?> targetClass)
168     {
169         return targetClass.getName();
170     }
171     
172     static Class JavaDoc<?> forName(String JavaDoc className) throws ClassNotFoundException JavaDoc
173     {
174         return (className != null) ? Class.forName(className) : null;
175     }
176 }
177
Popular Tags