Package org.rhq.core.db

Examples of org.rhq.core.db.DatabaseType


     * @throws Exception
     */
    public boolean upgrade(Connection connection, String version) throws Exception {
        PreparedStatement preparedStatement = null;
        try {
            DatabaseType db = DatabaseTypeFactory.getDatabaseType(connection);
            boolean columnExists = db.checkColumnExists(connection, "RHQ_STORAGE_NODE", "VERSION");
            if (!columnExists) {
                db.addColumn(connection, "RHQ_STORAGE_NODE", "VERSION", "VARCHAR2", "255");
                preparedStatement = connection.prepareStatement("UPDATE RHQ_STORAGE_NODE SET VERSION = ?");
                preparedStatement.setString(1, "PRE-" + version);
                preparedStatement.executeUpdate();
                db.closeStatement(preparedStatement);
                // set column not null after it's been set
                db.alterColumn(connection, "RHQ_STORAGE_NODE", "VERSION", "VARCHAR2", null, "255", false, false);
                return true;
            }
        } finally {
            JDBCUtil.safeClose(null, preparedStatement, null);
        }
View Full Code Here


     * @throws Exception
     */
    public boolean upgrade(Connection connection, String version) throws Exception {
        PreparedStatement preparedStatement = null;
        try {
            DatabaseType db = DatabaseTypeFactory.getDatabaseType(connection);
            if (!db.checkColumnExists(connection, "RHQ_SERVER", "VERSION")) {
                db.addColumn(connection, "RHQ_SERVER", "VERSION", "VARCHAR2", "255");
                preparedStatement = connection.prepareStatement("UPDATE RHQ_SERVER SET VERSION = ?");
                preparedStatement.setString(1, "PRE-" + version);
                preparedStatement.executeUpdate();
                db.closeStatement(preparedStatement);
                // set column not null after it's been set
                db.alterColumn(connection, "RHQ_SERVER", "VERSION", "VARCHAR2", null, "255", false, false);
                return true;
            }
        } finally {
            JDBCUtil.safeClose(null, preparedStatement, null);
        }
View Full Code Here

     */
    public static boolean isDatabaseSchemaExist(String connectionUrl, String username, String password)
        throws Exception {

        Connection conn = getDatabaseConnection(connectionUrl, username, password);
        DatabaseType db = DatabaseTypeFactory.getDatabaseType(conn);

        try {
            return db.checkTableExists(conn, "RHQ_PRINCIPAL");
        } catch (IllegalStateException e) {
            return false;
        } finally {
            db.closeConnection(conn);
        }
    }
View Full Code Here

     *
     * @throws Exception if failed to communicate with the database
     */
    public static ArrayList<String> getServerNames(String connectionUrl, String username, String password)
        throws Exception {
        DatabaseType db = null;
        Connection conn = null;
        Statement stm = null;
        ResultSet rs = null;
        ArrayList<String> result = new ArrayList<String>();

        try {
            conn = getDatabaseConnection(connectionUrl, username, password);
            db = DatabaseTypeFactory.getDatabaseType(conn);

            if (db.checkTableExists(conn, "rhq_server")) {

                stm = conn.createStatement();
                rs = stm.executeQuery("SELECT name FROM rhq_server ORDER BY name asc");

                while (rs.next()) {
                    result.add(rs.getString(1));
                }
            }
        } catch (IllegalStateException e) {
            // table does not exist
        } catch (SQLException e) {
            LOG.info("Unable to fetch existing server info: " + e.getMessage());
        } finally {
            if (null != db) {
                db.closeJDBCObjects(conn, stm, rs);
            }
        }

        return result;
    }
View Full Code Here

     * @return the information on the named server
     */
    public static ServerDetails getServerDetails(String connectionUrl, String username, String password,
        String serverName) {

        DatabaseType db = null;
        Connection conn = null;
        ServerDetails result = null;

        try {
            conn = getDatabaseConnection(connectionUrl, username, password);
            db = DatabaseTypeFactory.getDatabaseType(conn);

            result = getServerDetails(db, conn, serverName);

        } catch (Exception e) {
            LOG.info("Unable to get server detail: " + e.getMessage());
        } finally {
            if (null != db) {
                db.closeConnection(conn);
            }
        }

        return result;
    }
View Full Code Here

     * @throws Exception if the database is not supported
     */
    public static void ensureDatabaseIsSupported(String connectionUrl, String username, String password)
        throws Exception {
        Connection conn = null;
        DatabaseType db = null;

        try {
            conn = getDatabaseConnection(connectionUrl, username, password);
            db = DatabaseTypeFactory.getDatabaseType(conn);

            String version = db.getVersion();

            if (DatabaseTypeFactory.isPostgres(db)) {
                if (version.startsWith("7") || version.equals("8") || version.startsWith("8.0")
                    || version.startsWith("8.1")) {
                    throw new Exception("Unsupported PostgreSQL [" + db + "]");
                }
            } else if (DatabaseTypeFactory.isOracle(db)) {
                if (version.startsWith("8") || version.startsWith("9")) {
                    throw new Exception("Unsupported Oracle [" + db + "]");
                }
            } else {
                throw new Exception("Unsupported DB [" + db + "]");
            }

            LOG.info("Database is supported: " + db);
        } finally {
            if (db != null) {
                db.closeConnection(conn);
            }
        }
    }
View Full Code Here

     * @param dbpassword clear text password to connect to the database
     * @throws Exception
     */
    public static void persistAdminPasswordIfNecessary(HashMap<String, String> serverProperties, String dbpassword)
        throws Exception {
        DatabaseType db = null;
        Connection connection = null;
        Statement queryStatement = null;
        Statement insertStatement = null;
        ResultSet resultSet = null;

        try {
            String dbUrl = serverProperties.get(ServerProperties.PROP_DATABASE_CONNECTION_URL);
            String userName = serverProperties.get(ServerProperties.PROP_DATABASE_USERNAME);
            connection = getDatabaseConnection(dbUrl, userName, dbpassword);
            db = DatabaseTypeFactory.getDatabaseType(connection);

            if (!(db instanceof PostgresqlDatabaseType || db instanceof OracleDatabaseType)) {
                throw new IllegalArgumentException("Unknown database type, can't continue: " + db);
            }

            queryStatement = connection.createStatement();
            resultSet = queryStatement.executeQuery("SELECT count(*) FROM rhq_principal WHERE id=2");
            resultSet.next();

            if (resultSet.getInt(1) == 0) {
                connection.setAutoCommit(false);

                try {
                    LOG.info("Persisting admin password to database for property [rhq.autoinstall.server.admin.password]");

                    insertStatement = connection.createStatement();
                    insertStatement.executeUpdate("INSERT INTO rhq_principal VALUES (2, 'rhqadmin', '"
                        + serverProperties.get(ServerProperties.PROP_AUTOINSTALL_ADMIN_PASSWORD) + "')");

                    connection.commit();
                } catch (SQLException e) {
                    LOG.error(
                        "Failed to persist admin password to database for property [rhq.autoinstall.server.admin.password]. Transaction will be rolled back.",
                        e);
                    connection.rollback();
                    throw e;
                }
            } else {
                LOG.info("Admin user password is already set, property [rhq.autoinstall.server.admin.password] will be ignored.");
            }

        } finally {
            if (db != null) {
                db.closeResultSet(resultSet);
                db.closeStatement(queryStatement);
                db.closeStatement(insertStatement);
                db.closeConnection(connection);
            }
        }
    }
View Full Code Here

     * @param storageNodes the {@link StorageNode storage nodes} to persist
     * @throws Exception
     */
    public static void persistStorageNodesIfNecessary(HashMap<String, String> serverProperties, String password,
        Set<StorageNode> storageNodes) throws Exception {
        DatabaseType db = null;
        Connection connection = null;
        Statement queryStatement = null;
        ResultSet resultSet = null;
        PreparedStatement insertStorageNode = null;
        PreparedStatement deleteStorageNodes = null;

        try {
            String dbUrl = serverProperties.get(ServerProperties.PROP_DATABASE_CONNECTION_URL);
            String userName = serverProperties.get(ServerProperties.PROP_DATABASE_USERNAME);
            connection = getDatabaseConnection(dbUrl, userName, password);
            db = DatabaseTypeFactory.getDatabaseType(connection);

            if (!(db instanceof PostgresqlDatabaseType || db instanceof OracleDatabaseType)) {
                throw new IllegalArgumentException("Unknown database type, can't continue: " + db);
            }

            // IF there are no current storage nodes then we can persist the specified storage nodes.
            // IF there are current storage nodes but none are linked to resources, we replace them with
            // the currently specified addresses.  This allows an install or upgrade to be run again if the
            // initial address(es) were incorrect.

            queryStatement = connection.createStatement();
            resultSet = queryStatement
                .executeQuery("SELECT count(*) FROM rhq_storage_node sn WHERE NOT sn.resource_id IS NULL");
            resultSet.next();

            if (resultSet.getInt(1) == 0) {
                connection.setAutoCommit(false);

                try {
                    deleteStorageNodes = connection.prepareStatement("DELETE FROM rhq_storage_node");
                    int numRemoved = deleteStorageNodes.executeUpdate();
                    if (numRemoved > 0) {
                        LOG.info("Removed [" + numRemoved + "] storage nodes. They will be redefined now...");
                    }

                    LOG.info("Persisting to database new storage nodes for values specified in server configuration property [rhq.storage.nodes]");

                    insertStorageNode = connection
                        .prepareStatement("INSERT INTO rhq_storage_node (id, address, cql_port, operation_mode, ctime, mtime, maintenance_pending, version) "
                            + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");

                    int id = 1001;
                    for (StorageNode storageNode : storageNodes) {
                        insertStorageNode.setInt(1, id);
                        insertStorageNode.setString(2, storageNode.getAddress());
                        insertStorageNode.setInt(3, storageNode.getCqlPort());
                        insertStorageNode.setString(4, StorageNode.OperationMode.INSTALLED.toString());
                        insertStorageNode.setLong(5, System.currentTimeMillis());
                        insertStorageNode.setLong(6, System.currentTimeMillis());
                        insertStorageNode.setBoolean(7, false);
                        insertStorageNode.setString(8, version);

                        insertStorageNode.executeUpdate();
                        id += 1;
                    }

                    connection.commit();
                } catch (SQLException e) {
                    LOG.error("Failed to persist to database the storage nodes specified by server configuration "
                        + "property [rhq.storage.nodes]. Transaction will be rolled back.", e);
                    connection.rollback();
                    throw e;
                }
            } else {
                LOG.info("Storage nodes already exist in database. Server configuration property [rhq.storage.nodes] will be ignored.");
            }

        } finally {
            if (db != null) {
                db.closeResultSet(resultSet);
                db.closeStatement(queryStatement);
                db.closeStatement(deleteStorageNodes);
                db.closeStatement(insertStorageNode);
                db.closeConnection(connection);
            }
        }
    }
View Full Code Here

    public static Map<String, String> fetchStorageClusterSettings(HashMap<String, String> serverProperties,
        String password) throws Exception {

        Map<String, String> result = new HashMap<String, String>(5);
        DatabaseType db = null;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            String dbUrl = serverProperties.get(ServerProperties.PROP_DATABASE_CONNECTION_URL);
            String userName = serverProperties.get(ServerProperties.PROP_DATABASE_USERNAME);
            connection = getDatabaseConnection(dbUrl, userName, password);
            db = DatabaseTypeFactory.getDatabaseType(connection);

            if (!(db instanceof PostgresqlDatabaseType || db instanceof OracleDatabaseType)) {
                throw new IllegalArgumentException("Unknown database type, can't continue: " + db);
            }

            try {
                statement = connection.prepareStatement("" //
                    + "SELECT property_key, property_value FROM rhq_system_config " //
                    + " WHERE property_key LIKE 'STORAGE%' " //
                    + "   AND NOT property_value IS NULL ");
                resultSet = statement.executeQuery();

                while (resultSet.next()) {
                    String key = resultSet.getString(1);
                    String value = resultSet.getString(2);

                    if (key.equals("STORAGE_USERNAME")) {
                        result.put(ServerProperties.PROP_STORAGE_USERNAME, value);
                    } else if (key.equals("STORAGE_PASSWORD")) {
                        result.put(ServerProperties.PROP_STORAGE_PASSWORD, value);
                    } else if (key.equals("STORAGE_GOSSIP_PORT")) {
                        result.put(ServerProperties.PROP_STORAGE_GOSSIP_PORT, value);
                    } else if (key.equals("STORAGE_CQL_PORT")) {
                        result.put(ServerProperties.PROP_STORAGE_CQL_PORT, value);
                    }
                }
            } finally {
                db.closeResultSet(resultSet);
                db.closeStatement(statement);
            }

            try {
                statement = connection.prepareStatement("" //
                    + "SELECT address FROM rhq_storage_node " //
                    + " WHERE operation_mode in " + "('" + OperationMode.NORMAL.name()
                    + "', '"
                    + OperationMode.INSTALLED.name() + "') ");
                resultSet = statement.executeQuery();

                StringBuilder addressList = new StringBuilder();
                while (resultSet.next()) {
                    String address = resultSet.getString(1);

                    if (address != null && !address.trim().isEmpty()) {
                        if (addressList.length() != 0) {
                            addressList.append(',');
                        }
                        addressList.append(address);
                    }
                }

                if (addressList.length() != 0) {
                    result.put(ServerProperties.PROP_STORAGE_NODES, addressList.toString());
                }
            } finally {
                db.closeResultSet(resultSet);
                db.closeStatement(statement);
            }
        } catch (SQLException e) {
            LOG.error("Failed to fetch storage cluster settings.", e);
            throw e;
        } finally {
            if (db != null) {
                db.closeConnection(connection);
            }
        }

        return result;
    }
View Full Code Here

        return result;
    }

    public static void persistStorageClusterSettingsIfNecessary(HashMap<String, String> serverProperties,
        String password) throws Exception {
        DatabaseType db = null;
        Connection connection = null;
        PreparedStatement updateClusterSetting = null;

        try {
            String dbUrl = serverProperties.get(ServerProperties.PROP_DATABASE_CONNECTION_URL);
            String userName = serverProperties.get(ServerProperties.PROP_DATABASE_USERNAME);
            connection = getDatabaseConnection(dbUrl, userName, password);
            db = DatabaseTypeFactory.getDatabaseType(connection);

            if (!(db instanceof PostgresqlDatabaseType || db instanceof OracleDatabaseType)) {
                throw new IllegalArgumentException("Unknown database type, can't continue: " + db);
            }

            connection = getDatabaseConnection(dbUrl, userName, password);
            connection.setAutoCommit(false);

            updateClusterSetting = connection.prepareStatement("" //
                + "UPDATE rhq_system_config " //
                + "   SET property_value = ?, default_property_value = ? " //
                + " WHERE property_key = ? " //
                + "   AND ( property_value IS NULL OR property_value = '' OR property_value = 'UNSET' ) ");

            updateClusterSetting.setString(1, serverProperties.get(ServerProperties.PROP_STORAGE_USERNAME));
            updateClusterSetting.setString(2, serverProperties.get(ServerProperties.PROP_STORAGE_USERNAME));
            updateClusterSetting.setString(3, "STORAGE_USERNAME");
            updateClusterSetting.executeUpdate();

            updateClusterSetting.setString(1, serverProperties.get(ServerProperties.PROP_STORAGE_PASSWORD));
            updateClusterSetting.setString(2, serverProperties.get(ServerProperties.PROP_STORAGE_PASSWORD));
            updateClusterSetting.setString(3, "STORAGE_PASSWORD");
            updateClusterSetting.executeUpdate();

            updateClusterSetting.setString(1, serverProperties.get(ServerProperties.PROP_STORAGE_CQL_PORT));
            updateClusterSetting.setString(2, serverProperties.get(ServerProperties.PROP_STORAGE_CQL_PORT));
            updateClusterSetting.setString(3, "STORAGE_CQL_PORT");
            updateClusterSetting.executeUpdate();

            updateClusterSetting.setString(1, serverProperties.get(ServerProperties.PROP_STORAGE_GOSSIP_PORT));
            updateClusterSetting.setString(2, serverProperties.get(ServerProperties.PROP_STORAGE_GOSSIP_PORT));
            updateClusterSetting.setString(3, "STORAGE_GOSSIP_PORT");
            updateClusterSetting.executeUpdate();

            connection.commit();
        } catch (SQLException e) {
            LOG.error("Failed to initialize storage cluster settings. Transaction will be rolled back.", e);
            connection.rollback();
            throw e;
        } finally {
            if (db != null) {
                db.closeStatement(updateClusterSetting);
                db.closeConnection(connection);
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.rhq.core.db.DatabaseType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.