KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.PrintWriter JavaDoc;
24 import java.sql.SQLException JavaDoc;
25
26 import javax.naming.Reference JavaDoc;
27 import javax.naming.Referenceable JavaDoc;
28 import javax.naming.StringRefAddr JavaDoc;
29
30 import net.sf.hajdbc.Database;
31 import net.sf.hajdbc.Operation;
32
33
34 /**
35  * @author Paul Ferraro
36  * @version $Revision: 961 $
37  */

38 public class DataSource implements javax.sql.DataSource JavaDoc, Referenceable JavaDoc
39 {
40     /** Property that identifies this data source */
41     public static final String JavaDoc DATABASE_CLUSTER = "cluster";
42     
43     private String JavaDoc cluster;
44     private ConnectionFactory<javax.sql.DataSource JavaDoc> connectionFactory;
45
46     /**
47      * Returns the identifier of the database cluster represented by this DataSource
48      * @return a database cluster identifier
49      */

50     public String JavaDoc getCluster()
51     {
52         return this.cluster;
53     }
54     
55     /**
56      * Sets the identifier of the database cluster represented by this DataSource
57      * @param cluster a database cluster identifier
58      */

59     public void setCluster(String JavaDoc cluster)
60     {
61         this.cluster = cluster;
62     }
63     
64     /**
65      * @see javax.naming.Referenceable#getReference()
66      */

67     public final Reference JavaDoc getReference()
68     {
69         Reference JavaDoc ref = new Reference JavaDoc(this.getClass().getName(), DataSourceFactory.class.getName(), null);
70         
71         ref.add(new StringRefAddr JavaDoc(DATABASE_CLUSTER, this.cluster));
72         
73         return ref;
74     }
75     
76     /**
77      * Set the connection factory for this datasource.
78      * @param connectionFactory a factory for creating database connections
79      */

80     public void setConnectionFactory(ConnectionFactory<javax.sql.DataSource JavaDoc> connectionFactory)
81     {
82         this.connectionFactory = connectionFactory;
83     }
84     
85     /**
86      * @see javax.sql.DataSource#getLoginTimeout()
87      */

88     public int getLoginTimeout() throws SQLException JavaDoc
89     {
90         Operation<javax.sql.DataSource JavaDoc, Integer JavaDoc> operation = new Operation<javax.sql.DataSource JavaDoc, Integer JavaDoc>()
91         {
92             public Integer JavaDoc execute(Database database, javax.sql.DataSource JavaDoc dataSource) throws SQLException JavaDoc
93             {
94                 return dataSource.getLoginTimeout();
95             }
96         };
97         
98         return this.connectionFactory.executeReadFromDriver(operation);
99     }
100
101     /**
102      * @see javax.sql.DataSource#setLoginTimeout(int)
103      */

104     public void setLoginTimeout(final int seconds) throws SQLException JavaDoc
105     {
106         Operation<javax.sql.DataSource JavaDoc, Void JavaDoc> operation = new Operation<javax.sql.DataSource JavaDoc, Void JavaDoc>()
107         {
108             public Void JavaDoc execute(Database database, javax.sql.DataSource JavaDoc dataSource) throws SQLException JavaDoc
109             {
110                 dataSource.setLoginTimeout(seconds);
111                 
112                 return null;
113             }
114         };
115         
116         this.connectionFactory.executeWriteToDriver(operation);
117     }
118
119     /**
120      * @see javax.sql.DataSource#getLogWriter()
121      */

122     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc
123     {
124         Operation<javax.sql.DataSource JavaDoc, PrintWriter JavaDoc> operation = new Operation<javax.sql.DataSource JavaDoc, PrintWriter JavaDoc>()
125         {
126             public PrintWriter JavaDoc execute(Database database, javax.sql.DataSource JavaDoc dataSource) throws SQLException JavaDoc
127             {
128                 return dataSource.getLogWriter();
129             }
130         };
131         
132         return this.connectionFactory.executeReadFromDriver(operation);
133     }
134
135     /**
136      * @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
137      */

138     public void setLogWriter(final PrintWriter JavaDoc writer) throws SQLException JavaDoc
139     {
140         Operation<javax.sql.DataSource JavaDoc, Void JavaDoc> operation = new Operation<javax.sql.DataSource JavaDoc, Void JavaDoc>()
141         {
142             public Void JavaDoc execute(Database database, javax.sql.DataSource JavaDoc dataSource) throws SQLException JavaDoc
143             {
144                 dataSource.setLogWriter(writer);
145                 
146                 return null;
147             }
148         };
149         
150         this.connectionFactory.executeWriteToDriver(operation);
151     }
152
153     /**
154      * @see javax.sql.DataSource#getConnection()
155      */

156     public java.sql.Connection JavaDoc getConnection() throws SQLException JavaDoc
157     {
158         Operation<javax.sql.DataSource JavaDoc, java.sql.Connection JavaDoc> operation = new Operation<javax.sql.DataSource JavaDoc, java.sql.Connection JavaDoc>()
159         {
160             public java.sql.Connection JavaDoc execute(Database database, javax.sql.DataSource JavaDoc dataSource) throws SQLException JavaDoc
161             {
162                 return dataSource.getConnection();
163             }
164         };
165         
166         return new Connection<javax.sql.DataSource JavaDoc>(this.connectionFactory, operation, new FileSupportImpl());
167     }
168
169     /**
170      * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
171      */

172     public java.sql.Connection JavaDoc getConnection(final String JavaDoc user, final String JavaDoc password) throws SQLException JavaDoc
173     {
174         Operation<javax.sql.DataSource JavaDoc, java.sql.Connection JavaDoc> operation = new Operation<javax.sql.DataSource JavaDoc, java.sql.Connection JavaDoc>()
175         {
176             public java.sql.Connection JavaDoc execute(Database database, javax.sql.DataSource JavaDoc dataSource) throws SQLException JavaDoc
177             {
178                 return dataSource.getConnection(user, password);
179             }
180         };
181         
182         return new Connection<javax.sql.DataSource JavaDoc>(this.connectionFactory, operation, new FileSupportImpl());
183     }
184 }
185
Popular Tags