Search in sources :

Example 1 with JdbcDataSource

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

Example 2 with JdbcDataSource

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

Example 3 with JdbcDataSource

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

the class DatabaseChecker method check.

/**
 * <p>Check whether the data sources in opennms-datasources.xml are valid.</p>
 *
 * @throws MissingDataSourceException A required data source was not found in opennms-datasources.xml.
 * @throws InvalidDataSourceException A required data source could not be connected to.
 */
public void check() throws MissingDataSourceException, InvalidDataSourceException {
    // First, check to make sure the required datasources are there.
    boolean dataSourcesFound = true;
    for (final String dataSource : m_required) {
        if (!m_dataSources.containsKey(dataSource)) {
            LOG.error("Required data source '{}' is missing from opennms-datasources.xml", dataSource);
            dataSourcesFound = false;
        }
    }
    if (!dataSourcesFound) {
        throw new MissingDataSourceException("OpenNMS is missing one or more data sources required for startup.");
    }
    // Then, check for the optional ones so we can warn about them going missing.
    for (final String dataSource : m_optional) {
        if (!m_dataSources.containsKey(dataSource)) {
            LOG.info("Data source '{}' is missing from opennms-datasources.xml", dataSource);
        }
    }
    // Finally, try connecting to all data sources, and warn or error as appropriate.
    for (final JdbcDataSource dataSource : m_dataSources.values()) {
        Connection connection = null;
        try {
            final String name = dataSource.getName();
            if (!m_required.contains(name) && !m_optional.contains(name)) {
                LOG.warn("Unknown datasource '{}' was found.", name);
            }
            try {
                Class.forName(dataSource.getClassName());
                connection = DriverManager.getConnection(dataSource.getUrl(), dataSource.getUserName(), dataSource.getPassword());
            } catch (final Throwable t) {
                final String errorMessage = "Unable to connect to data source '{}' at URL '{}' with username '{}', check opennms-datasources.xml and your database permissions.";
                if (m_required.contains(name)) {
                    LOG.error(errorMessage, name, dataSource.getUrl(), dataSource.getUserName());
                    throw new InvalidDataSourceException("Data source '" + name + "' failed.", t);
                } else {
                    LOG.warn(errorMessage, name, dataSource.getUrl(), dataSource.getUserName());
                }
            }
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (final SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) Connection(java.sql.Connection)

Example 4 with JdbcDataSource

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

the class Installer method install.

/**
 * <p>install</p>
 *
 * @param argv an array of {@link java.lang.String} objects.
 * @throws java.lang.Exception if any.
 */
public void install(final String[] argv) throws Exception {
    printHeader();
    loadProperties();
    parseArguments(argv);
    final boolean doDatabase = (m_update_database || m_do_inserts || m_update_iplike || m_update_unicode || m_fix_constraint);
    if (!doDatabase && m_tomcat_conf == null && !m_install_webapp && m_library_search_path == null) {
        usage(options, m_commandLine, "Nothing to do.  Use -h for help.", null);
        System.exit(1);
    }
    if (doDatabase) {
        final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.OPENNMS_DATASOURCE_CONFIG_FILE_NAME);
        InputStream is = new FileInputStream(cfgFile);
        final JdbcDataSource adminDsConfig = new DataSourceConfigurationFactory(is).getJdbcDataSource(ADMIN_DATA_SOURCE_NAME);
        final DataSource adminDs = new SimpleDataSource(adminDsConfig);
        is.close();
        is = new FileInputStream(cfgFile);
        final JdbcDataSource dsConfig = new DataSourceConfigurationFactory(is).getJdbcDataSource(OPENNMS_DATA_SOURCE_NAME);
        final DataSource ds = new SimpleDataSource(dsConfig);
        is.close();
        m_installerDb.setForce(m_force);
        m_installerDb.setIgnoreNotNull(m_ignore_not_null);
        m_installerDb.setNoRevert(m_do_not_revert);
        m_installerDb.setAdminDataSource(adminDs);
        m_installerDb.setPostgresOpennmsUser(dsConfig.getUserName());
        m_installerDb.setDataSource(ds);
        m_installerDb.setDatabaseName(dsConfig.getDatabaseName());
        m_migrator.setDataSource(ds);
        m_migrator.setAdminDataSource(adminDs);
        m_migrator.setValidateDatabaseVersion(!m_ignore_database_version);
        m_migration.setDatabaseName(dsConfig.getDatabaseName());
        m_migration.setSchemaName(dsConfig.getSchemaName());
        m_migration.setAdminUser(adminDsConfig.getUserName());
        m_migration.setAdminPassword(adminDsConfig.getPassword());
        m_migration.setDatabaseUser(dsConfig.getUserName());
        m_migration.setDatabasePassword(dsConfig.getPassword());
        m_migration.setChangeLog("changelog.xml");
    }
    checkIPv6();
    /*
         * Make sure we can execute the rrdtool binary when the
         * JniRrdStrategy is enabled.
         */
    boolean using_jni_rrd_strategy = System.getProperty("org.opennms.rrd.strategyClass", "").contains("JniRrdStrategy");
    if (using_jni_rrd_strategy) {
        File rrd_binary = new File(System.getProperty("rrd.binary"));
        if (!rrd_binary.canExecute()) {
            throw new Exception("Cannot execute the rrdtool binary '" + rrd_binary.getAbsolutePath() + "' required by the current RRD strategy. Update the rrd.binary field in opennms.properties appropriately.");
        }
    }
    if (!Boolean.getBoolean("skip-native")) {
        String icmp_path = findLibrary("jicmp", m_library_search_path, false);
        String icmp6_path = findLibrary("jicmp6", m_library_search_path, false);
        String jrrd_path = findLibrary("jrrd", m_library_search_path, false);
        String jrrd2_path = findLibrary("jrrd2", m_library_search_path, false);
        writeLibraryConfig(icmp_path, icmp6_path, jrrd_path, jrrd2_path);
    }
    /*
         * Everything needs to use the administrative data source until we
         * verify that the opennms database is created below (and where we
         * create it if it doesn't already exist).
         */
    verifyFilesAndDirectories();
    if (m_install_webapp) {
        checkWebappOldOpennmsDir();
        checkServerXmlOldOpennmsContext();
    }
    if (m_update_database || m_fix_constraint) {
    // OLDINSTALL m_installerDb.readTables();
    }
    m_installerDb.disconnect();
    if (doDatabase) {
        m_migrator.validateDatabaseVersion();
        System.out.println(String.format("* using '%s' as the PostgreSQL user for OpenNMS", m_migration.getAdminUser()));
        System.out.println(String.format("* using '%s' as the PostgreSQL database name for OpenNMS", m_migration.getDatabaseName()));
        if (m_migration.getSchemaName() != null) {
            System.out.println(String.format("* using '%s' as the PostgreSQL schema name for OpenNMS", m_migration.getSchemaName()));
        }
    }
    if (m_update_database) {
        m_migrator.prepareDatabase(m_migration);
    }
    if (doDatabase) {
        m_installerDb.checkUnicode();
        m_installerDb.checkTime();
    }
    handleConfigurationChanges();
    final GenericApplicationContext context = new GenericApplicationContext();
    context.setClassLoader(Bootstrap.loadClasses(new File(m_opennms_home), true));
    if (m_update_database) {
        m_installerDb.databaseSetUser();
        m_installerDb.disconnect();
        for (final Resource resource : context.getResources("classpath*:/changelog.xml")) {
            System.out.println("- Running migration for changelog: " + resource.getDescription());
            m_migration.setAccessor(new ExistingResourceAccessor(resource));
            m_migrator.migrate(m_migration);
        }
    }
    if (m_update_unicode) {
        System.out.println("WARNING: the -U option is deprecated, it does nothing now");
    }
    if (m_do_vacuum) {
        m_installerDb.vacuumDatabase(m_do_full_vacuum);
    }
    if (m_install_webapp) {
        installWebApp();
    }
    if (m_tomcat_conf != null) {
        updateTomcatConf();
    }
    if (m_update_iplike) {
        m_installerDb.updateIplike();
    }
    if (m_update_database && m_remove_database) {
        m_installerDb.disconnect();
        m_installerDb.databaseRemoveDB();
    }
    if (doDatabase) {
        m_installerDb.disconnect();
    }
    if (m_update_database) {
        createConfiguredFile();
    }
    System.out.println();
    System.out.println("Installer completed successfully!");
    if (!m_skip_upgrade_tools) {
        System.setProperty("opennms.manager.class", "org.opennms.upgrade.support.Upgrade");
        Bootstrap.main(new String[] {});
    }
    context.close();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) Resource(org.springframework.core.io.Resource) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) SimpleDataSource(org.opennms.core.db.install.SimpleDataSource) DataSource(javax.sql.DataSource) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SimpleDataSource(org.opennms.core.db.install.SimpleDataSource) DataSourceConfigurationFactory(org.opennms.core.db.DataSourceConfigurationFactory) File(java.io.File)

Example 5 with JdbcDataSource

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

the class XADataSourceFactory method init.

/**
 * <p>init</p>
 *
 * @param dsName a {@link java.lang.String} object.
 */
public static synchronized void init(final String dsName) {
    if (isLoaded(dsName)) {
        // init already called, return
        return;
    }
    final JdbcDataSource ds = m_dataSourceConfigFactory.getJdbcDataSource(dsName);
    final ConnectionPool pool = m_dataSourceConfigFactory.getConnectionPool();
    String urlString = ds.getUrl();
    if (urlString.startsWith("jdbc:")) {
        urlString = urlString.substring("jdbc:".length());
    }
    URI url = URI.create(urlString);
    // TODO: Add support for more XADataSources (hsqldb, derby)
    if ("postgresql".equalsIgnoreCase(url.getScheme())) {
        PGXADataSource xaDataSource = new PGXADataSource();
        xaDataSource.setServerName(url.getHost());
        xaDataSource.setPortNumber(url.getPort());
        xaDataSource.setDatabaseName(ds.getDatabaseName());
        xaDataSource.setUser(ds.getUserName());
        xaDataSource.setPassword(ds.getPassword());
        if (pool != null) {
            if (pool.getLoginTimeout() > 0) {
                xaDataSource.setLoginTimeout(pool.getLoginTimeout());
            }
            if (pool.getIdleTimeout() > 0) {
                // Set the socket timeout so that connections that are stuck reading from
                // the database will be closed after the timeout
                xaDataSource.setSocketTimeout(pool.getIdleTimeout());
            }
        }
        setInstance(dsName, xaDataSource);
    } else {
        throw new UnsupportedOperationException("Data source scheme not supported: " + url.getScheme());
    }
}
Also used : ConnectionPool(org.opennms.netmgt.config.opennmsDataSources.ConnectionPool) JdbcDataSource(org.opennms.netmgt.config.opennmsDataSources.JdbcDataSource) PGXADataSource(org.postgresql.xa.PGXADataSource) URI(java.net.URI)

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