Skip to content

Commit

Permalink
Fixing NoClassDefFoundError (#366)
Browse files Browse the repository at this point in the history
* Fixing NoClassDefFoundError
  • Loading branch information
atshaw43 authored Dec 12, 2022
1 parent 1311d58 commit c912501
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amazonaws.xray;

import com.amazonaws.xray.entities.Cause;
import com.amazonaws.xray.serializers.CauseSerializer;
import com.amazonaws.xray.serializers.StackTraceElementSerializer;
import com.fasterxml.jackson.annotation.JsonInclude;
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.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

public class AWSXRayObjectMapper extends ObjectMapper {

private static AWSXRayObjectMapper instance;

public static AWSXRayObjectMapper getInstance() {
return instance;
}

static {
instance = new AWSXRayObjectMapper();

instance.findAndRegisterModules();
instance.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
instance.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
instance.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

instance.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;
}
});
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,15 @@

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 @@ -50,18 +41,6 @@
*
*/
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 @@ -121,37 +100,6 @@ 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 @@ -612,7 +560,7 @@ public boolean compareAndSetEmitted(boolean current, boolean next) {
@Override
public String serialize() {
try {
return mapper.writeValueAsString(this);
return AWSXRayObjectMapper.getInstance().writeValueAsString(this);
} catch (JsonProcessingException jpe) {
logger.error("Exception while serializing entity.", jpe);
}
Expand All @@ -622,7 +570,7 @@ public String serialize() {
@Override
public String prettySerialize() {
try {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
return AWSXRayObjectMapper.getInstance().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,6 +15,7 @@

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 @@ -145,7 +146,7 @@ public boolean shouldPropagate() {
}

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

0 comments on commit c912501

Please sign in to comment.