|
| 1 | +package org.postgresql.jdbc2.optional; |
| 2 | + |
| 3 | +import javax.naming.*; |
| 4 | +import java.io.PrintWriter; |
| 5 | +import java.sql.*; |
| 6 | + |
| 7 | +/** |
| 8 | + * Base class for data sources and related classes. |
| 9 | + * |
| 10 | + * @author Aaron Mulder (ammulder@chariotsolutions.com) |
| 11 | + * @version $Revision: 1.1 $ |
| 12 | + */ |
| 13 | +public abstract class BaseDataSource implements Referenceable { |
| 14 | + // Load the normal driver, since we'll use it to actually connect to the |
| 15 | + // database. That way we don't have to maintain the connecting code in |
| 16 | + // multiple places. |
| 17 | + static { |
| 18 | + try { |
| 19 | + Class.forName("org.postgresql.Driver"); |
| 20 | + } catch (ClassNotFoundException e) { |
| 21 | + System.err.println("PostgreSQL DataSource unable to load PostgreSQL JDBC Driver"); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Needed to implement the DataSource/ConnectionPoolDataSource interfaces |
| 26 | + private transient PrintWriter logger; |
| 27 | + // Don't track loginTimeout, since we'd just ignore it anyway |
| 28 | + |
| 29 | + // Standard properties, defined in the JDBC 2.0 Optional Package spec |
| 30 | + private String serverName = "localhost"; |
| 31 | + private String databaseName; |
| 32 | + private String user; |
| 33 | + private String password; |
| 34 | + private int portNumber; |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets a connection to the PostgreSQL database. The database is identified by the |
| 38 | + * DataSource properties serverName, databaseName, and portNumber. The user to |
| 39 | + * connect as is identified by the DataSource properties user and password. |
| 40 | + * |
| 41 | + * @return A valid database connection. |
| 42 | + * @throws SQLException |
| 43 | + * Occurs when the database connection cannot be established. |
| 44 | + */ |
| 45 | + public Connection getConnection() throws SQLException { |
| 46 | + return getConnection(user, password); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Gets a connection to the PostgreSQL database. The database is identified by the |
| 51 | + * DataAource properties serverName, databaseName, and portNumber. The user to |
| 52 | + * connect as is identified by the arguments user and password, which override |
| 53 | + * the DataSource properties by the same name. |
| 54 | + * |
| 55 | + * @return A valid database connection. |
| 56 | + * @throws SQLException |
| 57 | + * Occurs when the database connection cannot be established. |
| 58 | + */ |
| 59 | + public Connection getConnection(String user, String password) throws SQLException { |
| 60 | + try { |
| 61 | + Connection con = DriverManager.getConnection(getUrl(), user, password); |
| 62 | + if (logger != null) { |
| 63 | + logger.println("Created a non-pooled connection for " + user + " at " + getUrl()); |
| 64 | + } |
| 65 | + return con; |
| 66 | + } catch (SQLException e) { |
| 67 | + if (logger != null) { |
| 68 | + logger.println("Failed to create a non-pooled connection for " + user + " at " + getUrl() + ": " + e); |
| 69 | + } |
| 70 | + throw e; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * This DataSource does not support a configurable login timeout. |
| 76 | + * @return 0 |
| 77 | + */ |
| 78 | + public int getLoginTimeout() throws SQLException { |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * This DataSource does not support a configurable login timeout. Any value |
| 84 | + * provided here will be ignored. |
| 85 | + */ |
| 86 | + public void setLoginTimeout(int i) throws SQLException { |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Gets the log writer used to log connections opened. |
| 91 | + */ |
| 92 | + public PrintWriter getLogWriter() throws SQLException { |
| 93 | + return logger; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * The DataSource will note every connection opened to the provided log writer. |
| 98 | + */ |
| 99 | + public void setLogWriter(PrintWriter printWriter) throws SQLException { |
| 100 | + logger = printWriter; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets the name of the host the PostgreSQL database is running on. |
| 105 | + */ |
| 106 | + public String getServerName() { |
| 107 | + return serverName; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Sets the name of the host the PostgreSQL database is running on. If this |
| 112 | + * is changed, it will only affect future calls to getConnection. The default |
| 113 | + * value is <tt>localhost</tt>. |
| 114 | + */ |
| 115 | + public void setServerName(String serverName) { |
| 116 | + if(serverName == null || serverName.equals("")) { |
| 117 | + this.serverName = "localhost"; |
| 118 | + } else { |
| 119 | + this.serverName = serverName; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Gets the name of the PostgreSQL database, running on the server identified |
| 125 | + * by the serverName property. |
| 126 | + */ |
| 127 | + public String getDatabaseName() { |
| 128 | + return databaseName; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Sets the name of the PostgreSQL database, running on the server identified |
| 133 | + * by the serverName property. If this is changed, it will only affect |
| 134 | + * future calls to getConnection. |
| 135 | + */ |
| 136 | + public void setDatabaseName(String databaseName) { |
| 137 | + this.databaseName = databaseName; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Gets a description of this DataSource-ish thing. Must be customized by |
| 142 | + * subclasses. |
| 143 | + */ |
| 144 | + public abstract String getDescription(); |
| 145 | + |
| 146 | + /** |
| 147 | + * Gets the user to connect as by default. If this is not specified, you must |
| 148 | + * use the getConnection method which takes a user and password as parameters. |
| 149 | + */ |
| 150 | + public String getUser() { |
| 151 | + return user; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Sets the user to connect as by default. If this is not specified, you must |
| 156 | + * use the getConnection method which takes a user and password as parameters. |
| 157 | + * If this is changed, it will only affect future calls to getConnection. |
| 158 | + */ |
| 159 | + public void setUser(String user) { |
| 160 | + this.user = user; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Gets the password to connect with by default. If this is not specified but a |
| 165 | + * password is needed to log in, you must use the getConnection method which takes |
| 166 | + * a user and password as parameters. |
| 167 | + */ |
| 168 | + public String getPassword() { |
| 169 | + return password; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Sets the password to connect with by default. If this is not specified but a |
| 174 | + * password is needed to log in, you must use the getConnection method which takes |
| 175 | + * a user and password as parameters. If this is changed, it will only affect |
| 176 | + * future calls to getConnection. |
| 177 | + */ |
| 178 | + public void setPassword(String password) { |
| 179 | + this.password = password; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Gets the port which the PostgreSQL server is listening on for TCP/IP |
| 184 | + * connections. |
| 185 | + * |
| 186 | + * @return The port, or 0 if the default port will be used. |
| 187 | + */ |
| 188 | + public int getPortNumber() { |
| 189 | + return portNumber; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Gets the port which the PostgreSQL server is listening on for TCP/IP |
| 194 | + * connections. Be sure the -i flag is passed to postmaster when PostgreSQL |
| 195 | + * is started. If this is not set, or set to 0, the default port will be used. |
| 196 | + */ |
| 197 | + public void setPortNumber(int portNumber) { |
| 198 | + this.portNumber = portNumber; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Generates a DriverManager URL from the other properties supplied. |
| 203 | + */ |
| 204 | + private String getUrl() { |
| 205 | + return "jdbc:postgresql://"+serverName+(portNumber == 0 ? "" : ":"+portNumber)+"/"+databaseName; |
| 206 | + } |
| 207 | + |
| 208 | + public Reference getReference() throws NamingException { |
| 209 | + Reference ref = new Reference(getClass().getName(), PGObjectFactory.class.getName(), null); |
| 210 | + ref.add(new StringRefAddr("serverName", serverName)); |
| 211 | + if (portNumber != 0) { |
| 212 | + ref.add(new StringRefAddr("portNumber", Integer.toString(portNumber))); |
| 213 | + } |
| 214 | + ref.add(new StringRefAddr("databaseName", databaseName)); |
| 215 | + if (user != null) { |
| 216 | + ref.add(new StringRefAddr("user", user)); |
| 217 | + } |
| 218 | + if (password != null) { |
| 219 | + ref.add(new StringRefAddr("password", password)); |
| 220 | + } |
| 221 | + return ref; |
| 222 | + } |
| 223 | + |
| 224 | +} |
0 commit comments