com.alibaba.dubbo.common.json.JSON Java Examples

The following examples show how to use com.alibaba.dubbo.common.json.JSON. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StringUtils.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public static String toArgumentString(Object[] args) {
    StringBuilder buf = new StringBuilder();
       for (Object arg : args) {
           if (buf.length() > 0) {
               buf.append(Constants.COMMA_SEPARATOR);
           }
           if (arg == null || ReflectUtils.isPrimitives(arg.getClass())) {
               buf.append(arg);
           } else {
               try {
                   buf.append(JSON.json(arg));
               } catch (IOException e) {
                   logger.warn(e.getMessage(), e);
                   buf.append(arg);
               }
           }
       }
       return buf.toString();
}
 
Example #2
Source File: OrderControllerImpl.java    From SpringBoot-Dubbo-Docker-Jenkins with Apache License 2.0 6 votes vote down vote up
private void transferProdIdCountJson(OrderInsertReq orderInsertReq) {
    if (orderInsertReq != null && StringUtils.isNotEmpty(orderInsertReq.getProdIdCountJson())) {
        try {
            Map<String, Long> prodIdCountMapPre = JSON.parse(orderInsertReq.getProdIdCountJson(), Map.class);
            Map<String, Integer> prodIdCountMap = Maps.newHashMap();
            if (prodIdCountMapPre.size() > 0) {
                for (String key : prodIdCountMapPre.keySet()) {
                    prodIdCountMap.put(key, (Integer) prodIdCountMapPre.get(key).intValue());
                }
            }
            orderInsertReq.setProdIdCountMap(prodIdCountMap);
        } catch (ParseException e) {
            throw new CommonBizException(ExpCodeEnum.JSONERROR);
        }
    }

}
 
Example #3
Source File: JsonObjectInput.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Object readObject() throws IOException, ClassNotFoundException {
    try {
        String json = readLine();
        if (json.startsWith("{")) {
            return JSON.parse(json, Map.class);
        } else {
            json = "{\"value\":" + json + "}";
            
            @SuppressWarnings("unchecked")
            Map<String, Object> map = JSON.parse(json, Map.class);
            return map.get("value");
        }
    } catch (ParseException e) {
        throw new IOException(e.getMessage());
    }
}
 
Example #4
Source File: JsonObjectInput.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Object readObject() throws IOException, ClassNotFoundException {
    try {
        String json = readLine();
        if (json.startsWith("{")) {
            return JSON.parse(json, Map.class);
        } else {
            json = "{\"value\":" + json + "}";
            
            @SuppressWarnings("unchecked")
            Map<String, Object> map = JSON.parse(json, Map.class);
            return map.get("value");
        }
    } catch (ParseException e) {
        throw new IOException(e.getMessage());
    }
}
 
Example #5
Source File: StringUtils.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public static String toArgumentString(Object[] args) {
    StringBuilder buf = new StringBuilder();
       for (Object arg : args) {
           if (buf.length() > 0) {
               buf.append(Constants.COMMA_SEPARATOR);
           }
           if (arg == null || ReflectUtils.isPrimitives(arg.getClass())) {
               buf.append(arg);
           } else {
               try {
                   buf.append(JSON.json(arg));
               } catch (IOException e) {
                   logger.warn(e.getMessage(), e);
                   buf.append(arg);
               }
           }
       }
       return buf.toString();
}
 
Example #6
Source File: StringUtils.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public static String toArgumentString(Object[] args) {
    StringBuilder buf = new StringBuilder();
       for (Object arg : args) {
           if (buf.length() > 0) {
               buf.append(Constants.COMMA_SEPARATOR);
           }
           if (arg == null || ReflectUtils.isPrimitives(arg.getClass())) {
               buf.append(arg);
           } else {
               try {
                   buf.append(JSON.json(arg));
               } catch (IOException e) {
                   logger.warn(e.getMessage(), e);
                   buf.append(arg);
               }
           }
       }
       return buf.toString();
}
 
Example #7
Source File: JsonObjectInput.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Object readObject() throws IOException, ClassNotFoundException {
    try {
        String json = readLine();
        if (json.startsWith("{")) {
            return JSON.parse(json, Map.class);
        } else {
            json = "{\"value\":" + json + "}";
            
            @SuppressWarnings("unchecked")
            Map<String, Object> map = JSON.parse(json, Map.class);
            return map.get("value");
        }
    } catch (ParseException e) {
        throw new IOException(e.getMessage());
    }
}
 
