Search in sources :

Example 6 with JdbcDataSource

use of org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource 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") } });
}
Also used : ConnectionPool(org.opennms.netmgt.config.opennmsDataSources.ConnectionPool) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) Param(org.opennms.netmgt.config.opennmsDataSources.Param) DataSourceConfiguration(org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration) File(java.io.File) Parameters(org.junit.runners.Parameterized.Parameters)

Example 7 with JdbcDataSource

use of org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource in project opennms by OpenNMS.

the class TemporaryDatabaseExecutionListener method prepareTestInstance.

@Override
public void prepareTestInstance(final TestContext testContext) throws Exception {
    //System.err.println(String.format("TemporaryDatabaseExecutionListener.prepareTestInstance(%s); details: %s", testContext.hashCode(), testContext));
    final JUnitTemporaryDatabase jtd = findAnnotation(testContext);
    if (jtd == null) {
        return;
    }
    m_database = m_databases.remove();
    //
    if (jtd.poolConnections()) {
        JdbcDataSource ds = new JdbcDataSource();
        ds.setDatabaseName(m_database.getTestDatabase());
        ds.setUserName(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
        ds.setPassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
        ds.setUrl(System.getProperty(TemporaryDatabase.URL_PROPERTY, TemporaryDatabase.DEFAULT_URL) + m_database.getTestDatabase());
        ds.setClassName(System.getProperty(TemporaryDatabase.DRIVER_PROPERTY, TemporaryDatabase.DEFAULT_DRIVER));
        HikariCPConnectionFactory pool = new HikariCPConnectionFactory(ds);
        // NMS-8911: Reduce the max connection lifetime so that HikariCP recycles 
        // connections more aggressively during tests
        pool.setMaxLifetime(500);
        DataSourceFactory.setInstance(pool);
    } else {
        DataSourceFactory.setInstance(m_database);
    }
    XADataSourceFactory.setInstance(m_database);
//System.err.println(String.format("TemporaryDatabaseExecutionListener.prepareTestInstance(%s) prepared db %s; details: %s", testContext.hashCode(), m_database.toString(), testContext));
//System.err.println("Temporary Database Name: " + m_database.getTestDatabase());
}
Also used : JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) HikariCPConnectionFactory(org.opennms.core.db.HikariCPConnectionFactory)

Example 8 with JdbcDataSource

use of org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource 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());
    }
}
Also used : JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) Properties(java.util.Properties) DataSourceConfiguration(org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration) File(java.io.File)

Example 9 with JdbcDataSource

use of org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource 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);
    }
}
Also used : JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) DataSourceConfiguration(org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration) File(java.io.File) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) JRException(net.sf.jasperreports.engine.JRException) SQLException(java.sql.SQLException)

Example 10 with JdbcDataSource

use of org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource in project opennms by OpenNMS.

the class AbstractOnmsUpgrade method initializeDatasource.

/**
     * Initializes the data source.
     *
     * @throws OnmsUpgradeException the OpenNMS upgrade exception
     */
protected void initializeDatasource() throws OnmsUpgradeException {
    if (dataSource != null) {
        return;
    }
    try {
        final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.OPENNMS_DATASOURCE_CONFIG_FILE_NAME);
        final DataSourceConfiguration dsc = JaxbUtils.unmarshal(DataSourceConfiguration.class, cfgFile);
        for (JdbcDataSource jds : dsc.getJdbcDataSourceCollection()) {
            if (jds.getName().equals("opennms")) {
                dataSource = new SimpleDataSource(jds);
                DataSourceFactory.setInstance(dataSource);
            }
        }
        if (dataSource == null) {
            throw new OnmsUpgradeException("Can't find theOpenNMS Database settings.");
        }
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't connect to OpenNMS Database because " + e.getMessage(), e);
    }
}
Also used : SimpleDataSource(org.opennms.core.db.install.SimpleDataSource) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) DataSourceConfiguration(org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration) File(java.io.File) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

JdbcDataSource (org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource)11 DataSourceConfiguration (org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration)7 File (java.io.File)6 InputStream (java.io.InputStream)4 SQLException (java.sql.SQLException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 SimpleDataSource (org.opennms.core.db.install.SimpleDataSource)2 ConnectionPool (org.opennms.netmgt.config.opennmsDataSources.ConnectionPool)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URI (java.net.URI)1 URL (java.net.URL)1 Connection (java.sql.Connection)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 Properties (java.util.Properties)1 DataSource (javax.sql.DataSource)1