Search in sources :

Example 91 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project MSEC by Tencent.

the class ReleaseOnlyLibrary method CommitPlan.

private String CommitPlan(ReleasePlan plan) {
    //产生发布用的文件,并记录到数据库里
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        return "DB Connect Failed.";
    }
    String sql;
    List<Object> params = new ArrayList<Object>();
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        String dest_ip_list_json_str = objectMapper.writeValueAsString(plan.getDest_ip_list());
        //System.out.println(dest_ip_list_json_str);
        sql = "insert into t_release_plan(plan_id, first_level_service_name,second_level_service_name," + "config_tag,idl_tag, sharedobject_tag, dest_ip_list,status,memo,release_type) values(?,?,?,?,?,?,?,'creating',?,?)";
        params.add(plan.getPlan_id());
        params.add(plan.getFirst_level_service_name());
        params.add(plan.getSecond_level_service_name());
        params.add("");
        params.add("");
        params.add("");
        params.add(dest_ip_list_json_str);
        params.add(plan.getMemo());
        params.add(plan.getRelease_type());
        int addNum = util.updateByPreparedStatement(sql, params);
        if (addNum != 1) {
            return "addNum is not 1:" + addNum;
        }
        //打包文件
        new Thread(new PackReleaseFile(plan, getServlet().getServletContext())).start();
        return "success";
    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    } finally {
        util.releaseConn();
        ;
    }
}
Also used : DBUtil(ngse.org.DBUtil) ArrayList(java.util.ArrayList) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 92 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project MSEC by Tencent.

the class ReleaseStepsGO method CommitPlan.

private String CommitPlan(ReleasePlan plan) {
    //产生发布用的文件,并记录到数据库里
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        return "DB Connect Failed.";
    }
    String sql;
    List<Object> params = new ArrayList<Object>();
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        String dest_ip_list_json_str = objectMapper.writeValueAsString(plan.getDest_ip_list());
        //System.out.println(dest_ip_list_json_str);
        sql = "insert into t_release_plan(plan_id, first_level_service_name,second_level_service_name," + "config_tag,idl_tag, sharedobject_tag, dest_ip_list,status,memo,release_type) values(?,?,?,?,?,?,?,'creating',?,?)";
        params.add(plan.getPlan_id());
        params.add(plan.getFirst_level_service_name());
        params.add(plan.getSecond_level_service_name());
        params.add(plan.getConfig_tag());
        params.add(plan.getIdl_tag());
        params.add(plan.getSharedobject_tag());
        params.add(dest_ip_list_json_str);
        params.add(plan.getMemo());
        params.add(plan.getRelease_type());
        int addNum = util.updateByPreparedStatement(sql, params);
        if (addNum != 1) {
            return "addNum is not 1:" + addNum;
        }
        //打包文件
        new Thread(new PackReleaseFile(plan, getServlet().getServletContext())).start();
        return "success";
    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    } finally {
        util.releaseConn();
        ;
    }
}
Also used : DBUtil(ngse.org.DBUtil) ArrayList(java.util.ArrayList) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 93 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project MSEC by Tencent.

the class RollbackReleasePlan method exec.