Example #8
Source File: JsonObjectInput.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public Object readObject() throws IOException, ClassNotFoundException {
    try {
        String json = readLine();
        if (json.startsWith("{")) {
            return JSON.parse(json, Map.class);
        } else {
            json = "{\"value\":" + json + "}";
            
            @SuppressWarnings("unchecked")
            Map<String, Object> map = JSON.parse(json, Map.class);
            return map.get("value");
        }
    } catch (ParseException e) {
        throw new IOException(e.getMessage());
    }
}
 
Example #9
Source File: StringUtils.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public static String toArgumentString(Object[] args) {
    StringBuilder buf = new StringBuilder();
    for (Object arg : args) {
        if (buf.length() > 0) {
            buf.append(Constants.COMMA_SEPARATOR);
        }
        if (arg == null || ReflectUtils.isPrimitives(arg.getClass())) {
            buf.append(arg);
        } else {
            try {
                buf.append(JSON.json(arg));
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
                buf.append(arg);
            }
        }
    }
    return buf.toString();
}
 
Example #10
Source File: StringUtils.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public static String toArgumentString(Object[] args) {
    StringBuilder buf = new StringBuilder();
       for (Object arg : args) {
           if (buf.length() > 0) {
               buf.append(Constants.COMMA_SEPARATOR);
           }
           if (arg == null || ReflectUtils.isPrimitives(arg.getClass())) {
               buf.append(arg);
           } else {
               try {
                   buf.append(JSON.json(arg));
               } catch (IOException e) {
                   logger.warn(e.getMessage(), e);
                   buf.append(arg);
               }
           }
       }
       return buf.toString();
}
 
Example #11
Source File: MockInvoker.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public static Object parseMockValue(String mock, Type[] returnTypes) throws Exception {
    Object value;
    if ("empty".equals(mock)) {
        value = ReflectUtils.getEmptyObject(returnTypes != null && returnTypes.length > 0 ? (Class<?>) returnTypes[0] : null);
    } else if ("null".equals(mock)) {
        value = null;
    } else if ("true".equals(mock)) {
        value = true;
    } else if ("false".equals(mock)) {
        value = false;
    } else if (mock.length() >= 2 && (mock.startsWith("\"") && mock.endsWith("\"")
            || mock.startsWith("\'") && mock.endsWith("\'"))) {
        value = mock.subSequence(1, mock.length() - 1);
    } else if (returnTypes != null && returnTypes.length > 0 && returnTypes[0] == String.class) {
        value = mock;
    } else if (StringUtils.isNumeric(mock)) {
        value = JSON.parse(mock);
    } else if (mock.startsWith("{")) {
        value = JSON.parse(mock, Map.class);
    } else if (mock.startsWith("[")) {
        value = JSON.parse(mock, List.class);
    } else {
        value = mock;
    }
    if (returnTypes != null && returnTypes.length > 0) {
        value = PojoUtils.realize(value, (Class<?>) returnTypes[0], returnTypes.length > 1 ? returnTypes[1] : null);
    }
    return value;
}
 
Example #12
Source File: MockInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public static Object parseMockValue(String mock, Type[] returnTypes) throws Exception {
    Object value = null;
    if ("empty".equals(mock)) {
        value = ReflectUtils.getEmptyObject(returnTypes != null && returnTypes.length > 0 ? (Class<?>)returnTypes[0] : null);
    } else if ("null".equals(mock)) {
        value = null;
    } else if ("true".equals(mock)) {
        value = true;
    } else if ("false".equals(mock)) {
        value = false;
    } else if (mock.length() >=2 && (mock.startsWith("\"") && mock.endsWith("\"")
            || mock.startsWith("\'") && mock.endsWith("\'"))) {
        value = mock.subSequence(1, mock.length() - 1);
    } else if (returnTypes !=null && returnTypes.length >0 && returnTypes[0] == String.class) {
        value = mock;
    } else if (StringUtils.isNumeric(mock)) {
        value = JSON.parse(mock);
    }else if (mock.startsWith("{")) {
        value = JSON.parse(mock, Map.class);
    } else if (mock.startsWith("[")) {
        value = JSON.parse(mock, List.class);
    } else {
        value = mock;
    }
    if (returnTypes != null && returnTypes.length > 0) {
        value = PojoUtils.realize(value, (Class<?>)returnTypes[0], returnTypes.length > 1 ? returnTypes[1] : null);
    }
    return value;
}
 
