Last active
October 26, 2019 06:43
-
-
Save steinarb/3f3e370d687865408c6a59a464bee345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component(service=UkelonnDatabase.class, immediate=true) | |
public class PGUkelonnDatabaseProvider implements UkelonnDatabase { | |
@Activate | |
public void activate(Map<String, Object> config) { | |
createDatasource(config); | |
try(Connection connect = getConnection()) { | |
... (create schema and insert initial data) | |
} catch (Exception e) { | |
logError("Failed to create ukelonn database schema in the PostgreSQL ukelonn database", e); | |
} | |
} | |
void createDatasource(Map<String, Object> config) { | |
Properties properties = createDatabaseConnectionProperties(config); | |
try { | |
datasource = dataSourceFactory.createDataSource(properties); | |
} catch (Exception e) { | |
logError("PostgreSQL database service failed to create connection to local DB server", e); | |
} | |
} | |
Properties createDatabaseConnectionProperties(Map<String, Object> config) { | |
String jdbcUrl = (String) config.getOrDefault(UKELONN_JDBC_URL, "jdbc:postgresql:///ukelonn"); | |
String jdbcUser = (String) config.get(UKELONN_JDBC_USER); | |
String jdbcPassword = (String) config.get(UKELONN_JDBC_PASSWORD); | |
Properties properties = new Properties(); | |
properties.setProperty(DataSourceFactory.JDBC_URL, jdbcUrl); | |
if (jdbcUser != null) { | |
properties.setProperty(DataSourceFactory.JDBC_USER, jdbcUser); | |
} | |
if (jdbcPassword != null) { | |
properties.setProperty(DataSourceFactory.JDBC_PASSWORD, jdbcPassword); | |
} | |
return properties; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment