Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Fixing NoClassDefFoundError (#366)" #367

Merged
merged 1 commit into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@

package com.amazonaws.xray.entities;

import com.amazonaws.xray.AWSXRayObjectMapper;
import com.amazonaws.xray.AWSXRayRecorder;
import com.amazonaws.xray.exceptions.AlreadyEmittedException;
import com.amazonaws.xray.serializers.CauseSerializer;
import com.amazonaws.xray.serializers.StackTraceElementSerializer;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -41,6 +50,18 @@
*
*/
public abstract class EntityImpl implements Entity {

/**
* @deprecated For internal use only.
*/
@SuppressWarnings("checkstyle:ConstantName")
@Deprecated
protected static final ObjectMapper mapper = new ObjectMapper()
.findAndRegisterModules()
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

private static final Log logger = LogFactory.getLog(EntityImpl.class);

private static final String DEFAULT_METADATA_NAMESPACE = "default";
Expand Down Expand Up @@ -100,6 +121,37 @@ public abstract class EntityImpl implements Entity {
@JsonIgnore
private boolean emitted = false;

static {
/*
* Inject the CauseSerializer and StackTraceElementSerializer classes into the local mapper such that they will serialize
* their respective object types.
*/
mapper.registerModule(new SimpleModule() {
private static final long serialVersionUID = 545800949242254918L;

@Override
public void setupModule(SetupContext setupContext) {
super.setupModule(setupContext);
setupContext.addBeanSerializerModifier(new BeanSerializerModifier() {
@SuppressWarnings("unchecked")
@Override
public JsonSerializer<?> modifySerializer(
SerializationConfig serializationConfig,
BeanDescription beanDescription,
JsonSerializer<?> jsonSerializer) {
Class<?> beanClass = beanDescription.getBeanClass();
if (Cause.class.isAssignableFrom(beanClass)) {
return new CauseSerializer((JsonSerializer<Object>) jsonSerializer);
} else if (StackTraceElement.class.isAssignableFrom(beanClass)) {
return new StackTraceElementSerializer();
}
return jsonSerializer;
}
});
}
});
}

// default constructor for Jackson, so it can understand the default values to compare when using the Include.NON_DEFAULT
// annotation.
@SuppressWarnings("nullness")
Expand Down Expand Up @@ -560,7 +612,7 @@ public boolean compareAndSetEmitted(boolean current, boolean next) {
@Override
public String serialize() {
try {
return AWSXRayObjectMapper.getInstance().writeValueAsString(this);
return mapper.writeValueAsString(this);
} catch (JsonProcessingException jpe) {
logger.error("Exception while serializing entity.", jpe);
}
Expand All @@ -570,7 +622,7 @@ public String serialize() {
@Override
public String prettySerialize() {
try {
return AWSXRayObjectMapper.getInstance().writerWithDefaultPrettyPrinter().writeValueAsString(this);
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
} catch (JsonProcessingException jpe) {
logger.error("Exception while serializing segment.", jpe);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

package com.amazonaws.xray.entities;

import com.amazonaws.xray.AWSXRayObjectMapper;
import com.amazonaws.xray.AWSXRayRecorder;
import com.amazonaws.xray.internal.SamplingStrategyOverride;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down Expand Up @@ -146,7 +145,7 @@ public boolean shouldPropagate() {
}

private ObjectNode getStreamSerializeObjectNode() {
ObjectNode obj = (ObjectNode) AWSXRayObjectMapper.getInstance().valueToTree(this);
ObjectNode obj = (ObjectNode) mapper.valueToTree(this);
obj.put("type", "subsegment");
obj.put("parent_id", getParent().getId());
obj.put("trace_id", parentSegment.getTraceId().toString());
Expand All @@ -157,7 +156,7 @@ private ObjectNode getStreamSerializeObjectNode() {
public String streamSerialize() {
String ret = "";
try {
ret = AWSXRayObjectMapper.getInstance().writeValueAsString(getStreamSerializeObjectNode());
ret = mapper.writeValueAsString(getStreamSerializeObjectNode());
} catch (JsonProcessingException jpe) {
logger.error("Exception while serializing entity.", jpe);
}
Expand All @@ -168,8 +167,7 @@ public String streamSerialize() {
public String prettyStreamSerialize() {
String ret = "";
try {
ret = AWSXRayObjectMapper.getInstance().writerWithDefaultPrettyPrinter()
.writeValueAsString(getStreamSerializeObjectNode());
ret = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(getStreamSerializeObjectNode());
} catch (JsonProcessingException jpe) {
logger.error("Exception while serializing entity.", jpe);
}
Expand Down