Example #13
Source File: MockInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public static Object parseMockValue(String mock, Type[] returnTypes) throws Exception {
    Object value = null;
    if ("empty".equals(mock)) {
        value = ReflectUtils.getEmptyObject(returnTypes != null && returnTypes.length > 0 ? (Class<?>)returnTypes[0] : null);
    } else if ("null".equals(mock)) {
        value = null;
    } else if ("true".equals(mock)) {
        value = true;
    } else if ("false".equals(mock)) {
        value = false;
    } else if (mock.length() >=2 && (mock.startsWith("\"") && mock.endsWith("\"")
            || mock.startsWith("\'") && mock.endsWith("\'"))) {
        value = mock.subSequence(1, mock.length() - 1);
    } else if (returnTypes !=null && returnTypes.length >0 && returnTypes[0] == String.class) {
        value = mock;
    } else if (StringUtils.isNumeric(mock)) {
        value = JSON.parse(mock);
    }else if (mock.startsWith("{")) {
        value = JSON.parse(mock, Map.class);
    } else if (mock.startsWith("[")) {
        value = JSON.parse(mock, List.class);
    } else {
        value = mock;
    }
    if (returnTypes != null && returnTypes.length > 0) {
        value = PojoUtils.realize(value, (Class<?>)returnTypes[0], returnTypes.length > 1 ? returnTypes[1] : null);
    }
    return value;
}
 
Example #14
Source File: DubboUtil.java    From DevToolBox with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 调用方法
 *
 * @param c
 * @param method
 * @param fullUrl
 * @param args
 * @return
 * @throws java.lang.Exception
 */
public static Object invoke(Class c, Method method, String fullUrl, Object... args) throws Exception {
    DubboInvoker<?> invoker = (DubboInvoker<?>) PROTOCOL.refer(c, URL.valueOf(fullUrl));
    if (invoker.isAvailable()) {
        Invocation inv = new RpcInvocation(method, args);
        Result ret = invoker.invoke(inv);
        PROTOCOL.destroy();
        return JSON.json(ret.getValue());
    }
    return null;
}
 
Example #15
Source File: MockInvoker.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public static Object parseMockValue(String mock, Type[] returnTypes) throws Exception {
    Object value = null;
    if ("empty".equals(mock)) {
        value = ReflectUtils.getEmptyObject(returnTypes != null && returnTypes.length > 0 ? (Class<?>)returnTypes[0] : null);
    } else if ("null".equals(mock)) {
        value = null;
    } else if ("true".equals(mock)) {
        value = true;
    } else if ("false".equals(mock)) {
        value = false;
    } else if (mock.length() >=2 && (mock.startsWith("\"") && mock.endsWith("\"")
            || mock.startsWith("\'") && mock.endsWith("\'"))) {
        value = mock.subSequence(1, mock.length() - 1);
    } else if (returnTypes !=null && returnTypes.length >0 && returnTypes[0] == String.class) {
        value = mock;
    } else if (StringUtils.isNumeric(mock)) {
        value = JSON.parse(mock);
    }else if (mock.startsWith("{")) {
        value = JSON.parse(mock, Map.class);
    } else if (mock.startsWith("[")) {
        value = JSON.parse(mock, List.class);
    } else {
        value = mock;
    }
    if (returnTypes != null && returnTypes.length > 0) {
        value = PojoUtils.realize(value, (Class<?>)returnTypes[0], returnTypes.length > 1 ? returnTypes[1] : null);
    }
    return value;
}
 
Example #16
Source File: MockInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public static Object parseMockValue(String mock, Type[] returnTypes) throws Exception {
    Object value = null;
    if ("empty".equals(mock)) {
        value = ReflectUtils.getEmptyObject(returnTypes != null && returnTypes.length > 0 ? (Class<?>)returnTypes[0] : null);
    } else if ("null".equals(mock)) {
        value = null;
    } else if ("true".equals(mock)) {
        value = true;
    } else if ("false".equals(mock)) {
        value = false;
    } else if (mock.length() >=2 && (mock.startsWith("\"") && mock.endsWith("\"")
            || mock.startsWith("\'") && mock.endsWith("\'"))) {
        value = mock.subSequence(1, mock.length() - 1);
    } else if (returnTypes !=null && returnTypes.length >0 && returnTypes[0] == String.class) {
        value = mock;
    } else if (StringUtils.isNumeric(mock)) {
        value = JSON.parse(mock);
    }else if (mock.startsWith("{")) {
        value = JSON.parse(mock, Map.class);
    } else if (mock.startsWith("[")) {
        value = JSON.parse(mock, List.class);
    } else {
        value = mock;
    }
    if (returnTypes != null && returnTypes.length > 0) {
        value = PojoUtils.realize(value, (Class<?>)returnTypes[0], returnTypes.length > 1 ? returnTypes[1] : null);
    }
    return value;
}
 
