This commit is contained in:
crusader 2018-04-26 02:22:19 +09:00
parent b8971008be
commit b48500832b
25 changed files with 171 additions and 118 deletions

22
.vscode/test-launch.json vendored Normal file
View File

@ -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": ""
}
]
}

18
pom.xml
View File

@ -17,6 +17,8 @@
<name>com.loafle.overflow.central</name> <name>com.loafle.overflow.central</name>
<properties> <properties>
<commons.rpc-java.version>1.0.0-SNAPSHOT</commons.rpc-java.version>
<grpc.version>1.2.0</grpc.version> <grpc.version>1.2.0</grpc.version>
<jedis.version>2.9.0</jedis.version> <jedis.version>2.9.0</jedis.version>
<caffeine.version>2.5.6</caffeine.version> <caffeine.version>2.5.6</caffeine.version>
@ -25,7 +27,8 @@
<spring.data.jpa.version>1.11.4.RELEASE</spring.data.jpa.version> <spring.data.jpa.version>1.11.4.RELEASE</spring.data.jpa.version>
<spring.data.redis.version>1.8.7.RELEASE</spring.data.redis.version> <spring.data.redis.version>1.8.7.RELEASE</spring.data.redis.version>
<spring.crypto.version>4.2.3.RELEASE</spring.crypto.version> <spring.crypto.version>4.2.3.RELEASE</spring.crypto.version>
<gson.version>2.8.2</gson.version> <!-- <gson.version>2.8.2</gson.version> -->
<jackson.mapper.version>1.9.13</jackson.mapper.version>
<hibernate.version>5.2.10.Final</hibernate.version> <hibernate.version>5.2.10.Final</hibernate.version>
<javax.mail.version>1.4.7</javax.mail.version> <javax.mail.version>1.4.7</javax.mail.version>
<javax.mail-api.version>1.6.0</javax.mail-api.version> <javax.mail-api.version>1.6.0</javax.mail-api.version>
@ -37,6 +40,12 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>com.loafle.commons</groupId>
<artifactId>rpc-java</artifactId>
<version>${commons.rpc-java.version}</version>
</dependency>
<dependency> <dependency>
<groupId>io.grpc</groupId> <groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId> <artifactId>grpc-netty</artifactId>
@ -118,10 +127,15 @@
<version>${spring.crypto.version}</version> <version>${spring.crypto.version}</version>
</dependency> </dependency>
<dependency> <!-- <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>${gson.version}</version> <version>${gson.version}</version>
</dependency> -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.mapper.version}</version>
</dependency> </dependency>
<!-- Mail --> <!-- Mail -->

View File

@ -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 {
}

View File

@ -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 {
}

View File

