1
package org.postgresql.jdbc2.optional;
4
import java.io.PrintWriter;
7
import java.io.ObjectOutputStream;
8
import java.io.ObjectInputStream;
9
import java.io.IOException;
12
* Base class for data sources and related classes.
14
* @author Aaron Mulder (ammulder@chariotsolutions.com)
15
* @version $Revision: 1.3.6.1 $
17
public abstract class BaseDataSource implements Referenceable
19
// Load the normal driver, since we'll use it to actually connect to the
20
// database. That way we don't have to maintain the connecting code in
25
Class.forName("org.postgresql.Driver");
27
catch (ClassNotFoundException e)
29
System.err.println("PostgreSQL DataSource unable to load PostgreSQL JDBC Driver");
33
// Needed to implement the DataSource/ConnectionPoolDataSource interfaces
34
private transient PrintWriter logger;
35
// Don't track loginTimeout, since we'd just ignore it anyway
37
// Standard properties, defined in the JDBC 2.0 Optional Package spec
38
private String serverName = "localhost";
39
private String databaseName;
41
private String password;
42
private int portNumber;
45
* Gets a connection to the PostgreSQL database. The database is identified by the
46
* DataSource properties serverName, databaseName, and portNumber. The user to
47
* connect as is identified by the DataSource properties user and password.
49
* @return A valid database connection.
50
* @throws SQLException
51
* Occurs when the database connection cannot be established.
53
public Connection getConnection() throws SQLException
55
return getConnection(user, password);
59
* Gets a connection to the PostgreSQL database. The database is identified by the
60
* DataSource properties serverName, databaseName, and portNumber. The user to
61
* connect as is identified by the arguments user and password, which override
62
* the DataSource properties by the same name.
64
* @return A valid database connection.
65
* @throws SQLException
66
* Occurs when the database connection cannot be established.
68
public Connection getConnection(String user, String password) throws SQLException
72
Connection con = DriverManager.getConnection(getUrl(), user, password);
75
logger.println("Created a non-pooled connection for " + user + " at " + getUrl());
79
catch (SQLException e)
83
logger.println("Failed to create a non-pooled connection for " + user + " at " + getUrl() + ": " + e);
90
* This DataSource does not support a configurable login timeout.
93
public int getLoginTimeout() throws SQLException
99
* This DataSource does not support a configurable login timeout. Any value
100
* provided here will be ignored.
102
public void setLoginTimeout(int i) throws SQLException
106
* Gets the log writer used to log connections opened.
108
public PrintWriter getLogWriter() throws SQLException
114
* The DataSource will note every connection opened to the provided log writer.
116
public void setLogWriter(PrintWriter printWriter) throws SQLException
118
logger = printWriter;
122
* Gets the name of the host the PostgreSQL database is running on.
124
public String getServerName()
130
* Sets the name of the host the PostgreSQL database is running on. If this
131
* is changed, it will only affect future calls to getConnection. The default
132
* value is <tt>localhost</tt>.
134
public void setServerName(String serverName)
136
if (serverName == null || serverName.equals(""))
138
this.serverName = "localhost";
142
this.serverName = serverName;
147
* Gets the name of the PostgreSQL database, running on the server identified
148
* by the serverName property.
150
public String getDatabaseName()
156
* Sets the name of the PostgreSQL database, running on the server identified
157
* by the serverName property. If this is changed, it will only affect
158
* future calls to getConnection.
160
public void setDatabaseName(String databaseName)
162
this.databaseName = databaseName;
166
* Gets a description of this DataSource-ish thing. Must be customized by
169
public abstract String getDescription();
172
* Gets the user to connect as by default. If this is not specified, you must
173
* use the getConnection method which takes a user and password as parameters.
175
public String getUser()
181
* Sets the user to connect as by default. If this is not specified, you must
182
* use the getConnection method which takes a user and password as parameters.
183
* If this is changed, it will only affect future calls to getConnection.
185
public void setUser(String user)
191
* Gets the password to connect with by default. If this is not specified but a
192
* password is needed to log in, you must use the getConnection method which takes
193
* a user and password as parameters.
195
public String getPassword()
201
* Sets the password to connect with by default. If this is not specified but a
202
* password is needed to log in, you must use the getConnection method which takes
203
* a user and password as parameters. If this is changed, it will only affect
204
* future calls to getConnection.
206
public void setPassword(String password)
208
this.password = password;
212
* Gets the port which the PostgreSQL server is listening on for TCP/IP
215
* @return The port, or 0 if the default port will be used.
217
public int getPortNumber()
223
* Gets the port which the PostgreSQL server is listening on for TCP/IP
224
* connections. Be sure the -i flag is passed to postmaster when PostgreSQL
225
* is started. If this is not set, or set to 0, the default port will be used.
227
public void setPortNumber(int portNumber)
229
this.portNumber = portNumber;
233
* Generates a DriverManager URL from the other properties supplied.
235
private String getUrl()
237
return "jdbc:postgresql://" + serverName + (portNumber == 0 ? "" : ":" + portNumber) + "/" + databaseName;
241
* Generates a reference using the appropriate object factory. This
242
* implementation uses the JDBC 2 optional package object factory.
244
protected Reference createReference()
246
return new Reference(getClass().getName(), PGObjectFactory.class.getName(), null);
249
public Reference getReference() throws NamingException
251
Reference ref = createReference();
252
ref.add(new StringRefAddr("serverName", serverName));
255
ref.add(new StringRefAddr("portNumber", Integer.toString(portNumber)));
257
ref.add(new StringRefAddr("databaseName", databaseName));
260
ref.add(new StringRefAddr("user", user));
262
if (password != null)
264
ref.add(new StringRefAddr("password", password));
269
protected void writeBaseObject(ObjectOutputStream out) throws IOException
271
out.writeObject(serverName);
272
out.writeObject(databaseName);
273
out.writeObject(user);
274
out.writeObject(password);
275
out.writeInt(portNumber);
278
protected void readBaseObject(ObjectInputStream in) throws IOException, ClassNotFoundException
280
serverName = (String)in.readObject();
281
databaseName = (String)in.readObject();
282
user = (String)in.readObject();
283
password = (String)in.readObject();
284
portNumber = in.readInt();