DatabaseBuilder.java [src/m/utils] Revision: default Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package m.utils;
import csip.api.server.ServiceException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author ktraff
*/
public class DatabaseBuilder {
/**
* Represents all possible target database connections.
*/
public static enum DB {
POSTGRES, POSTGRES_GIS, POSTGRES_WRAP
};
private final String CONF_FILE = "resources/connection.properties";
private DB type;
private Logger log;
public String port;
public String dbName;
public String hostname;
public String username;
public String password;
public DatabaseBuilder() {
}
public DatabaseBuilder setAll() throws FileNotFoundException, IOException {
return setAll(DB.POSTGRES);
}
public DatabaseBuilder setAll(DB dbType) throws FileNotFoundException, IOException {
Properties prop = new Properties();
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(CONF_FILE);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + CONF_FILE + "' not found in the classpath");
}
setType(dbType)
.setHostname(prop.getProperty("hostname"))
.setPort(prop.getProperty("port"))
.setDbName(prop.getProperty("dbName"))
.setUsername(prop.getProperty("username"))
.setPassword(prop.getProperty("password"));
return this;
}
public DatabaseBuilder setType(DB type) {
this.type = type;
return this;
}
public DatabaseBuilder setHostname(String hostname) {
this.hostname = hostname;
return this;
}
public DatabaseBuilder setPort(String port) {
this.port = port;
return this;
}
public DatabaseBuilder setDbName(String dbName) {
this.dbName = dbName;
return this;
}
public DatabaseBuilder setUsername(String username) {
this.username = username;
return this;
}
public DatabaseBuilder setPassword(String password) {
this.password = password;
return this;
}
public IDatabase create() throws ServiceException {
switch (type) {
case POSTGRES_GIS:
return new PostgresqlGISDatabase(hostname, port, dbName, username, password);
default:
return new PostgresqlDatabase(hostname, port, dbName, username, password);
}
}
}