@ -1,9 +1,7 @@
package com.loafle.overflow.central.module.auth.service; package com.loafle.overflow.central.module.auth.service;
import com.loafle.overflow.central.module.auth.dao.AuthCrawlerDAO; import com.loafle.overflow.central.module.auth.dao.AuthCrawlerDAO;
import com.loafle.overflow.central.module.infra.service.CentralInfraService; import com.loafle.overflow.central.module.infra.service.CentralInfraService;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.model.auth.AuthCrawler; import com.loafle.overflow.model.auth.AuthCrawler;
@ -26,11 +24,12 @@ public class CentralAuthCrawlerService implements AuthCrawlerService {
@Autowired @Autowired
private CentralInfraService infraService; 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); return this.authCrawlerDAO.save(authCrawler);
} }
@ -41,7 +40,6 @@ public class CentralAuthCrawlerService implements AuthCrawlerService {
return this.authCrawlerDAO.save(dbAuthCrawler); return this.authCrawlerDAO.save(dbAuthCrawler);
} }
public boolean checkAuthCrawler(long infraId, MetaCrawler crawler, String authJson) throws OverflowException { public boolean checkAuthCrawler(long infraId, MetaCrawler crawler, String authJson) throws OverflowException {
Infra infra = this.infraService.read(infraId); Infra infra = this.infraService.read(infraId);

View File

@ -1,9 +1,6 @@
package com.loafle.overflow.central.module.generator.service; 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.central.commons.utils.StringConvertor;
import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.core.type.PortType; import com.loafle.overflow.core.type.PortType;
import com.loafle.overflow.model.auth.AuthCrawler; import com.loafle.overflow.model.auth.AuthCrawler;
import com.loafle.overflow.model.infra.Infra; 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.model.sensorconfig.Target;
import com.loafle.overflow.service.central.auth.AuthCrawlerService; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -42,7 +41,7 @@ public class InfraHostGenerator {
private AuthCrawlerService authCrawlerService; private AuthCrawlerService authCrawlerService;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
public String process(Sensor dbSensor, List<SensorItem> sensorItems, Infra infra) throws Exception { public String process(Sensor dbSensor, List<SensorItem> sensorItems, Infra infra) throws Exception {
@ -73,7 +72,7 @@ public class InfraHostGenerator {
this.infraHostWMIGenerator.process(sensorItems, keyMap, dbSensor, sensorConfig); this.infraHostWMIGenerator.process(sensorItems, keyMap, dbSensor, sensorConfig);
} }
return gson.toJson(sensorConfig); return objectMapper.writeValueAsString(sensorConfig);
} }
private Target createTarget(InfraHost infraHost, Sensor dbSensor) throws Exception { private Target createTarget(InfraHost infraHost, Sensor dbSensor) throws Exception {
@ -88,7 +87,7 @@ public class InfraHostGenerator {
Connection connection = new Connection(); Connection connection = new Connection();
connection.setIp(StringConvertor.intToIp(infraHost.getIp())); connection.setIp(StringConvertor.intToIp(infraHost.getIp()));
HashMap<String, String> optionMap = this.gson.fromJson(authCrawler.getAuthJson(), TypeToken.getParameterized(HashMap.class, String.class, String.class).getType()); HashMap<String, String> optionMap = this.objectMapper.readValue(authCrawler.getAuthJson(), new TypeReference<Map<String, String>>(){});
if (dbSensor.getCrawler().getId() == MetaCrawlerEnum.WMI_CRAWLER.getValue()) { if (dbSensor.getCrawler().getId() == MetaCrawlerEnum.WMI_CRAWLER.getValue()) {
connection.setPort(135); connection.setPort(135);

View File

@ -1,7 +1,5 @@
package com.loafle.overflow.central.module.generator.service; 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.meta.MetaSensorItemKey;
import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem; 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.QueryInfo;
import com.loafle.overflow.model.sensorconfig.SensorConfig; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -30,7 +29,7 @@ public class InfraHostWMIGenerator {
private GenerateUtil generateUtil; private GenerateUtil generateUtil;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor, public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor,
SensorConfig sensorConfig) throws Exception { SensorConfig sensorConfig) throws Exception {
@ -87,7 +86,7 @@ public class InfraHostWMIGenerator {
String json = tempItemKey.getOption(); String json = tempItemKey.getOption();
if (json != null && json.length() > 0) { if (json != null && json.length() > 0) {
HashMap<String, String> optionMap = this.gson.fromJson(json, TypeToken.getParameterized(HashMap.class, String.class, String.class).getType()); HashMap<String, String> optionMap = this.objectMapper.readValue(json, new TypeReference<Map<String, String>>(){});
Object obj = null; Object obj = null;
obj = optionMap.get("appends"); obj = optionMap.get("appends");

View File

@ -1,7 +1,5 @@
package com.loafle.overflow.central.module.generator.service; 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.central.commons.utils.StringConvertor;
import com.loafle.overflow.core.type.PortType; import com.loafle.overflow.core.type.PortType;
import com.loafle.overflow.model.auth.AuthCrawler; 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.model.sensorconfig.Target;
import com.loafle.overflow.service.central.auth.AuthCrawlerService; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -45,7 +44,7 @@ public class InfraServiceGenerator {
private AuthCrawlerService authCrawlerService; private AuthCrawlerService authCrawlerService;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
public String process(Sensor dbSensor, List<SensorItem> sensorItems, Infra infra) throws Exception { public String process(Sensor dbSensor, List<SensorItem> sensorItems, Infra infra) throws Exception {
com.loafle.overflow.model.infra.InfraService infraService = (com.loafle.overflow.model.infra.InfraService)infra; 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); this.infraServiceJMXGenerator.process(sensorItems, keyMap, dbSensor, sensorConfig);
} }
return gson.toJson(sensorConfig); return objectMapper.writeValueAsString(sensorConfig);
} }
private Target createTarget(InfraService infraService, Sensor sensor) throws Exception { private Target createTarget(InfraService infraService, Sensor sensor) throws Exception {
@ -98,7 +97,7 @@ public class InfraServiceGenerator {
target.setConnection(connection); target.setConnection(connection);
HashMap<String, String> optionMap = this.gson.fromJson(authCrawler.getAuthJson(), TypeToken.getParameterized(HashMap.class, String.class, String.class).getType()); HashMap<String, String> optionMap = this.objectMapper.readValue(authCrawler.getAuthJson(), new TypeReference<Map<String, String>>(){});
Map<String, Object> auth = new HashMap<>(); Map<String, Object> auth = new HashMap<>();

View File

@ -1,7 +1,5 @@
package com.loafle.overflow.central.module.generator.service; 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.meta.MetaSensorItemKey;
import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem; 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.QueryInfo;
import com.loafle.overflow.model.sensorconfig.SensorConfig; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -29,7 +29,7 @@ public class InfraServiceJMXGenerator {
private GenerateUtil generateUtil; private GenerateUtil generateUtil;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor, public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor,
SensorConfig sensorConfig) throws Exception { SensorConfig sensorConfig) throws Exception {
@ -73,7 +73,7 @@ public class InfraServiceJMXGenerator {
List<String> arrayCol = null; List<String> arrayCol = null;
if (json != null && json.length() > 0) { if (json != null && json.length() > 0) {
HashMap<String, String> optionMap = this.gson.fromJson(json, TypeToken.getParameterized(HashMap.class, String.class, String.class).getType()); HashMap<String, String> optionMap = this.objectMapper.readValue(json, new TypeReference<Map<String, String>>(){});
Object obj = null; Object obj = null;
obj = optionMap.get("aliases"); obj = optionMap.get("aliases");

View File

@ -1,8 +1,5 @@
package com.loafle.overflow.central.module.generator.service; 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.meta.MetaSensorItemKey;
import com.loafle.overflow.model.sensor.Sensor; import com.loafle.overflow.model.sensor.Sensor;
import com.loafle.overflow.model.sensor.SensorItem; 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.QueryInfo;
import com.loafle.overflow.model.sensorconfig.SensorConfig; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -30,7 +28,7 @@ public class InfraServiceMysqlGenerator {
@Autowired @Autowired
private GenerateUtil generateUtil; private GenerateUtil generateUtil;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor, public void process(List<SensorItem> sensorItems, Map<Integer, MetaSensorItemKey> keyMap, Sensor dbSensor,
SensorConfig sensorConfig) throws Exception { SensorConfig sensorConfig) throws Exception {
@ -83,7 +81,7 @@ public class InfraServiceMysqlGenerator {
String json = tempItemKey.getOption(); String json = tempItemKey.getOption();
if (json != null && json.length() > 0) { if (json != null && json.length() > 0) {
HashMap<String, String> optionMap = this.gson.fromJson(json, TypeToken.getParameterized(HashMap.class, String.class, String.class).getType()); HashMap<String, String> optionMap = this.objectMapper.readValue(json, new TypeReference<Map<String, String>>(){});
Object obj = null; Object obj = null;
obj = optionMap.get("valueColumn"); obj = optionMap.get("valueColumn");

View File

@ -1,6 +1,6 @@
package com.loafle.overflow.central.module.member.service; 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.EmailSender;
import com.loafle.overflow.central.commons.utils.SessionMetadata; import com.loafle.overflow.central.commons.utils.SessionMetadata;
import com.loafle.overflow.central.module.email.service.EmailAuthService; import com.loafle.overflow.central.module.email.service.EmailAuthService;

View File

@ -1,15 +1,11 @@
package com.loafle.overflow.central.module.noauthprobe.service; 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.service.MessagePublisher;
import com.loafle.overflow.central.commons.stereotype.ProbeAPI; import com.loafle.overflow.core.annotation.ProbeAPI;
import com.loafle.overflow.central.commons.stereotype.WebappAPI; import com.loafle.overflow.core.annotation.WebappAPI;
import com.loafle.overflow.central.commons.utils.GenerateKey; import com.loafle.overflow.central.commons.utils.GenerateKey;
import com.loafle.overflow.central.commons.utils.SessionMetadata; import com.loafle.overflow.central.commons.utils.SessionMetadata;
import com.loafle.overflow.central.commons.utils.StringConvertor; 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.central.module.noauthprobe.dao.NoAuthProbeDAO;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.model.apikey.ApiKey; 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.ProbeHostService;
import com.loafle.overflow.service.central.probe.ProbeService; 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.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -68,7 +67,7 @@ public class CentralNoAuthProbeService implements NoAuthProbeService {
private DomainMemberService domainMemberService; private DomainMemberService domainMemberService;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
@Autowired @Autowired
private InfraMachineService infraMachineService; private InfraMachineService infraMachineService;
@ -106,8 +105,13 @@ public class CentralNoAuthProbeService implements NoAuthProbeService {
@Transactional @Transactional
public List<NoAuthProbe> acceptNoAuthProbe(NoAuthProbe noAuthProbe) throws OverflowException { public List<NoAuthProbe> acceptNoAuthProbe(NoAuthProbe noAuthProbe) throws OverflowException {
HashMap<String, Object> objMap = this.gson.fromJson(noAuthProbe.getDescription(), HashMap<String, Object> objMap;
TypeToken.getParameterized(HashMap.class, String.class, Object.class).getType()); try {
objMap = this.objectMapper.readValue(noAuthProbe.getDescription(), new TypeReference<Map<String, Object>>(){});
} catch (IOException e) {
throw new OverflowException("json error", e);
}
Map<String, String> hostMap = (Map<String, String>) objMap.get("host"); Map<String, String> hostMap = (Map<String, String>) objMap.get("host");
Map<String, String> netMap = (Map<String, String>) objMap.get("network"); Map<String, String> netMap = (Map<String, String>) objMap.get("network");

View File

@ -7,16 +7,14 @@ import io.grpc.*;
public class ProxyServerInterceptor implements ServerInterceptor { public class ProxyServerInterceptor implements ServerInterceptor {
@Override @Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
String clientType = headers.get(SessionMetadata.METADATA_CLIENT_TYPE_KEY); String clientType = headers.get(SessionMetadata.METADATA_CLIENT_TYPE_KEY);
String sessionID = headers.get(SessionMetadata.METADATA_SESSION_ID_KEY); String sessionID = headers.get(SessionMetadata.METADATA_SESSION_ID_KEY);
String targetID = headers.get(SessionMetadata.METADATA_TARGET_ID_KEY); String targetID = headers.get(SessionMetadata.METADATA_TARGET_ID_KEY);
Context ctx = Context.current().withValues( Context ctx = Context.current().withValues(SessionMetadata.CTX_CLIENT_TYPE_KEY, clientType,
SessionMetadata.CTX_CLIENT_TYPE_KEY, clientType, SessionMetadata.CTX_SESSION_ID_KEY, sessionID, SessionMetadata.CTX_TARGET_ID_KEY, targetID);
SessionMetadata.CTX_SESSION_ID_KEY, sessionID,
SessionMetadata.CTX_TARGET_ID_KEY, targetID
);
return Contexts.interceptCall(ctx, call, headers, next); return Contexts.interceptCall(ctx, call, headers, next);
} }

View File

@ -1,15 +1,17 @@
package com.loafle.overflow.central.proxy; package com.loafle.overflow.central.proxy;
import com.google.gson.Gson; import com.google.gson.internal.Primitives;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import com.loafle.overflow.core.exception.OverflowException; 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.BeansException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -20,16 +22,15 @@ import java.util.Map;
*/ */
@Component @Component
public class ServiceInvoker { public class ServiceInvoker {
// @Autowired @Autowired
private ApplicationContext context; private ApplicationContext context;
private Map<String, Cache> serviceCacheMap; private Map<String, Cache> serviceCacheMap;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
public ServiceInvoker(ApplicationContext context) { public ServiceInvoker() {
this.context = context;
serviceCacheMap = new HashMap<>(); serviceCacheMap = new HashMap<>();
} }
@ -75,7 +76,8 @@ public class ServiceInvoker {
} }
private Method getMethod(String methodName) { 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[] methods = clazz.getMethods();
Method targetMethod = null; Method targetMethod = null;
@ -117,7 +119,16 @@ public class ServiceInvoker {
} }
private Object getValue(Type parameterType, String json) throws IllegalArgumentException { 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<ByteString> params) throws IllegalArgumentException { private Object[] getParameters(Type[] parameterTypes, List<ByteString> params) throws IllegalArgumentException {
@ -174,7 +185,11 @@ public class ServiceInvoker {
throw new OverflowException("internal error", e); 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; return jsonInString;
} }

View File

@ -3,14 +3,17 @@ package com.loafle.overflow.central.proxy;
import com.loafle.overflow.central.api.CentralAPIGrpc; import com.loafle.overflow.central.api.CentralAPIGrpc;
import com.loafle.overflow.central.api.ServerInput; import com.loafle.overflow.central.api.ServerInput;
import com.loafle.overflow.central.api.ServerOutput; import com.loafle.overflow.central.api.ServerOutput;
import com.loafle.overflow.central.spring.AppConfig;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import io.grpc.*; import io.grpc.*;
import io.grpc.Status.Code; import io.grpc.Status.Code;
import io.grpc.netty.NettyServerBuilder; import io.grpc.netty.NettyServerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -25,12 +28,13 @@ public class ServiceProxy {
private ApplicationContext ctx; private ApplicationContext ctx;
public void start(int port) throws IOException { public void start(int port) throws IOException {
ctx = new AnnotationConfigApplicationContext("com.loafle.overflow"); ctx = new AnnotationConfigApplicationContext(AppConfig.class);
ProxyServerInterceptor proxyServerInterceptor = new ProxyServerInterceptor(); ProxyServerInterceptor proxyServerInterceptor = new ProxyServerInterceptor();
ServiceImpl serviceImpl = ctx.getBean(ServiceImpl.class);
server = NettyServerBuilder.forPort(port) server = NettyServerBuilder.forPort(port)
.addService(ServerInterceptors.intercept(new ServiceImpl(new ServiceInvoker(ctx)), proxyServerInterceptor)) .addService(ServerInterceptors.intercept(serviceImpl, proxyServerInterceptor))
.build().start(); .build().start();
logger.info("Server started, listening on " + port); logger.info("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread() { Runtime.getRuntime().addShutdownHook(new Thread() {
@ -56,8 +60,10 @@ public class ServiceProxy {
} }
} }
@Component
static class ServiceImpl extends CentralAPIGrpc.CentralAPIImplBase { static class ServiceImpl extends CentralAPIGrpc.CentralAPIImplBase {
@Autowired
private ServiceInvoker serviceInvoker; private ServiceInvoker serviceInvoker;
ServiceImpl(ServiceInvoker serviceInvoker) { ServiceImpl(ServiceInvoker serviceInvoker) {
@ -99,3 +105,4 @@ public class ServiceProxy {
} }
} }

View File

@ -7,6 +7,7 @@ import com.loafle.overflow.core.model.PublishMessage;
import com.loafle.overflow.model.member.Member; import com.loafle.overflow.model.member.Member;
import com.loafle.overflow.service.central.member.MemberService; import com.loafle.overflow.service.central.member.MemberService;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
@ -27,7 +28,7 @@ public class RedisMessagePublisher implements MessagePublisher {
@Autowired @Autowired
private RedisTemplate<String, Object> redisTemplate; private RedisTemplate<String, Object> redisTemplate;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
@Autowired @Autowired
private MemberService memberService; private MemberService memberService;
@ -113,8 +114,12 @@ public class RedisMessagePublisher implements MessagePublisher {
ChannelTopic topic = this.topics.get(channel); ChannelTopic topic = this.topics.get(channel);
message.setMessage(new PublishMessage.PublishMessageBody(method, this.getMessageBody(params))); message.setMessage(new PublishMessage.PublishMessageBody(method, this.getMessageBody(params)));
String json = this.gson.toJson(message); try {
redisTemplate.convertAndSend(topic.getTopic(), json); String json = this.objectMapper.writeValueAsString(message);
redisTemplate.convertAndSend(topic.getTopic(), json);
} catch (IOException e) {
e.printStackTrace();
}
} }
protected List<String> getMessageBody(final Object... params) { protected List<String> getMessageBody(final Object... params) {
@ -123,7 +128,12 @@ public class RedisMessagePublisher implements MessagePublisher {
if (param.getClass().equals(String.class)) { if (param.getClass().equals(String.class)) {
results.add((String) param); results.add((String) param);
} else { } else {
results.add(this.gson.toJson(param)); try {
String json = this.objectMapper.writeValueAsString(param);
results.add(json);
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
return results; return results;

View File

@ -1,7 +1,7 @@
package com.loafle.overflow.central.spring; 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.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@ -29,14 +29,22 @@ public class AppConfig {
// return ppc; // return ppc;
// } // }
@Bean @Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer(); return new PropertySourcesPlaceholderConfigurer();
} }
// @Bean
// public Gson gson() {
// GsonBuilder builder = new GsonBuilder();
// builder.excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.PRIVATE);
// // builder.excludeFieldsWithoutExposeAnnotation();
// return builder.create();
// }
@Bean @Bean
public Gson gson() { public ObjectMapper getObjectMapper() {
Gson gson = new Gson(); ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return gson; return objectMapper;
} }
} }

View File

@ -8,6 +8,7 @@ import com.loafle.overflow.model.infra.InfraOS;
import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.service.central.infra.InfraHostService; import com.loafle.overflow.service.central.infra.InfraHostService;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -27,7 +28,7 @@ public class InfraHostServiceTest {
private InfraHostService infraHostService; private InfraHostService infraHostService;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
@Test @Test
// @Ignore // @Ignore
@ -58,7 +59,7 @@ public class InfraHostServiceTest {
InfraHost infraHost = this.infraHostService.read(1); InfraHost infraHost = this.infraHostService.read(1);
String json = gson.toJson(infraHost); String json = objectMapper.writeValueAsString(infraHost);
System.out.println(json); System.out.println(json);

View File

@ -6,6 +6,7 @@ import com.loafle.overflow.model.infra.InfraMachine;
import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.service.central.infra.InfraMachineService; import com.loafle.overflow.service.central.infra.InfraMachineService;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -25,7 +26,7 @@ public class InfraMachineServiceTest {
private InfraMachineService infraMachineService; private InfraMachineService infraMachineService;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
// @Ignore // @Ignore
@Test @Test
@ -52,7 +53,7 @@ public class InfraMachineServiceTest {
InfraMachine infraMachine = this.infraMachineService.read(1); InfraMachine infraMachine = this.infraMachineService.read(1);
String json = gson.toJson(infraMachine); String json = objectMapper.writeValueAsString(infraMachine);
System.out.println(json); System.out.println(json);
} }

View File

@ -7,6 +7,7 @@ import com.loafle.overflow.model.infra.InfraOSApplication;
import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.service.central.infra.InfraOSApplicationService; import com.loafle.overflow.service.central.infra.InfraOSApplicationService;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -25,7 +26,7 @@ public class InfraOSApplicationServiceTest {
private InfraOSApplicationService infraOSApplicationService; private InfraOSApplicationService infraOSApplicationService;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
// @Ignore // @Ignore
@Test @Test
@ -56,7 +57,7 @@ public class InfraOSApplicationServiceTest {
Assert.assertNotNull(infraOSApplication); Assert.assertNotNull(infraOSApplication);
String json = gson.toJson(infraOSApplication); String json = objectMapper.writeValueAsString(infraOSApplication);
System.out.println(json); System.out.println(json);
} }

View File

@ -7,6 +7,7 @@ import com.loafle.overflow.model.infra.InfraOSDaemon;
import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.service.central.infra.InfraOSDaemonService; import com.loafle.overflow.service.central.infra.InfraOSDaemonService;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -25,7 +26,7 @@ public class InfraOSDaemonServiceTest {
private InfraOSDaemonService infraOSDaemonService; private InfraOSDaemonService infraOSDaemonService;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
// @Ignore // @Ignore
@Test @Test
@ -55,7 +56,7 @@ public class InfraOSDaemonServiceTest {
InfraOSDaemon infraOSDaemon = this.infraOSDaemonService.read(1); InfraOSDaemon infraOSDaemon = this.infraOSDaemonService.read(1);
Assert.assertNotNull(infraOSDaemon); Assert.assertNotNull(infraOSDaemon);
String json = gson.toJson(infraOSDaemon); String json = objectMapper.writeValueAsString(infraOSDaemon);
System.out.println(json); System.out.println(json);
} }

View File

@ -8,6 +8,7 @@ import com.loafle.overflow.model.infra.InfraOSPort;
import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.service.central.infra.InfraOSPortService; import com.loafle.overflow.service.central.infra.InfraOSPortService;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -25,7 +26,7 @@ public class InfraOSPortServiceTest {
@Autowired @Autowired
private InfraOSPortService infraOSPortService; private InfraOSPortService infraOSPortService;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
// @Ignore // @Ignore
@Test @Test

View File

@ -8,6 +8,7 @@ import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.model.meta.MetaInfraVendor; import com.loafle.overflow.model.meta.MetaInfraVendor;
import com.loafle.overflow.service.central.infra.InfraOSService; import com.loafle.overflow.service.central.infra.InfraOSService;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -24,7 +25,7 @@ public class InfraOSServiceTest {
@Autowired @Autowired
private InfraOSService infraOSService; private InfraOSService infraOSService;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
@Test @Test
// @Ignore // @Ignore
@ -55,7 +56,7 @@ public class InfraOSServiceTest {
public void read() throws Exception { public void read() throws Exception {
InfraOS infraOS = this.infraOSService.read(1); InfraOS infraOS = this.infraOSService.read(1);
String json = gson.toJson(infraOS); String json = objectMapper.writeValueAsString(infraOS);
System.out.println(json); System.out.println(json);
} }

View File

@ -8,6 +8,7 @@ import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.model.meta.MetaInfraVendor; import com.loafle.overflow.model.meta.MetaInfraVendor;
import com.loafle.overflow.service.central.infra.InfraServiceService; import com.loafle.overflow.service.central.infra.InfraServiceService;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -25,6 +26,6 @@ public class InfraServiceServiceTest {
@Autowired @Autowired
private InfraServiceService infraServiceService; private InfraServiceService infraServiceService;
@Autowired @Autowired
private Gson gson; private ObjectMapper objectMapper;
} }

View File

@ -1,17 +1,13 @@
package com.loafle.overflow.central.spring; 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.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 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 org.springframework.test.context.TestPropertySource;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import com.google.gson.Gson;
/** /**
* Created by geek on 17. 8. 8. * Created by geek on 17. 8. 8.
*/ */
@ -51,9 +47,10 @@ public class AppConfigTest {
} }
@Bean @Bean
public Gson gson() { public ObjectMapper getObjectMapper() {
Gson gson = new Gson(); ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return gson; return objectMapper;
} }
} }