Example #17
Source File: MainAction.java    From DevToolBox with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void createShowDoc(String respBody, HttpRequestData.Request req) throws Exception {
    String tmp = respBody.startsWith("\"") ? respBody.substring(1, respBody.length() - 1) : respBody;
    JsonObject json = gson.fromJson(tmp, JsonObject.class);
    StringBuilder sb = new StringBuilder();
    String urlstr = req.getUrl();
    String[] adds = urlstr.split("/");
    int idx = urlstr.indexOf(adds[3]);
    sb.append("**简要描述:**\n\n- \n\n**请求URL:**\n\n- ` ").append(urlstr.substring(idx));
    sb.append(" `\n\n**请求方式:**\n\n- ").append(req.getMethod()).append(" \n\n**参数:** \n\n|参数名|必选|类型|说明|\n|:----|:---|:-----|-----|\n");
    String param = req.getBody();

    if (param != null && !param.trim().isEmpty()) {
        if ((param.startsWith("{") || param.startsWith("[")) && (param.endsWith("{") || param.endsWith("["))) {
            JsonObject jo = JSON.parse(param, JsonObject.class
            );
            jo.entrySet().forEach((entry) -> {
                removeArrayEle(entry.getValue());
            });
            analysisJsonStr(sb, jo);
        } else {
            String[] pms = param.split("&");
            for (String pm : pms) {
                sb.append("|").append(pm.split("=")[0]).append("|  |String|  |\n");
            }
        }
    }
    json.entrySet().forEach((entry) -> {
        removeArrayEle(entry.getValue());
    });
    sb.append("**返回示例**\n\n```\n").append(Formatter.formatJson(json.toString())).append("\n```\n\n**返回参数说明** \n\n|参数名|类型|说明|备注|\n|:-----|:-----|-----| |\n");
    analysisJsonStr(sb, json);
    sb.append(" **备注** \n\n- 更多返回错误代码请看首页的错误代码描述");
    mc.showDoc.setText(sb.toString());
}
 
Example #18
Source File: AccessLogFilter.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
    try {
        String accesslog = invoker.getUrl().getParameter(Constants.ACCESS_LOG_KEY);
        if (ConfigUtils.isNotEmpty(accesslog)) {
            RpcContext context = RpcContext.getContext();
            String serviceName = invoker.getInterface().getName();
            String version = invoker.getUrl().getParameter(Constants.VERSION_KEY);
            String group = invoker.getUrl().getParameter(Constants.GROUP_KEY);
            StringBuilder sn = new StringBuilder();
            sn.append("[").append(new SimpleDateFormat(MESSAGE_DATE_FORMAT).format(new Date())).append("] ").append(context.getRemoteHost()).append(":").append(context.getRemotePort())
            .append(" -> ").append(context.getLocalHost()).append(":").append(context.getLocalPort())
            .append(" - ");
            if (null != group && group.length() > 0) {
                sn.append(group).append("/");
            }
            sn.append(serviceName);
            if (null != version && version.length() > 0) {
                sn.append(":").append(version);
            }
            sn.append(" ");
            sn.append(inv.getMethodName());
            sn.append("(");
            Class<?>[] types = inv.getParameterTypes();
            if (types != null && types.length > 0) {
                boolean first = true;
                for (Class<?> type : types) {
                    if (first) {
                        first = false;
                    } else {
                        sn.append(",");
                    }
                    sn.append(type.getName());
                }
            }
            sn.append(") ");
            Object[] args = inv.getArguments();
            if (args != null && args.length > 0) {
                sn.append(JSON.json(args));
            }
            String msg = sn.toString();
            if (ConfigUtils.isDefault(accesslog)) {
                LoggerFactory.getLogger(ACCESS_LOG_KEY + "." + invoker.getInterface().getName()).info(msg);
            } else {
                log(accesslog, msg);
            }
        }
    } catch (Throwable t) {
        logger.warn("Exception in AcessLogFilter of service(" + invoker + " -> " + inv + ")", t);
    }
    return invoker.invoke(inv);
}
 
