Search in sources :

Example 1 with DbMediaSource

use of com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource in project otter by alibaba.

the class DataSourceChecker method check.

@SuppressWarnings("resource")
public String check(String url, String username, String password, String encode, String sourceType) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    // boolean typeConflict = true;
    // if ((sourceType.toLowerCase().equals(MYSQL_FLAG) &&
    // url.toLowerCase().contains(MYSQL_FLAG))
    // || sourceType.toLowerCase().equals(ORACLE_FLAG) &&
    // url.toLowerCase().contains(ORACLE_FLAG)) {
    // typeConflict = false;
    // }
    // 
    // if (typeConflict) {
    // return DBTYPE_CONFLICT;
    // }
    DataSource dataSource = null;
    try {
        DbMediaSource dbMediaSource = new DbMediaSource();
        dbMediaSource.setUrl(url);
        dbMediaSource.setUsername(username);
        dbMediaSource.setPassword(password);
        dbMediaSource.setEncode(encode);
        if (sourceType.equalsIgnoreCase("MYSQL")) {
            dbMediaSource.setType(DataMediaType.MYSQL);
            dbMediaSource.setDriver("com.mysql.jdbc.Driver");
        } else if (sourceType.equalsIgnoreCase("ORACLE")) {
            dbMediaSource.setType(DataMediaType.ORACLE);
            dbMediaSource.setDriver("oracle.jdbc.driver.OracleDriver");
        }
        dataSource = dataSourceCreator.createDataSource(dbMediaSource);
        try {
            conn = dataSource.getConnection();
        } catch (Exception e) {
            logger.error("check error!", e);
        }
        if (null == conn) {
            return DATABASE_FAIL;
        }
        stmt = conn.createStatement();
        String sql = null;
        if (sourceType.equals("MYSQL")) {
            sql = "SHOW VARIABLES LIKE 'character_set_database'";
        } else if (sourceType.equals("ORACLE")) {
            // sql
            // ="select * from V$NLS_PARAMETERS where parameter in('NLS_LANGUAGE','NLS_TERRITORY','NLS_CHARACTERSET')";
            sql = "select * from V$NLS_PARAMETERS where parameter in('NLS_CHARACTERSET')";
        }
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            String defaultEncode = null;
            if (sourceType.equals("MYSQL")) {
                defaultEncode = ((String) rs.getObject(2)).toLowerCase();
                defaultEncode = defaultEncode.equals("iso-8859-1") ? "latin1" : defaultEncode;
                if (!encode.toLowerCase().equals(defaultEncode)) {
                    return ENCODE_FAIL + defaultEncode;
                }
            } else if (sourceType.equals("ORACLE")) {
                // ORACLE查询服务器默认字符集需要管理员权限
                defaultEncode = ((String) rs.getObject(2)).toLowerCase();
                defaultEncode = defaultEncode.equalsIgnoreCase("zhs16gbk") ? "gbk" : defaultEncode;
                defaultEncode = defaultEncode.equalsIgnoreCase("us7ascii") ? "iso-8859-1" : defaultEncode;
                if (!encode.toLowerCase().equals(defaultEncode)) {
                    return ENCODE_FAIL + defaultEncode;
                }
            }
        }
    } catch (SQLException se) {
        logger.error("check error!", se);
        return ENCODE_QUERY_ERROR;
    } catch (Exception e) {
        logger.error("check error!", e);
        return DATABASE_FAIL;
    } finally {
        closeConnection(conn);
        dataSourceCreator.destroyDataSource(dataSource);
    }
    return DATABASE_SUCCESS;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource)

Example 2 with DbMediaSource

use of com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource in project otter by alibaba.

the class DataSourceChecker method checkNamespaceTables.

