Skip to content

Instantly share code, notes, and snippets.

@steinarb
Last active October 26, 2019 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steinarb/3f3e370d687865408c6a59a464bee345 to your computer and use it in GitHub Desktop.
Save steinarb/3f3e370d687865408c6a59a464bee345 to your computer and use it in GitHub Desktop.
@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