Example #19
Source File: JsonObjectOutput.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public void writeObject(Object obj) throws IOException {
    JSON.json(obj, writer, writeClass);
    writer.println();
    writer.flush();
}
 
Example #20
Source File: AccessLogFilter.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
    try {
        String accesslog = invoker.getUrl().getParameter(Constants.ACCESS_LOG_KEY);
        if (ConfigUtils.isNotEmpty(accesslog)) {
            RpcContext context = RpcContext.getContext();
            String serviceName = invoker.getInterface().getName();
            String version = invoker.getUrl().getParameter(Constants.VERSION_KEY);
            String group = invoker.getUrl().getParameter(Constants.GROUP_KEY);
            StringBuilder sn = new StringBuilder();
            sn.append("[").append(new SimpleDateFormat(MESSAGE_DATE_FORMAT).format(new Date())).append("] ").append(context.getRemoteHost()).append(":").append(context.getRemotePort())
            .append(" -> ").append(context.getLocalHost()).append(":").append(context.getLocalPort())
            .append(" - ");
            if (null != group && group.length() > 0) {
                sn.append(group).append("/");
            }
            sn.append(serviceName);
            if (null != version && version.length() > 0) {
                sn.append(":").append(version);
            }
            sn.append(" ");
            sn.append(inv.getMethodName());
            sn.append("(");
            Class<?>[] types = inv.getParameterTypes();
            if (types != null && types.length > 0) {
                boolean first = true;
                for (Class<?> type : types) {
                    if (first) {
                        first = false;
                    } else {
                        sn.append(",");
                    }
                    sn.append(type.getName());
                }
            }
            sn.append(") ");
            Object[] args = inv.getArguments();
            if (args != null && args.length > 0) {
                sn.append(JSON.json(args));
            }
            String msg = sn.toString();
            if (ConfigUtils.isDefault(accesslog)) {
                LoggerFactory.getLogger(ACCESS_LOG_KEY + "." + invoker.getInterface().getName()).info(msg);
            } else {
                log(accesslog, msg);
            }
        }
    } catch (Throwable t) {
        logger.warn("Exception in AcessLogFilter of service(" + invoker + " -> " + inv + ")", t);
    }
    return invoker.invoke(inv);
}
 
Example #21
Source File: AccessLogFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
    try {
        String accesslog = invoker.getUrl().getParameter(Constants.ACCESS_LOG_KEY);
        if (ConfigUtils.isNotEmpty(accesslog)) {
            RpcContext context = RpcContext.getContext();
            String serviceName = invoker.getInterface().getName();
            String version = invoker.getUrl().getParameter(Constants.VERSION_KEY);
            String group = invoker.getUrl().getParameter(Constants.GROUP_KEY);
            StringBuilder sn = new StringBuilder();
            sn.append("[").append(new SimpleDateFormat(MESSAGE_DATE_FORMAT).format(new Date())).append("] ").append(context.getRemoteHost()).append(":").append(context.getRemotePort())
            .append(" -> ").append(context.getLocalHost()).append(":").append(context.getLocalPort())
            .append(" - ");
            if (null != group && group.length() > 0) {
                sn.append(group).append("/");
            }
            sn.append(serviceName);
            if (null != version && version.length() > 0) {
                sn.append(":").append(version);
            }
            sn.append(" ");
            sn.append(inv.getMethodName());
            sn.append("(");
            Class<?>[] types = inv.getParameterTypes();
            if (types != null && types.length > 0) {
                boolean first = true;
                for (Class<?> type : types) {
                    if (first) {
                        first = false;
                    } else {
                        sn.append(",");
                    }
                    sn.append(type.getName());
                }
            }
            sn.append(") ");
            Object[] args = inv.getArguments();
            if (args != null && args.length > 0) {
                sn.append(JSON.json(args));
            }
            String msg = sn.toString();
            if (ConfigUtils.isDefault(accesslog)) {
                LoggerFactory.getLogger(ACCESS_LOG_KEY + "." + invoker.getInterface().getName()).info(msg);
            } else {
                log(accesslog, msg);
            }
        }
    } catch (Throwable t) {
        logger.warn("Exception in AcessLogFilter of service(" + invoker + " -> " + inv + ")", t);
    }
    return invoker.invoke(inv);
}
 
Example #22
Source File: JsonObjectOutput.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public void writeObject(Object obj) throws IOException {
    JSON.json(obj, writer, writeClass);
    writer.println();
    writer.flush();
}
 
