Search in sources :

Example 1 with JDBCDriverInfo

use of com.varsql.core.connection.beans.JDBCDriverInfo in project varsql by varsqlinfo.

the class ConnectionInfoComponent method getConnectionInfo.

@Override
public ConnectionInfo getConnectionInfo(String connid) throws EncryptDecryptException {
    logger.debug("create connection info : {}", connid);
    ConnectionInfo connInfo = new ConnectionInfo();
    ConnectionInfoDTO dto = dbConnectionEntityRepository.findByConnInfo(connid);
    connInfo.setConnid(dto.getConnection().getVconnid());
    connInfo.setAliasName(dto.getConnection().getVname());
    connInfo.setType(dto.getProvider().getDbType().toLowerCase());
    connInfo.setUsername(dto.getConnection().getVid());
    String pw = dto.getConnection().getVpw();
    connInfo.setPassword("");
    if (!StringUtils.isBlank(pw)) {
        connInfo.setPassword(DBPasswordCryptionFactory.getInstance().decrypt(pw));
    }
    connInfo.setPoolOptions(dto.getConnection().getVpoolopt());
    connInfo.setConnectionOptions(dto.getConnection().getVconnopt());
    connInfo.setMaxActive(NumberUtils.toInt(dto.getConnection().getMaxActive() + "", 10));
    connInfo.setMinIdle(NumberUtils.toInt(dto.getConnection().getMinIdle() + "", 3));
    connInfo.setConnectionTimeOut(NumberUtils.toInt(dto.getConnection().getTimeout() + "", 18000));
    connInfo.setExportCount(NumberUtils.toInt(dto.getConnection().getExportcount() + "", 1000));
    connInfo.setTestWhileIdle("Y".equals(dto.getConnection().getTestWhileIdle()));
    String defaultDriverValidationQuery = ValidationProperty.getInstance().validationQuery(connInfo.getType());
    String urlDirectYn = dto.getConnection().getUrlDirectYn();
    if ("Y".equals(urlDirectYn)) {
        connInfo.setUrl(dto.getConnection().getVurl());
    } else {
        connInfo.setUrl(VarsqlJdbcUtil.getJdbcUrl(dto.getDriver().getUrlFormat(), JdbcURLFormatParam.builder().serverIp(dto.getConnection().getVserverip()).port(Integer.parseInt(dto.getConnection().getVport() + "")).databaseName(dto.getConnection().getVdatabasename()).build()));
    }
    defaultDriverValidationQuery = StringUtils.isBlank(dto.getDriver().getValidationQuery()) ? defaultDriverValidationQuery : dto.getDriver().getValidationQuery();
    String validation_query = dto.getProvider().getValidationQuery();
    validation_query = StringUtils.isBlank(validation_query) ? defaultDriverValidationQuery : validation_query;
    connInfo.setValidationQuery(validation_query);
    List<FileInfo> driverFileInfos;
    if (PathType.PATH.equals(PathType.getPathType(dto.getProvider().getPathType()))) {
        try {
            driverFileInfos = FileServiceUtils.getFileInfos(dto.getProvider().getDriverPath().split(";"));
        } catch (IOException e) {
            logger.error("driver load fail : " + dto.getProvider().getDriverPath());
            driverFileInfos = null;
        }
    } else {
        driverFileInfos = FileServiceUtils.getFileInfos(dbTypeDriverFileEntityRepository.findByFileContId(dto.getProvider().getDriverProviderId()));
    }
    JDBCDriverInfo jdbcDriverInfo = new JDBCDriverInfo(dto.getDriver().getDriverId(), dto.getDriver().getDbdriver());
    jdbcDriverInfo.setDriverFiles(driverFileInfos);
    connInfo.setJdbcDriverInfo(jdbcDriverInfo);
    return connInfo;
}
Also used : FileInfo(com.varsql.core.common.beans.FileInfo) ConnectionInfo(com.varsql.core.connection.beans.ConnectionInfo) IOException(java.io.IOException) JDBCDriverInfo(com.varsql.core.connection.beans.JDBCDriverInfo)

Example 2 with JDBCDriverInfo

use of com.varsql.core.connection.beans.JDBCDriverInfo 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)

Example 3 with JDBCDriverInfo

use of com.varsql.core.connection.beans.JDBCDriverInfo in project varsql by varsqlinfo.

the class BaseTest method getBaseConnection.

