Skip to content

Commit

Permalink
[#9037] Refactor JsonSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jul 14, 2022
1 parent 97557c8 commit 91a37a8
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 103 deletions.
12 changes: 12 additions & 0 deletions commons-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<!-- commons module may be used at agent so there will be problems. you should set optional -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,27 @@

package com.navercorp.pinpoint.common.server.util;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.navercorp.pinpoint.common.util.apache.IntHashMap;

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum AgentLifeCycleState {
RUNNING((short)100, "Running"),
SHUTDOWN((short)200, "Shutdown"),
UNEXPECTED_SHUTDOWN((short)201, "Unexpected Shutdown"),
DISCONNECTED((short)300, "Disconnected"),
UNKNOWN((short)-1, "Unknown");
RUNNING((short) 100, "Running"),
SHUTDOWN((short) 200, "Shutdown"),
UNEXPECTED_SHUTDOWN((short) 201, "Unexpected Shutdown"),
DISCONNECTED((short) 300, "Disconnected"),
UNKNOWN((short) -1, "Unknown");

private static final IntHashMap<AgentLifeCycleState> MAPPING = initializeCodeMapping();

private final short code;
private final short code;
private final String desc;

AgentLifeCycleState(short code, String desc) {
this.code = code;
this.desc = desc;
}

public short getCode() {
return this.code;
}
Expand All @@ -43,12 +49,19 @@ public String getDesc() {
public String toString() {
return this.desc;
}

public static AgentLifeCycleState getStateByCode(short code) {

private static IntHashMap<AgentLifeCycleState> initializeCodeMapping() {
IntHashMap<AgentLifeCycleState> codeMap = new IntHashMap<>();
for (AgentLifeCycleState state : AgentLifeCycleState.values()) {
if (state.code == code) {
return state;
}
codeMap.put(state.getCode(), state);
}
return codeMap;
}

public static AgentLifeCycleState getStateByCode(short code) {
AgentLifeCycleState state = MAPPING.get(code);
if (state != null) {
return state;
}
return UNKNOWN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,48 @@
*/
package com.navercorp.pinpoint.web.applicationmap.link;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* @author minwoo.jung<[email protected]>
*
*/
public class LinkInfo {
private String linkName;
private String linkUrl;
public LinkType linkType;
private final String linkName;
private final String linkUrl;
private final LinkType linkType;

LinkInfo(String linkName, String linkUrl, LinkType linkType) {
public LinkInfo(String linkName, String linkUrl, LinkType linkType) {
this.linkName = linkName;
this.linkUrl = linkUrl;
this.linkType = linkType;
}

@JsonProperty("linkName")
public String getLinkName() {
return linkName;
}

public void setLinkName(String linkName) {
this.linkName = linkName;
}

@JsonProperty("linkURL")
public String getLinkUrl() {
return linkUrl;
}

public void setLinkUrl(String linkUrl) {
this.linkUrl = linkUrl;
}

public String getLinktype() {


@JsonProperty("linkType")
public String getLinkType() {
return linkType.getName();
}

public void setLinkType(LinkType linkType) {
this.linkType = linkType;

@Override
public String toString() {
return "LinkInfo{" +
"linkName='" + linkName + '\'' +
", linkUrl='" + linkUrl + '\'' +
", linkType=" + linkType +
'}';
}

public enum LinkType {
ATAG("aTag"),
BUTTON("button");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.navercorp.pinpoint.common.server.util.AgentLifeCycleState;
import com.navercorp.pinpoint.web.view.AgentLifeCycleStateSerializer;
import com.navercorp.pinpoint.web.view.ServerInstanceSerializer;
import com.navercorp.pinpoint.web.vo.AgentInfo;
import com.navercorp.pinpoint.web.vo.AgentStatus;
Expand All @@ -43,7 +42,6 @@ public class ServerInstance {

private final ServerType serverType;

@JsonSerialize(using = AgentLifeCycleStateSerializer.class)
private final AgentLifeCycleState status;

public ServerInstance(AgentInfo agentInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class AgentInfoSerializer extends JsonSerializer<AgentInfo> {
private List<MatcherGroup> matcherGroupList;

@Autowired
private ServiceTypeRegistryService serviceTypeRegistryService;
ServiceTypeRegistryService serviceTypeRegistryService;

@Override
public void serialize(AgentInfo agentInfo, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
Expand Down Expand Up @@ -75,11 +75,7 @@ public void serialize(AgentInfo agentInfo, JsonGenerator jgen, SerializerProvide
for (MatcherGroup matcherGroup : matcherGroupList) {
if (matcherGroup.ismatchingType(agentInfo)) {
LinkInfo linkInfo = matcherGroup.makeLinkInfo(agentInfo);
jgen.writeStartObject();
jgen.writeStringField("linkName", linkInfo.getLinkName());
jgen.writeStringField("linkURL", linkInfo.getLinkUrl());
jgen.writeStringField("linkType", linkInfo.getLinktype());
jgen.writeEndObject();
jgen.writeObject(linkInfo);
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ public void serialize(ServerInstanceList serverInstanceList, JsonGenerator jgen,
for (MatcherGroup matcherGroup : matcherGroupList) {
if (matcherGroup.ismatchingType(entry.getValue().get(0))) {
LinkInfo linkInfo = matcherGroup.makeLinkInfo(entry.getValue().get(0));
jgen.writeStartObject();
jgen.writeStringField("linkName", linkInfo.getLinkName());
jgen.writeStringField("linkURL", linkInfo.getLinkUrl());
jgen.writeStringField("linkType", linkInfo.getLinktype());
jgen.writeEndObject();
jgen.writeObject(linkInfo);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import com.navercorp.pinpoint.loader.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.server.util.AgentLifeCycleState;
import com.navercorp.pinpoint.web.applicationmap.nodes.ServerInstance;

import org.springframework.stereotype.Component;
Expand All @@ -39,11 +38,8 @@ public class ServerInstanceSerializer extends JsonSerializer<ServerInstance> {

private final ServiceTypeRegistryService serviceTypeRegistryService;

private final AgentLifeCycleStateSerializer agentLifeCycleStateSerializer;

public ServerInstanceSerializer(ServiceTypeRegistryService serviceTypeRegistryService, AgentLifeCycleStateSerializer agentLifeCycleStateSerializer) {
public ServerInstanceSerializer(ServiceTypeRegistryService serviceTypeRegistryService) {
this.serviceTypeRegistryService = Objects.requireNonNull(serviceTypeRegistryService, "serviceTypeRegistryService");
this.agentLifeCycleStateSerializer = Objects.requireNonNull(agentLifeCycleStateSerializer, "agentLifeCycleStateSerializer");
}

@Override
Expand All @@ -58,17 +54,11 @@ public void serialize(ServerInstance serverInstance, JsonGenerator jgen, Seriali
jgen.writeStringField("agentName", serverInstance.getAgentName());
jgen.writeStringField("serviceType", serviceType.getName());

jgen.writeFieldName("status");
write(serverInstance.getStatus(), jgen, provider);

jgen.writeObjectField("status", serverInstance.getStatus());
jgen.writeEndObject();

}

public void write(AgentLifeCycleState value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
this.agentLifeCycleStateSerializer.serialize(value, jgen, provider);
}


public boolean hasInspector(ServiceType serviceType) {
if (serviceType.isWas()) {
Expand All @@ -78,5 +68,4 @@ public boolean hasInspector(ServiceType serviceType) {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package com.navercorp.pinpoint.web.vo;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.navercorp.pinpoint.common.server.util.AgentLifeCycleState;
import com.navercorp.pinpoint.web.view.AgentLifeCycleStateSerializer;

import java.util.Objects;

Expand All @@ -33,7 +31,6 @@ public class AgentStatus {

private long eventTimestamp;

@JsonSerialize(using = AgentLifeCycleStateSerializer.class)
private final AgentLifeCycleState state;

public AgentStatus(String agentId, AgentLifeCycleState state, long eventTimestamp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ public TestHandlerInstantiator() {
public JsonSerializer<?> serializerInstance(SerializationConfig config, Annotated annotated, Class<?> keyDeserClass) {
if (annotated.getName().equals("com.navercorp.pinpoint.web.applicationmap.nodes.ServerInstance")) {
final ServiceTypeRegistryService serviceTypeRegistryService = mockServiceTypeRegistryService();
final AgentLifeCycleStateSerializer agentLifeCycleStateSerializer = new AgentLifeCycleStateSerializer();

return new ServerInstanceSerializer(serviceTypeRegistryService, agentLifeCycleStateSerializer);
return new ServerInstanceSerializer(serviceTypeRegistryService);
}
return null;
}
Expand Down

0 comments on commit 91a37a8

Please sign in to comment.