001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.postgresql;
007:
008: import org.apache.commons.dbcp.BasicDataSource;
009: import org.openrdf.sail.SailException;
010: import org.openrdf.sail.rdbms.RdbmsStore;
011: import org.openrdf.sail.rdbms.exceptions.RdbmsException;
012:
013: /**
014: * A convenient way to initialise a PostgreSQL RDF store.
015: *
016: * @author James Leigh
017: *
018: */
019: public class PgSqlStore extends RdbmsStore {
020: private String serverName;
021: private String databaseName;
022: private int portNumber;
023: private String user;
024: private String password;
025:
026: public PgSqlStore() {
027: super ();
028: }
029:
030: public PgSqlStore(String databaseName) {
031: setDatabaseName(databaseName);
032: }
033:
034: public String getServerName() {
035: return serverName;
036: }
037:
038: public void setServerName(String serverName) {
039: this .serverName = serverName;
040: }
041:
042: public String getDatabaseName() {
043: return databaseName;
044: }
045:
046: public void setDatabaseName(String databaseName) {
047: this .databaseName = databaseName;
048: }
049:
050: public int getPortNumber() {
051: return portNumber;
052: }
053:
054: public void setPortNumber(int portNumber) {
055: this .portNumber = portNumber;
056: }
057:
058: public String getUser() {
059: return user;
060: }
061:
062: public void setUser(String user) {
063: this .user = user;
064: }
065:
066: public String getPassword() {
067: return password;
068: }
069:
070: public void setPassword(String password) {
071: this .password = password;
072: }
073:
074: @Override
075: public void initialize() throws SailException {
076: try {
077: Class.forName("org.postgresql.Driver");
078: } catch (ClassNotFoundException e) {
079: throw new RdbmsException(e.toString(), e);
080: }
081: StringBuilder url = new StringBuilder();
082: url.append("jdbc:postgresql:");
083: if (serverName != null) {
084: url.append("//").append(serverName);
085: if (portNumber > 0) {
086: url.append(":").append(portNumber);
087: }
088: url.append("/");
089: }
090: url.append(databaseName);
091: BasicDataSource ds = new BasicDataSource();
092: ds.setUrl(url.toString());
093: if (user != null) {
094: ds.setUsername(user);
095: } else {
096: ds.setUsername(System.getProperty("user.name"));
097: }
098: if (password != null) {
099: ds.setPassword(password);
100: }
101: PgSqlConnectionFactory factory = new PgSqlConnectionFactory();
102: factory.setSail(this);
103: factory.setDataSource(ds);
104: setConnectionFactory(factory);
105: super.initialize();
106: }
107: }
|