Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@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
You can’t perform that action at this time.