From ce435bceb2a887df1c255bf7f48d86d2ea2c24bb Mon Sep 17 00:00:00 2001 From: Woonduk Kang Date: Tue, 6 Oct 2015 11:28:30 +0900 Subject: [PATCH] refactoring AgentInfo, AgentInfoBo --- .../mapper/thrift/AgentInfoBoMapper.java | 5 -- .../pinpoint/common/bo/AgentInfoBo.java | 21 +---- .../web/applicationmap/ServerBuilder.java | 2 +- .../web/applicationmap/ServerInstance.java | 82 +++++------------ .../web/dao/hbase/HbaseAgentInfoDao.java | 3 +- .../view/ApplicationAgentListSerializer.java | 12 ++- .../web/view/ServerInstanceSerializer.java | 87 +++++++++++++++++++ .../navercorp/pinpoint/web/vo/AgentInfo.java | 51 +++++------ .../ServerInstanceListTest.java | 3 - .../ServerInstanceListSerializerTest.java | 62 +++++++++++-- 10 files changed, 196 insertions(+), 132 deletions(-) create mode 100644 web/src/main/java/com/navercorp/pinpoint/web/view/ServerInstanceSerializer.java diff --git a/collector/src/main/java/com/navercorp/pinpoint/collector/mapper/thrift/AgentInfoBoMapper.java b/collector/src/main/java/com/navercorp/pinpoint/collector/mapper/thrift/AgentInfoBoMapper.java index 4d0979131..fc62da1b8 100644 --- a/collector/src/main/java/com/navercorp/pinpoint/collector/mapper/thrift/AgentInfoBoMapper.java +++ b/collector/src/main/java/com/navercorp/pinpoint/collector/mapper/thrift/AgentInfoBoMapper.java @@ -17,10 +17,8 @@ package com.navercorp.pinpoint.collector.mapper.thrift; import com.navercorp.pinpoint.common.bo.AgentInfoBo; -import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService; import com.navercorp.pinpoint.thrift.dto.TAgentInfo; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** @@ -28,8 +26,6 @@ import org.springframework.stereotype.Component; */ @Component public class AgentInfoBoMapper implements ThriftBoMapper { - @Autowired - private ServiceTypeRegistryService registry; @Override public AgentInfoBo map(TAgentInfo thriftObject) { @@ -53,7 +49,6 @@ public class AgentInfoBoMapper implements ThriftBoMapper> map = applicationAgentList.getApplicationAgentList(); @@ -66,7 +70,9 @@ public class ApplicationAgentListSerializer extends JsonSerializer { + + @Autowired(required=false) + private MatcherGroup matcherGroup = new MatcherGroup(); + @Autowired + private ServiceTypeRegistryService serviceTypeRegistryService; + + private AgentLifeCycleStateSerializer agentLifeCycleStateSerializer = new AgentLifeCycleStateSerializer(); + + + public void setServiceTypeRegistryService(ServiceTypeRegistryService serviceTypeRegistryService) { + this.serviceTypeRegistryService = serviceTypeRegistryService; + } + + + @Override + public void serialize(ServerInstance serverInstance, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { + + jgen.writeStartObject(); + final short serviceTypeCode = serverInstance.getServiceTypeCode(); + final ServiceType serviceType = serviceTypeRegistryService.findServiceType(serviceTypeCode); + + jgen.writeBooleanField("hasInspector", hasInspector(serviceType)); + final ServerMatcher serverMatcher = this.matcherGroup.match(serverInstance.getHostName()); + jgen.writeStringField("linkName", serverMatcher.getLinkName()); + jgen.writeStringField("linkURL", serverMatcher.getLink(serverInstance.getHostName())); + jgen.writeStringField("name", serverInstance.getName()); + jgen.writeStringField("serviceType", serviceType.getName()); + + jgen.writeFieldName("status"); + write(serverInstance.getStatus(), jgen, provider); + + jgen.writeEndObject(); + + } + + public void write(AgentLifeCycleState value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { + agentLifeCycleStateSerializer.serialize(value, jgen, provider); + } + + + public boolean hasInspector(ServiceType serviceType) { + if (serviceType.isWas()) { + return true; + } else { + return false; + } + } + + +} diff --git a/web/src/main/java/com/navercorp/pinpoint/web/vo/AgentInfo.java b/web/src/main/java/com/navercorp/pinpoint/web/vo/AgentInfo.java index 6a980b727..11b34644a 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/vo/AgentInfo.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/vo/AgentInfo.java @@ -44,7 +44,7 @@ public class AgentInfo { private String hostName; private String ip; private String ports; - private ServiceType serviceType; + private short serviceTypeCode; private int pid; private String vmVersion; private String agentVersion; @@ -66,7 +66,7 @@ public class AgentInfo { this.hostName = agentInfoBo.getHostName(); this.ip = agentInfoBo.getIp(); this.ports = agentInfoBo.getPorts(); - this.serviceType = agentInfoBo.getServiceType(); + this.serviceTypeCode = agentInfoBo.getServiceTypeCode(); this.pid = agentInfoBo.getPid(); this.vmVersion = agentInfoBo.getVmVersion(); this.agentVersion = agentInfoBo.getAgentVersion(); @@ -121,12 +121,12 @@ public class AgentInfo { this.ports = ports; } - public ServiceType getServiceType() { - return serviceType; + public short getServiceTypeCode() { + return serviceTypeCode; } - public void setServiceType(ServiceType serviceType) { - this.serviceType = serviceType; + public void setServiceTypeCode(short serviceTypeCode) { + this.serviceTypeCode = serviceTypeCode; } public int getPid() { @@ -179,40 +179,29 @@ public class AgentInfo { @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((agentId == null) ? 0 : agentId.hashCode()); - result = prime * result + ((serviceType == null) ? 0 : serviceType.hashCode()); + int result = agentId != null ? agentId.hashCode() : 0; + result = 31 * result + (int) serviceTypeCode; return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - AgentInfo other = (AgentInfo)obj; - if (agentId == null) { - if (other.agentId != null) - return false; - } else if (!agentId.equals(other.agentId)) - return false; - if (serviceType == null) { - if (other.serviceType != null) - return false; - } else if (!serviceType.equals(other.serviceType)) - return false; - return true; + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + AgentInfo agentInfo = (AgentInfo) o; + + if (serviceTypeCode != agentInfo.serviceTypeCode) return false; + return !(agentId != null ? !agentId.equals(agentInfo.agentId) : agentInfo.agentId != null); + } + @Override public String toString() { return "AgentInfo [applicationName=" + applicationName + ", agentId=" + agentId + ", startTimestamp=" - + startTimestamp + ", hostName=" + hostName + ", ip=" + ip + ", ports=" + ports + ", serviceType=" - + serviceType + ", pid=" + pid + ", vmVersion=" + vmVersion + ", agentVersion=" + agentVersion + + startTimestamp + ", hostName=" + hostName + ", ip=" + ip + ", ports=" + ports + ", serviceTypeCode=" + + serviceTypeCode + ", pid=" + pid + ", vmVersion=" + vmVersion + ", agentVersion=" + agentVersion + ", serverMetaData=" + serverMetaData + ", initialStartTimestamp=" + initialStartTimestamp + ", status=" + status + "]"; } diff --git a/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/ServerInstanceListTest.java b/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/ServerInstanceListTest.java index dd88678d0..a82e011c6 100644 --- a/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/ServerInstanceListTest.java +++ b/web/src/test/java/com/navercorp/pinpoint/web/applicationmap/ServerInstanceListTest.java @@ -58,9 +58,6 @@ public class ServerInstanceListTest { ServiceType serviceType = ServiceType.TEST_STAND_ALONE; agentInfoBuilder.setServiceTypeCode(serviceType.getCode()); - // TODO FIX api - agentInfoBuilder.setServiceType(serviceType); - agentInfoBuilder.setHostName(hostName); return new AgentInfo(agentInfoBuilder.build()); diff --git a/web/src/test/java/com/navercorp/pinpoint/web/view/ServerInstanceListSerializerTest.java b/web/src/test/java/com/navercorp/pinpoint/web/view/ServerInstanceListSerializerTest.java index a36e7208a..0e26d20a2 100644 --- a/web/src/test/java/com/navercorp/pinpoint/web/view/ServerInstanceListSerializerTest.java +++ b/web/src/test/java/com/navercorp/pinpoint/web/view/ServerInstanceListSerializerTest.java @@ -17,14 +17,22 @@ package com.navercorp.pinpoint.web.view; import java.util.HashSet; +import java.util.Set; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.cfg.HandlerInstantiator; +import com.fasterxml.jackson.databind.cfg.MapperConfig; +import com.fasterxml.jackson.databind.introspect.Annotated; +import com.fasterxml.jackson.databind.jsontype.TypeIdResolver; +import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder; +import com.navercorp.pinpoint.common.service.DefaultServiceTypeRegistryService; +import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService; import com.navercorp.pinpoint.web.applicationmap.ServerInstanceListTest; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.databind.ObjectWriter; import com.navercorp.pinpoint.web.applicationmap.ServerBuilder; import com.navercorp.pinpoint.web.applicationmap.ServerInstanceList; import com.navercorp.pinpoint.web.vo.AgentInfo; @@ -39,13 +47,11 @@ public class ServerInstanceListSerializerTest { @Test public void testSerialize() throws Exception { - PinpointObjectMapper mapper = new PinpointObjectMapper(); - mapper.afterPropertiesSet(); - + PinpointObjectMapper mapper = createMapper(); AgentInfo agentInfo = ServerInstanceListTest.createAgentInfo("agentId1", "testHost"); - HashSet agentInfoSet = new HashSet(); + Set agentInfoSet = new HashSet<>(); agentInfoSet.add(agentInfo); ServerBuilder builder = new ServerBuilder(); @@ -57,4 +63,50 @@ public class ServerInstanceListSerializerTest { logger.debug(json); } + + private PinpointObjectMapper createMapper() throws Exception { + PinpointObjectMapper mapper = new PinpointObjectMapper(); + // TODO FIX spring managed object + mapper.setHandlerInstantiator(new TestHandlerInstantiator()); + mapper.afterPropertiesSet(); + return mapper; + } + + public class TestHandlerInstantiator extends HandlerInstantiator { + + public TestHandlerInstantiator() { + } + + public JsonSerializer serializerInstance(SerializationConfig config, Annotated annotated, Class keyDeserClass) { + if (annotated.getName().equals("com.navercorp.pinpoint.web.applicationmap.ServerInstance")) { + final ServiceTypeRegistryService serviceTypeRegistryService = new DefaultServiceTypeRegistryService(); + final ServerInstanceSerializer serverInstanceSerializer = new ServerInstanceSerializer(); + serverInstanceSerializer.setServiceTypeRegistryService(serviceTypeRegistryService); + return serverInstanceSerializer; + } + return null; + } + + @Override + public JsonDeserializer deserializerInstance(DeserializationConfig config, Annotated annotated, Class deserClass) { + return null; + } + + @Override + public KeyDeserializer keyDeserializerInstance(DeserializationConfig config, Annotated annotated, Class keyDeserClass) { + return null; + } + + @Override + public TypeResolverBuilder typeResolverBuilderInstance(MapperConfig config, Annotated annotated, Class builderClass) { + return null; + } + + @Override + public TypeIdResolver typeIdResolverInstance(MapperConfig config, Annotated annotated, Class resolverClass) { + return null; + } + } + + }