Skip to content

Commit

Permalink
Replaced Json Library (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
yush1ga authored and merlimat committed Dec 1, 2016
1 parent 5af7d3f commit 6ea847c
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 98 deletions.
8 changes: 1 addition & 7 deletions pom.xml
Expand Up @@ -227,16 +227,10 @@ flexible messaging model and an intuitive client API.</description>
<version>3.10.1.Final</version>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
<version>2.8.0</version>
</dependency>

<dependency>
Expand Down
4 changes: 2 additions & 2 deletions pulsar-broker/pom.xml
Expand Up @@ -194,8 +194,8 @@
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<dependency>
Expand Down
Expand Up @@ -19,7 +19,8 @@
import com.yahoo.pulsar.broker.ServiceConfiguration;
import com.yahoo.pulsar.zookeeper.LocalBookkeeperEnsemble;
import org.apache.zookeeper.data.Stat;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -67,9 +68,9 @@ public void testAdvertisedAddress() throws Exception {
Assert.assertEquals( pulsar.getWebServiceAddress(), String.format("http://%s:%d", advertisedAddress, BROKER_WEBSERVICE_PORT) );
String brokerZkPath = String.format("/loadbalance/brokers/%s:%d", pulsar.getAdvertisedAddress(), BROKER_WEBSERVICE_PORT);
String bkBrokerData = new String(bkEnsemble.getZkClient().getData(brokerZkPath, false, new Stat()), StandardCharsets.UTF_8);
JSONObject jsonBkBrokerData = new JSONObject(bkBrokerData);
Assert.assertEquals( jsonBkBrokerData.get("pulsarServiceUrl"), pulsar.getBrokerServiceUrl() );
Assert.assertEquals( jsonBkBrokerData.get("webServiceUrl"), pulsar.getWebServiceAddress() );
JsonObject jsonBkBrokerData = new Gson().fromJson(bkBrokerData, JsonObject.class);
Assert.assertEquals( jsonBkBrokerData.get("pulsarServiceUrl").getAsString(), pulsar.getBrokerServiceUrl() );
Assert.assertEquals( jsonBkBrokerData.get("webServiceUrl").getAsString(), pulsar.getWebServiceAddress() );
}

}
Expand Up @@ -30,9 +30,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -301,23 +300,23 @@ public void testBrokerStatsMetrics() throws Exception {
}
consumer.close();
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
JSONArray metrics = brokerStatsClient.getMetrics();
assertEquals(metrics.length(), 4, metrics.toString());
JsonArray metrics = brokerStatsClient.getMetrics();
assertEquals(metrics.size(), 4, metrics.toString());

// these metrics seem to be arriving in different order at different times...
// is the order really relevant here?
boolean namespaceDimensionFound = false;
boolean topicLoadTimesDimensionFound = false;
for ( int i=0; i<metrics.length(); i++ ) {
for ( int i=0; i<metrics.size(); i++ ) {
try {
String data = metrics.getJSONObject(i).getString("dimensions");
String data = metrics.get(i).getAsJsonObject().get("dimensions").toString();
if (!namespaceDimensionFound && data.contains("prop/use/ns-abc")) {
namespaceDimensionFound = true;
}
if (!topicLoadTimesDimensionFound && data.contains("prop/use/ns-abc")) {
topicLoadTimesDimensionFound = true;
}
} catch (JSONException e) { /* it's possible there's no dimensions */ }
} catch (Exception e) { /* it's possible there's no dimensions */ }
}

assertTrue(namespaceDimensionFound && topicLoadTimesDimensionFound);
Expand Down Expand Up @@ -346,20 +345,20 @@ public void testBrokerServiceNamespaceStats() throws Exception {
}

rolloverPerIntervalStats();
JSONObject destinationStats = brokerStatsClient.getDestinations();
assertEquals(destinationStats.length(), 2, destinationStats.toString());
JsonObject destinationStats = brokerStatsClient.getDestinations();
assertEquals(destinationStats.size(), 2, destinationStats.toString());

for (String ns : nsList) {
JSONObject nsObject = destinationStats.getJSONObject(ns);
JsonObject nsObject = destinationStats.getAsJsonObject(ns);
List<String> topicList = admin.namespaces().getDestinations(ns);
for (String topic : topicList) {
NamespaceBundle bundle = (NamespaceBundle) pulsar.getNamespaceService()
.getBundle(DestinationName.get(topic));
JSONObject bundleObject = nsObject.getJSONObject(bundle.getBundleRange());
JSONObject topicObject = bundleObject.getJSONObject("persistent");
JsonObject bundleObject = nsObject.getAsJsonObject(bundle.getBundleRange());
JsonObject topicObject = bundleObject.getAsJsonObject("persistent");
AtomicBoolean topicPresent = new AtomicBoolean();
topicObject.keys().forEachRemaining(persistentTopic -> {
if (persistentTopic.equals(topic)) {
topicObject.entrySet().iterator().forEachRemaining(persistentTopic -> {
if (persistentTopic.getKey().equals(topic)) {
topicPresent.set(true);
}
});
Expand Down
Expand Up @@ -35,8 +35,9 @@
import org.apache.zookeeper.ZooKeeper;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.filter.LoggingFilter;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonObject;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -103,7 +104,7 @@ public void testRiderectUrlWithServerStarted() throws Exception {

}

public String hitBrokerService(String method, String url, Object data) throws JSONException {
public String hitBrokerService(String method, String url, Object data) throws JsonParseException {

Response response = null;
try {
Expand All @@ -123,8 +124,8 @@ public String hitBrokerService(String method, String url, Object data) throws JS
fail();
}

JSONObject jsonObject = new JSONObject(response.readEntity(String.class));
String serviceResponse = jsonObject.getString("reason");
JsonObject jsonObject = new Gson().fromJson(response.readEntity(String.class), JsonObject.class);
String serviceResponse = jsonObject.get("reason").getAsString();
return serviceResponse;
}

Expand Down
Expand Up @@ -26,8 +26,10 @@
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -63,12 +65,12 @@ public void onConnect(Session session) throws InterruptedException {
}

@OnWebSocketMessage
public void onMessage(String msg) throws JSONException, IOException {
JSONObject message = new JSONObject(msg);
JSONObject ack = new JSONObject();
String messageId = message.getString(X_PULSAR_MESSAGE_ID).toString();
public void onMessage(String msg) throws JsonParseException, IOException {
JsonObject message = new Gson().fromJson(msg, JsonObject.class);
JsonObject ack = new JsonObject();
String messageId = message.get(X_PULSAR_MESSAGE_ID).getAsString();
consumerBuffer.add(messageId);
ack.put("messageId", messageId);
ack.add("messageId", new JsonPrimitive(messageId));
// Acking the proxy
this.getRemote().sendString(ack.toString());
}
Expand Down
Expand Up @@ -27,8 +27,9 @@
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -67,7 +68,7 @@ public void onClose(int statusCode, String reason) {
}

@OnWebSocketConnect
public void onConnect(Session session) throws InterruptedException, IOException, JSONException {
public void onConnect(Session session) throws InterruptedException, IOException, JsonParseException {
log.info("Got connect: {}", session);
this.session = session;
for (int i = 0; i < 10; i++) {
Expand All @@ -77,9 +78,9 @@ public void onConnect(Session session) throws InterruptedException, IOException,
}

@OnWebSocketMessage
public void onMessage(String msg) throws JSONException {
JSONObject ack = new JSONObject(msg);
producerBuffer.add((String) ack.get("messageId"));
public void onMessage(String msg) throws JsonParseException {
JsonObject ack = new Gson().fromJson(msg, JsonObject.class);
producerBuffer.add(ack.get("messageId").getAsString());
}

public RemoteEndpoint getRemote() {
Expand Down
9 changes: 2 additions & 7 deletions pulsar-client-admin/pom.xml
Expand Up @@ -69,8 +69,8 @@
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
<build>
Expand Down Expand Up @@ -99,7 +99,6 @@
<include>com.google.protobuf:protobuf-java</include>
<include>com.google.guava:guava</include>
<include>com.google.code.gson:gson</include>
<include>org.json:json</include>
<include>io.netty:netty</include>
<include>io.netty:netty-all</include>
</includes>
Expand All @@ -117,10 +116,6 @@
<pattern>com.google</pattern>
<shadedPattern>pulsar-admin-shade.com.google</shadedPattern>
</relocation>
<relocation>
<pattern>org.json</pattern>
<shadedPattern>pulsar-admin-shade.org.json</shadedPattern>
</relocation>
<relocation>
<pattern>org.jboss.netty</pattern>
<shadedPattern>pulsar-admin-shade.org.jboss.netty</shadedPattern>
Expand Down
Expand Up @@ -15,8 +15,8 @@
*/
package com.yahoo.pulsar.client.admin;

import org.json.JSONArray;
import org.json.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import com.yahoo.pulsar.common.stats.AllocatorStats;

Expand All @@ -32,7 +32,7 @@ public interface BrokerStats {
* @throws PulsarAdminException
*/

JSONArray getMetrics() throws PulsarAdminException;
JsonArray getMetrics() throws PulsarAdminException;

/**
* Requests JSON string server mbean dump
Expand All @@ -42,7 +42,7 @@ public interface BrokerStats {
* @return
* @throws PulsarAdminException
*/
JSONArray getMBeans() throws PulsarAdminException;
JsonArray getMBeans() throws PulsarAdminException;

/**
* Returns JSON string destination stats
Expand All @@ -52,9 +52,9 @@ public interface BrokerStats {
* @return
* @throws PulsarAdminException
*/
JSONObject getDestinations() throws PulsarAdminException;
JsonObject getDestinations() throws PulsarAdminException;

JSONObject getPendingBookieOpsStats() throws PulsarAdminException;
JsonObject getPendingBookieOpsStats() throws PulsarAdminException;

AllocatorStats getAllocatorStats(String allocatorName) throws PulsarAdminException;
}
Expand Up @@ -17,8 +17,9 @@

import javax.ws.rs.client.WebTarget;

import org.json.JSONArray;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import com.yahoo.pulsar.client.admin.BrokerStats;
import com.yahoo.pulsar.client.admin.PulsarAdminException;
Expand All @@ -40,10 +41,10 @@ public BrokerStatsImpl(WebTarget target, Authentication auth) {
}

@Override
public JSONArray getMetrics() throws PulsarAdminException {
public JsonArray getMetrics() throws PulsarAdminException {
try {
String json = request(brokerStats.path("/metrics")).get(String.class);
return new JSONArray(json);
return new Gson().fromJson(json, JsonArray.class);
} catch (Exception e) {
throw getApiException(e);
}
Expand All @@ -59,51 +60,51 @@ public AllocatorStats getAllocatorStats(String allocatorName) throws PulsarAdmin
}

@Override
public JSONArray getMBeans() throws PulsarAdminException {
public JsonArray getMBeans() throws PulsarAdminException {
try {
String json = request(brokerStats.path("/mbeans")).get(String.class);
return new JSONArray(json);
return new Gson().fromJson(json, JsonArray.class);
} catch (Exception e) {
throw getApiException(e);
}
}

@Override
public JSONObject getDestinations() throws PulsarAdminException {
public JsonObject getDestinations() throws PulsarAdminException {
try {
String json = request(brokerStats.path("/destinations")).get(String.class);
return new JSONObject(json);
return new Gson().fromJson(json, JsonObject.class);
} catch (Exception e) {
throw getApiException(e);
}
}

public JSONObject getLoadReport() throws PulsarAdminException {
public JsonObject getLoadReport() throws PulsarAdminException {
try {
String json = request(brokerStats.path("/load-report")).get(String.class);
return new JSONObject(json);
return new Gson().fromJson(json, JsonObject.class);
} catch (Exception e) {
throw getApiException(e);
}
}

@Override
public JSONObject getPendingBookieOpsStats() throws PulsarAdminException {
public JsonObject getPendingBookieOpsStats() throws PulsarAdminException {
try {
String json = request(brokerStats.path("/bookieops")).get(String.class);
return new JSONObject(json);
return new Gson().fromJson(json, JsonObject.class);
} catch (Exception e) {
throw getApiException(e);
}
}

public JSONObject getBrokerResourceAvailability(String property, String cluster, String namespace)
public JsonObject getBrokerResourceAvailability(String property, String cluster, String namespace)
throws PulsarAdminException {
try {
String json = request(
brokerStats.path("/broker-resource-availability").path(property).path(cluster).path(namespace))
.get(String.class);
return new JSONObject(json);
return new Gson().fromJson(json, JsonObject.class);
} catch (Exception e) {
throw getApiException(e);
}
Expand Down

0 comments on commit 6ea847c

Please sign in to comment.