blob: 93474067b03e7253dc5c17be3041a9bedd98fc5b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
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 Connection conn;
public static Connection getConnection()
throws SQLException {
if ( conn == null ) {
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", dbUser);
dbProps.setProperty("password", dbPass);
dbProps.setProperty("ssl", "true");
dbProps.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");
conn = (new Driver()).connect(dbUrl, dbProps);
conn.createStatement().execute("SET TIME ZONE 0");
}
return conn;
}
}
|