summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine
diff options
context:
space:
mode:
authorDavid A. Madore <david+git@madore.org>2011-09-01 18:36:00 +0200
committerDavid A. Madore <david+git@madore.org>2011-09-01 18:36:00 +0200
commit7d269b2558b781d9f1265a12b1e245315412e02a (patch)
treef74c3d65cd552eb52a2b5aefc3b655897c50a310 /org/madore/damlengine
parentacdbedc507679be7b35a0fe10990bfb579bdc9d9 (diff)
downloaddamlengine-7d269b2558b781d9f1265a12b1e245315412e02a.tar.gz
damlengine-7d269b2558b781d9f1265a12b1e245315412e02a.tar.bz2
damlengine-7d269b2558b781d9f1265a12b1e245315412e02a.zip
Introduce (limited) support for PG* environment variables and .pgpass file.
Diffstat (limited to 'org/madore/damlengine')
-rw-r--r--org/madore/damlengine/WeblogDatabaseConnection.java51
1 files changed, 48 insertions, 3 deletions
diff --git a/org/madore/damlengine/WeblogDatabaseConnection.java b/org/madore/damlengine/WeblogDatabaseConnection.java
index c88f5a8..9347406 100644
--- a/org/madore/damlengine/WeblogDatabaseConnection.java
+++ b/org/madore/damlengine/WeblogDatabaseConnection.java
@@ -1,6 +1,10 @@
package org.madore.damlengine;
import java.util.Properties;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.BufferedReader;
+import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import org.postgresql.Driver;
@@ -16,10 +20,51 @@ public final class WeblogDatabaseConnection {
public static Connection getConnection()
throws SQLException {
if ( conn == null ) {
- final String dbUrl = "jdbc:postgresql://localhost/weblog";
+ String dbHost = System.getenv("PGHOST");
+ if ( dbHost == null )
+ dbHost = "localhost";
+ String dbPort = System.getenv("PGPORT");
+ if ( dbPort == null )
+ dbPort = "5432";
+ String dbName = "weblog";
+ String dbUser = System.getenv("PGUSER");
+ if ( dbUser == null )
+ dbUser = System.getenv("USER");
+ if ( dbUser == null )
+ dbUser = System.getProperty("user.name");
+ if ( dbUser == null )
+ dbUser = dbName;
+ String dbPass = System.getenv("PGPASSWORD");
+ if ( dbPass == null )
+ try {
+ String dbPassFile = System.getenv("PGPASSFILE");
+ if ( dbPassFile == null )
+ dbPassFile = System.getProperty("user.home")
+ + "/.pgpass";
+ BufferedReader buf
+ = new BufferedReader(new InputStreamReader(new FileInputStream(dbPassFile)));
+ String line;
+ while ( ( line = buf.readLine() ) != null ) {
+ String[] elts = line.split(":");
+ if ( elts.length == 5
+ && ( elts[0].equals("*") || elts[0].equals(dbHost) )
+ && ( elts[1].equals("*") || elts[1].equals(dbPort) )
+ && ( elts[2].equals("*") || elts[2].equals(dbName) )
+ && ( elts[3].equals("*") || elts[3].equals(dbUser) ) ) {
+ dbPass = elts[4];
+ break;
+ }
+ }
+ } catch (IOException e) {
+ // Ignore
+ }
+ if ( dbPass == null )
+ dbPass = "";
+ final String dbUrl
+ = "jdbc:postgresql://"+dbHost+":"+dbPort+"/"+dbName;
final Properties dbProps = new Properties();
- dbProps.setProperty("user", "david");
- dbProps.setProperty("password", "IHATETHISWHYCANTIUSEUNIXDOMAINSOCKETS");
+ dbProps.setProperty("user", dbUser);
+ dbProps.setProperty("password", dbPass);
dbProps.setProperty("ssl", "true");
dbProps.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");
conn = (new Driver()).connect(dbUrl, dbProps);