use of org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration in project opennms by OpenNMS.
the class HikariCPConnectionFactoryIT method makeFactory.
private HikariCPConnectionFactory makeFactory(String database) throws PropertyVetoException, SQLException, IOException {
final DataSourceConfiguration config = new DataSourceConfiguration();
final JdbcDataSource opennms = new JdbcDataSource();
opennms.setName("opennms");
opennms.setClassName("org.postgresql.Driver");
opennms.setUserName("opennms");
opennms.setPassword("opennms");
opennms.setUrl("jdbc:postgresql://localhost:5432/template1");
config.addJdbcDataSource(opennms);
final JdbcDataSource opennms2 = new JdbcDataSource();
opennms2.setName("opennms2");
opennms2.setClassName("org.postgresql.Driver");
opennms2.setUserName("opennms");
opennms2.setPassword("opennms");
opennms2.setUrl("jdbc:postgresql://localhost:5432/template1");
config.addJdbcDataSource(opennms2);
final String mockDbUrl = System.getProperty("mock.db.url");
if (mockDbUrl != null && !"".equals(mockDbUrl.trim())) {
opennms.setUrl(mockDbUrl + "template1");
opennms2.setUrl(mockDbUrl + "template1");
}
final String mockDbAdminUser = System.getProperty("mock.db.adminUser");
if (mockDbAdminUser != null && !"".equals(mockDbAdminUser.trim())) {
opennms.setUserName(mockDbAdminUser);
opennms2.setUserName(mockDbAdminUser);
}
final String mockDbAdminPassword = System.getProperty("mock.db.adminPassword");
if (mockDbAdminPassword != null) {
opennms.setPassword(mockDbAdminPassword);
opennms2.setPassword(mockDbAdminPassword);
}
final StringWriter sw = new StringWriter();
JaxbUtils.marshal(config, sw);
final String configString = sw.toString();
InputStream stream = new ByteArrayInputStream(configString.getBytes());
final DataSourceConfigurationFactory factory = new DataSourceConfigurationFactory(stream);
try {
return new HikariCPConnectionFactory(factory.getJdbcDataSource(database));
} finally {
IOUtils.closeQuietly(stream);
}
}
use of org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration in project opennms by OpenNMS.
the class C3P0ConnectionFactoryIT method makeFactory.
private C3P0ConnectionFactory makeFactory(String database) throws PropertyVetoException, SQLException, IOException {
final DataSourceConfiguration config = new DataSourceConfiguration();
final JdbcDataSource opennms = new JdbcDataSource();
opennms.setName("opennms");
opennms.setClassName("org.postgresql.Driver");
opennms.setUserName("opennms");
opennms.setPassword("opennms");
opennms.setUrl("jdbc:postgresql://localhost:5432/template1");
config.addJdbcDataSource(opennms);
final JdbcDataSource opennms2 = new JdbcDataSource();
opennms2.setName("opennms2");
opennms2.setClassName("org.postgresql.Driver");
opennms2.setUserName("opennms");
opennms2.setPassword("opennms");
opennms2.setUrl("jdbc:postgresql://localhost:5432/template1");
config.addJdbcDataSource(opennms2);
final String mockDbUrl = System.getProperty("mock.db.url");
if (mockDbUrl != null && !"".equals(mockDbUrl.trim())) {
opennms.setUrl(mockDbUrl + "template1");
opennms2.setUrl(mockDbUrl + "template1");
}
final String mockDbAdminUser = System.getProperty("mock.db.adminUser");
if (mockDbAdminUser != null && !"".equals(mockDbAdminUser.trim())) {
opennms.setUserName(mockDbAdminUser);
opennms2.setUserName(mockDbAdminUser);
}
final String mockDbAdminPassword = System.getProperty("mock.db.adminPassword");
if (mockDbAdminPassword != null) {
opennms.setPassword(mockDbAdminPassword);
opennms2.setPassword(mockDbAdminPassword);
}
final StringWriter sw = new StringWriter();
JaxbUtils.marshal(config, sw);
final String configString = sw.toString();
InputStream stream = new ByteArrayInputStream(configString.getBytes());
final DataSourceConfigurationFactory factory = new DataSourceConfigurationFactory(stream);
try {
return new C3P0ConnectionFactory(factory.getJdbcDataSource(database));
} finally {
IOUtils.closeQuietly(stream);
}
}
use of org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration in project opennms by OpenNMS.
the class DataSourceConfigurationTest method data.
@Parameters
public static Collection<Object[]> data() throws Exception {
DataSourceConfiguration config = new DataSourceConfiguration();
ConnectionPool connectionPool = new ConnectionPool();
connectionPool.setFactory("org.opennms.core.db.HikariCPConnectionFactory");
connectionPool.setIdleTimeout(600);
connectionPool.setLoginTimeout(3);
connectionPool.setMinPool(50);
connectionPool.setMaxPool(50);
connectionPool.setMaxSize(50);
config.setConnectionPool(connectionPool);
JdbcDataSource opennmsDs = new JdbcDataSource();
opennmsDs.setName("opennms");
opennmsDs.setClassName("org.postgresql.Driver");
opennmsDs.setUrl("jdbc:postgresql://localhost:5432/template1");
opennmsDs.setUserName("opennms");
opennmsDs.setPassword("opennms");
config.addJdbcDataSource(opennmsDs);
JdbcDataSource opennmsDeuceDs = new JdbcDataSource();
opennmsDeuceDs.setName("opennms2");
opennmsDeuceDs.setClassName("org.postgresql.Driver");
opennmsDeuceDs.setUrl("jdbc:postgresql://localhost:5432/template1");
opennmsDeuceDs.addParam(new Param("user", "opennms"));
opennmsDeuceDs.addParam(new Param("password", "opennms"));
config.addJdbcDataSource(opennmsDeuceDs);
return Arrays.asList(new Object[][] { { config, new File("src/test/resources/org/opennms/core/db/opennms-datasources.xml") } });
}
use of org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration in project opennms by OpenNMS.
the class OnmsProperties method initialize.
/**
* Initialize.
*/
public static void initialize() {
try {
final Properties mainProperties = new Properties();
loadProperties(mainProperties, "opennms.properties");
registerProperties(mainProperties);
final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.OPENNMS_DATASOURCE_CONFIG_FILE_NAME);
final DataSourceConfiguration dsc = JaxbUtils.unmarshal(DataSourceConfiguration.class, cfgFile);
boolean found = false;
for (JdbcDataSource jds : dsc.getJdbcDataSourceCollection()) {
if (jds.getName().equals("opennms")) {
SimpleDataSource ds = new SimpleDataSource(jds);
DataSourceFactory.setInstance(ds);
found = true;
}
}
if (!found) {
throw NewtsConverterError.create("Can't find OpenNMS database configuration");
}
} catch (Exception e) {
throw NewtsConverterError.create(e, "Can't initialize OpenNMS database connection factory: {}", e.getMessage());
}
}
use of org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration in project opennms by OpenNMS.
the class ReportCompiler method initializeSingleInstanceDatabase.
/**
* Initializes a single instance DB connection factory.
* <p>From <code>org.opennms.netmgt.poller.MonitorTester</code></p>
*/
private static void initializeSingleInstanceDatabase() {
try {
final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.OPENNMS_DATASOURCE_CONFIG_FILE_NAME);
final DataSourceConfiguration dsc = JaxbUtils.unmarshal(DataSourceConfiguration.class, cfgFile);
boolean found = false;
for (JdbcDataSource jds : dsc.getJdbcDataSourceCollection()) {
if (jds.getName().equals("opennms")) {
System.out.printf("Initializing datatabase %s\n", jds.getUrl());
DataSourceFactory.setInstance(new SimpleDataSource(jds));
found = true;
}
}
if (!found) {
System.err.printf("Error: Can't find OpenNMS database configuration.\n");
System.exit(1);
}
} catch (Exception e) {
System.err.printf("Error: Can't initialize OpenNMS database connection factory. %s\n", e.getMessage());
System.exit(1);
}
}
Aggregations