From b48500832b3d6c18ff1a6f9f7ed588df14991a0b Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 26 Apr 2018 02:22:19 +0900 Subject: [PATCH] ing --- .vscode/test-launch.json | 22 +++++++++++++ pom.xml | 18 ++++++++-- .../central/commons/stereotype/ProbeAPI.java | 11 ------- .../central/commons/stereotype/WebappAPI.java | 11 ------- .../service/CentralAuthCrawlerService.java | 10 +++--- .../generator/service/InfraHostGenerator.java | 11 +++---- .../service/InfraHostWMIGenerator.java | 9 +++-- .../service/InfraServiceGenerator.java | 11 +++---- .../service/InfraServiceJMXGenerator.java | 8 ++--- .../service/InfraServiceMysqlGenerator.java | 10 +++--- .../member/service/CentralMemberService.java | 2 +- .../service/CentralNoAuthProbeService.java | 22 ++++++++----- .../central/proxy/ProxyServerInterceptor.java | 10 +++--- .../central/proxy/ServiceInvoker.java | 33 ++++++++++++++----- .../overflow/central/proxy/ServiceProxy.java | 11 +++++-- .../redis/service/RedisMessagePublisher.java | 18 +++++++--- .../overflow/central/spring/AppConfig.java | 22 +++++++++---- .../infra/service/InfraHostServiceTest.java | 5 +-- .../service/InfraMachineServiceTest.java | 5 +-- .../InfraOSApplicationServiceTest.java | 5 +-- .../service/InfraOSDaemonServiceTest.java | 5 +-- .../infra/service/InfraOSPortServiceTest.java | 3 +- .../infra/service/InfraOSServiceTest.java | 5 +-- .../service/InfraServiceServiceTest.java | 3 +- .../central/spring/AppConfigTest.java | 19 +++++------ 25 files changed, 171 insertions(+), 118 deletions(-) create mode 100644 .vscode/test-launch.json delete mode 100644 src/main/java/com/loafle/overflow/central/commons/stereotype/ProbeAPI.java delete mode 100644 src/main/java/com/loafle/overflow/central/commons/stereotype/WebappAPI.java diff --git a/.vscode/test-launch.json b/.vscode/test-launch.json new file mode 100644 index 0000000..d9a72db --- /dev/null +++ b/.vscode/test-launch.json @@ -0,0 +1,22 @@ +{ + "run": [ + { + "name": "central", + "projectName": "central", + "workingDirectory": "/project/loafle/overflow/central", + "args": [], + "vmargs": [], + "preLaunchTask": "" + } + ], + "debug": [ + { + "name": "central", + "projectName": "central", + "workingDirectory": "/project/loafle/overflow/central", + "args": [], + "vmargs": [], + "preLaunchTask": "" + } + ] +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index e86ff0e..1aec8c7 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,8 @@ com.loafle.overflow.central + 1.0.0-SNAPSHOT + 1.2.0 2.9.0 2.5.6 @@ -25,7 +27,8 @@ 1.11.4.RELEASE 1.8.7.RELEASE 4.2.3.RELEASE - 2.8.2 + + 1.9.13 5.2.10.Final 1.4.7 1.6.0 @@ -37,6 +40,12 @@ + + com.loafle.commons + rpc-java + ${commons.rpc-java.version} + + io.grpc grpc-netty @@ -118,10 +127,15 @@ ${spring.crypto.version} - + + + org.codehaus.jackson + jackson-mapper-asl + ${jackson.mapper.version} diff --git a/src/main/java/com/loafle/overflow/central/commons/stereotype/ProbeAPI.java b/src/main/java/com/loafle/overflow/central/commons/stereotype/ProbeAPI.java deleted file mode 100644 index 7e29759..0000000 --- a/src/main/java/com/loafle/overflow/central/commons/stereotype/ProbeAPI.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.loafle.overflow.central.commons.stereotype; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface ProbeAPI { -} diff --git a/src/main/java/com/loafle/overflow/central/commons/stereotype/WebappAPI.java b/src/main/java/com/loafle/overflow/central/commons/stereotype/WebappAPI.java deleted file mode 100644 index feb636b..0000000 --- a/src/main/java/com/loafle/overflow/central/commons/stereotype/WebappAPI.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.loafle.overflow.central.commons.stereotype; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface WebappAPI { -} diff --git a/src/main/java/com/loafle/overflow/central/module/auth/service/CentralAuthCrawlerService.java b/src/main/java/com/loafle/overflow/central/module/auth/service/CentralAuthCrawlerService.java index 3162083..901d6bb 100644 --- a/src/main/java/com/loafle/overflow/central/module/auth/service/CentralAuthCrawlerService.java +++ b/src/main/java/com/loafle/overflow/central/module/auth/service/CentralAuthCrawlerService.java @@ -1,9 +1,7 @@ package com.loafle.overflow.central.module.auth.service; - import com.loafle.overflow.central.module.auth.dao.AuthCrawlerDAO; - import com.loafle.overflow.central.module.infra.service.CentralInfraService; import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.model.auth.AuthCrawler; @@ -26,11 +24,12 @@ public class CentralAuthCrawlerService implements AuthCrawlerService { @Autowired private CentralInfraService infraService; - public AuthCrawler regist(AuthCrawler authCrawler) { + public AuthCrawler regist(AuthCrawler authCrawler) { - AuthCrawler dbAuthCrawler = this.authCrawlerDAO.findByCrawlerAndTarget(authCrawler.getCrawler(), authCrawler.getTarget()); + AuthCrawler dbAuthCrawler = this.authCrawlerDAO.findByCrawlerAndTarget(authCrawler.getCrawler(), + authCrawler.getTarget()); - if(authCrawler == null) { + if (authCrawler == null) { return this.authCrawlerDAO.save(authCrawler); } @@ -41,7 +40,6 @@ public class CentralAuthCrawlerService implements AuthCrawlerService { return this.authCrawlerDAO.save(dbAuthCrawler); } - public boolean checkAuthCrawler(long infraId, MetaCrawler crawler, String authJson) throws OverflowException { Infra infra = this.infraService.read(infraId); diff --git a/src/main/java/com/loafle/overflow/central/module/generator/service/InfraHostGenerator.java b/src/main/java/com/loafle/overflow/central/module/generator/service/InfraHostGenerator.java index b8082b3..1f76d2a 100644 --- a/src/main/java/com/loafle/overflow/central/module/generator/service/InfraHostGenerator.java +++ b/src/main/java/com/loafle/overflow/central/module/generator/service/InfraHostGenerator.java @@ -1,9 +1,6 @@ package com.loafle.overflow.central.module.generator.service; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; import com.loafle.overflow.central.commons.utils.StringConvertor; -import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.type.PortType; import com.loafle.overflow.model.auth.AuthCrawler; import com.loafle.overflow.model.infra.Infra; @@ -19,6 +16,8 @@ import com.loafle.overflow.model.sensorconfig.SensorConfig; import com.loafle.overflow.model.sensorconfig.Target; import com.loafle.overflow.service.central.auth.AuthCrawlerService; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -42,7 +41,7 @@ public class InfraHostGenerator { private AuthCrawlerService authCrawlerService; @Autowired - private Gson gson; + private ObjectMapper objectMapper; public String process(Sensor dbSensor, List sensorItems, Infra infra) throws Exception { @@ -73,7 +72,7 @@ public class InfraHostGenerator { this.infraHostWMIGenerator.process(sensorItems, keyMap, dbSensor, sensorConfig); } - return gson.toJson(sensorConfig); + return objectMapper.writeValueAsString(sensorConfig); } private Target createTarget(InfraHost infraHost, Sensor dbSensor) throws Exception { @@ -88,7 +87,7 @@ public class InfraHostGenerator { Connection connection = new Connection(); connection.setIp(StringConvertor.intToIp(infraHost.getIp())); - HashMap optionMap = this.gson.fromJson(authCrawler.getAuthJson(), TypeToken.getParameterized(HashMap.class, String.class, String.class).getType()); + HashMap optionMap = this.objectMapper.readValue(authCrawler.getAuthJson(), new TypeReference>(){}); if (dbSensor.getCrawler().getId() == MetaCrawlerEnum.WMI_CRAWLER.getValue()) { connection.setPort(135); diff --git a/src/main/java/com/loafle/overflow/central/module/generator/service/InfraHostWMIGenerator.java b/src/main/java/com/loafle/overflow/central/module/generator/service/InfraHostWMIGenerator.java index dd178cb..6d46827 100644 --- a/src/main/java/com/loafle/overflow/central/module/generator/service/InfraHostWMIGenerator.java +++ b/src/main/java/com/loafle/overflow/central/module/generator/service/InfraHostWMIGenerator.java @@ -1,7 +1,5 @@ package com.loafle.overflow.central.module.generator.service; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; import com.loafle.overflow.model.meta.MetaSensorItemKey; import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.SensorItem; @@ -11,10 +9,11 @@ import com.loafle.overflow.model.sensorconfig.MappingInfo; import com.loafle.overflow.model.sensorconfig.QueryInfo; import com.loafle.overflow.model.sensorconfig.SensorConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -30,7 +29,7 @@ public class InfraHostWMIGenerator { private GenerateUtil generateUtil; @Autowired - private Gson gson; + private ObjectMapper objectMapper; public void process(List sensorItems, Map keyMap, Sensor dbSensor, SensorConfig sensorConfig) throws Exception { @@ -87,7 +86,7 @@ public class InfraHostWMIGenerator { String json = tempItemKey.getOption(); if (json != null && json.length() > 0) { - HashMap optionMap = this.gson.fromJson(json, TypeToken.getParameterized(HashMap.class, String.class, String.class).getType()); + HashMap optionMap = this.objectMapper.readValue(json, new TypeReference>(){}); Object obj = null; obj = optionMap.get("appends"); diff --git a/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceGenerator.java b/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceGenerator.java index af97307..4b25ca8 100644 --- a/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceGenerator.java +++ b/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceGenerator.java @@ -1,7 +1,5 @@ package com.loafle.overflow.central.module.generator.service; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; import com.loafle.overflow.central.commons.utils.StringConvertor; import com.loafle.overflow.core.type.PortType; import com.loafle.overflow.model.auth.AuthCrawler; @@ -18,10 +16,11 @@ import com.loafle.overflow.model.sensorconfig.SensorConfig; import com.loafle.overflow.model.sensorconfig.Target; import com.loafle.overflow.service.central.auth.AuthCrawlerService; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -45,7 +44,7 @@ public class InfraServiceGenerator { private AuthCrawlerService authCrawlerService; @Autowired - private Gson gson; + private ObjectMapper objectMapper; public String process(Sensor dbSensor, List sensorItems, Infra infra) throws Exception { com.loafle.overflow.model.infra.InfraService infraService = (com.loafle.overflow.model.infra.InfraService)infra; @@ -78,7 +77,7 @@ public class InfraServiceGenerator { this.infraServiceJMXGenerator.process(sensorItems, keyMap, dbSensor, sensorConfig); } - return gson.toJson(sensorConfig); + return objectMapper.writeValueAsString(sensorConfig); } private Target createTarget(InfraService infraService, Sensor sensor) throws Exception { @@ -98,7 +97,7 @@ public class InfraServiceGenerator { target.setConnection(connection); - HashMap optionMap = this.gson.fromJson(authCrawler.getAuthJson(), TypeToken.getParameterized(HashMap.class, String.class, String.class).getType()); + HashMap optionMap = this.objectMapper.readValue(authCrawler.getAuthJson(), new TypeReference>(){}); Map auth = new HashMap<>(); diff --git a/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceJMXGenerator.java b/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceJMXGenerator.java index 355f947..0a99701 100644 --- a/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceJMXGenerator.java +++ b/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceJMXGenerator.java @@ -1,7 +1,5 @@ package com.loafle.overflow.central.module.generator.service; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; import com.loafle.overflow.model.meta.MetaSensorItemKey; import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.SensorItem; @@ -11,6 +9,8 @@ import com.loafle.overflow.model.sensorconfig.MappingInfo; import com.loafle.overflow.model.sensorconfig.QueryInfo; import com.loafle.overflow.model.sensorconfig.SensorConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -29,7 +29,7 @@ public class InfraServiceJMXGenerator { private GenerateUtil generateUtil; @Autowired - private Gson gson; + private ObjectMapper objectMapper; public void process(List sensorItems, Map keyMap, Sensor dbSensor, SensorConfig sensorConfig) throws Exception { @@ -73,7 +73,7 @@ public class InfraServiceJMXGenerator { List arrayCol = null; if (json != null && json.length() > 0) { - HashMap optionMap = this.gson.fromJson(json, TypeToken.getParameterized(HashMap.class, String.class, String.class).getType()); + HashMap optionMap = this.objectMapper.readValue(json, new TypeReference>(){}); Object obj = null; obj = optionMap.get("aliases"); diff --git a/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceMysqlGenerator.java b/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceMysqlGenerator.java index 6ddd27f..f3c5362 100644 --- a/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceMysqlGenerator.java +++ b/src/main/java/com/loafle/overflow/central/module/generator/service/InfraServiceMysqlGenerator.java @@ -1,8 +1,5 @@ package com.loafle.overflow.central.module.generator.service; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.model.meta.MetaSensorItemKey; import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.SensorItem; @@ -12,10 +9,11 @@ import com.loafle.overflow.model.sensorconfig.MappingInfo; import com.loafle.overflow.model.sensorconfig.QueryInfo; import com.loafle.overflow.model.sensorconfig.SensorConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -30,7 +28,7 @@ public class InfraServiceMysqlGenerator { @Autowired private GenerateUtil generateUtil; @Autowired - private Gson gson; + private ObjectMapper objectMapper; public void process(List sensorItems, Map keyMap, Sensor dbSensor, SensorConfig sensorConfig) throws Exception { @@ -83,7 +81,7 @@ public class InfraServiceMysqlGenerator { String json = tempItemKey.getOption(); if (json != null && json.length() > 0) { - HashMap optionMap = this.gson.fromJson(json, TypeToken.getParameterized(HashMap.class, String.class, String.class).getType()); + HashMap optionMap = this.objectMapper.readValue(json, new TypeReference>(){}); Object obj = null; obj = optionMap.get("valueColumn"); diff --git a/src/main/java/com/loafle/overflow/central/module/member/service/CentralMemberService.java b/src/main/java/com/loafle/overflow/central/module/member/service/CentralMemberService.java index b7fa4bb..682f1e7 100644 --- a/src/main/java/com/loafle/overflow/central/module/member/service/CentralMemberService.java +++ b/src/main/java/com/loafle/overflow/central/module/member/service/CentralMemberService.java @@ -1,6 +1,6 @@ package com.loafle.overflow.central.module.member.service; -import com.loafle.overflow.central.commons.stereotype.WebappAPI; +import com.loafle.overflow.core.annotation.WebappAPI; import com.loafle.overflow.central.commons.utils.EmailSender; import com.loafle.overflow.central.commons.utils.SessionMetadata; import com.loafle.overflow.central.module.email.service.EmailAuthService; diff --git a/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java b/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java index 50693ba..93522ef 100644 --- a/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java +++ b/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java @@ -1,15 +1,11 @@ package com.loafle.overflow.central.module.noauthprobe.service; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; import com.loafle.overflow.central.commons.service.MessagePublisher; -import com.loafle.overflow.central.commons.stereotype.ProbeAPI; -import com.loafle.overflow.central.commons.stereotype.WebappAPI; +import com.loafle.overflow.core.annotation.ProbeAPI; +import com.loafle.overflow.core.annotation.WebappAPI; import com.loafle.overflow.central.commons.utils.GenerateKey; import com.loafle.overflow.central.commons.utils.SessionMetadata; import com.loafle.overflow.central.commons.utils.StringConvertor; -import com.loafle.overflow.central.module.infra.service.CentralInfraMachineService; -import com.loafle.overflow.central.module.infra.service.CentralInfraOSService; import com.loafle.overflow.central.module.noauthprobe.dao.NoAuthProbeDAO; import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.model.apikey.ApiKey; @@ -35,10 +31,13 @@ import com.loafle.overflow.service.central.noauthprobe.NoAuthProbeService; import com.loafle.overflow.service.central.probe.ProbeHostService; import com.loafle.overflow.service.central.probe.ProbeService; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; +import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -68,7 +67,7 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { private DomainMemberService domainMemberService; @Autowired - private Gson gson; + private ObjectMapper objectMapper; @Autowired private InfraMachineService infraMachineService; @@ -106,8 +105,13 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { @Transactional public List acceptNoAuthProbe(NoAuthProbe noAuthProbe) throws OverflowException { - HashMap objMap = this.gson.fromJson(noAuthProbe.getDescription(), - TypeToken.getParameterized(HashMap.class, String.class, Object.class).getType()); + HashMap objMap; + try { + objMap = this.objectMapper.readValue(noAuthProbe.getDescription(), new TypeReference>(){}); + } catch (IOException e) { + throw new OverflowException("json error", e); + } + Map hostMap = (Map) objMap.get("host"); Map netMap = (Map) objMap.get("network"); diff --git a/src/main/java/com/loafle/overflow/central/proxy/ProxyServerInterceptor.java b/src/main/java/com/loafle/overflow/central/proxy/ProxyServerInterceptor.java index 4fd50a0..e6e486e 100644 --- a/src/main/java/com/loafle/overflow/central/proxy/ProxyServerInterceptor.java +++ b/src/main/java/com/loafle/overflow/central/proxy/ProxyServerInterceptor.java @@ -7,16 +7,14 @@ import io.grpc.*; public class ProxyServerInterceptor implements ServerInterceptor { @Override - public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, ServerCallHandler next) { + public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, + ServerCallHandler next) { String clientType = headers.get(SessionMetadata.METADATA_CLIENT_TYPE_KEY); String sessionID = headers.get(SessionMetadata.METADATA_SESSION_ID_KEY); String targetID = headers.get(SessionMetadata.METADATA_TARGET_ID_KEY); - Context ctx = Context.current().withValues( - SessionMetadata.CTX_CLIENT_TYPE_KEY, clientType, - SessionMetadata.CTX_SESSION_ID_KEY, sessionID, - SessionMetadata.CTX_TARGET_ID_KEY, targetID - ); + Context ctx = Context.current().withValues(SessionMetadata.CTX_CLIENT_TYPE_KEY, clientType, + SessionMetadata.CTX_SESSION_ID_KEY, sessionID, SessionMetadata.CTX_TARGET_ID_KEY, targetID); return Contexts.interceptCall(ctx, call, headers, next); } diff --git a/src/main/java/com/loafle/overflow/central/proxy/ServiceInvoker.java b/src/main/java/com/loafle/overflow/central/proxy/ServiceInvoker.java index 1fe80f8..71e9e9c 100644 --- a/src/main/java/com/loafle/overflow/central/proxy/ServiceInvoker.java +++ b/src/main/java/com/loafle/overflow/central/proxy/ServiceInvoker.java @@ -1,15 +1,17 @@ package com.loafle.overflow.central.proxy; -import com.google.gson.Gson; +import com.google.gson.internal.Primitives; import com.google.protobuf.ByteString; import com.loafle.overflow.core.exception.OverflowException; -import org.springframework.aop.support.AopUtils; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.JavaType; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; +import java.io.IOException; import java.lang.reflect.*; import java.util.HashMap; import java.util.List; @@ -20,16 +22,15 @@ import java.util.Map; */ @Component public class ServiceInvoker { - // @Autowired + @Autowired private ApplicationContext context; private Map serviceCacheMap; @Autowired - private Gson gson; + private ObjectMapper objectMapper; - public ServiceInvoker(ApplicationContext context) { - this.context = context; + public ServiceInvoker() { serviceCacheMap = new HashMap<>(); } @@ -75,7 +76,8 @@ public class ServiceInvoker { } private Method getMethod(String methodName) { - Class clazz = AopUtils.getTargetClass(serviceCache.bean); + // Class clazz = AopUtils.getTargetClass(serviceCache.bean); + Class clazz = serviceCache.bean.getClass(); Method[] methods = clazz.getMethods(); Method targetMethod = null; @@ -117,7 +119,16 @@ public class ServiceInvoker { } private Object getValue(Type parameterType, String json) throws IllegalArgumentException { - return this.gson.fromJson(json, parameterType); + JavaType targetType = objectMapper.getTypeFactory().constructType(parameterType); + if (!Primitives.isPrimitive(parameterType) && !parameterType.getTypeName().equals(String.class.getName())) { + try { + return objectMapper.readValue(json, targetType); + } catch (IOException e) { + throw new IllegalArgumentException(); + } + } + return objectMapper.convertValue(json, targetType); + } private Object[] getParameters(Type[] parameterTypes, List params) throws IllegalArgumentException { @@ -174,7 +185,11 @@ public class ServiceInvoker { throw new OverflowException("internal error", e); } - jsonInString = this.gson.toJson(result); + try { + jsonInString = objectMapper.writeValueAsString(result); + } catch (IOException e) { + throw new OverflowException("InternalError"); + } return jsonInString; } diff --git a/src/main/java/com/loafle/overflow/central/proxy/ServiceProxy.java b/src/main/java/com/loafle/overflow/central/proxy/ServiceProxy.java index 0e3a2d2..b58db4e 100644 --- a/src/main/java/com/loafle/overflow/central/proxy/ServiceProxy.java +++ b/src/main/java/com/loafle/overflow/central/proxy/ServiceProxy.java @@ -3,14 +3,17 @@ package com.loafle.overflow.central.proxy; import com.loafle.overflow.central.api.CentralAPIGrpc; import com.loafle.overflow.central.api.ServerInput; import com.loafle.overflow.central.api.ServerOutput; +import com.loafle.overflow.central.spring.AppConfig; import com.loafle.overflow.core.exception.OverflowException; import io.grpc.*; import io.grpc.Status.Code; import io.grpc.netty.NettyServerBuilder; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.stereotype.Component; import java.io.IOException; import java.util.logging.Logger; @@ -25,12 +28,13 @@ public class ServiceProxy { private ApplicationContext ctx; public void start(int port) throws IOException { - ctx = new AnnotationConfigApplicationContext("com.loafle.overflow"); + ctx = new AnnotationConfigApplicationContext(AppConfig.class); ProxyServerInterceptor proxyServerInterceptor = new ProxyServerInterceptor(); + ServiceImpl serviceImpl = ctx.getBean(ServiceImpl.class); server = NettyServerBuilder.forPort(port) - .addService(ServerInterceptors.intercept(new ServiceImpl(new ServiceInvoker(ctx)), proxyServerInterceptor)) + .addService(ServerInterceptors.intercept(serviceImpl, proxyServerInterceptor)) .build().start(); logger.info("Server started, listening on " + port); Runtime.getRuntime().addShutdownHook(new Thread() { @@ -56,8 +60,10 @@ public class ServiceProxy { } } + @Component static class ServiceImpl extends CentralAPIGrpc.CentralAPIImplBase { + @Autowired private ServiceInvoker serviceInvoker; ServiceImpl(ServiceInvoker serviceInvoker) { @@ -99,3 +105,4 @@ public class ServiceProxy { } } + diff --git a/src/main/java/com/loafle/overflow/central/redis/service/RedisMessagePublisher.java b/src/main/java/com/loafle/overflow/central/redis/service/RedisMessagePublisher.java index 3b2a2fb..3534233 100644 --- a/src/main/java/com/loafle/overflow/central/redis/service/RedisMessagePublisher.java +++ b/src/main/java/com/loafle/overflow/central/redis/service/RedisMessagePublisher.java @@ -7,6 +7,7 @@ import com.loafle.overflow.core.model.PublishMessage; import com.loafle.overflow.model.member.Member; import com.loafle.overflow.service.central.member.MemberService; +import org.codehaus.jackson.map.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.redis.core.RedisTemplate; @@ -27,7 +28,7 @@ public class RedisMessagePublisher implements MessagePublisher { @Autowired private RedisTemplate redisTemplate; @Autowired - private Gson gson; + private ObjectMapper objectMapper; @Autowired private MemberService memberService; @@ -113,8 +114,12 @@ public class RedisMessagePublisher implements MessagePublisher { ChannelTopic topic = this.topics.get(channel); message.setMessage(new PublishMessage.PublishMessageBody(method, this.getMessageBody(params))); - String json = this.gson.toJson(message); - redisTemplate.convertAndSend(topic.getTopic(), json); + try { + String json = this.objectMapper.writeValueAsString(message); + redisTemplate.convertAndSend(topic.getTopic(), json); + } catch (IOException e) { + e.printStackTrace(); + } } protected List getMessageBody(final Object... params) { @@ -123,7 +128,12 @@ public class RedisMessagePublisher implements MessagePublisher { if (param.getClass().equals(String.class)) { results.add((String) param); } else { - results.add(this.gson.toJson(param)); + try { + String json = this.objectMapper.writeValueAsString(param); + results.add(json); + } catch (IOException e) { + e.printStackTrace(); + } } } return results; diff --git a/src/main/java/com/loafle/overflow/central/spring/AppConfig.java b/src/main/java/com/loafle/overflow/central/spring/AppConfig.java index 1459a96..b71a508 100644 --- a/src/main/java/com/loafle/overflow/central/spring/AppConfig.java +++ b/src/main/java/com/loafle/overflow/central/spring/AppConfig.java @@ -1,7 +1,7 @@ package com.loafle.overflow.central.spring; -import com.google.gson.Gson; - +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; import org.springframework.context.annotation.*; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @@ -29,14 +29,22 @@ public class AppConfig { // return ppc; // } @Bean - public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } + // @Bean + // public Gson gson() { + // GsonBuilder builder = new GsonBuilder(); + // builder.excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.PRIVATE); + // // builder.excludeFieldsWithoutExposeAnnotation(); + // return builder.create(); + // } @Bean - public Gson gson() { - Gson gson = new Gson(); - - return gson; + public ObjectMapper getObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return objectMapper; } + } diff --git a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraHostServiceTest.java b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraHostServiceTest.java index 0a07a0b..2ac8b23 100644 --- a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraHostServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraHostServiceTest.java @@ -8,6 +8,7 @@ import com.loafle.overflow.model.infra.InfraOS; import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.service.central.infra.InfraHostService; +import org.codehaus.jackson.map.ObjectMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -27,7 +28,7 @@ public class InfraHostServiceTest { private InfraHostService infraHostService; @Autowired - private Gson gson; + private ObjectMapper objectMapper; @Test // @Ignore @@ -58,7 +59,7 @@ public class InfraHostServiceTest { InfraHost infraHost = this.infraHostService.read(1); - String json = gson.toJson(infraHost); + String json = objectMapper.writeValueAsString(infraHost); System.out.println(json); diff --git a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraMachineServiceTest.java b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraMachineServiceTest.java index 95ce3ab..a2c2d91 100644 --- a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraMachineServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraMachineServiceTest.java @@ -6,6 +6,7 @@ import com.loafle.overflow.model.infra.InfraMachine; import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.service.central.infra.InfraMachineService; +import org.codehaus.jackson.map.ObjectMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -25,7 +26,7 @@ public class InfraMachineServiceTest { private InfraMachineService infraMachineService; @Autowired - private Gson gson; + private ObjectMapper objectMapper; // @Ignore @Test @@ -52,7 +53,7 @@ public class InfraMachineServiceTest { InfraMachine infraMachine = this.infraMachineService.read(1); - String json = gson.toJson(infraMachine); + String json = objectMapper.writeValueAsString(infraMachine); System.out.println(json); } diff --git a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSApplicationServiceTest.java b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSApplicationServiceTest.java index 4d48a81..d6826be 100644 --- a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSApplicationServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSApplicationServiceTest.java @@ -7,6 +7,7 @@ import com.loafle.overflow.model.infra.InfraOSApplication; import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.service.central.infra.InfraOSApplicationService; +import org.codehaus.jackson.map.ObjectMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -25,7 +26,7 @@ public class InfraOSApplicationServiceTest { private InfraOSApplicationService infraOSApplicationService; @Autowired - private Gson gson; + private ObjectMapper objectMapper; // @Ignore @Test @@ -56,7 +57,7 @@ public class InfraOSApplicationServiceTest { Assert.assertNotNull(infraOSApplication); - String json = gson.toJson(infraOSApplication); + String json = objectMapper.writeValueAsString(infraOSApplication); System.out.println(json); } diff --git a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSDaemonServiceTest.java b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSDaemonServiceTest.java index 6fd0036..93a8a79 100644 --- a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSDaemonServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSDaemonServiceTest.java @@ -7,6 +7,7 @@ import com.loafle.overflow.model.infra.InfraOSDaemon; import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.service.central.infra.InfraOSDaemonService; +import org.codehaus.jackson.map.ObjectMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -25,7 +26,7 @@ public class InfraOSDaemonServiceTest { private InfraOSDaemonService infraOSDaemonService; @Autowired - private Gson gson; + private ObjectMapper objectMapper; // @Ignore @Test @@ -55,7 +56,7 @@ public class InfraOSDaemonServiceTest { InfraOSDaemon infraOSDaemon = this.infraOSDaemonService.read(1); Assert.assertNotNull(infraOSDaemon); - String json = gson.toJson(infraOSDaemon); + String json = objectMapper.writeValueAsString(infraOSDaemon); System.out.println(json); } diff --git a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSPortServiceTest.java b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSPortServiceTest.java index 0b2c02e..cfefd2e 100644 --- a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSPortServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSPortServiceTest.java @@ -8,6 +8,7 @@ import com.loafle.overflow.model.infra.InfraOSPort; import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.service.central.infra.InfraOSPortService; +import org.codehaus.jackson.map.ObjectMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -25,7 +26,7 @@ public class InfraOSPortServiceTest { @Autowired private InfraOSPortService infraOSPortService; @Autowired - private Gson gson; + private ObjectMapper objectMapper; // @Ignore @Test diff --git a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSServiceTest.java b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSServiceTest.java index 1360f15..70adbbc 100644 --- a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraOSServiceTest.java @@ -8,6 +8,7 @@ import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.model.meta.MetaInfraVendor; import com.loafle.overflow.service.central.infra.InfraOSService; +import org.codehaus.jackson.map.ObjectMapper; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -24,7 +25,7 @@ public class InfraOSServiceTest { @Autowired private InfraOSService infraOSService; @Autowired - private Gson gson; + private ObjectMapper objectMapper; @Test // @Ignore @@ -55,7 +56,7 @@ public class InfraOSServiceTest { public void read() throws Exception { InfraOS infraOS = this.infraOSService.read(1); - String json = gson.toJson(infraOS); + String json = objectMapper.writeValueAsString(infraOS); System.out.println(json); } diff --git a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraServiceServiceTest.java b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraServiceServiceTest.java index d0570f3..b91d6a2 100644 --- a/src/test/java/com/loafle/overflow/central/module/infra/service/InfraServiceServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/infra/service/InfraServiceServiceTest.java @@ -8,6 +8,7 @@ import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.model.meta.MetaInfraVendor; import com.loafle.overflow.service.central.infra.InfraServiceService; +import org.codehaus.jackson.map.ObjectMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -25,6 +26,6 @@ public class InfraServiceServiceTest { @Autowired private InfraServiceService infraServiceService; @Autowired - private Gson gson; + private ObjectMapper objectMapper; } \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/central/spring/AppConfigTest.java b/src/test/java/com/loafle/overflow/central/spring/AppConfigTest.java index 8159c07..b0a8eb4 100644 --- a/src/test/java/com/loafle/overflow/central/spring/AppConfigTest.java +++ b/src/test/java/com/loafle/overflow/central/spring/AppConfigTest.java @@ -1,17 +1,13 @@ package com.loafle.overflow.central.spring; -import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; import org.springframework.context.annotation.*; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.TestPropertySource; import static org.junit.Assert.*; -import com.google.gson.Gson; - /** * Created by geek on 17. 8. 8. */ @@ -51,9 +47,10 @@ public class AppConfigTest { } @Bean - public Gson gson() { - Gson gson = new Gson(); - - return gson; + public ObjectMapper getObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return objectMapper; } -} \ No newline at end of file +} +