Search in sources :

Example 6 with DataSourceConfiguration

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());
    }
}
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 7 with DataSourceConfiguration

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);
    }
}
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)

Aggregations

DataSourceConfiguration (org.opennms.netmgt.config.opennmsDataSources.DataSourceConfiguration)7 JdbcDataSource (org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource)7 File (java.io.File)5 InputStream (java.io.InputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StringWriter (java.io.StringWriter)2 SQLException (java.sql.SQLException)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URL (java.net.URL)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 Properties (java.util.Properties)1 JRException (net.sf.jasperreports.engine.JRException)1 Parameters (org.junit.runners.Parameterized.Parameters)1 SimpleDataSource (org.opennms.core.db.install.SimpleDataSource)1 ConnectionPool (org.opennms.netmgt.config.opennmsDataSources.ConnectionPool)1 Param (org.opennms.netmgt.config.opennmsDataSources.Param)1