Skip to content

Commit

Permalink
Replaced org.json library with a self-written JSON parser due to imco…
Browse files Browse the repository at this point in the history
…mpatibilities with the Apache 2.0 licence
  • Loading branch information
Roman Vottner authored and oscerd committed Nov 29, 2017
1 parent 0ceca02 commit 2cdaa43
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 47 deletions.
6 changes: 3 additions & 3 deletions components/camel-aws-xray/pom.xml
Expand Up @@ -91,9 +91,9 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.json</groupId> <groupId>commons-lang</groupId>
<artifactId>json</artifactId> <artifactId>commons-lang</artifactId>
<version>20170516</version> <version>2.6</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>


Expand Down
Expand Up @@ -28,13 +28,15 @@
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;

import org.apache.camel.component.aws.xray.TestDataBuilder.TestEntity; import org.apache.camel.component.aws.xray.TestDataBuilder.TestEntity;
import org.apache.camel.component.aws.xray.TestDataBuilder.TestSegment; import org.apache.camel.component.aws.xray.TestDataBuilder.TestSegment;
import org.apache.camel.component.aws.xray.TestDataBuilder.TestSubsegment; import org.apache.camel.component.aws.xray.TestDataBuilder.TestSubsegment;
import org.apache.camel.component.aws.xray.TestDataBuilder.TestTrace; import org.apache.camel.component.aws.xray.TestDataBuilder.TestTrace;
import org.json.JSONArray; import org.apache.camel.component.aws.xray.json.JsonArray;
import org.json.JSONException; import org.apache.camel.component.aws.xray.json.JsonObject;
import org.json.JSONObject; import org.apache.camel.component.aws.xray.json.JsonParser;
import org.apache.commons.lang.StringUtils;
import org.junit.rules.ExternalResource; import org.junit.rules.ExternalResource;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -95,8 +97,8 @@ public void run() {
serverSocket = new DatagramSocket(2000); serverSocket = new DatagramSocket(2000);


StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
byte[] receiveData = new byte[8096];
while (!done) { while (!done) {
byte[] receiveData = new byte[2048];
DatagramPacket receivedPacket = new DatagramPacket(receiveData, receiveData.length); DatagramPacket receivedPacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivedPacket); serverSocket.receive(receivedPacket);


Expand All @@ -105,17 +107,26 @@ public void run() {


String locSegment = null; String locSegment = null;
try { try {
String raw = sb.toString(); String raw = sb.toString().trim();
String[] segments = raw.split("\\n"); String[] segments = raw.split("\\n");
for (String segment : segments) { for (String segment : segments) {
locSegment = segment; locSegment = segment;
LOG.trace("Processing received segment: {}", segment); LOG.trace("Processing received segment: {}", segment);
if (!"".equals(segment)) { if (!"".equals(segment)) {
if (!segment.endsWith("}")
|| StringUtils.countMatches(segment, "{") != StringUtils.countMatches(segment, "}")
|| StringUtils.countMatches(segment, "[") != StringUtils.countMatches(segment, "]")) {
LOG.trace("Skipping incomplete content: {}", segment);
continue;
}
if (segment.contains("format") && segment.contains("version")) { if (segment.contains("format") && segment.contains("version")) {
LOG.trace("Skipping format and version JSON"); LOG.trace("Skipping format and version JSON");
} else { } else {
LOG.trace("Converting segment {} to a Java object", segment); LOG.trace("Converting segment {} to a Java object", segment);
JSONObject json = new JSONObject(segment); // clean the JSON string received
LOG.trace("Original JSON content: {}", segment);
locSegment = segment;
JsonObject json = (JsonObject) JsonParser.parse(segment);
String traceId = json.getString("trace_id"); String traceId = json.getString("trace_id");
TestTrace testTrace = receivedTraces.get(traceId); TestTrace testTrace = receivedTraces.get(traceId);
if (null == testTrace) { if (null == testTrace) {
Expand All @@ -131,8 +142,8 @@ public void run() {
} }
} }
LOG.trace("Item {} received. JSON content: {}, Raw: {}", LOG.trace("Item {} received. JSON content: {}, Raw: {}",
receivedTraces.size(), receivedTraces, raw); receivedTraces.size(), receivedTraces, raw);
} catch (JSONException jsonEx) { } catch (Exception jsonEx) {
LOG.warn("Could not convert segment " + locSegment + " to a Java object", jsonEx); LOG.warn("Could not convert segment " + locSegment + " to a Java object", jsonEx);
} }
} }
Expand All @@ -143,12 +154,12 @@ public void run() {
} }
} }


private TestSegment convertData(JSONObject json) { private TestSegment convertData(JsonObject json) {
String name = json.getString("name"); String name = json.getString("name");
double startTime = json.getDouble("start_time"); double startTime = json.getDouble("start_time");
TestSegment segment = new TestSegment(name, startTime); TestSegment segment = new TestSegment(name, startTime);
if (json.has("subsegments")) { if (json.has("subsegments")) {
JSONArray jsonSubsegments = json.getJSONArray("subsegments"); JsonArray jsonSubsegments = (JsonArray) json.get("subsegments");
List<TestSubsegment> subsegments = convertSubsegments(jsonSubsegments); List<TestSubsegment> subsegments = convertSubsegments(jsonSubsegments);
for (TestSubsegment subsegment : subsegments) { for (TestSubsegment subsegment : subsegments) {
segment.withSubsegment(subsegment); segment.withSubsegment(subsegment);
Expand All @@ -159,19 +170,19 @@ private TestSegment convertData(JSONObject json) {
return segment; return segment;
} }


private List<TestSubsegment> convertSubsegments(JSONArray jsonSubsegments) { private List<TestSubsegment> convertSubsegments(JsonArray jsonSubsegments) {
List<TestSubsegment> subsegments = new ArrayList<>(jsonSubsegments.length()); List<TestSubsegment> subsegments = new ArrayList<>(jsonSubsegments.size());
for (int i = 0; i < jsonSubsegments.length(); i++) { for (int i = 0; i < jsonSubsegments.size(); i++) {
JSONObject jsonSubsegment = jsonSubsegments.getJSONObject(i); JsonObject jsonSubsegment = jsonSubsegments.toArray(new JsonObject[jsonSubsegments.size()])[i];
subsegments.add(convertSubsegment(jsonSubsegment)); subsegments.add(convertSubsegment(jsonSubsegment));
} }
return subsegments; return subsegments;
} }


private TestSubsegment convertSubsegment(JSONObject json) { private TestSubsegment convertSubsegment(JsonObject json) {
TestSubsegment subsegment = new TestSubsegment(json.getString("name")); TestSubsegment subsegment = new TestSubsegment((String)json.get("name"));
if (json.has("subsegments")) { if (json.has("subsegments")) {
List<TestSubsegment> subsegments = convertSubsegments(json.getJSONArray("subsegments")); List<TestSubsegment> subsegments = convertSubsegments((JsonArray) json.get("subsegments"));
for (TestSubsegment tss : subsegments) { for (TestSubsegment tss : subsegments) {
subsegment.withSubsegment(tss); subsegment.withSubsegment(tss);
} }
Expand All @@ -181,18 +192,19 @@ private TestSubsegment convertSubsegment(JSONObject json) {
return subsegment; return subsegment;
} }


private void addAnnotationsIfAvailable(TestEntity<?> entity, JSONObject json) { private void addAnnotationsIfAvailable(TestEntity<?> entity, JsonObject json) {
if (json.has("annotations")) { if (json.has("annotations")) {
Map<String, Object> annotations = parseAnnotations(json.getJSONObject("annotations")); JsonObject annotations = (JsonObject) json.get("annotations");
for (String key : annotations.keySet()) { for (String key : annotations.getKeys()) {
entity.withAnnotation(key, annotations.get(key)); entity.withAnnotation((String)key, annotations.get(key));
} }
} }
} }


private void addMetadataIfAvailable(TestEntity<?> entity, JSONObject json) { private void addMetadataIfAvailable(TestEntity<?> entity, JsonObject json) {
if (json.has("metadata")) { if (json.has("metadata")) {
Map<String, Map<String, Object>> metadata = parseMetadata(json.getJSONObject("metadata")); JsonObject rawMetadata = (JsonObject) json.get("metadata");
Map<String, Map<String, Object>> metadata = parseMetadata(rawMetadata);
for (String namespace : metadata.keySet()) { for (String namespace : metadata.keySet()) {
for (String key : metadata.get(namespace).keySet()) { for (String key : metadata.get(namespace).keySet()) {
entity.withMetadata(namespace, key, metadata.get(namespace).get(key)); entity.withMetadata(namespace, key, metadata.get(namespace).get(key));
Expand All @@ -201,22 +213,7 @@ private void addMetadataIfAvailable(TestEntity<?> entity, JSONObject json) {
} }
} }


private Map<String, Object> parseAnnotations(JSONObject json) { private Map<String, Map<String, Object>> parseMetadata(JsonObject json) {
/*
"annotations" : {
"test2" : 1,
"test3" : true,
"test1" : "test"
}
*/
Map<String, Object> annotations = new LinkedHashMap<>(json.keySet().size());
for (String key : json.keySet()) {
annotations.put(key, json.get(key));
}
return annotations;
}

private Map<String, Map<String, Object>> parseMetadata(JSONObject json) {
/* /*
"metadata" : { "metadata" : {
"default" : { "default" : {
Expand All @@ -227,13 +224,13 @@ private Map<String, Map<String, Object>> parseMetadata(JSONObject json) {
} }
} }
*/ */
Map<String, Map<String, Object>> metadata = new LinkedHashMap<>(json.keySet().size()); Map<String, Map<String, Object>> metadata = new LinkedHashMap<>(json.getKeys().size());
for (String namespace : json.keySet()) { for (String namespace : json.getKeys()) {
JSONObject namespaceData = json.getJSONObject(namespace); JsonObject namespaceData = (JsonObject) json.get(namespace);
if (!metadata.containsKey(namespace)) { if (!metadata.containsKey(namespace)) {
metadata.put(namespace, new LinkedHashMap<>(namespaceData.keySet().size())); metadata.put(namespace, new LinkedHashMap<>(namespaceData.getKeys().size()));
} }
for (String key : namespaceData.keySet()) { for (String key : namespaceData.getKeys()) {
metadata.get(namespace).put(key, namespaceData.get(key)); metadata.get(namespace).put(key, namespaceData.get(key));
} }
} }
Expand Down
@@ -0,0 +1,23 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.apache.camel.component.aws.xray.json;

import java.util.ArrayList;

public class JsonArray extends ArrayList<JsonStructure> implements JsonStructure {

}
@@ -0,0 +1,93 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.apache.camel.component.aws.xray.json;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class JsonObject implements JsonStructure {

private Map<String, Object> data = new LinkedHashMap<>();

void addElement(String key, Object value) {
data.put(key, value);
}

public Object get(String key) {
return data.get(key);
}

public Set<String> getKeys() {
return data.keySet();
}

public boolean has(String key) {
return data.containsKey(key);
}

public String getString(String key) {
if (data.containsKey(key) && null != data.get(key)) {
return data.get(key).toString();
}
return null;
}

public Double getDouble(String key) {
if (data.containsKey(key) && null != data.get(key)) {
Object value = data.get(key);
if (value instanceof String) {
return Double.valueOf((String) value);
}
return (Double) value;
}
return 0D;
}

public Long getLong(String key) {
if (data.containsKey(key) && null != data.get(key)) {
Object value = data.get(key);
if (value instanceof String) {
return Long.valueOf((String) value);
}
return (Long) value;
}
return 0L;
}

public Integer getInteger(String key) {
if (data.containsKey(key) && null != data.get(key)) {
Object value = data.get(key);
if (value instanceof String) {
return Integer.valueOf((String) value);
}
return (Integer) value;
}
return 0;
}

public Boolean getBoolean(String key) {
if (data.containsKey(key) && null != data.get(key)) {
Object value = data.get(key);
if (value instanceof String) {
return Boolean.valueOf((String) value);
}
return (Boolean) value;
}
return null;
}
}

0 comments on commit 2cdaa43

Please sign in to comment.