public String checkNamespaceTables(final String namespace, final String name, final Long dataSourceId) {
    DataSource dataSource = null;
    try {
        DataMediaSource source = dataMediaSourceService.findById(dataSourceId);
        DbMediaSource dbMediaSource = (DbMediaSource) source;
        dataSource = dataSourceCreator.createDataSource(dbMediaSource);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        List<String> schemaList;
        {
            ModeValue mode = ConfigHelper.parseMode(namespace);
            String schemaPattern = ConfigHelper.makeSQLPattern(mode, namespace);
            final ModeValueFilter modeValueFilter = ConfigHelper.makeModeValueFilter(mode, namespace);
            if (source.getType().isOracle()) {
                schemaList = Arrays.asList(namespace);
            } else {
                schemaList = DdlUtils.findSchemas(jdbcTemplate, schemaPattern, new DdlSchemaFilter() {

                    @Override
                    public boolean accept(String schemaName) {
                        return modeValueFilter.accept(schemaName);
                    }
                });
            }
        }
        final List<String> matchSchemaTables = new ArrayList<String>();
        matchSchemaTables.add("Find schema and tables:");
        if (schemaList != null) {
            ModeValue mode = ConfigHelper.parseMode(name);
            String tableNamePattern = ConfigHelper.makeSQLPattern(mode, name);
            final ModeValueFilter modeValueFilter = ConfigHelper.makeModeValueFilter(mode, name);
            for (String schema : schemaList) {
                DdlUtils.findTables(jdbcTemplate, schema, schema, tableNamePattern, null, new DdlTableNameFilter() {

                    @Override
                    public boolean accept(String catalogName, String schemaName, String tableName) {
                        if (modeValueFilter.accept(tableName)) {
                            matchSchemaTables.add(schemaName + "." + tableName);
                        }
                        return false;
                    }
                });
            }
        }
        if (matchSchemaTables.size() == 1) {
            return TABLE_FAIL;
        }
        return StringUtils.join(matchSchemaTables, "<br>\n");
    } catch (Exception e) {
        logger.error("check error!", e);
        return TABLE_FAIL;
    } finally {
        dataSourceCreator.destroyDataSource(dataSource);
    }
}
Also used : ModeValue(com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue) ModeValueFilter(com.alibaba.otter.shared.common.model.config.ModeValueFilter) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) DdlTableNameFilter(com.alibaba.otter.shared.common.utils.meta.DdlTableNameFilter) DataMediaSource(com.alibaba.otter.shared.common.model.config.data.DataMediaSource) DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource) DdlSchemaFilter(com.alibaba.otter.shared.common.utils.meta.DdlSchemaFilter)

Example 3 with DbMediaSource

use of com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource in project otter by alibaba.

the class DataMediaSourceAction method doEdit.

public void doEdit(@FormGroup("dataMediaSourceInfo") Group dataMediaSourceInfo, @Param("pageIndex") int pageIndex, @Param("searchKey") String searchKey, @FormField(name = "formDataMediaSourceError", group = "dataMediaSourceInfo") CustomErrors err, Navigator nav) throws Exception {
    DbMediaSource dbMediaSource = new DbMediaSource();
    dataMediaSourceInfo.setProperties(dbMediaSource);
    if (dbMediaSource.getType().isMysql()) {
        dbMediaSource.setDriver("com.mysql.jdbc.Driver");
    } else if (dbMediaSource.getType().isOracle()) {
        dbMediaSource.setDriver("oracle.jdbc.driver.OracleDriver");
    }
    try {
        dataMediaSourceService.modify(dbMediaSource);
    } catch (RepeatConfigureException rce) {
        err.setMessage("invalidDataMediaSource");
        return;
    }
    nav.redirectToLocation("dataSourceList.htm?pageIndex=" + pageIndex + "&searchKey=" + urlEncode(searchKey));
}
Also used : RepeatConfigureException(com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException) DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)

Example 4 with DbMediaSource

use of com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource in project otter by alibaba.

the class DataMediaSourceAction method doAdd.

