comparison artifact-database/src/main/java/de/intevation/artifactdatabase/DBConnection.java @ 87:0f48188a6e02

Added some javadoc to the artifactdatabase module. Not done yet. artifacts/trunk@839 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 26 Mar 2010 11:40:28 +0000
parents 93edc04f3a10
children d348fe1fd822
comparison
equal deleted inserted replaced
86:b2e0cb83631c 87:0f48188a6e02
9 import java.io.File; 9 import java.io.File;
10 10
11 import org.apache.log4j.Logger; 11 import org.apache.log4j.Logger;
12 12
13 /** 13 /**
14 * @author Sascha L. Teichmann 14 * This class encapsulate the creation and pooling of database connections used
15 * by the artifact database. The credential to open the database connections
16 * are taken from the global configuratiion.
17 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
15 */ 18 */
16 public class DBConnection 19 public class DBConnection
17 { 20 {
18 private static Logger logger = Logger.getLogger(DBConnection.class); 21 private static Logger logger = Logger.getLogger(DBConnection.class);
19 22
23 /**
24 * XPath to access the database driver within the global configuration.
25 */
20 public static final String DB_DRIVER = 26 public static final String DB_DRIVER =
21 "/artifact-database/database/driver/text()"; 27 "/artifact-database/database/driver/text()";
28 /**
29 * XPath to access the database URL within the global configuration.
30 */
22 public static final String DB_URL = 31 public static final String DB_URL =
23 "/artifact-database/database/url/text()"; 32 "/artifact-database/database/url/text()";
33 /**
34 * XPath to access the database use within the global configuration.
35 */
24 public static final String DB_USER = 36 public static final String DB_USER =
25 "/artifact-database/database/user/text()"; 37 "/artifact-database/database/user/text()";
38 /**
39 * XPath to access the database password within the global configuration.
40 */
26 public static final String DB_PASSWORD = 41 public static final String DB_PASSWORD =
27 "/artifact-database/database/password/text()"; 42 "/artifact-database/database/password/text()";
28 43
44 /**
45 * The default database driver: H2
46 */
29 public static final String DEFAULT_DRIVER = 47 public static final String DEFAULT_DRIVER =
30 "org.h2.Driver"; 48 "org.h2.Driver";
31 49
50 /**
51 * The default database name: artifacts.db
52 */
32 public static final String DEFAULT_DATABASE_FILE = 53 public static final String DEFAULT_DATABASE_FILE =
33 "artifacts.db"; 54 "artifacts.db";
34 55
56 /**
57 * The default database URL: This is created once by #getDefaultURL()
58 */
35 public static final String DEFAULT_URL = getDefaultURL(); 59 public static final String DEFAULT_URL = getDefaultURL();
36 60
61 /**
62 * The default database user: ""
63 */
37 public static final String DEFAULT_USER = ""; 64 public static final String DEFAULT_USER = "";
65
66 /**
67 * The default database password: ""
68 */
38 public static final String DEFAULT_PASSWORD = ""; 69 public static final String DEFAULT_PASSWORD = "";
39 70
40 private DBConnection() { 71 private DBConnection() {
41 } 72 }
42 73
74 /**
75 * Constructs the default databse URL. It concats the
76 * config directory and the #DEFAULT_DATABASE_FILE
77 * to the string with is needed to access H2 databases.
78 * @return
79 */
43 public static final String getDefaultURL() { 80 public static final String getDefaultURL() {
44 File configDir = Config.getConfigDirectory(); 81 File configDir = Config.getConfigDirectory();
45 File databaseFile = new File(configDir, DEFAULT_DATABASE_FILE); 82 File databaseFile = new File(configDir, DEFAULT_DATABASE_FILE);
46 return "jdbc:h2:" + databaseFile; 83 return "jdbc:h2:" + databaseFile;
47 } 84 }
48 85
49 private static BasicDataSource dataSource; 86 private static BasicDataSource dataSource;
50 87
51 private static final void addShutdownHook() { 88 private static final void addShutdownHook() {
52 Runtime.getRuntime().addShutdownHook(new Thread() { 89 Runtime.getRuntime().addShutdownHook(new Thread() {
90 @Override
53 public void run() { 91 public void run() {
54 if (dataSource != null) { 92 if (dataSource != null) {
55 try { 93 try {
56 dataSource.close(); 94 dataSource.close();
57 } 95 }
61 } 99 }
62 } 100 }
63 }); 101 });
64 } 102 }
65 103
104 /**
105 * Static method to fetch a database connection.
106 * @return a DataSource to access the database connection.
107 */
66 public static synchronized DataSource getDataSource() { 108 public static synchronized DataSource getDataSource() {
67 if (dataSource == null) { 109 if (dataSource == null) {
68 dataSource = new BasicDataSource(); 110 dataSource = new BasicDataSource();
69 111
70 String driver = Config.getStringXPath( 112 String driver = Config.getStringXPath(

http://dive4elements.wald.intevation.org