Skip to content

Commit

Permalink
chore: use ObjectMapper singleton object
Browse files Browse the repository at this point in the history
Signed-off-by: xcaspar <x.caspar@gmail.com>
  • Loading branch information
xcaspar authored and tiny-x committed Sep 21, 2020
1 parent 638a67c commit 3d36397
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import com.alibaba.chaosblade.exec.common.model.action.ActionSpec;
import com.alibaba.chaosblade.exec.common.model.action.returnv.UnsupportedReturnTypeException;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.StringUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -62,11 +62,10 @@ public static void inject(EnhancerModel enhancerModel) throws InterruptProcessEx
try {
boolean pass = limitAndIncrease(statusMetric);
if (!pass) {
LOGGER.info("Limited by: {}",
new ObjectMapper().writer().writeValueAsString(model));
LOGGER.info("Limited by: {}", JsonUtil.writer().writeValueAsString(model));
break;
}
LOGGER.info("Match rule: {}", new ObjectMapper().writer().writeValueAsString(model));
LOGGER.info("Match rule: {}", JsonUtil.writer().writeValueAsString(model));
enhancerModel.merge(model);
ModelSpec modelSpec = ManagerFactory.getModelSpecManager().getModelSpec(target);
ActionSpec actionSpec = modelSpec.getActionSpec(model.getActionName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

import com.alibaba.chaosblade.exec.common.model.action.ActionModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;

/**
* @author Changjun Xiao
Expand Down Expand Up @@ -66,7 +65,7 @@ public void setMatcher(MatcherModel matcher) {
@Override
public String toString() {
try {
return new ObjectMapper().writer().writeValueAsString(this);
return JsonUtil.writer().writeValueAsString(this);
} catch (Throwable e) {
}
return "Model{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.alibaba.chaosblade.exec.common.transport;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;

/**
* @author Changjun Xiao
Expand Down Expand Up @@ -136,7 +136,7 @@ public int hashCode() {
@Override
public String toString() {
try {
return new ObjectMapper().writer().writeValueAsString(this);
return JsonUtil.writer().writeValueAsString(this);
} catch (Throwable e) {
}
return "Response{" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.chaosblade.exec.common.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;

/**
* @author Changjun Xiao
*/
public class JsonUtil {
final static ObjectMapper mapper = new ObjectMapper();

public static ObjectReader reader() {
return mapper.reader();
}

public static ObjectWriter writer() {
return mapper.writer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
import com.alibaba.chaosblade.exec.common.model.action.delay.TimeoutExecutor;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;
import com.alibaba.chaosblade.exec.plugin.dubbo.model.DubboThreadPoolFullExecutor;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -101,8 +101,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
matcherModel.add(DubboConstant.TIMEOUT_KEY, timeout + "");

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("dubbo matchers: {}",
new ObjectMapper().writer().writeValueAsString(matcherModel));
LOGGER.debug("dubbo matchers: {}", JsonUtil.writer().writeValueAsString(matcherModel));
}

EnhancerModel enhancerModel = new EnhancerModel(classLoader, matcherModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -43,8 +43,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
MatcherModel matcherModel = new MatcherModel();
matcherModel.add(HttpConstant.URI_KEY, getUrl(methodArguments));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("http matchers: {}",
new ObjectMapper().writer().writeValueAsString(matcherModel));
LOGGER.debug("http matchers: {}", JsonUtil.writer().writeValueAsString(matcherModel));
}
EnhancerModel enhancerModel = new EnhancerModel(classLoader, matcherModel);
postDoBeforeAdvice(enhancerModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;
import com.alibaba.chaosblade.exec.plugin.http.HttpConstant;
import com.alibaba.chaosblade.exec.plugin.http.UrlUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -40,8 +40,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader,
MatcherModel matcherModel = new MatcherModel();
matcherModel.add(HttpConstant.URI_KEY, getUrl(object));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("http matchers: {}",
new ObjectMapper().writer().writeValueAsString(matcherModel));
LOGGER.debug("http matchers: {}", JsonUtil.writer().writeValueAsString(matcherModel));
}
EnhancerModel enhancerModel = new EnhancerModel(classLoader, matcherModel);
enhancerModel.addMatcher(OKHTTP3, "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -76,8 +76,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
matcherModel.add(JedisConstant.KEY_MATCHER_NAME, key);
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("jedis matchers: {}",
new ObjectMapper().writer().writeValueAsString(matcherModel));
LOGGER.debug("jedis matchers: {}", JsonUtil.writer().writeValueAsString(matcherModel));
}
return new EnhancerModel(classLoader, matcherModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;
import com.alibaba.chaosblade.exec.common.util.SQLParserUtil;
import com.alibaba.chaosblade.exec.common.util.SQLParserUtil.SqlType;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -87,8 +87,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
matcherModel.add(MysqlConstant.PORT_MATCHER_NAME, port.toString());
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("mysql matchers: {}",
new ObjectMapper().writer().writeValueAsString(matcherModel));
LOGGER.debug("mysql matchers: {}", JsonUtil.writer().writeValueAsString(matcherModel));
}
return new EnhancerModel(classLoader, matcherModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;
import com.alibaba.chaosblade.exec.common.util.SQLParserUtil;
import com.alibaba.chaosblade.exec.common.util.SQLParserUtil.SqlType;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -76,8 +76,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
matcherModel.add(PostgrelsqlConstant.PORT_MATCHER_NAME, port.toString());
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("postgrelsql matchers: {}",
new ObjectMapper().writer().writeValueAsString(matcherModel));
LOGGER.debug("postgrelsql matchers: {}", JsonUtil.writer().writeValueAsString(matcherModel));
}
return new EnhancerModel(classLoader, matcherModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -75,8 +75,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
matcherModel.add(FLAG_CONSUMER_GROUP, consumerGroup);
matcherModel.add(FLAG_PRODUCER_GROUP, producerGroup);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("rocketmq matchers: {}",
new ObjectMapper().writer().writeValueAsString(matcherModel));
LOGGER.debug("rocketmq matchers: {}", JsonUtil.writer().writeValueAsString(matcherModel));
}
return new EnhancerModel(classLoader, matcherModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;
import com.alibaba.chaosblade.exec.common.util.StringUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -51,13 +50,10 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
MatcherModel matcherModel = new MatcherModel();
matcherModel.add(ServletConstant.METHOD_KEY, requestMethod);
matcherModel.add(ServletConstant.REQUEST_PATH_KEY, requestURI);
ObjectWriter objectWriter = new ObjectMapper().writer();
LOOGER.debug("servlet matchers: {}",
objectWriter.writeValueAsString(matcherModel));
LOOGER.debug("servlet matchers: {}", JsonUtil.writer().writeValueAsString(matcherModel));

Map<String, Object> queryString = getQueryString(requestMethod, request);
LOOGER.debug("origin params: {}",
objectWriter.writeValueAsString(queryString));
LOOGER.debug("origin params: {}", JsonUtil.writer().writeValueAsString(queryString));

EnhancerModel enhancerModel = new EnhancerModel(classLoader, matcherModel);
enhancerModel.addCustomMatcher(ServletConstant.QUERY_STRING_KEY, queryString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import com.alibaba.chaosblade.exec.common.model.action.delay.BaseTimeoutExecutor;
import com.alibaba.chaosblade.exec.common.model.action.delay.TimeoutExecutor;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;
import com.alibaba.chaosblade.exec.plugin.tars.TarsConstant;
import com.alibaba.chaosblade.exec.plugin.tars.TarsEnhancer;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -56,8 +56,7 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
matcherModel.add(TarsConstant.CLIENT, "true");

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("tars matchers: {}",
new ObjectMapper().writer().writeValueAsString(matcherModel));
LOGGER.debug("tars matchers: {}", JsonUtil.writer().writeValueAsString(matcherModel));
}

EnhancerModel enhancerModel = new EnhancerModel(classLoader, matcherModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

import com.alibaba.chaosblade.exec.common.transport.Request;
import com.alibaba.chaosblade.exec.common.transport.Response;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.StringUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -51,7 +51,7 @@ public Response dispatch(String command, Request request) {
return Response.ofFailure(Response.Code.ILLEGAL_PARAMETER, "less request command");
}
try {
String requestJson = new ObjectMapper().writer().writeValueAsString(request);
String requestJson = JsonUtil.writer().writeValueAsString(request);
LOGGER.info("command: {}, request: {}", command, requestJson);
} catch (Throwable e) {
LOGGER.warn("marshal request failed, command: {}", command, e);
Expand Down

0 comments on commit 3d36397

Please sign in to comment.