public JsonRPCResponseBase exec(ReleasePlan request) {
    Logger logger = Logger.getLogger(RollbackReleasePlan.class);
    JsonRPCResponseBase resp = new JsonRPCResponseBase();
    plan_id = request.getPlan_id();
    flsn = request.getFirst_level_service_name();
    slsn = request.getSecond_level_service_name();
    String result = checkIdentity();
    if (!result.equals("success")) {
        resp.setStatus(99);
        resp.setMessage(result);
        return resp;
    }
    DBUtil util = new DBUtil();
    if (util.getConnection() == null) {
        resp.setStatus(100);
        resp.setMessage("db connect failed!");
        return resp;
    }
    try {
        String sql = "select dest_ip_list from t_release_plan where plan_id=?";
        ArrayList<Object> params = new ArrayList<Object>();
        params.add(request.getPlan_id());
        Map<String, Object> res = util.findSimpleResult(sql, params);
        if (res.get("dest_ip_list") == null) {
            resp.setStatus(100);
            resp.setMessage("query dest ip list failed.");
            return resp;
        }
        String dest_ip_list = (String) (res.get("dest_ip_list"));
        ObjectMapper objectMapper = new ObjectMapper();
        IPPortPair[] ips = objectMapper.readValue(dest_ip_list, IPPortPair[].class);
        logger.info("destination IP number;" + ips.length);
        //对每个IP执行回滚
        getHttpResponse().setContentType("text/html");
        getHttpResponse().setCharacterEncoding("UTF-8");
        ServletOutputStream out = getHttpResponse().getOutputStream();
        safeWrite("<html>", out);
        safeWrite("<head>", out);
        safeWrite("<title>release result</title>", out);
        safeWrite("</head>", out);
        safeWrite("<body>", out);
        //这个标签用于原样输出
        safeWrite("<pre>", out);
        updatePlanStatus(util, "rolling back");
        doRollback(ips, out, util);
        safeWrite("</pre>", out);
        safeWrite("</body>", out);
        safeWrite("</html>", out);
        //update plan status
        updatePlanStatus(util, "roll back successfully");
        try {
            out.close();
        } catch (Exception e) {
        }
        return null;
    } catch (Exception e) {
        resp.setStatus(100);
        resp.setMessage(e.getMessage());
        e.printStackTrace();
        try {
            updatePlanStatus(util, "failed to roll back");
        } catch (Exception e1) {
        }
        return resp;
    } finally {
        util.releaseConn();
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ArrayList(java.util.ArrayList) Logger(org.apache.log4j.Logger) IPPortPair(beans.request.IPPortPair) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 94 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project MSEC by Tencent.

the class Worker method returnErrorMessage.

private void returnErrorMessage(String msg) {
    JsonRPCResponseBase r = new JsonRPCResponseBase();
    ObjectMapper objectMapper = new ObjectMapper();
    r.setMessage(msg);
    r.setStatus(100);
    try {
        String s = objectMapper.writeValueAsString(r);
        socket.getOutputStream().write(s.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
}
Also used : ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 95 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project MSEC by Tencent.

the class Worker method handleRequest.

//���÷�����ƣ�
// 1.��������json�ַ����е�handleClass�ֶΣ�ʵ����ʵ�ʵĴ�����
// 2. �Զ�������json�ַ���ʵ����Ϊjava�࣬�൱�ڲ������
// 3. �Զ�������󷵻ص�java��ʵ�������л�Ϊjson �ַ�������ΪӦ���͸�ǰ��
private String handleRequest(String jsonStr, InputStream in, OutputStream out) {
    JSONObject jsonObject = new JSONObject(jsonStr);
    String handleClassStr = jsonObject.getString("handleClass");
    JSONObject requestBodyObj = jsonObject.getJSONObject("requestBody");
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        Class<?> clazz = Class.forName(handleClassStr);
        //ʵ����handle classָ������
        JsonRPCHandler handler = (JsonRPCHandler) clazz.newInstance();
        //��ø�������з������ҵ�exec����������
        Method[] methods = clazz.getMethods();
        boolean execFound = false;
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals("exec")) {
                execFound = true;
                //exec�����IJ��������ͣ���json�����л�Ϊ�ò�����ʵ��
                Class<?>[] paramTypes = methods[i].getParameterTypes();
                if (paramTypes.length != 1) {
                    returnErrorMessage("handle class's exec() method's param number is not 1!");
                    return "failed";
                }
                Object exec_request = objectMapper.readValue(requestBodyObj.toString(), paramTypes[0]);
                //���exec�����ķ�������,Ҫ��̳���JsonRPCResponseBase
                Class<?> returnType = methods[i].getReturnType();
                String superClass = returnType.getSuperclass().getName();
                if (superClass == null || !superClass.equals("ngse.remote_shell.JsonRPCResponseBase")) {
                    returnErrorMessage("method exec() should return a class extending JsonRPCResponseBase.");
                    return "failed";
                }
                //����exec����,�������ص�java��ʵ�����л�Ϊjson�ַ�����Ӧ��ǰ��
                try {
                    Object exec_result = methods[i].invoke(handler, exec_request);
                    if (exec_request != null) {
                        //???????bean???��??json?????
                        String s = objectMapper.writeValueAsString(exec_result);
                        out.write(s.getBytes());
                    }
                    return "success";
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                    return "failed";
                }
            }
        }
        if (!execFound) {
            return "exec method not found";
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return "ClassNotFoundException:" + e.toString();
    } catch (InstantiationException e) {
        e.printStackTrace();
        return "InstantiationException:" + e.toString();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return "IllegalAccessException:" + e.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return "IllegalAccessException:" + e.toString();
    }
    return "failed";
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

ObjectMapper (org.codehaus.jackson.map.ObjectMapper)386 IOException (java.io.IOException)85 Test (org.junit.Test)61 JsonNode (org.codehaus.jackson.JsonNode)50 HashMap (java.util.HashMap)46 ArrayList (java.util.ArrayList)45 Test (org.testng.annotations.Test)37 Map (java.util.Map)34 List (java.util.List)26 File (java.io.File)22 StringWriter (java.io.StringWriter)22 JSONObject (org.json.JSONObject)18 SimpleModule (org.codehaus.jackson.map.module.SimpleModule)17 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 Version (org.codehaus.jackson.Version)14 JsonFactory (org.codehaus.jackson.JsonFactory)13 JSONObject (org.codehaus.jettison.json.JSONObject)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 JsonGenerator (org.codehaus.jackson.JsonGenerator)11