Skip to content
Snippets Groups Projects
Commit 08372956 authored by Nikolaus Krismer's avatar Nikolaus Krismer
Browse files

refactored constructors

parent 81ffa6ca
No related branches found
No related tags found
No related merge requests found
...@@ -28,57 +28,43 @@ import java.util.Properties; ...@@ -28,57 +28,43 @@ import java.util.Properties;
* @author <a href="mailto:markus.innerebner@inf.unibz.it">Markus Innerebner</a>. * @author <a href="mailto:markus.innerebner@inf.unibz.it">Markus Innerebner</a>.
* @version 2.2 * @version 2.2
*/ */
//TODO: Shouldn't Config be a singleton class
public class Config { public class Config {
protected Properties internalProperties; protected Properties internalProperties;
protected final ConnectionFactory factory; protected final ConnectionFactory factory;
protected short densityLimit = Short.MIN_VALUE; protected short densityLimit = Short.MIN_VALUE;
// Constructors
public Config() { public Config() {
this(null); this("config.xml");
} }
public Config(final Dataset dataset) { public Config(final String fileName) {
internalProperties = new Properties(); appendPropertiesFromFile(fileName);
final String fileName = "config.xml";
final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
appendPropertyFile(in);
if (dataset != null) {
final String fileName2 = "config_" + dataset.toString().toLowerCase() + ".xml";
final InputStream in2 = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName2);
appendPropertyFile(in2);
}
factory = initConnectionFactory(); factory = initConnectionFactory();
} }
protected Properties getInternalProperties() { public Config(final Dataset dataset) {
return internalProperties; appendPropertiesFromFile("config.xml");
} appendPropertiesFromFile("config_" + dataset.toString().toLowerCase() + ".xml");
protected ConnectionFactory initConnectionFactory() { factory = initConnectionFactory();
final String serverName = getDBServerName(); }
final String dbName = getDBName();
final int dbPort = getDBPort();
final String dbUser = getDBUser();
final String dbPassword = getDBPassword();
final boolean isPooling = getDBPooling();
ConnectionFactory f = null; public Config(final Config config) {
try { if (config == null) {
f = new ConnectionFactory(isPooling); throw new NullPointerException("Invalid config in clone constructor given!");
f.register(serverName, dbName, dbPort, dbUser, dbPassword);
} catch (final SQLException e) {
throw new RuntimeException("Unable to connect to the database: " + e);
} catch (final ClassNotFoundException e) {
throw new RuntimeException("Class not found: " + e);
} }
return f; internalProperties = new Properties();
appendProperties(config.internalProperties);
factory = config.factory;
} }
// Public methods
public Connection getConnection() { public Connection getConnection() {
try { try {
return factory.getConnection(); return factory.getConnection();
...@@ -127,34 +113,6 @@ public class Config { ...@@ -127,34 +113,6 @@ public class Config {
return Integer.parseInt(getProperty("org.postgresql.port")); return Integer.parseInt(getProperty("org.postgresql.port"));
} }
/**
* <p>
* Method appendPropertyFile
* </p>
* appends the properties of that stream. An existing property is overwritten
*
* @param inputStream
*/
protected void appendPropertyFile(final InputStream inputStream) {
try {
internalProperties.loadFromXML(inputStream);
internalProperties.putAll(System.getProperties());
final String density = getProperty("limit.density");
if (densityLimit == Short.MIN_VALUE && density != null) {
densityLimit = Short.parseShort(density);
}
} catch (final InvalidPropertiesFormatException e) {
throw new RuntimeException(e);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
public void appendProperties(final Properties properties) {
internalProperties.putAll(properties);
}
public String getEdgeTable() { public String getEdgeTable() {
return getProperty("tbl.links"); return getProperty("tbl.links");
} }
...@@ -183,4 +141,67 @@ public class Config { ...@@ -183,4 +141,67 @@ public class Config {
return Integer.parseInt(getProperty("sql.spatial.srid")); return Integer.parseInt(getProperty("sql.spatial.srid"));
} }
// Protected methods
/**
* This appends properties from the given file to the configuration.
* Old values will be kept, but overwritten by new values (if the same property has been specified)
*/
protected void appendPropertiesFromFile(final String fileName) {
internalProperties = new Properties();
final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
appendPropertiesFromStream(in);
}
/**
* This appends properties from the given stream to the configuration.
* Old values will be kept, but overwritten by new values (if the same property has been specified)
*/
protected void appendPropertiesFromStream(final InputStream inputStream) {
try {
internalProperties.loadFromXML(inputStream);
internalProperties.putAll(System.getProperties());
final String density = getProperty("limit.density");
if (densityLimit == Short.MIN_VALUE && density != null) {
densityLimit = Short.parseShort(density);
}
} catch (final InvalidPropertiesFormatException e) {
throw new RuntimeException(e);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
/**
* This merges properties to the configuration.
* Old values will be kept, but overwritten by new values (if the same property has been specified)
*/
protected void appendProperties(final Properties properties) {
internalProperties.putAll(properties);
}
// Private methods
private ConnectionFactory initConnectionFactory() {
final String serverName = getDBServerName();
final String dbName = getDBName();
final int dbPort = getDBPort();
final String dbUser = getDBUser();
final String dbPassword = getDBPassword();
final boolean isPooling = getDBPooling();
ConnectionFactory f = null;
try {
f = new ConnectionFactory(isPooling);
f.register(serverName, dbName, dbPort, dbUser, dbPassword);
} catch (final SQLException e) {
throw new RuntimeException("Unable to connect to the database: " + e);
} catch (final ClassNotFoundException e) {
throw new RuntimeException("Class not found: " + e);
}
return f;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment