SLING-6527 : Remove usage of org.json from metrics module
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1783721 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index d395d4c..7937633 100644
--- a/pom.xml
+++ b/pom.xml
@@ -18,148 +18,147 @@
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling</artifactId>
+ <version>29</version>
+ </parent>
- <modelVersion>4.0.0</modelVersion>
+ <artifactId>org.apache.sling.commons.metrics</artifactId>
+ <packaging>bundle</packaging>
+ <version>1.2.1-SNAPSHOT</version>
- <parent>
- <groupId>org.apache.sling</groupId>
- <artifactId>sling</artifactId>
- <version>29</version>
- </parent>
+ <name>Apache Sling Metrics</name>
+ <description>
+ Integrates Metric library with Sling. Refer to
+ https://sling.apache.org/documentation/bundles/metrics.html
+ for details
+ </description>
- <artifactId>org.apache.sling.commons.metrics</artifactId>
- <packaging>bundle</packaging>
- <version>1.2.1-SNAPSHOT</version>
+ <scm>
+ <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics</connection>
+ <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics</developerConnection>
+ <url>http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics</url>
+ </scm>
- <name>Apache Sling Metrics</name>
- <description>
- Integrates Metric library with Sling. Refer to
- https://sling.apache.org/documentation/bundles/metrics.html
- for details
- </description>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Bundle-DocURL>
+ https://sling.apache.org/documentation/bundles/metrics.html
+ </Bundle-DocURL>
+ <DynamicImport-Package>
+ javax.servlet,
+ javax.servlet.http;version="[2.3,3)",
+ org.apache.commons.io.output;version="[2.4.0,3)",
+ org.apache.felix.inventory;version="[1.0.0,2)"
+ </DynamicImport-Package>
+ <Embed-Dependency>
+ org.apache.felix.utils;inline=org/apache/felix/utils/json/JSONWriter**
+ </Embed-Dependency>
+ </instructions>
+ <!-- Export SCR metadata to classpath to have them available in unit tests -->
+ <exportScr>true</exportScr>
+ </configuration>
+ <executions>
+ <!-- Configure extra execution of 'manifest' in process-classes phase to make sure SCR metadata is generated before unit test runs -->
+ <execution>
+ <id>scr-metadata</id>
+ <goals>
+ <goal>manifest</goal>
+ </goals>
+ <configuration>
+ <supportIncrementalBuild>true</supportIncrementalBuild>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
- <scm>
- <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics</connection>
- <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics</developerConnection>
- <url>http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics</url>
- </scm>
+ <dependencies>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.dropwizard.metrics</groupId>
+ <artifactId>metrics-core</artifactId>
+ <version>3.1.0</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.cmpn</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.inventory</artifactId>
+ <version>1.0.2</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.2</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.utils</artifactId>
+ <version>1.9.0</version>
+ <optional>true</optional>
+ </dependency>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-bundle-plugin</artifactId>
- <extensions>true</extensions>
- <configuration>
- <instructions>
- <Bundle-DocURL>
- https://sling.apache.org/documentation/bundles/metrics.html
- </Bundle-DocURL>
- <DynamicImport-Package>
- javax.servlet,
- javax.servlet.http;version="[2.3,3)",
- org.apache.commons.io.output;version="[2.4.0,3)",
- org.apache.felix.inventory;version="[1.0.0,2)"
- </DynamicImport-Package>
- </instructions>
- <!-- Export SCR metadata to classpath to have them available in unit tests -->
- <exportScr>true</exportScr>
- </configuration>
- <executions>
- <!-- Configure extra execution of 'manifest' in process-classes phase to make sure SCR metadata is generated before unit test runs -->
- <execution>
- <id>scr-metadata</id>
- <goals>
- <goal>manifest</goal>
- </goals>
- <configuration>
- <supportIncrementalBuild>true</supportIncrementalBuild>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
-
- <dependencies>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- </dependency>
- <dependency>
- <groupId>io.dropwizard.metrics</groupId>
- <artifactId>metrics-core</artifactId>
- <version>3.1.0</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>osgi.core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>osgi.cmpn</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.inventory</artifactId>
- <version>1.0.2</version>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.2</version>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
- <version>20090211</version>
- <optional>true</optional>
- </dependency>
-
- <dependency>
- <groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-all</artifactId>
- <version>1.3</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
- <version>2.1.0</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-core</artifactId>
- <version>1.10.19</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-simple</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>net.sourceforge.htmlunit</groupId>
- <artifactId>htmlunit</artifactId>
- <version>2.12</version>
- <scope>test</scope>
- </dependency>
-
- </dependencies>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-all</artifactId>
+ <version>1.3</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
+ <version>2.1.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>1.10.19</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>net.sourceforge.htmlunit</groupId>
+ <artifactId>htmlunit</artifactId>
+ <version>2.12</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
</project>
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java b/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java
index 9c631f7..8e630d0 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java
@@ -20,6 +20,7 @@
package org.apache.sling.commons.metrics.internal;
import java.io.Closeable;
+import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Locale;
@@ -27,6 +28,8 @@
import java.util.SortedMap;
import java.util.concurrent.TimeUnit;
+import org.apache.felix.utils.json.JSONWriter;
+
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
@@ -37,8 +40,6 @@
import com.codahale.metrics.Reporter;
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;
-import org.json.JSONException;
-import org.json.JSONWriter;
class JSONReporter implements Reporter, Closeable {
@@ -147,7 +148,7 @@
registry.getHistograms(filter),
registry.getMeters(filter),
registry.getTimers(filter));
- } catch (JSONException e) {
+ } catch (IOException e) {
throw new RuntimeException(e);
}
}
@@ -159,7 +160,7 @@
private void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
- SortedMap<String, Timer> timers) throws JSONException {
+ SortedMap<String, Timer> timers) throws IOException {
json.object();
if (!gauges.isEmpty()) {
json.key("guages").object();
@@ -205,7 +206,7 @@
}
- private void printTimer(Map.Entry<String, Timer> e) throws JSONException {
+ private void printTimer(Map.Entry<String, Timer> e) throws IOException {
json.key(e.getKey()).object();
Timer timer = e.getValue();
Snapshot snapshot = timer.getSnapshot();
@@ -233,7 +234,7 @@
json.endObject();
}
- private void printMeter(Map.Entry<String, Meter> e) throws JSONException {
+ private void printMeter(Map.Entry<String, Meter> e) throws IOException {
json.key(e.getKey()).object();
Meter meter = e.getValue();
json.key("count").value(e.getValue().getCount());
@@ -245,7 +246,7 @@
json.endObject();
}
- private void printHistogram(Map.Entry<String, Histogram> e) throws JSONException {
+ private void printHistogram(Map.Entry<String, Histogram> e) throws IOException {
json.key(e.getKey()).object();
json.key("count").value(e.getValue().getCount());
@@ -264,13 +265,13 @@
json.endObject();
}
- private void printCounter(Map.Entry<String, Counter> e) throws JSONException {
+ private void printCounter(Map.Entry<String, Counter> e) throws IOException {
json.key(e.getKey()).object();
json.key("count").value(e.getValue().getCount());
json.endObject();
}
- private void printGauge(Map.Entry<String, Gauge> e) throws JSONException {
+ private void printGauge(Map.Entry<String, Gauge> e) throws IOException {
json.key(e.getKey()).object();
Object v = e.getValue().getValue();
json.key("value").value(jsonSafeValue(v));
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/JSONReporterTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/JSONReporterTest.java
index 1f13872..49a6b46 100644
--- a/src/test/java/org/apache/sling/commons/metrics/internal/JSONReporterTest.java
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/JSONReporterTest.java
@@ -19,21 +19,24 @@
package org.apache.sling.commons.metrics.internal;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
import java.io.PrintStream;
import java.io.StringWriter;
+import java.util.Map;
+
+import org.apache.commons.io.output.WriterOutputStream;
+import org.apache.felix.utils.json.JSONParser;
+import org.junit.Test;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.JvmAttributeGaugeSet;
import com.codahale.metrics.MetricRegistry;
-import org.apache.commons.io.output.WriterOutputStream;
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.junit.Test;
-
-import static org.junit.Assert.assertTrue;
public class JSONReporterTest {
+ @SuppressWarnings("unchecked")
@Test
public void jsonOutput() throws Exception {
MetricRegistry registry = new MetricRegistry();
@@ -43,21 +46,22 @@
registry.counter("test4").inc(9);
registry.registerAll(new JvmAttributeGaugeSet());
- JSONObject json = getJSON(registry);
+ Map<String, Object> json = getJSON(registry);
- assertTrue(json.has("meters"));
- assertTrue(json.has("guages"));
- assertTrue(json.has("timers"));
- assertTrue(json.has("counters"));
- assertTrue(json.has("histograms"));
- assertTrue(json.has("meters"));
+ assertTrue(json.containsKey("meters"));
+ assertTrue(json.containsKey("guages"));
+ assertTrue(json.containsKey("timers"));
+ assertTrue(json.containsKey("counters"));
+ assertTrue(json.containsKey("histograms"));
+ assertTrue(json.containsKey("meters"));
- assertTrue(json.getJSONObject("meters").has("test1"));
- assertTrue(json.getJSONObject("timers").has("test2"));
- assertTrue(json.getJSONObject("counters").has("test4"));
- assertTrue(json.getJSONObject("histograms").has("test3"));
+ assertTrue(((Map<String, Object>)json.get("meters")).containsKey("test1"));
+ assertTrue(((Map<String, Object>)json.get("timers")).containsKey("test2"));
+ assertTrue(((Map<String, Object>)json.get("counters")).containsKey("test4"));
+ assertTrue(((Map<String, Object>)json.get("histograms")).containsKey("test3"));
}
+ @SuppressWarnings("unchecked")
@Test
public void nan_value() throws Exception{
MetricRegistry registry = new MetricRegistry();
@@ -70,18 +74,18 @@
});
- JSONObject json = getJSON(registry);
- assertTrue(json.getJSONObject("guages").has("test"));
+ Map<String, Object> json = getJSON(registry);
+ assertTrue(((Map<String, Object>)json.get("guages")).containsKey("test"));
}
- private static JSONObject getJSON(MetricRegistry registry) throws JSONException {
+ private static Map<String, Object> getJSON(MetricRegistry registry) throws IOException {
StringWriter sw = new StringWriter();
JSONReporter reporter = JSONReporter.forRegistry(registry)
.outputTo(new PrintStream(new WriterOutputStream(sw)))
.build();
reporter.report();
reporter.close();
- return new JSONObject(sw.toString());
+ return new JSONParser(sw.toString()).getParsed();
}
}
\ No newline at end of file