Search in sources :

Example 1 with VarsqlAppCode

use of com.varsql.core.common.code.VarsqlAppCode in project varsql by varsqlinfo.

the class AdminDbMgmtServiceImpl method connectionCheck.

/**
 * @Method Name  : connectionCheck
 * @Method 설명 : 커넥션 체크.
 * @작성자   : ytkim
 * @작성일   : 2018. 1. 22.
 * @변경이력  :
 * @param vtConnection
 * @return
 * @throws EncryptDecryptException
 */
public ResponseResult connectionCheck(DBConnectionRequestDTO vtConnection) throws EncryptDecryptException {
    ResponseResult resultObject = new ResponseResult();
    this.dbConnectionModelRepository.findByConnInfo(vtConnection.getVconnid());
    logger.debug("connection check object param :  {}", VartechUtils.reflectionToString(vtConnection));
    String username = vtConnection.getVid();
    DBConnectionEntity dbInfo = this.dbConnectionModelRepository.findOne(DBConnectionSpec.detailInfo(vtConnection.getVconnid())).orElseThrow(NullPointerException::new);
    DBTypeDriverProviderEntity driverProviderEntity = dbInfo.getDbTypeDriverProvider();
    String url = vtConnection.getVurl();
    String defaultDriverValidationQuery = ValidationProperty.getInstance().validationQuery(driverProviderEntity.getDbType());
    if (!"Y".equals(driverProviderEntity.getDirectYn())) {
        DBTypeDriverEntity dbDriverModel = this.dbTypeDriverModelRepository.findByDriverId(driverProviderEntity.getDriverId());
        if (!"Y".equals(vtConnection.getUrlDirectYn())) {
            url = VarsqlJdbcUtil.getJdbcUrl(dbDriverModel.getUrlFormat(), JdbcURLFormatParam.builder().serverIp(vtConnection.getVserverip()).port(vtConnection.getVport()).databaseName(vtConnection.getVdatabasename()).build());
        }
        defaultDriverValidationQuery = StringUtils.isBlank(dbDriverModel.getValidationQuery()) ? defaultDriverValidationQuery : dbDriverModel.getValidationQuery();
    }
    logger.debug("connection check url :  {}", url);
    String validation_query = driverProviderEntity.getValidationQuery();
    validation_query = StringUtils.isBlank(validation_query) ? defaultDriverValidationQuery : validation_query;
    String failMessage = "";
    PreparedStatement pstmt = null;
    Connection connChk = null;
    VarsqlAppCode resultCode = VarsqlAppCode.SUCCESS;
    try {
        String pwd = vtConnection.getVpw();
        if (pwd == null || "".equals(pwd))
            pwd = dbInfo.getVpw();
        pwd = (pwd == null) ? "" : pwd;
        pwd = DBPasswordCryptionFactory.getInstance().decrypt(pwd);
        Properties p = new Properties();
        p.setProperty("user", username);
        p.setProperty("password", pwd);
        List<FileInfo> driverJarFiles;
        if (PathType.PATH.equals(PathType.getPathType(driverProviderEntity.getPathType()))) {
            driverJarFiles = FileServiceUtils.getFileInfos(driverProviderEntity.getDriverPath().split(";"));
        } else {
            driverJarFiles = FileServiceUtils.getFileInfos(dbTypeDriverFileEntityRepository.findByFileContId(driverProviderEntity.getDriverProviderId()));
        }
        JDBCDriverInfo jdbcDriverInfo = new JDBCDriverInfo(dbInfo.getDbTypeDriverProvider().getDriverProviderId(), dbInfo.getDbTypeDriverProvider().getDriverClass());
        jdbcDriverInfo.setDriverFiles(driverJarFiles);
        Driver dbDriver = JdbcDriverLoader.checkDriver(jdbcDriverInfo);
        connChk = dbDriver.connect(url, p);
        pstmt = connChk.prepareStatement(validation_query);
        pstmt.executeQuery();
        connChk.close();
    } catch (EncryptDecryptException e) {
        resultCode = VarsqlAppCode.ERROR;
        failMessage = "password decrypt error : " + e.getMessage();
        logger.error(getClass().getName(), (Throwable) e);
    } catch (ClassNotFoundException e) {
        resultCode = VarsqlAppCode.ERROR;
        failMessage = e.getMessage();
        logger.error("url :{}", url);
        logger.error(getClass().getName(), e);
    } catch (Exception e) {
        resultCode = VarsqlAppCode.ERROR;
        failMessage = e.getMessage();
        logger.error(getClass().getName(), e);
    } finally {
        JdbcUtils.close(connChk, pstmt, null);
    }
    resultObject.setResultCode(resultCode);
    resultObject.setMessage(failMessage);
    return resultObject;
}
Also used : DBTypeDriverProviderEntity(com.varsql.web.model.entity.db.DBTypeDriverProviderEntity) Connection(java.sql.Connection) Driver(java.sql.Driver) DBTypeDriverEntity(com.varsql.web.model.entity.db.DBTypeDriverEntity) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) EncryptDecryptException(com.vartech.common.crypto.EncryptDecryptException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BeansException(org.springframework.beans.BeansException) FileInfo(com.varsql.core.common.beans.FileInfo) ResponseResult(com.vartech.common.app.beans.ResponseResult) DBConnectionEntity(com.varsql.web.model.entity.db.DBConnectionEntity) JDBCDriverInfo(com.varsql.core.connection.beans.JDBCDriverInfo) EncryptDecryptException(com.vartech.common.crypto.EncryptDecryptException) VarsqlAppCode(com.varsql.core.common.code.VarsqlAppCode)

Aggregations

FileInfo (com.varsql.core.common.beans.FileInfo)1 VarsqlAppCode (com.varsql.core.common.code.VarsqlAppCode)1 JDBCDriverInfo (com.varsql.core.connection.beans.JDBCDriverInfo)1 DBConnectionEntity (com.varsql.web.model.entity.db.DBConnectionEntity)1 DBTypeDriverEntity (com.varsql.web.model.entity.db.DBTypeDriverEntity)1 DBTypeDriverProviderEntity (com.varsql.web.model.entity.db.DBTypeDriverProviderEntity)1 ResponseResult (com.vartech.common.app.beans.ResponseResult)1 EncryptDecryptException (com.vartech.common.crypto.EncryptDecryptException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Connection (java.sql.Connection)1 Driver (java.sql.Driver)1 PreparedStatement (java.sql.PreparedStatement)1 Properties (java.util.Properties)1 BeansException (org.springframework.beans.BeansException)1