Example #23
Source File: JsonObjectOutput.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public void writeObject(Object obj) throws IOException {
    JSON.json(obj, writer, writeClass);
    writer.println();
    writer.flush();
}
 
Example #24
Source File: AccessLogFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
    try {
        String accesslog = invoker.getUrl().getParameter(Constants.ACCESS_LOG_KEY);
        if (ConfigUtils.isNotEmpty(accesslog)) {
            RpcContext context = RpcContext.getContext();
            String serviceName = invoker.getInterface().getName();
            String version = invoker.getUrl().getParameter(Constants.VERSION_KEY);
            String group = invoker.getUrl().getParameter(Constants.GROUP_KEY);
            StringBuilder sn = new StringBuilder();
            sn.append("[").append(new SimpleDateFormat(MESSAGE_DATE_FORMAT).format(new Date())).append("] ").append(context.getRemoteHost()).append(":").append(context.getRemotePort())
            .append(" -> ").append(context.getLocalHost()).append(":").append(context.getLocalPort())
            .append(" - ");
            if (null != group && group.length() > 0) {
                sn.append(group).append("/");
            }
            sn.append(serviceName);
            if (null != version && version.length() > 0) {
                sn.append(":").append(version);
            }
            sn.append(" ");
            sn.append(inv.getMethodName());
            sn.append("(");
            Class<?>[] types = inv.getParameterTypes();
            if (types != null && types.length > 0) {
                boolean first = true;
                for (Class<?> type : types) {
                    if (first) {
                        first = false;
                    } else {
                        sn.append(",");
                    }
                    sn.append(type.getName());
                }
            }
            sn.append(") ");
            Object[] args = inv.getArguments();
            if (args != null && args.length > 0) {
                sn.append(JSON.json(args));
            }
            String msg = sn.toString();
            if (ConfigUtils.isDefault(accesslog)) {
                LoggerFactory.getLogger(ACCESS_LOG_KEY + "." + invoker.getInterface().getName()).info(msg);
            } else {
                log(accesslog, msg);
            }
        }
    } catch (Throwable t) {
        logger.warn("Exception in AcessLogFilter of service(" + invoker + " -> " + inv + ")", t);
    }
    return invoker.invoke(inv);
}
 
Example #25
Source File: AccessLogFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
    try {
        String accesslog = invoker.getUrl().getParameter(Constants.ACCESS_LOG_KEY);
        if (ConfigUtils.isNotEmpty(accesslog)) {
            RpcContext context = RpcContext.getContext();
            String serviceName = invoker.getInterface().getName();
            String version = invoker.getUrl().getParameter(Constants.VERSION_KEY);
            String group = invoker.getUrl().getParameter(Constants.GROUP_KEY);
            StringBuilder sn = new StringBuilder();
            sn.append("[").append(new SimpleDateFormat(MESSAGE_DATE_FORMAT).format(new Date())).append("] ").append(context.getRemoteHost()).append(":").append(context.getRemotePort())
            .append(" -> ").append(context.getLocalHost()).append(":").append(context.getLocalPort())
            .append(" - ");
            if (null != group && group.length() > 0) {
                sn.append(group).append("/");
            }
            sn.append(serviceName);
            if (null != version && version.length() > 0) {
                sn.append(":").append(version);
            }
            sn.append(" ");
            sn.append(inv.getMethodName());
            sn.append("(");
            Class<?>[] types = inv.getParameterTypes();
            if (types != null && types.length > 0) {
                boolean first = true;
                for (Class<?> type : types) {
                    if (first) {
                        first = false;
                    } else {
                        sn.append(",");
                    }
                    sn.append(type.getName());
                }
            }
            sn.append(") ");
            Object[] args = inv.getArguments();
            if (args != null && args.length > 0) {
                sn.append(JSON.json(args));
            }
            String msg = sn.toString();
            if (ConfigUtils.isDefault(accesslog)) {
                LoggerFactory.getLogger(ACCESS_LOG_KEY + "." + invoker.getInterface().getName()).info(msg);
            } else {
                log(accesslog, msg);
            }
        }
    } catch (Throwable t) {
        logger.warn("Exception in AcessLogFilter of service(" + invoker + " -> " + inv + ")", t);
    }
    return invoker.invoke(inv);
}
 
Example #26
Source File: JsonObjectOutput.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public void writeObject(Object obj) throws IOException {
    JSON.json(obj, writer, writeClass);
    writer.println();
    writer.flush();
}