Search in sources :

Example 31 with DBUtil

use of ngse.org.DBUtil in project MSEC by Tencent.

the class AddOddService method exec.

public AddServiceResponse exec(AddServiceRequest request) {
    AddServiceResponse response = new AddServiceResponse();
    response.setMessage("unkown error.");
    response.setStatus(100);
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    if (request.getService_name() == null || request.getService_name().equals("") || request.getService_level() == null || request.getService_level().equals("")) {
        response.setMessage("The name/level of service to be added should NOT be empty.");
        response.setStatus(100);
        return response;
    }
    if (request.getService_level().equals("second_level") && (request.getService_parent() == null || request.getService_parent().equals(""))) {
        response.setMessage("The first level service name  should NOT be empty.");
        response.setStatus(100);
        return response;
    }
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setMessage("DB connect failed.");
        response.setStatus(100);
        return response;
    }
    String sql;
    List<Object> params = new ArrayList<Object>();
    if (request.getService_level().equals("first_level")) {
        sql = "insert into t_first_level_service( first_level_service_name, type) values(?, 'odd')";
        params.add(request.getService_name());
    } else {
        sql = "insert into t_second_level_service(second_level_service_name, first_level_service_name, type) values(?,?, 'odd')";
        params.add(request.getService_name());
        params.add(request.getService_parent());
    }
    try {
        int addNum = util.updateByPreparedStatement(sql, params);
        if (addNum >= 0) {
            response.setAddNum(addNum);
            response.setMessage("success");
            response.setStatus(0);
            return response;
        }
    } catch (SQLException e) {
        response.setMessage("add record failed:" + e.toString());
        response.setStatus(100);
        e.printStackTrace();
        return response;
    } finally {
        util.releaseConn();
    }
    return response;
}
Also used : AddServiceResponse(beans.response.AddServiceResponse) DBUtil(ngse.org.DBUtil) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList)

Example 32 with DBUtil

use of ngse.org.DBUtil in project MSEC by Tencent.

the class AddSecondLevelServiceConfigTag method exec.

