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; public final class WeblogDatabaseConnection { private WeblogDatabaseConnection() { // Forbid instantiation throw new AssertionError("WeblogDatabaseConnection cannot be instantiated"); } public static ThreadLocal pconn = new ThreadLocal(); public static Connection getConnection() throws SQLException { Connection conn = pconn.get(); if ( conn == null || conn.isClosed() || ! conn.isValid(5) ) { String dbHost = null; if ( ! DamlEngine.runAsServlet ) dbHost = System.getenv("DAMLENGINE_PGHOST"); if ( dbHost == null ) dbHost = DamlEngine.appProps.getProperty("pghost"); if ( dbHost == null && ! DamlEngine.runAsServlet ) dbHost = System.getenv("PGHOST"); if ( dbHost == null ) dbHost = "localhost"; String dbPort = null; if ( ! DamlEngine.runAsServlet ) dbPort = System.getenv("DAMLENGINE_PGPORT"); if ( dbPort == null ) dbPort = DamlEngine.appProps.getProperty("pgport"); if ( dbPort == null && ! DamlEngine.runAsServlet ) dbPort = System.getenv("PGPORT"); if ( dbPort == null ) dbPort = "5432"; String dbName = null; if ( ! DamlEngine.runAsServlet ) dbName = System.getenv("DAMLENGINE_DBNAME"); if ( dbName == null ) dbName = DamlEngine.appProps.getProperty("dbname"); if ( dbName == null ) dbName = "weblog"; String dbUser = null; if ( ! DamlEngine.runAsServlet ) dbUser = System.getenv("DAMLENGINE_PGUSER"); if ( dbUser == null ) dbUser = DamlEngine.appProps.getProperty("pguser"); if ( dbUser == null && ! DamlEngine.runAsServlet ) dbUser = System.getenv("PGUSER"); if ( dbUser == null && ! DamlEngine.runAsServlet ) dbUser = System.getenv("USER"); if ( dbUser == null ) dbUser = System.getProperty("user.name"); if ( dbUser == null ) dbUser = dbName; String dbPass = null; if ( ! DamlEngine.runAsServlet ) dbPass = System.getenv("DAMLENGINE_PGPASSWORD"); if ( dbPass == null ) dbPass = DamlEngine.appProps.getProperty("pgpassword"); if ( dbPass == null && ! DamlEngine.runAsServlet ) dbPass = System.getenv("PGPASSWORD"); if ( dbPass == null ) { String dbPassFile = null; if ( ! DamlEngine.runAsServlet ) dbPassFile = System.getenv("DAMLENGINE_PGPASSFILE"); if ( dbPassFile == null ) dbPassFile = DamlEngine.appProps.getProperty("pgpassfile"); if ( dbPassFile == null && ! DamlEngine.runAsServlet ) dbPassFile = System.getenv("PGPASSFILE"); if ( dbPassFile == null && ! DamlEngine.runAsServlet ) dbPassFile = System.getProperty("user.home") + "/.pgpass"; if ( dbPassFile != null ) try { 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(); final Driver drv = new Driver(); dbProps.setProperty("user", dbUser); dbProps.setProperty("password", dbPass); dbProps.setProperty("ssl", "true"); if ( drv.getMajorVersion() >= 42 ) dbProps.setProperty("sslmode", "prefer"); dbProps.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory"); conn = drv.connect(dbUrl, dbProps); conn.createStatement().execute("SET TIME ZONE 0"); pconn.set(conn); } return conn; } }