public ConnectionInfo getBaseConnection() {
    ConnectionInfo connInfo = new ConnectionInfo();
    connInfo.setConnid(TEST_CON_UID);
    connInfo.setAliasName(TEST_CON_UID);
    connInfo.setType(DBType.H2.name());
    connInfo.setJdbcDriverInfo(new JDBCDriverInfo("testid", "org.h2.Driver"));
    connInfo.setUrl("jdbc:h2:file:C:/zzz/resources/varsql");
    connInfo.setUsername("sa");
    connInfo.setPassword("sa");
    connInfo.setValidation_query("select 1");
    connInfo.setPool_opt("max_idle=5;min_idle=5;");
    connInfo.setConnection_opt("sslConnection=true");
    connInfo.setTimebetweenevictionrunsmillis(60000);
    connInfo.setTest_while_idle("true");
    connInfo.setMax_active(2);
    connInfo.setMin_idle(1);
    try {
        ConnectionFactory.getInstance().createPool(connInfo);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
    return connInfo;
}
Also used : ConnectionInfo(com.varsql.core.connection.beans.ConnectionInfo) JDBCDriverInfo(com.varsql.core.connection.beans.JDBCDriverInfo)

Example 4 with JDBCDriverInfo

use of com.varsql.core.connection.beans.JDBCDriverInfo in project varsql by varsqlinfo.

the class Configuration method parseXml.

/**
 * 커넥션 정보 파싱
 * @Method Name  : parseXml
 * @Author : ytkim
 * @Method desc :
 * @History :
 * @param connectionResource
 */
private void parseXml(Resource connectionResource) {
    try {
        JsonNode jsonInfo = VartechUtils.xmlToJsonNode(ResourceUtils.getResourceString(connectionResource, getCharset()));
        vConInfo.setConnid(ConnectionContext.DEFAULT_CONN_ID);
        vConInfo.setAliasName(jsonInfo.get("name").asText(""));
        vConInfo.setType(jsonInfo.get("name").asText("local"));
        vConInfo.setUrl(jsonInfo.get("url").asText("jdbc:h2:file:#resourePath#/varsql;MV_STORE=FALSE;CACHE_SIZE=131072;").replace("#resourePath#", getInstallRoot()));
        vConInfo.setUsername(jsonInfo.get("username").asText(""));
        vConInfo.setPassword(jsonInfo.get("password").asText(""));
        vConInfo.setMaxActive(jsonInfo.get("max_active").asInt(10));
        vConInfo.setPoolOptions(jsonInfo.get("pool_option").asText(""));
        vConInfo.setConnectionOptions(jsonInfo.get("connection_option").asText(""));
        vConInfo.setTimebetweenevictionrunsmillis(jsonInfo.get("timebetweenevictionrunsmillis").asLong());
        vConInfo.setTestWhileIdle(Boolean.parseBoolean(jsonInfo.get("test_while_idle").asText()));
        JDBCDriverInfo jdbcDriverInfo = new JDBCDriverInfo("base", jsonInfo.get("driver").asText("org.h2.Driver"));
        this.vConInfo.setJdbcDriverInfo(jdbcDriverInfo);
    } catch (IOException io) {
        logger.error("CONNECTION_FILE IOException", io);
        throw new Error(io);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) JDBCDriverInfo(com.varsql.core.connection.beans.JDBCDriverInfo) IOException(java.io.IOException)

Example 5 with JDBCDriverInfo

use of com.varsql.core.connection.beans.JDBCDriverInfo in project varsql by varsqlinfo.

the class SimpleConnectionInfoDao method getConnectionInfo.

@Override
public ConnectionInfo getConnectionInfo(String connid) {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        conn = ConnectionFactory.getInstance().getConnection();
        StringBuffer querySb = new StringBuffer();
        querySb.append(" select ");
        querySb.append("  a.*  ");
        querySb.append("  , b.DRIVER_PROVIDER_ID, b.DRIVER_CLASS, b.VALIDATION_QUERY, b.DB_TYPE, b.PATH_TYPE, b.DRIVER_PATH ");
        querySb.append("  , c.URL_FORMAT ");
        querySb.append(" from ");
        querySb.append("  VTCONNECTION a left outer join VTDBTYPE_DRIVER_PROVIDER b on a.VDRIVER = b.DRIVER_PROVIDER_ID ");
        querySb.append("  left outer join VTDBTYPE_DRIVER_TYPE c on b.DRIVER_ID = c.DRIVER_ID  ");
        querySb.append(" where 1=1 ");
        querySb.append(" and a.VCONNID = ? ");
        pstmt = conn.prepareStatement(querySb.toString());
        pstmt.setString(1, connid);
        rs = pstmt.executeQuery();
        if (rs == null)
            throw new ConnectionFactoryException("not valid connection infomation :" + connid);
        rs.next();
        ConnectionInfo connInfo = new ConnectionInfo();
        connInfo.setConnid(rs.getString("VCONNID"));
        connInfo.setAliasName(rs.getString("VNAME"));
        connInfo.setType(rs.getString("VTYPE").toLowerCase());
        String urlDirectYn = rs.getString("URL_DIRECT_YN");
        if ("Y".equals(urlDirectYn)) {
            connInfo.setUrl(rs.getString("VURL"));
        } else {
            connInfo.setUrl(VarsqlJdbcUtil.getJdbcUrl(rs.getString("URL_FORMAT"), JdbcURLFormatParam.builder().serverIp(rs.getString("VSERVERIP")).port(Integer.parseInt(rs.getString("VPORT"))).databaseName(rs.getString("VDATABASENAME")).build()));
        }
        connInfo.setUsername(rs.getString("VID"));
        if ("varsql".equals(connInfo.getConnid())) {
            connInfo.setPassword(rs.getString("VPW"));
        } else {
            String str = rs.getString("VPW");
            connInfo.setPassword("");
            if (str != null && !"".equals(str))
                connInfo.setPassword(DBPasswordCryptionFactory.getInstance().decrypt(rs.getString("VPW")));
        }
        connInfo.setPoolOptions(rs.getString("VPOOLOPT"));
        connInfo.setConnectionOptions(rs.getString("VCONNOPT"));
        connInfo.setMaxActive(NumberUtils.toInt(rs.getString("MAX_ACTIVE"), 10));
        connInfo.setMinIdle(NumberUtils.toInt(rs.getString("MIN_IDLE"), 3));
        connInfo.setConnectionTimeOut(NumberUtils.toInt(rs.getString("TIMEOUT"), 18000));
        connInfo.setExportCount(NumberUtils.toInt(rs.getString("EXPORTCOUNT"), 1000));
        connInfo.setTestWhileIdle("Y".equals(rs.getString("TEST_WHILE_IDLE")));
        String conn_query = rs.getString("VQUERY");
        String dbvalidation_query = rs.getString("VALIDATION_QUERY");
        conn_query = (conn_query == null) ? "" : conn_query;
        dbvalidation_query = (dbvalidation_query == null) ? "" : dbvalidation_query;
        String validation_query = !"".equals(conn_query.trim()) ? conn_query : (!"".equals(dbvalidation_query.trim()) ? dbvalidation_query : ValidationProperty.getInstance().validationQuery(connInfo.getType()));
        this.logger.debug("valication_query : {}", validation_query);
        connInfo.setValidationQuery(validation_query);
        String driverProviderId = rs.getString("DRIVER_PROVIDER_ID");
        pstmt.close();
        pstmt = conn.prepareStatement(" select FILE_NAME,FILE_PATH,FILE_EXT from VTDBTYPE_DRIVER_FILE where FILE_CONT_ID = ? ");
        pstmt.setString(1, driverProviderId);
        rs.close();
        rs = pstmt.executeQuery();
        if (rs == null) {
            throw new ConnectionFactoryException("jdbc driver file not found :" + connid);
        }
        List<FileInfo> jarFileList = new ArrayList<>();
        while (rs.next()) {
            FileInfo fi = new FileInfo();
            fi.setName(rs.getString("FILE_NAME"));
            fi.setPath(Paths.get(VarsqlConstants.UPLOAD_PATH).toAbsolutePath().resolve(rs.getString("FILE_PATH")).normalize().toAbsolutePath().toString());
            fi.setExt(rs.getString("FILE_EXT"));
            jarFileList.add(fi);
        }
        JDBCDriverInfo jdbcDriverInfo = new JDBCDriverInfo(rs.getString("DRIVER_PROVIDER_ID"), rs.getString("DRIVER_CLASS"));
        jdbcDriverInfo.setDriverFiles(jarFileList);
        connInfo.setJdbcDriverInfo(jdbcDriverInfo);
        return connInfo;
    } catch (EncryptDecryptException e) {
        this.logger.error("password decrypt error", e);
        throw new ConnectionFactoryException("password decrypt error", e);
    } catch (Exception e) {
        this.logger.error("empty connection info", e);
        throw new ConnectionFactoryException("empty connection info : [" + connid + "]", e);
    } finally {
        JdbcUtils.close(conn, pstmt, rs);
    }
}
Also used : Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ConnectionFactoryException(com.varsql.core.exception.ConnectionFactoryException) ConnectionFactoryException(com.varsql.core.exception.ConnectionFactoryException) EncryptDecryptException(com.vartech.common.crypto.EncryptDecryptException) FileInfo(com.varsql.core.common.beans.FileInfo) ResultSet(java.sql.ResultSet) ConnectionInfo(com.varsql.core.connection.beans.ConnectionInfo) JDBCDriverInfo(com.varsql.core.connection.beans.JDBCDriverInfo) EncryptDecryptException(com.vartech.common.crypto.EncryptDecryptException)

Aggregations

JDBCDriverInfo (com.varsql.core.connection.beans.JDBCDriverInfo)6 FileInfo (com.varsql.core.common.beans.FileInfo)4 ConnectionInfo (com.varsql.core.connection.beans.ConnectionInfo)3 EncryptDecryptException (com.vartech.common.crypto.EncryptDecryptException)3 IOException (java.io.IOException)3 DBTypeDriverProviderEntity (com.varsql.web.model.entity.db.DBTypeDriverProviderEntity)2 ResponseResult (com.vartech.common.app.beans.ResponseResult)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 BeansException (org.springframework.beans.BeansException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 VarsqlAppCode (com.varsql.core.common.code.VarsqlAppCode)1 ConnectionFactoryException (com.varsql.core.exception.ConnectionFactoryException)1 VarsqlRuntimeException (com.varsql.core.exception.VarsqlRuntimeException)1 DBConnectionEntity (com.varsql.web.model.entity.db.DBConnectionEntity)1 DBTypeDriverEntity (com.varsql.web.model.entity.db.DBTypeDriverEntity)1 Driver (java.sql.Driver)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1