public AddSecondLevelServiceConfigTagResponse exec(AddSecondLevelServiceConfigTagRequest request) {
    AddSecondLevelServiceConfigTagResponse response = new AddSecondLevelServiceConfigTagResponse();
    response.setMessage("unkown error.");
    response.setStatus(100);
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    if (request.getTag_name() == null || request.getTag_name().equals("") || request.getFirst_level_service_name() == null || request.getFirst_level_service_name().length() < 1 || request.getSecond_level_service_name() == null || request.getSecond_level_service_name().length() < 1 || request.getContent() == null || request.getContent().length() < 1) {
        response.setMessage("Some request field is  empty.");
        response.setStatus(100);
        return response;
    }
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setMessage("DB connect failed.");
        response.setStatus(100);
        return response;
    }
    try {
        //保存文件内容到服务器磁盘
        String filename = SecondLevelServiceConfigTag.getConfigFileName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), request.getTag_name());
        if (!SaveConfigContent(filename, request.getContent())) {
            response.setMessage("save file failed.");
            response.setStatus(100);
            return response;
        }
        //写入端口配置信息
        int port = getStandardServicePort(util, request.getFirst_level_service_name(), request.getSecond_level_service_name());
        addPortConfig(filename, port);
        //插入数据库
        String sql;
        List<Object> params = new ArrayList<Object>();
        sql = "insert into t_config_tag(tag_name, memo, second_level_service_name,first_level_service_name) values(?,?,?,?)";
        params.add(request.getTag_name());
        if (request.getMemo() == null) {
            params.add("");
        } else {
            params.add(request.getMemo());
        }
        params.add(request.getSecond_level_service_name());
        params.add(request.getFirst_level_service_name());
        int addNum = util.updateByPreparedStatement(sql, params);
        if (addNum > 0) {
            response.setAddNumber(addNum);
            response.setMessage("success");
            response.setStatus(0);
            return response;
        } else {
            response.setAddNumber(addNum);
            response.setMessage("failed to insert table");
            response.setStatus(100);
            return response;
        }
    } catch (Exception e) {
        response.setMessage("add record failed:" + e.toString());
        response.setStatus(100);
        e.printStackTrace();
        return response;
    } finally {
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) AddSecondLevelServiceConfigTagResponse(beans.response.AddSecondLevelServiceConfigTagResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Example 33 with DBUtil

use of ngse.org.DBUtil in project MSEC by Tencent.

the class ChangeCapacity method exec.

public JsonRPCResponseBase exec(ChangeCapacityRequest request) {
    JsonRPCResponseBase response = new JsonRPCResponseBase();
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    Logger logger = Logger.getLogger(ChangeCapacity.class);
    //把ip列表刷新到LB的服务器里并下发给各个客户端
    AccessZooKeeper azk = new AccessZooKeeper();
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setStatus(100);
        response.setMessage("db connect failed");
        return response;
    }
    try {
        //step1:更新扩缩容的IP的状态
        if (//扩容
        request.getAction_type().equals("expand")) {
            updateIPStatus(request, true, util);
        } else // 缩容
        {
            updateIPStatus(request, false, util);
        }
        logger.info("update matchine's status OK, matchine Numbers:" + request.getIp_list().size());
        //step2: 获得该服务下所有enabled的IP,写入LB系统
        ArrayList<IPPortPair> ips = LoadBalance.getIPPortInfoByServiceName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), util);
        if (ips == null) {
            response.setStatus(100);
            response.setMessage("get ip info from db failed");
            return response;
        }
        logger.info("get enabled IP, they will be write to LB server, number:" + ips.size());
        result = LoadBalance.writeOneServiceConfigInfo(azk, request.getFirst_level_service_name() + "/" + request.getSecond_level_service_name(), true, ips);
        if (result == null || !result.equals("success")) {
            response.setStatus(100);
            response.setMessage(result);
            return response;
        }
        logger.info("write into LB server successfully.");
        //step3: 更新涉及到的IP为key的配置信息
        ArrayList<String> whiteNameList = new ArrayList<>();
        for (int i = 0; i < request.getIp_list().size(); i++) {
            whiteNameList.add(request.getIp_list().get(i).getIp());
        }
        logger.info("update the IP-key information in LB server");
        result = LoadBalance.writeIPConfigInfo(azk, whiteNameList);
        if (result == null || !result.equals("success")) {
            response.setStatus(100);
            response.setMessage(result);
            return response;
        }
        response.setStatus(0);
        response.setMessage("success");
        return response;
    } catch (Exception e) {
        e.printStackTrace();
        response.setStatus(100);
        response.setMessage(e.getMessage());
        return response;
    } finally {
        azk.disconnect();
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) JsonRPCResponseBase(ngse.org.JsonRPCResponseBase) IPPortPair(beans.request.IPPortPair) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) AccessZooKeeper(ngse.org.AccessZooKeeper)

Example 34 with DBUtil

use of ngse.org.DBUtil in project MSEC by Tencent.

the class DelIDLTag method exec.

