KickJava   Java API By Example, From Geeks To Geeks.

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


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.DriverManager JavaDoc;
24 import java.sql.DriverPropertyInfo JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.regex.Matcher JavaDoc;
28 import java.util.regex.Pattern JavaDoc;
29
30 import net.sf.hajdbc.Database;
31 import net.sf.hajdbc.DatabaseCluster;
32 import net.sf.hajdbc.DatabaseClusterFactory;
33 import net.sf.hajdbc.Messages;
34 import net.sf.hajdbc.Operation;
35
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * @author Paul Ferraro
41  * @version $Revision: 942 $
42  */

43 public final class Driver implements java.sql.Driver JavaDoc
44 {
45     private static final Pattern JavaDoc URL_PATTERN = Pattern.compile("jdbc:ha-jdbc:(.+)");
46     
47     private static Logger logger = LoggerFactory.getLogger(Driver.class);
48     
49     static
50     {
51         Driver driver = new Driver();
52         
53         try
54         {
55             DriverManager.registerDriver(driver);
56         }
57         catch (SQLException JavaDoc e)
58         {
59             logger.error(Messages.getMessage(Messages.DRIVER_REGISTER_FAILED, driver.getClass().getName()), e);
60         }
61     }
62     
63     /**
64      * @see java.sql.Driver#acceptsURL(java.lang.String)
65      */

66     public boolean acceptsURL(String JavaDoc url)
67     {
68         return (this.getDatabaseCluster(url) != null);
69     }
70     
71     /**
72      * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
73      */

74     public java.sql.Connection JavaDoc connect(String JavaDoc url, final Properties JavaDoc properties) throws SQLException JavaDoc
75     {
76         DatabaseCluster databaseCluster = this.getDatabaseCluster(url);
77         
78         if (databaseCluster == null)
79         {
80             return null;
81         }
82         
83         Operation<java.sql.Driver JavaDoc, java.sql.Connection JavaDoc> operation = new Operation<java.sql.Driver JavaDoc, java.sql.Connection JavaDoc>()
84         {
85             public java.sql.Connection JavaDoc execute(Database database, java.sql.Driver JavaDoc driver) throws SQLException JavaDoc
86             {
87                 return driver.connect(DriverDatabase.class.cast(database).getUrl(), properties);
88             }
89         };
90         
91         return new Connection<java.sql.Driver JavaDoc>(this.getConnectionFactory(databaseCluster), operation, new FileSupportImpl());
92     }
93     
94     /**
95      * @see java.sql.Driver#getMajorVersion()
96      */

97     public int getMajorVersion()
98     {
99         return Integer.parseInt(DatabaseClusterFactory.getVersion().split(Pattern.quote("."))[0]);
100     }
101     
102     /**
103      * @see java.sql.Driver#getMinorVersion()
104      */

105     public int getMinorVersion()
106     {
107         return Integer.parseInt(DatabaseClusterFactory.getVersion().split(Pattern.quote("."))[1].split("-")[0]);
108     }
109     
110     /**
111      * @see java.sql.Driver#getPropertyInfo(java.lang.String, java.util.Properties)
112      */

113     public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, final Properties JavaDoc properties) throws SQLException JavaDoc
114     {
115         DatabaseCluster databaseCluster = this.getDatabaseCluster(url);
116         
117         if (databaseCluster == null)
118         {
119             return null;
120         }
121         
122         Operation<java.sql.Driver JavaDoc, DriverPropertyInfo JavaDoc[]> operation = new Operation<java.sql.Driver JavaDoc, DriverPropertyInfo JavaDoc[]>()
123         {
124             public DriverPropertyInfo JavaDoc[] execute(Database database, java.sql.Driver JavaDoc driver) throws SQLException JavaDoc
125             {
126                 return driver.getPropertyInfo(DriverDatabase.class.cast(database).getUrl(), properties);
127             }
128         };
129         
130         return this.getConnectionFactory(databaseCluster).executeReadFromDriver(operation);
131     }
132     
133     /**
134      * @see java.sql.Driver#jdbcCompliant()
135      */

136     public boolean jdbcCompliant()
137     {
138         return true;
139     }
140     
141     private DatabaseCluster getDatabaseCluster(String JavaDoc url)
142     {
143         Matcher JavaDoc matcher = URL_PATTERN.matcher(url);
144         
145         if (!matcher.matches())
146         {
147             return null;
148         }
149         
150         String JavaDoc name = matcher.group(1);
151         
152         return DatabaseClusterFactory.getInstance().getDatabaseCluster(name);
153     }
154     
155     private ConnectionFactory<java.sql.Driver JavaDoc> getConnectionFactory(DatabaseCluster databaseCluster)
156     {
157         return new ConnectionFactory<java.sql.Driver JavaDoc>(databaseCluster, java.sql.Driver JavaDoc.class);
158     }
159 }
160
Popular Tags