/**
 * 添加Channel
 *
 * @param channelInfo
 * @param channelParameterInfo
 * @throws Exception
 */
public void doAdd(@FormGroup("dataMediaSourceInfo") Group dataMediaSourceInfo, @FormField(name = "formDataMediaSourceError", group = "dataMediaSourceInfo") CustomErrors err, Navigator nav) throws Exception {
    DataMediaSource dataMediaSource = new DataMediaSource();
    dataMediaSourceInfo.setProperties(dataMediaSource);
    if (dataMediaSource.getType().isMysql() || dataMediaSource.getType().isOracle()) {
        DbMediaSource dbMediaSource = new DbMediaSource();
        dataMediaSourceInfo.setProperties(dbMediaSource);
        if (dataMediaSource.getType().isMysql()) {
            dbMediaSource.setDriver("com.mysql.jdbc.Driver");
        } else if (dataMediaSource.getType().isOracle()) {
            dbMediaSource.setDriver("oracle.jdbc.driver.OracleDriver");
        }
        try {
            dataMediaSourceService.create(dbMediaSource);
        } catch (RepeatConfigureException rce) {
            err.setMessage("invalidDataMediaSource");
            return;
        }
    } else if (dataMediaSource.getType().isNapoli() || dataMediaSource.getType().isMq()) {
        MqMediaSource mqMediaSource = new MqMediaSource();
        dataMediaSourceInfo.setProperties(mqMediaSource);
        try {
            dataMediaSourceService.create(mqMediaSource);
        } catch (RepeatConfigureException rce) {
            err.setMessage("invalidDataMediaSource");
            return;
        }
    }
    nav.redirectTo(WebConstant.DATA_MEDIA_SOURCE_LIST_LINK);
}
Also used : RepeatConfigureException(com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException) MqMediaSource(com.alibaba.otter.shared.common.model.config.data.mq.MqMediaSource) DataMediaSource(com.alibaba.otter.shared.common.model.config.data.DataMediaSource) DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)

Example 5 with DbMediaSource

use of com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource in project otter by alibaba.

the class BaseDbTest method getOracleMedia.

public DbDataMedia getOracleMedia() {
    DbMediaSource dbMediaSource = new DbMediaSource();
    dbMediaSource.setId(11L);
    dbMediaSource.setDriver("oracle.jdbc.OracleDriver");
    dbMediaSource.setUsername("xxxxx");
    dbMediaSource.setPassword("xxxxx");
    dbMediaSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:crmgsb");
    dbMediaSource.setEncode("UTF-8");
    dbMediaSource.setType(DataMediaType.ORACLE);
    DbDataMedia dataMedia = new DbDataMedia();
    dataMedia.setSource(dbMediaSource);
    dataMedia.setId(2L);
    dataMedia.setName("columns");
    dataMedia.setNamespace("srf");
    return dataMedia;
}
Also used : DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource) DbDataMedia(com.alibaba.otter.shared.common.model.config.data.db.DbDataMedia)

Aggregations

DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)20 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)6 DataMediaSource (com.alibaba.otter.shared.common.model.config.data.DataMediaSource)6 SQLException (java.sql.SQLException)6 DataSource (javax.sql.DataSource)6 Table (org.apache.ddlutils.model.Table)6 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)6 Test (org.testng.annotations.Test)5 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)4 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)4 PreparedStatement (java.sql.PreparedStatement)4 TransactionStatus (org.springframework.transaction.TransactionStatus)4 TransactionCallback (org.springframework.transaction.support.TransactionCallback)4 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)4 SqlTemplate (com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate)3 DbDataMedia (com.alibaba.otter.shared.common.model.config.data.db.DbDataMedia)3 MqMediaSource (com.alibaba.otter.shared.common.model.config.data.mq.MqMediaSource)3 ArrayList (java.util.ArrayList)3 PreparedStatementCallback (org.springframework.jdbc.core.PreparedStatementCallback)3 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)2