~ubuntu-branches/ubuntu/gutsy/libpgjava/gutsy

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/jdbc2/optional/BaseDataSource.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2005-04-21 14:25:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050421142511-wibh5vc31fkrorx7
Tags: 7.4.7-3
Built with sources...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.postgresql.jdbc2.optional;
 
2
 
 
3
import javax.naming.*;
 
4
import java.io.PrintWriter;
 
5
import java.sql.*;
 
6
 
 
7
import java.io.ObjectOutputStream;
 
8
import java.io.ObjectInputStream;
 
9
import java.io.IOException;
 
10
 
 
11
/**
 
12
 * Base class for data sources and related classes.
 
13
 *
 
14
 * @author Aaron Mulder (ammulder@chariotsolutions.com)
 
15
 * @version $Revision: 1.3.6.1 $
 
16
 */
 
17
public abstract class BaseDataSource implements Referenceable
 
18
{
 
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
 
21
        // multiple places.
 
22
        static {
 
23
                try
 
24
                {
 
25
                        Class.forName("org.postgresql.Driver");
 
26
                }
 
27
                catch (ClassNotFoundException e)
 
28
                {
 
29
                        System.err.println("PostgreSQL DataSource unable to load PostgreSQL JDBC Driver");
 
30
                }
 
31
        }
 
32
 
 
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
 
36
 
 
37
        // Standard properties, defined in the JDBC 2.0 Optional Package spec
 
38
        private String serverName = "localhost";
 
39
        private String databaseName;
 
40
        private String user;
 
41
        private String password;
 
42
        private int portNumber;
 
43
 
 
44
        /**
 
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.
 
48
         *
 
49
         * @return A valid database connection.
 
50
         * @throws SQLException
 
51
         *                 Occurs when the database connection cannot be established.
 
52
         */
 
53
        public Connection getConnection() throws SQLException
 
54
        {
 
55
                return getConnection(user, password);
 
56
        }
 
57
 
 
58
        /**
 
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.
 
63
         *
 
64
         * @return A valid database connection.
 
65
         * @throws SQLException
 
66
         *                 Occurs when the database connection cannot be established.
 
67
         */
 
68
        public Connection getConnection(String user, String password) throws SQLException
 
69
        {
 
70
                try
 
71
                {
 
72
                        Connection con = DriverManager.getConnection(getUrl(), user, password);
 
73
                        if (logger != null)
 
74
                        {
 
75
                                logger.println("Created a non-pooled connection for " + user + " at " + getUrl());
 
76
                        }
 
77
                        return con;
 
78
                }
 
79
                catch (SQLException e)
 
80
                {
 
81
                        if (logger != null)
 
82
                        {
 
83
                                logger.println("Failed to create a non-pooled connection for " + user + " at " + getUrl() + ": " + e);
 
84
                        }
 
85
                        throw e;
 
86
                }
 
87
        }
 
88
 
 
89
        /**
 
90
         * This DataSource does not support a configurable login timeout.
 
91
         * @return 0
 
92
         */
 
93
        public int getLoginTimeout() throws SQLException
 
94
        {
 
95
                return 0;
 
96
        }
 
97
 
 
98
        /**
 
99
         * This DataSource does not support a configurable login timeout.  Any value
 
100
         * provided here will be ignored.
 
101
         */
 
102
        public void setLoginTimeout(int i) throws SQLException
 
103
                {}
 
104
 
 
105
        /**
 
106
         * Gets the log writer used to log connections opened.
 
107
         */
 
108
        public PrintWriter getLogWriter() throws SQLException
 
109
        {
 
110
                return logger;
 
111
        }
 
112
 
 
113
        /**
 
114
         * The DataSource will note every connection opened to the provided log writer.
 
115
         */
 
116
        public void setLogWriter(PrintWriter printWriter) throws SQLException
 
117
        {
 
118
                logger = printWriter;
 
119
        }
 
120
 
 
121
        /**
 
122
         * Gets the name of the host the PostgreSQL database is running on.
 
123
         */
 
124
        public String getServerName()
 
125
        {
 
126
                return serverName;
 
127
        }
 
128
 
 
129
        /**
 
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>.
 
133
         */
 
134
        public void setServerName(String serverName)
 
135
        {
 
136
                if (serverName == null || serverName.equals(""))
 
137
                {
 
138
                        this.serverName = "localhost";
 
139
                }
 
140
                else
 
141
                {
 
142
                        this.serverName = serverName;
 
143
                }
 
144
        }
 
145
 
 
146
        /**
 
147
         * Gets the name of the PostgreSQL database, running on the server identified
 
148
         * by the serverName property.
 
149
         */
 
150
        public String getDatabaseName()
 
151
        {
 
152
                return databaseName;
 
153
        }
 
154
 
 
155
        /**
 
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.
 
159
         */
 
160
        public void setDatabaseName(String databaseName)
 
161
        {
 
162
                this.databaseName = databaseName;
 
163
        }
 
164
 
 
165
        /**
 
166
         * Gets a description of this DataSource-ish thing.  Must be customized by
 
167
         * subclasses.
 
168
         */
 
169
        public abstract String getDescription();
 
170
 
 
171
        /**
 
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.
 
174
         */
 
175
        public String getUser()
 
176
        {
 
177
                return user;
 
178
        }
 
179
 
 
180
        /**
 
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.
 
184
         */
 
185
        public void setUser(String user)
 
186
        {
 
187
                this.user = user;
 
188
        }
 
189
 
 
190
        /**
 
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.
 
194
         */
 
195
        public String getPassword()
 
196
        {
 
197
                return password;
 
198
        }
 
199
 
 
200
        /**
 
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.
 
205
         */
 
206
        public void setPassword(String password)
 
207
        {
 
208
                this.password = password;
 
209
        }
 
210
 
 
211
        /**
 
212
         * Gets the port which the PostgreSQL server is listening on for TCP/IP
 
213
         * connections.
 
214
         *
 
215
         * @return The port, or 0 if the default port will be used.
 
216
         */
 
217
        public int getPortNumber()
 
218
        {
 
219
                return portNumber;
 
220
        }
 
221
 
 
222
        /**
 
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.
 
226
         */
 
227
        public void setPortNumber(int portNumber)
 
228
        {
 
229
                this.portNumber = portNumber;
 
230
        }
 
231
 
 
232
        /**
 
233
         * Generates a DriverManager URL from the other properties supplied.
 
234
         */
 
235
        private String getUrl()
 
236
        {
 
237
                return "jdbc:postgresql://" + serverName + (portNumber == 0 ? "" : ":" + portNumber) + "/" + databaseName;
 
238
        }
 
239
 
 
240
    /**
 
241
     * Generates a reference using the appropriate object factory.  This
 
242
     * implementation uses the JDBC 2 optional package object factory.
 
243
     */
 
244
    protected Reference createReference()
 
245
    {
 
246
        return new Reference(getClass().getName(), PGObjectFactory.class.getName(), null);
 
247
    }
 
248
 
 
249
        public Reference getReference() throws NamingException
 
250
        {
 
251
                Reference ref = createReference();
 
252
                ref.add(new StringRefAddr("serverName", serverName));
 
253
                if (portNumber != 0)
 
254
                {
 
255
                        ref.add(new StringRefAddr("portNumber", Integer.toString(portNumber)));
 
256
                }
 
257
                ref.add(new StringRefAddr("databaseName", databaseName));
 
258
                if (user != null)
 
259
                {
 
260
                        ref.add(new StringRefAddr("user", user));
 
261
                }
 
262
                if (password != null)
 
263
                {
 
264
                        ref.add(new StringRefAddr("password", password));
 
265
                }
 
266
                return ref;
 
267
        }
 
268
 
 
269
        protected void writeBaseObject(ObjectOutputStream out) throws IOException
 
270
        {
 
271
                out.writeObject(serverName);
 
272
                out.writeObject(databaseName);
 
273
                out.writeObject(user);
 
274
                out.writeObject(password);
 
275
                out.writeInt(portNumber);
 
276
        }
 
277
 
 
278
        protected void readBaseObject(ObjectInputStream in) throws IOException, ClassNotFoundException
 
279
        {
 
280
                serverName = (String)in.readObject();
 
281
                databaseName = (String)in.readObject();
 
282
                user = (String)in.readObject();
 
283
                password = (String)in.readObject();
 
284
                portNumber = in.readInt();
 
285
        }
 
286
 
 
287
}