|
| 1 | +packageorg.postgresql.jdbc2.optional; |
| 2 | + |
| 3 | +importjavax.naming.*; |
| 4 | +importjava.io.PrintWriter; |
| 5 | +importjava.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 | +publicabstractclassBaseDataSourceimplementsReferenceable { |
| 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 (ClassNotFoundExceptione) { |
| 21 | +System.err.println("PostgreSQL DataSource unable to load PostgreSQL JDBC Driver"); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | +// Needed to implement the DataSource/ConnectionPoolDataSource interfaces |
| 26 | +privatetransientPrintWriterlogger; |
| 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 | +privateStringserverName ="localhost"; |
| 31 | +privateStringdatabaseName; |
| 32 | +privateStringuser; |
| 33 | +privateStringpassword; |
| 34 | +privateintportNumber; |
| 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 | +publicConnectiongetConnection()throwsSQLException { |
| 46 | +returngetConnection(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 | +publicConnectiongetConnection(Stringuser,Stringpassword)throwsSQLException { |
| 60 | +try { |
| 61 | +Connectioncon =DriverManager.getConnection(getUrl(),user,password); |
| 62 | +if (logger !=null) { |
| 63 | +logger.println("Created a non-pooled connection for " +user +" at " +getUrl()); |
| 64 | + } |
| 65 | +returncon; |
| 66 | + }catch (SQLExceptione) { |
| 67 | +if (logger !=null) { |
| 68 | +logger.println("Failed to create a non-pooled connection for " +user +" at " +getUrl() +": " +e); |
| 69 | + } |
| 70 | +throwe; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | +/** |
| 75 | + * This DataSource does not support a configurable login timeout. |
| 76 | + * @return 0 |
| 77 | + */ |
| 78 | +publicintgetLoginTimeout()throwsSQLException { |
| 79 | +return0; |
| 80 | + } |
| 81 | + |
| 82 | +/** |
| 83 | + * This DataSource does not support a configurable login timeout. Any value |
| 84 | + * provided here will be ignored. |
| 85 | + */ |
| 86 | +publicvoidsetLoginTimeout(inti)throwsSQLException { |
| 87 | + } |
| 88 | + |
| 89 | +/** |
| 90 | + * Gets the log writer used to log connections opened. |
| 91 | + */ |
| 92 | +publicPrintWritergetLogWriter()throwsSQLException { |
| 93 | +returnlogger; |
| 94 | + } |
| 95 | + |
| 96 | +/** |
| 97 | + * The DataSource will note every connection opened to the provided log writer. |
| 98 | + */ |
| 99 | +publicvoidsetLogWriter(PrintWriterprintWriter)throwsSQLException { |
| 100 | +logger =printWriter; |
| 101 | + } |
| 102 | + |
| 103 | +/** |
| 104 | + * Gets the name of the host the PostgreSQL database is running on. |
| 105 | + */ |
| 106 | +publicStringgetServerName() { |
| 107 | +returnserverName; |
| 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 | +publicvoidsetServerName(StringserverName) { |
| 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 | +publicStringgetDatabaseName() { |
| 128 | +returndatabaseName; |
| 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 | +publicvoidsetDatabaseName(StringdatabaseName) { |
| 137 | +this.databaseName =databaseName; |
| 138 | + } |
| 139 | + |
| 140 | +/** |
| 141 | + * Gets a description of this DataSource-ish thing. Must be customized by |
| 142 | + * subclasses. |
| 143 | + */ |
| 144 | +publicabstractStringgetDescription(); |
| 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 | +publicStringgetUser() { |
| 151 | +returnuser; |
| 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 | +publicvoidsetUser(Stringuser) { |
| 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 | +publicStringgetPassword() { |
| 169 | +returnpassword; |
| 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 | +publicvoidsetPassword(Stringpassword) { |
| 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 | +publicintgetPortNumber() { |
| 189 | +returnportNumber; |
| 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 | +publicvoidsetPortNumber(intportNumber) { |
| 198 | +this.portNumber =portNumber; |
| 199 | + } |
| 200 | + |
| 201 | +/** |
| 202 | + * Generates a DriverManager URL from the other properties supplied. |
| 203 | + */ |
| 204 | +privateStringgetUrl() { |
| 205 | +return"jdbc:postgresql://"+serverName+(portNumber ==0 ?"" :":"+portNumber)+"/"+databaseName; |
| 206 | + } |
| 207 | + |
| 208 | +publicReferencegetReference()throwsNamingException { |
| 209 | +Referenceref =newReference(getClass().getName(),PGObjectFactory.class.getName(),null); |
| 210 | +ref.add(newStringRefAddr("serverName",serverName)); |
| 211 | +if (portNumber !=0) { |
| 212 | +ref.add(newStringRefAddr("portNumber",Integer.toString(portNumber))); |
| 213 | + } |
| 214 | +ref.add(newStringRefAddr("databaseName",databaseName)); |
| 215 | +if (user !=null) { |
| 216 | +ref.add(newStringRefAddr("user",user)); |
| 217 | + } |
| 218 | +if (password !=null) { |
| 219 | +ref.add(newStringRefAddr("password",password)); |
| 220 | + } |
| 221 | +returnref; |
| 222 | + } |
| 223 | + |
| 224 | +} |