public DelIDLTagResponse exec(IDL request) {
    Logger logger = Logger.getLogger(this.getClass().getName());
    DelIDLTagResponse response = new DelIDLTagResponse();
    response.setMessage("unkown error.");
    response.setStatus(100);
    String result = checkIdentity();
    if (!result.equals("success")) {
        response.setStatus(99);
        response.setMessage(result);
        return response;
    }
    if (request.getTag_name() == null || request.getTag_name().equals("") || request.getSecond_level_service_name() == null || request.getSecond_level_service_name().equals("") || request.getFirst_level_service_name() == null || request.getFirst_level_service_name().equals("")) {
        response.setMessage("tag name and service name  should NOT be empty.");
        response.setStatus(100);
        return response;
    }
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        response.setMessage("DB connect failed.");
        response.setStatus(100);
        return response;
    }
    String sql = "delete from t_idl_tag where tag_name=? and first_level_service_name=? and second_level_service_name=?";
    List<Object> params = new ArrayList<Object>();
    params.add(request.getTag_name());
    params.add(request.getFirst_level_service_name());
    params.add(request.getSecond_level_service_name());
    try {
        //删除数据库记录
        int delNum = util.updateByPreparedStatement(sql, params);
        String filename = IDL.getIDLFileName(request.getFirst_level_service_name(), request.getSecond_level_service_name(), request.getTag_name());
        //删除文件
        logger.error("delte file:" + filename);
        RemoveFile(filename);
        if (delNum > 0) {
            response.setMessage("success");
            response.setDelNumber(delNum);
            response.setStatus(0);
            return response;
        } else {
            response.setMessage("delete record number is " + delNum);
            response.setDelNumber(delNum);
            response.setStatus(100);
            return response;
        }
    } catch (SQLException e) {
        response.setMessage("Delete record failed:" + e.toString());
        response.setStatus(100);
        e.printStackTrace();
        return response;
    } finally {
        util.releaseConn();
    }
}
Also used : DBUtil(ngse.org.DBUtil) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DelIDLTagResponse(beans.response.DelIDLTagResponse) Logger(org.apache.log4j.Logger)

Example 35 with DBUtil

use of ngse.org.DBUtil in project MSEC by Tencent.

the class DelIDLTag method deleteAll.

public static String deleteAll(String flsn, String slsn) {
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        return "DB connect failed.";
    }
    try {
        String sql = "select tag_name from t_idl_tag where  first_level_service_name=? and second_level_service_name=?";
        List<Object> params = new ArrayList<Object>();
        params.add(flsn);
        params.add(slsn);
        ArrayList<IDL> result = util.findMoreRefResult(sql, params, IDL.class);
        for (int i = 0; i < result.size(); i++) {
            String tag_name = result.get(i).getTag_name();
            sql = "delete from t_idl_tag where tag_name=? and first_level_service_name=? and second_level_service_name=?";
            params = new ArrayList<Object>();
            params.add(tag_name);
            params.add(flsn);
            params.add(slsn);
            //删除数据库记录
            int delNum = util.updateByPreparedStatement(sql, params);
            String filename = IDL.getIDLFileName(flsn, slsn, tag_name);
            RemoveFile(filename);
        }
        return "success";
    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    } finally {
        util.releaseConn();
    }
}
Also used : IDL(beans.dbaccess.IDL) DBUtil(ngse.org.DBUtil) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException)

Aggregations

DBUtil (ngse.org.DBUtil)51 ArrayList (java.util.ArrayList)36 SQLException (java.sql.SQLException)23 Logger (org.apache.log4j.Logger)20 IPPortPair (beans.request.IPPortPair)12 OddSecondLevelServiceIPInfo (beans.dbaccess.OddSecondLevelServiceIPInfo)7 AccessZooKeeper (ngse.org.AccessZooKeeper)6 JsonRPCResponseBase (ngse.org.JsonRPCResponseBase)6 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)5 SecondLevelService (beans.dbaccess.SecondLevelService)4 OddSecondLevelService (beans.dbaccess.OddSecondLevelService)2 ReleasePlan (beans.request.ReleasePlan)2 IOException (java.io.IOException)2 HttpSession (javax.servlet.http.HttpSession)2 CapacityBaseInfo (beans.dbaccess.CapacityBaseInfo)1 CapacityDetailInfo (beans.dbaccess.CapacityDetailInfo)1 IDL (beans.dbaccess.IDL)1 LibraryFile (beans.dbaccess.LibraryFile)1 MachineInfo (beans.dbaccess.MachineInfo)1 OddFirstLevelService (beans.dbaccess.OddFirstLevelService)1