From 222513c30f3c0c0e7bfce48b6c96d9ddfba75dba Mon Sep 17 00:00:00 2001 From: Ryan Goulding Date: Wed, 13 Sep 2017 16:58:19 -0400 Subject: Clean up BaseDBConfiguration class This change isn't too big; it mainly just attempts to clean up the BaseDBConfiguration class using best practices. For example: * documentation is added surrounding public methods, public constants, protected variables, and class headers. * code logic for parsing "optional" integer properties is abstracted into a separate private static function, which returns a default value if the given input property cannot be parsed or is not present. * public functions where the NumberFormatException RuntimeException might be thrown have the appriorate "throws" modifier added to their function signatures to indicate that the consumer is responsible for exception handling. * constants are extracted in place of "magic numbers" This is really just code sanitation work. Change-Id: Ifaf092e10b17f54c7cce0a888aa49bfe8377cdd3 Issue-Id: SDNC-79 Signed-off-by: Ryan Goulding --- .../sli/core/dblib/config/BaseDBConfiguration.java | 289 +++++++++++++++------ 1 file changed, 211 insertions(+), 78 deletions(-) (limited to 'dblib') diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/BaseDBConfiguration.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/BaseDBConfiguration.java index 4b738d42..d53be0e7 100644 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/BaseDBConfiguration.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/BaseDBConfiguration.java @@ -22,83 +22,216 @@ package org.onap.ccsdk.sli.core.dblib.config; import java.util.Properties; +/** + * Base class responsible for parsing business logic for database configuration from given Properties. + */ public abstract class BaseDBConfiguration { - public static final String DATABASE_TYPE = "org.onap.ccsdk.sli.dbtype"; - public static final String DATABASE_URL = "org.onap.ccsdk.sli.jdbc.url"; - public static final String DATABASE_NAME = "org.onap.ccsdk.sli.jdbc.database"; - public static final String CONNECTION_NAME = "org.onap.ccsdk.sli.jdbc.connection.name"; - public static final String DATABASE_USER = "org.onap.ccsdk.sli.jdbc.user"; - public static final String DATABASE_PSSWD = "org.onap.ccsdk.sli.jdbc.password"; - public static final String CONNECTION_TIMEOUT="org.onap.ccsdk.sli.jdbc.connection.timeout"; - public static final String REQUEST_TIMEOUT = "org.onap.ccsdk.sli.jdbc.request.timeout"; - public static final String MIN_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.min"; - public static final String MAX_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.max"; - public static final String INIT_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.init"; - public static final String DATABASE_HOSTS = "org.onap.ccsdk.sli.jdbc.hosts"; - - - protected final Properties props; - - public BaseDBConfiguration(Properties properties) { - this.props = properties; - } - - public int getConnTimeout() { - try { - String value = props.getProperty(CONNECTION_TIMEOUT); - return Integer.parseInt(value); - } catch(Exception exc) { - return -1; - } - } - - public int getRequestTimeout() { - try { - String value = props.getProperty(REQUEST_TIMEOUT); - if(value == null) - return -1; - return Integer.parseInt(value); - } catch(Exception exc) { - return -1; - } - } - - public String getDbConnectionName() { - return props.getProperty(CONNECTION_NAME); - } - - public String getDatabaseName() { - return props.getProperty(DATABASE_NAME); - } - - public String getDbUserId() { - return props.getProperty(DATABASE_USER); - } - - public String getDbPasswd() { - return props.getProperty(DATABASE_PSSWD); - } - - public int getDbMinLimit() { - String value = props.getProperty(MIN_LIMIT); - return Integer.parseInt(value); - } - - public int getDbMaxLimit() { - String value = props.getProperty(MAX_LIMIT); - return Integer.parseInt(value); - } - - public int getDbInitialLimit() { - String value = props.getProperty(INIT_LIMIT); - return Integer.parseInt(value); - } - - public String getDbUrl() { - return props.getProperty(DATABASE_URL); - } - - public String getServerGroup() { - return null; - } + + /** + * Property key within a properties configuration File for db type + */ + public static final String DATABASE_TYPE = "org.onap.ccsdk.sli.dbtype"; + + /** + * Property key with a properties configuration File for db url + */ + public static final String DATABASE_URL = "org.onap.ccsdk.sli.jdbc.url"; + + /** + * Property key with a properties configuration File for database name + */ + public static final String DATABASE_NAME = "org.onap.ccsdk.sli.jdbc.database"; + + /** + * Property key with a properties configuration File for db database connection name + */ + public static final String CONNECTION_NAME = "org.onap.ccsdk.sli.jdbc.connection.name"; + + /** + * Property key with a properties configuration File for database user + */ + public static final String DATABASE_USER = "org.onap.ccsdk.sli.jdbc.user"; + + /** + * Property key with a properties configuration File for database password for associated with + * org.onap.ccsdk.sli.jdbc.user. + */ + public static final String DATABASE_PSSWD = "org.onap.ccsdk.sli.jdbc.password"; + + /** + * Property key with a properties configuration File for database connection timeout + */ + public static final String CONNECTION_TIMEOUT="org.onap.ccsdk.sli.jdbc.connection.timeout"; + + /** + * Property key with a properties configuration File for database request timeout + */ + public static final String REQUEST_TIMEOUT = "org.onap.ccsdk.sli.jdbc.request.timeout"; + + /** + * Property key with a properties configuration File for database minimum limit + */ + public static final String MIN_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.min"; + + /** + * Property key with a properties configuration File for database maximum limit + */ + public static final String MAX_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.max"; + + /** + * Property key with a properties configuration File for database initial limit + */ + public static final String INIT_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.init"; + + /** + * Property key with a properties configuration File for database hosts + */ + public static final String DATABASE_HOSTS = "org.onap.ccsdk.sli.jdbc.hosts"; + + /** + * default value when the connection timeout is not present or cannot be parsed + */ + private static final int DEFAULT_CONNECTION_TIMEOUT = -1; + + /** + * default value when the request timeout is not present or cannot be parsed + */ + private static final int DEFAULT_REQUEST_TIMEOUT = -1; + + /** + * A set of properties with database configuration information. + */ + protected final Properties properties; + + /** + * Builds a configuration based on given properties + * + * @param properties properties represented by the public constant keys defined by this class + */ + public BaseDBConfiguration(final Properties properties) { + this.properties = properties; + } + + /** + * Extracts the connection timeout. + * + * @return the connection timeout, or DEFAULT_CONNECTION_TIMEOUT if not present + */ + public int getConnTimeout() { + return extractProperty(properties, CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); + } + + /** + * Extracts the request timeout. + * + * @return the request timeout, or DEFAULT_REQUEST_TIMEOUT if not present + */ + public int getRequestTimeout() { + return extractProperty(properties, REQUEST_TIMEOUT, DEFAULT_REQUEST_TIMEOUT); + } + + /** + * A utility method to extract int property from Properties. + * + * @param properties a set of Properties + * @param propertyKey the key in question + * @param defaultValue the value to return if the key does not exist + * @return Either the property value for propertyKey or defaultValue if not present + */ + private static int extractProperty(final Properties properties, final String propertyKey, final int defaultValue) { + try { + final String valueString = properties.getProperty(propertyKey, Integer.toString(defaultValue)); + return Integer.parseInt(valueString); + } catch(final NumberFormatException e) { + return defaultValue; + } + } + + /** + * Extracts the db connection name. + * + * @return the db connection name, or null if not present + */ + public String getDbConnectionName() { + return properties.getProperty(CONNECTION_NAME); + } + + /** + * Extracts the db name. + * + * @return the db name, or null if not present + */ + public String getDatabaseName() { + return properties.getProperty(DATABASE_NAME); + } + + /** + * Extracts the db user id. + * + * @return the db user id, or null if not present + */ + public String getDbUserId() { + return properties.getProperty(DATABASE_USER); + } + + /** + * Extracts the db password. + * + * @return the db password, or null if not present + */ + public String getDbPasswd() { + return properties.getProperty(DATABASE_PSSWD); + } + + /** + * Extracts the db min limit. + * + * @return the db min limit + * @throws NumberFormatException if the property is not specified, or cannot be parsed as an Integer. + */ + public int getDbMinLimit() throws NumberFormatException { + String value = properties.getProperty(MIN_LIMIT); + return Integer.parseInt(value); + } + + /** + * Extracts the db max limit. + * + * @return the db max limit + * @throws NumberFormatException if the property is not specified, or cannot be parsed as an Integer. + */ + public int getDbMaxLimit() throws NumberFormatException { + String value = properties.getProperty(MAX_LIMIT); + return Integer.parseInt(value); + } + + /** + * Extracts the db initial limit. + * + * @return the db initial limit + * @throws NumberFormatException if the property is not specified, or cannot be parsed as an Integer. + */ + public int getDbInitialLimit() throws NumberFormatException { + String value = properties.getProperty(INIT_LIMIT); + return Integer.parseInt(value); + } + + /** + * Extracts the db url. + * + * @return the db url, or null if not present + */ + public String getDbUrl() { + return properties.getProperty(DATABASE_URL); + } + + /** + * Extracts the db server group. + * + * @return null + */ + @Deprecated + public String getServerGroup() { + return null; + } } -- cgit 1.2.3-korg