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