From 39e99cc5f049aca32dbc9ce25f28b85500b843f1 Mon Sep 17 00:00:00 2001 From: Abraham Elmahrek Date: Wed, 26 Nov 2014 02:47:11 -0800 Subject: [PATCH] SQOOP-1786: Sqoop2: Stop using JSONValue.parse method call (Jarek Jarcec Cecho via Abraham Elmahrek) --- .../request/ConnectorResourceRequest.java | 4 +- .../client/request/DriverResourceRequest.java | 4 +- .../client/request/JobResourceRequest.java | 10 +-- .../client/request/LinkResourceRequest.java | 8 +-- .../sqoop/client/request/ResourceRequest.java | 4 +- .../request/SubmissionResourceRequest.java | 4 +- .../request/VersionResourceRequest.java | 4 +- .../java/org/apache/sqoop/json/JSONUtils.java | 70 +++++++++++++++++++ .../sqoop/json/util/SerializationError.java | 2 + .../org/apache/sqoop/model/ConfigUtils.java | 4 +- .../apache/sqoop/json/TestConnectorBean.java | 3 +- .../apache/sqoop/json/TestConnectorsBean.java | 7 +- .../org/apache/sqoop/json/TestDriverBean.java | 3 +- .../org/apache/sqoop/json/TestJSONUtils.java | 64 +++++++++++++++++ .../org/apache/sqoop/json/TestJobBean.java | 3 +- .../org/apache/sqoop/json/TestJobsBean.java | 3 +- .../org/apache/sqoop/json/TestLinkBean.java | 3 +- .../org/apache/sqoop/json/TestLinksBean.java | 3 +- .../org/apache/sqoop/json/TestSchemaBean.java | 3 +- .../apache/sqoop/json/TestSubmissionBean.java | 5 +- .../apache/sqoop/json/TestThrowableBean.java | 3 +- .../sqoop/json/TestValidationResultBean.java | 5 +- .../json/util/TestConfigSerialization.java | 8 +-- .../json/util/TestSchemaSerialization.java | 6 +- .../sqoop/job/mr/MRConfigurationUtils.java | 4 +- .../sqoop/handler/JobRequestHandler.java | 4 +- .../sqoop/handler/LinkRequestHandler.java | 4 +- .../sqoop/tools/tool/RepositoryLoadTool.java | 4 +- 28 files changed, 187 insertions(+), 62 deletions(-) create mode 100644 common/src/main/java/org/apache/sqoop/json/JSONUtils.java create mode 100644 common/src/test/java/org/apache/sqoop/json/TestJSONUtils.java diff --git a/client/src/main/java/org/apache/sqoop/client/request/ConnectorResourceRequest.java b/client/src/main/java/org/apache/sqoop/client/request/ConnectorResourceRequest.java index 5d41b3f1..c6232d0b 100644 --- a/client/src/main/java/org/apache/sqoop/client/request/ConnectorResourceRequest.java +++ b/client/src/main/java/org/apache/sqoop/client/request/ConnectorResourceRequest.java @@ -19,8 +19,8 @@ import org.apache.sqoop.json.ConnectorBean; import org.apache.sqoop.json.ConnectorsBean; +import org.apache.sqoop.json.JSONUtils; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; /** * Provide Read semantics over RESTfull HTTP API for connectors. Only read is @@ -37,7 +37,7 @@ public ConnectorBean read(String serverUrl, Long cid) { } else { response = super.get(serverUrl + RESOURCE + cid); } - JSONObject jsonObject = (JSONObject) JSONValue.parse(response); + JSONObject jsonObject = JSONUtils.parse(response); // defaults to all ConnectorBean bean = new ConnectorsBean(); if (cid != null) { diff --git a/client/src/main/java/org/apache/sqoop/client/request/DriverResourceRequest.java b/client/src/main/java/org/apache/sqoop/client/request/DriverResourceRequest.java index ca82f59c..95bb12cb 100644 --- a/client/src/main/java/org/apache/sqoop/client/request/DriverResourceRequest.java +++ b/client/src/main/java/org/apache/sqoop/client/request/DriverResourceRequest.java @@ -18,8 +18,8 @@ package org.apache.sqoop.client.request; import org.apache.sqoop.json.DriverBean; +import org.apache.sqoop.json.JSONUtils; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; /** * Provide read semantics over RESTfull HTTP API for driver @@ -31,7 +31,7 @@ public class DriverResourceRequest extends ResourceRequest { public DriverBean read(String serverUrl) { String response = null; response = super.get(serverUrl + RESOURCE); - JSONObject jsonObject = (JSONObject) JSONValue.parse(response); + JSONObject jsonObject = JSONUtils.parse(response); DriverBean driverBean = new DriverBean(); driverBean.restore(jsonObject); return driverBean; diff --git a/client/src/main/java/org/apache/sqoop/client/request/JobResourceRequest.java b/client/src/main/java/org/apache/sqoop/client/request/JobResourceRequest.java index 55c8db22..167e8be4 100644 --- a/client/src/main/java/org/apache/sqoop/client/request/JobResourceRequest.java +++ b/client/src/main/java/org/apache/sqoop/client/request/JobResourceRequest.java @@ -17,13 +17,13 @@ */ package org.apache.sqoop.client.request; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.json.JobBean; import org.apache.sqoop.json.JobsBean; import org.apache.sqoop.json.SubmissionBean; import org.apache.sqoop.json.ValidationResultBean; import org.apache.sqoop.model.MJob; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; /** * Provide CRUD semantics over RESTfull HTTP API for jobs. All operations are @@ -46,7 +46,7 @@ public JobBean read(String serverUrl, Long jobId) { } else { response = super.get(serverUrl + RESOURCE + jobId); } - JSONObject jsonObject = (JSONObject) JSONValue.parse(response); + JSONObject jsonObject = JSONUtils.parse(response); // defaults to all JobBean bean = new JobsBean(); if (jobId != null) { @@ -62,7 +62,7 @@ public ValidationResultBean create(String serverUrl, MJob job) { JSONObject jobJson = jobBean.extract(false); String response = super.post(serverUrl + RESOURCE, jobJson.toJSONString()); ValidationResultBean validationResultBean = new ValidationResultBean(); - validationResultBean.restore((JSONObject) JSONValue.parse(response)); + validationResultBean.restore(JSONUtils.parse(response)); return validationResultBean; } @@ -73,7 +73,7 @@ public ValidationResultBean update(String serverUrl, MJob job) { String response = super.put(serverUrl + RESOURCE + job.getPersistenceId(), jobJson.toJSONString()); ValidationResultBean validationBean = new ValidationResultBean(); - validationBean.restore((JSONObject) JSONValue.parse(response)); + validationBean.restore(JSONUtils.parse(response)); return validationBean; } @@ -106,7 +106,7 @@ public SubmissionBean status(String serverUrl, Long jobId) { private SubmissionBean createJobSubmissionResponse(String response) { SubmissionBean submissionBean = new SubmissionBean(); - submissionBean.restore((JSONObject) JSONValue.parse(response)); + submissionBean.restore(JSONUtils.parse(response)); return submissionBean; } } diff --git a/client/src/main/java/org/apache/sqoop/client/request/LinkResourceRequest.java b/client/src/main/java/org/apache/sqoop/client/request/LinkResourceRequest.java index f03acb40..9a4a134f 100644 --- a/client/src/main/java/org/apache/sqoop/client/request/LinkResourceRequest.java +++ b/client/src/main/java/org/apache/sqoop/client/request/LinkResourceRequest.java @@ -17,12 +17,12 @@ */ package org.apache.sqoop.client.request; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.json.LinkBean; import org.apache.sqoop.json.LinksBean; import org.apache.sqoop.json.ValidationResultBean; import org.apache.sqoop.model.MLink; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; /** * Provide CRUD semantics over RESTfull HTTP API for links. All operations @@ -42,7 +42,7 @@ public LinkBean read(String serverUrl, Long linkId) { } else { response = super.get(serverUrl + LINK_RESOURCE + linkId); } - JSONObject jsonObject = (JSONObject) JSONValue.parse(response); + JSONObject jsonObject = JSONUtils.parse(response); // defaults to all LinkBean bean = new LinksBean(); if (linkId != null) { @@ -58,7 +58,7 @@ public ValidationResultBean create(String serverUrl, MLink link) { JSONObject linkJson = linkBean.extract(false); String response = super.post(serverUrl + LINK_RESOURCE, linkJson.toJSONString()); ValidationResultBean validationBean = new ValidationResultBean(); - validationBean.restore((JSONObject) JSONValue.parse(response)); + validationBean.restore(JSONUtils.parse(response)); return validationBean; } @@ -68,7 +68,7 @@ public ValidationResultBean update(String serverUrl, MLink link) { JSONObject linkJson = linkBean.extract(false); String response = super.put(serverUrl + LINK_RESOURCE + link.getPersistenceId(), linkJson.toJSONString()); ValidationResultBean validationBean = new ValidationResultBean(); - validationBean.restore((JSONObject) JSONValue.parse(response)); + validationBean.restore(JSONUtils.parse(response)); return validationBean; } diff --git a/client/src/main/java/org/apache/sqoop/client/request/ResourceRequest.java b/client/src/main/java/org/apache/sqoop/client/request/ResourceRequest.java index 3c4a4d23..6be1db87 100644 --- a/client/src/main/java/org/apache/sqoop/client/request/ResourceRequest.java +++ b/client/src/main/java/org/apache/sqoop/client/request/ResourceRequest.java @@ -23,9 +23,9 @@ import org.apache.sqoop.client.ClientError; import org.apache.sqoop.common.SqoopException; import org.apache.sqoop.common.SqoopProtocolConstants; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.json.ThrowableBean; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import javax.ws.rs.HttpMethod; import javax.ws.rs.core.MediaType; @@ -107,7 +107,7 @@ protected String doHttpRequest(String strURL, String method, String data) { } reader.close(); - JSONObject json = (JSONObject) JSONValue.parse(result.toString()); + JSONObject json = JSONUtils.parse(result.toString()); ex.restore(json); throw new SqoopException(ClientError.CLIENT_0001, ex.getThrowable()); diff --git a/client/src/main/java/org/apache/sqoop/client/request/SubmissionResourceRequest.java b/client/src/main/java/org/apache/sqoop/client/request/SubmissionResourceRequest.java index e3b70bc0..47a0a941 100644 --- a/client/src/main/java/org/apache/sqoop/client/request/SubmissionResourceRequest.java +++ b/client/src/main/java/org/apache/sqoop/client/request/SubmissionResourceRequest.java @@ -17,9 +17,9 @@ */ package org.apache.sqoop.client.request; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.json.SubmissionsBean; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; /** * Provide CRD semantics over RESTfull HTTP API for submissions. Please note @@ -36,7 +36,7 @@ public SubmissionsBean read(String serverUrl, Long jid) { } else { response = super.get(serverUrl + RESOURCE + jid); } - JSONObject jsonObject = (JSONObject) JSONValue.parse(response); + JSONObject jsonObject = JSONUtils.parse(response); SubmissionsBean submissionBean = new SubmissionsBean(); submissionBean.restore(jsonObject); return submissionBean; diff --git a/client/src/main/java/org/apache/sqoop/client/request/VersionResourceRequest.java b/client/src/main/java/org/apache/sqoop/client/request/VersionResourceRequest.java index cdd2a9b9..7bd0dba7 100644 --- a/client/src/main/java/org/apache/sqoop/client/request/VersionResourceRequest.java +++ b/client/src/main/java/org/apache/sqoop/client/request/VersionResourceRequest.java @@ -17,15 +17,15 @@ */ package org.apache.sqoop.client.request; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.json.VersionBean; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; public class VersionResourceRequest extends ResourceRequest { public VersionBean read(String serverUrl) { String response = super.get(serverUrl + "version"); - JSONObject jsonObject = (JSONObject)JSONValue.parse(response); + JSONObject jsonObject = JSONUtils.parse(response); VersionBean versionBean = new VersionBean(); versionBean.restore(jsonObject); diff --git a/common/src/main/java/org/apache/sqoop/json/JSONUtils.java b/common/src/main/java/org/apache/sqoop/json/JSONUtils.java new file mode 100644 index 00000000..e7551d2c --- /dev/null +++ b/common/src/main/java/org/apache/sqoop/json/JSONUtils.java @@ -0,0 +1,70 @@ +/** + * 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.sqoop.json; + +import org.apache.sqoop.common.SqoopException; +import org.apache.sqoop.json.util.SerializationError; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import org.json.simple.parser.ParseException; + +import java.io.IOException; +import java.io.Reader; + +/** + */ +public class JSONUtils { + + /** + * Parse given string as JSON and return corresponding JSONObject. + * + * This method will throw SqoopException on any parsing error. + * + * @param input JSON encoded String + * @return + */ + public static JSONObject parse(String input) { + try { + return (JSONObject) JSONValue.parseWithException(input); + } catch (ParseException e) { + throw new SqoopException(SerializationError.SERIALIZATION_002, e); + } + } + + /** + * Parse given reader as JSON and return corresponding JSONObject. + * + * This method will throw SqoopException on any parsing error. + * + * @param reader + * @return + */ + public static JSONObject parse(Reader reader) { + try { + return (JSONObject) JSONValue.parseWithException(reader); + } catch (ParseException e) { + throw new SqoopException(SerializationError.SERIALIZATION_002, e); + } catch (IOException e) { + throw new SqoopException(SerializationError.SERIALIZATION_002, e); + } + } + + private JSONUtils() { + // Instantiation is prohibited + } +} diff --git a/common/src/main/java/org/apache/sqoop/json/util/SerializationError.java b/common/src/main/java/org/apache/sqoop/json/util/SerializationError.java index 13989a37..3d440da4 100644 --- a/common/src/main/java/org/apache/sqoop/json/util/SerializationError.java +++ b/common/src/main/java/org/apache/sqoop/json/util/SerializationError.java @@ -23,6 +23,8 @@ public enum SerializationError implements ErrorCode { SERIALIZATION_001("Attempt to pass a non-map object to MAP type."), + SERIALIZATION_002("Invalid JSON"), + ; private final String message; diff --git a/common/src/main/java/org/apache/sqoop/model/ConfigUtils.java b/common/src/main/java/org/apache/sqoop/model/ConfigUtils.java index 40db6f49..3765a6db 100644 --- a/common/src/main/java/org/apache/sqoop/model/ConfigUtils.java +++ b/common/src/main/java/org/apache/sqoop/model/ConfigUtils.java @@ -19,12 +19,12 @@ import org.apache.commons.lang.StringUtils; import org.apache.sqoop.common.SqoopException; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.utils.ClassUtils; import org.apache.sqoop.validation.ConfigValidationRunner; import org.apache.sqoop.validation.Message; import org.apache.sqoop.validation.ConfigValidationResult; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import java.lang.reflect.Field; import java.util.HashMap; @@ -456,7 +456,7 @@ public static void fillValues(String json, Object configuration) { Class klass = configuration.getClass(); Set configNames = new HashSet(); - JSONObject jsonConfigs = (JSONObject) JSONValue.parse(json); + JSONObject jsonConfigs = JSONUtils.parse(json); for(Field configField : klass.getDeclaredFields()) { configField.setAccessible(true); diff --git a/common/src/test/java/org/apache/sqoop/json/TestConnectorBean.java b/common/src/test/java/org/apache/sqoop/json/TestConnectorBean.java index bc73f1e2..44d7dd99 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestConnectorBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestConnectorBean.java @@ -30,7 +30,6 @@ import org.apache.sqoop.json.util.ConfigTestUtil; import org.apache.sqoop.model.MConnector; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.junit.Test; public class TestConnectorBean { @@ -57,7 +56,7 @@ public void testConnectorSerialization() { String connectorJSONString = connectorJSON.toJSONString(); // Retrieved transferred object - JSONObject parsedConnectors = (JSONObject) JSONValue.parse(connectorJSONString); + JSONObject parsedConnectors = JSONUtils.parse(connectorJSONString); ConnectorBean parsedConnectorBean = new ConnectorBean(); parsedConnectorBean.restore(parsedConnectors); assertEquals(connectors.size(), 1); diff --git a/common/src/test/java/org/apache/sqoop/json/TestConnectorsBean.java b/common/src/test/java/org/apache/sqoop/json/TestConnectorsBean.java index c76e4072..b4370e64 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestConnectorsBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestConnectorsBean.java @@ -30,7 +30,6 @@ import org.apache.sqoop.json.util.ConfigTestUtil; import org.apache.sqoop.model.MConnector; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.junit.Test; public class TestConnectorsBean { @@ -55,7 +54,7 @@ public void testConnectorsSerialization() { String connectorsJSONString = connectorsJSON.toJSONString(); // Retrieved transferred object - JSONObject parsedConnectors = (JSONObject) JSONValue.parse(connectorsJSONString); + JSONObject parsedConnectors = JSONUtils.parse(connectorsJSONString); ConnectorsBean parsedConnectorsBean = new ConnectorsBean(); parsedConnectorsBean.restore(parsedConnectors); @@ -89,7 +88,7 @@ public void testSingleDirection() { String string = json.toJSONString(); // Retrieved transferred object - JSONObject retrievedJson = (JSONObject) JSONValue.parse(string); + JSONObject retrievedJson = JSONUtils.parse(string); ConnectorsBean retrievedBean = new ConnectorsBean(); retrievedBean.restore(retrievedJson); @@ -118,7 +117,7 @@ public void testNoDirection() { String string = json.toJSONString(); // Retrieved transferred object - JSONObject retrievedJson = (JSONObject) JSONValue.parse(string); + JSONObject retrievedJson = JSONUtils.parse(string); ConnectorsBean retrievedBean = new ConnectorsBean(); retrievedBean.restore(retrievedJson); diff --git a/common/src/test/java/org/apache/sqoop/json/TestDriverBean.java b/common/src/test/java/org/apache/sqoop/json/TestDriverBean.java index 0c7f8bf2..b72c432b 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestDriverBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestDriverBean.java @@ -24,7 +24,6 @@ import org.apache.sqoop.json.util.ConfigTestUtil; import org.apache.sqoop.model.MDriver; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.junit.Test; /** @@ -48,7 +47,7 @@ public void testSerialization() { String string = json.toJSONString(); // Retrieved transferred object - JSONObject retrievedJson = (JSONObject) JSONValue.parse(string); + JSONObject retrievedJson = JSONUtils.parse(string); DriverBean retrievedBean = new DriverBean(); retrievedBean.restore(retrievedJson); diff --git a/common/src/test/java/org/apache/sqoop/json/TestJSONUtils.java b/common/src/test/java/org/apache/sqoop/json/TestJSONUtils.java new file mode 100644 index 00000000..7efee21f --- /dev/null +++ b/common/src/test/java/org/apache/sqoop/json/TestJSONUtils.java @@ -0,0 +1,64 @@ +/** + * 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.sqoop.json; + +import org.apache.sqoop.common.SqoopException; +import org.json.simple.JSONObject; +import org.junit.Test; + +import java.io.Reader; +import java.io.StringReader; + +import static org.junit.Assert.assertEquals; + +/** + */ +public class TestJSONUtils { + + @Test + public void testString() { + JSONObject object = JSONUtils.parse("{\"id\":3}"); + assertEquals((long)3, object.get("id")); + } + + @Test(expected = SqoopException.class) + public void testStringInvalid() { + JSONUtils.parse("{"); + } + + @Test(expected = NullPointerException.class) + public void testStringNull() { + JSONUtils.parse((String)null); + } + + @Test + public void testReader() { + JSONObject object = JSONUtils.parse(new StringReader("{\"id\":3}")); + assertEquals((long)3, object.get("id")); + } + + @Test(expected = SqoopException.class) + public void testReaderInvalid() { + JSONUtils.parse(new StringReader("{")); + } + + @Test(expected = NullPointerException.class) + public void testReaderNull() { + JSONUtils.parse((Reader)null); + } +} diff --git a/common/src/test/java/org/apache/sqoop/json/TestJobBean.java b/common/src/test/java/org/apache/sqoop/json/TestJobBean.java index d01ab014..98cfee84 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestJobBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestJobBean.java @@ -26,7 +26,6 @@ import org.apache.sqoop.model.MJob; import org.apache.sqoop.model.MStringInput; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.json.simple.parser.ParseException; import org.junit.Test; @@ -53,7 +52,7 @@ public void testJobSerialization() throws ParseException { String jobJsonString = json.toJSONString(); // Retrieved transferred object - JSONObject parsedJobJson = (JSONObject) JSONValue.parse(jobJsonString); + JSONObject parsedJobJson = JSONUtils.parse(jobJsonString); JobBean parsedJobBean = new JobBean(); parsedJobBean.restore(parsedJobJson); MJob target = parsedJobBean.getJobs().get(0); diff --git a/common/src/test/java/org/apache/sqoop/json/TestJobsBean.java b/common/src/test/java/org/apache/sqoop/json/TestJobsBean.java index c365dac3..42fbf9d1 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestJobsBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestJobsBean.java @@ -28,7 +28,6 @@ import org.apache.sqoop.model.MJob; import org.apache.sqoop.model.MStringInput; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.json.simple.parser.ParseException; import org.junit.Test; @@ -60,7 +59,7 @@ public void testJobsSerialization() throws ParseException { String jobJsonString = json.toJSONString(); // Retrieved transferred object - JSONObject parsedJobsJson = (JSONObject) JSONValue.parse(jobJsonString); + JSONObject parsedJobsJson = JSONUtils.parse(jobJsonString); JobsBean parsedJobsBean = new JobsBean(); parsedJobsBean.restore(parsedJobsJson); MJob retrievedJob1 = parsedJobsBean.getJobs().get(0); diff --git a/common/src/test/java/org/apache/sqoop/json/TestLinkBean.java b/common/src/test/java/org/apache/sqoop/json/TestLinkBean.java index d97b3ffd..704e4da4 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestLinkBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestLinkBean.java @@ -29,7 +29,6 @@ import org.apache.sqoop.model.MStringInput; import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.junit.Test; public class TestLinkBean { @@ -62,7 +61,7 @@ public void testLinkSerialization() { String linkJsonString = json.toJSONString(); // Retrieved transferred object - JSONObject parsedLinkJson = (JSONObject) JSONValue.parse(linkJsonString); + JSONObject parsedLinkJson = JSONUtils.parse(linkJsonString); LinkBean retrievedBean = new LinkBean(); retrievedBean.restore(parsedLinkJson); MLink retrievedLink = retrievedBean.getLinks().get(0); diff --git a/common/src/test/java/org/apache/sqoop/json/TestLinksBean.java b/common/src/test/java/org/apache/sqoop/json/TestLinksBean.java index b9f12cc0..9633c5e3 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestLinksBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestLinksBean.java @@ -30,7 +30,6 @@ import org.apache.sqoop.model.MStringInput; import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.junit.Test; public class TestLinksBean { @@ -70,7 +69,7 @@ public void testLinksSerialization() { String linkJsonString = json.toJSONString(); // Retrieved transferred object - JSONObject parsedLinkJson = (JSONObject) JSONValue.parse(linkJsonString); + JSONObject parsedLinkJson = JSONUtils.parse(linkJsonString); LinksBean retrievedBean = new LinksBean(); retrievedBean.restore(parsedLinkJson); MLink targetLink1 = retrievedBean.getLinks().get(0); diff --git a/common/src/test/java/org/apache/sqoop/json/TestSchemaBean.java b/common/src/test/java/org/apache/sqoop/json/TestSchemaBean.java index c7a7b2e5..e8bc0854 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestSchemaBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestSchemaBean.java @@ -20,7 +20,6 @@ import org.apache.sqoop.json.util.TestSchemaSerialization; import org.apache.sqoop.schema.Schema; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; /** * Run the same tests as TestSchemaSerialization, but using the SchamaBean @@ -41,7 +40,7 @@ protected Schema transfer(Schema schema) { String transferredString = extractJson.toJSONString(); - JSONObject restoreJson = (JSONObject) JSONValue.parse(transferredString); + JSONObject restoreJson = JSONUtils.parse(transferredString); SchemaBean restoreBean = new SchemaBean(); restoreBean.restore(restoreJson); diff --git a/common/src/test/java/org/apache/sqoop/json/TestSubmissionBean.java b/common/src/test/java/org/apache/sqoop/json/TestSubmissionBean.java index 99435c5d..d07eda91 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestSubmissionBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestSubmissionBean.java @@ -26,7 +26,6 @@ import org.apache.sqoop.submission.counter.CounterGroup; import org.apache.sqoop.submission.counter.Counters; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.junit.Test; import java.util.ArrayList; @@ -460,7 +459,7 @@ private MSubmission transfer(MSubmission submission) { String string = json.toString(); - JSONObject retrievedJson = (JSONObject) JSONValue.parse(string); + JSONObject retrievedJson = JSONUtils.parse(string); SubmissionsBean retrievedBean = new SubmissionsBean(); retrievedBean.restore(retrievedJson); @@ -479,7 +478,7 @@ private List transfer(List submissions) { String string = json.toString(); - JSONObject retrievedJson = (JSONObject) JSONValue.parse(string); + JSONObject retrievedJson = JSONUtils.parse(string); SubmissionsBean retrievedBean = new SubmissionsBean(); retrievedBean.restore(retrievedJson); diff --git a/common/src/test/java/org/apache/sqoop/json/TestThrowableBean.java b/common/src/test/java/org/apache/sqoop/json/TestThrowableBean.java index 3614ec5c..a67283cd 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestThrowableBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestThrowableBean.java @@ -20,7 +20,6 @@ import org.apache.sqoop.common.SqoopException; import org.apache.sqoop.json.util.SerializationError; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -68,7 +67,7 @@ public Throwable transfer(Throwable source) { String string = json.toJSONString(); // Retrieved transferred object - JSONObject retrievedJson = (JSONObject) JSONValue.parse(string); + JSONObject retrievedJson = JSONUtils.parse(string); ThrowableBean retrievedBean = new ThrowableBean(); retrievedBean.restore(retrievedJson); return retrievedBean.getThrowable(); diff --git a/common/src/test/java/org/apache/sqoop/json/TestValidationResultBean.java b/common/src/test/java/org/apache/sqoop/json/TestValidationResultBean.java index 6691c41f..c119e02c 100644 --- a/common/src/test/java/org/apache/sqoop/json/TestValidationResultBean.java +++ b/common/src/test/java/org/apache/sqoop/json/TestValidationResultBean.java @@ -21,7 +21,6 @@ import org.apache.sqoop.validation.Status; import org.apache.sqoop.validation.ConfigValidationResult; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.junit.Test; import java.util.LinkedList; @@ -115,7 +114,7 @@ private Long transfer(Long id) { String string = json.toString(); - JSONObject retrievedJson = (JSONObject) JSONValue.parse(string); + JSONObject retrievedJson = JSONUtils.parse(string); ValidationResultBean retrievedBean = new ValidationResultBean(); retrievedBean.restore(retrievedJson); @@ -128,7 +127,7 @@ private ConfigValidationResult[] transfer(ConfigValidationResult [] results) { String string = json.toString(); - JSONObject retrievedJson = (JSONObject) JSONValue.parse(string); + JSONObject retrievedJson = JSONUtils.parse(string); ValidationResultBean retrievedBean = new ValidationResultBean(); retrievedBean.restore(retrievedJson); diff --git a/common/src/test/java/org/apache/sqoop/json/util/TestConfigSerialization.java b/common/src/test/java/org/apache/sqoop/json/util/TestConfigSerialization.java index 61fffad0..9ae764a4 100644 --- a/common/src/test/java/org/apache/sqoop/json/util/TestConfigSerialization.java +++ b/common/src/test/java/org/apache/sqoop/json/util/TestConfigSerialization.java @@ -26,6 +26,7 @@ import java.util.Map; import org.apache.sqoop.common.SqoopException; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.model.MBooleanInput; import org.apache.sqoop.model.MConfig; import org.apache.sqoop.model.MConfigType; @@ -35,7 +36,6 @@ import org.apache.sqoop.model.MMapInput; import org.apache.sqoop.model.MStringInput; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.junit.Test; /** @@ -63,7 +63,7 @@ public void testAllDataTypes() { // Exchange the data on string level String serializedJson = jsonObject.toJSONString(); - JSONObject retrievedJson = (JSONObject) JSONValue.parse(serializedJson); + JSONObject retrievedJson = JSONUtils.parse(serializedJson); // And retrieve back from JSON representation MConfig retrieved = ConfigInputSerialization.restoreConfig(retrievedJson); @@ -90,7 +90,7 @@ public void testMapDataType() { String serializedJson = jsonObject.toJSONString(); // Deserialize - JSONObject retrievedJson = (JSONObject) JSONValue.parse(serializedJson); + JSONObject retrievedJson = JSONUtils.parse(serializedJson); MConfig retrieved = ConfigInputSerialization.restoreConfig(retrievedJson); assertEquals(map, retrieved.getMapInput("Map").getValue()); } @@ -111,7 +111,7 @@ public void testMapDataTypeException() { // Replace map value with a fake string to force exception String badSerializedJson = serializedJson.replace("{\"A\":\"B\"}", "\"nonsensical string\""); System.out.println(badSerializedJson); - JSONObject retrievedJson = (JSONObject) JSONValue.parse(badSerializedJson); + JSONObject retrievedJson = JSONUtils.parse(badSerializedJson); ConfigInputSerialization.restoreConfig(retrievedJson); } diff --git a/common/src/test/java/org/apache/sqoop/json/util/TestSchemaSerialization.java b/common/src/test/java/org/apache/sqoop/json/util/TestSchemaSerialization.java index 8ae856f4..1779bce1 100644 --- a/common/src/test/java/org/apache/sqoop/json/util/TestSchemaSerialization.java +++ b/common/src/test/java/org/apache/sqoop/json/util/TestSchemaSerialization.java @@ -23,6 +23,7 @@ import java.util.Collections; import java.util.HashSet; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.schema.NullSchema; import org.apache.sqoop.schema.Schema; import org.apache.sqoop.schema.type.Array; @@ -40,7 +41,6 @@ import org.apache.sqoop.schema.type.Time; import org.apache.sqoop.schema.type.Unknown; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.junit.Test; /** @@ -52,7 +52,7 @@ public class TestSchemaSerialization { public void testSchemaNull() { // a null schema is treated as a NullSchema JSONObject extractJson = SchemaSerialization.extractSchema(null); - JSONObject restoreJson = (JSONObject) JSONValue.parse(extractJson.toJSONString()); + JSONObject restoreJson = JSONUtils.parse(extractJson.toJSONString()); assertEquals(NullSchema.getInstance(), SchemaSerialization.restoreSchema(restoreJson)); } @@ -189,7 +189,7 @@ private void transferAndAssert(Schema schema) { protected Schema transfer(Schema schema) { JSONObject extractJson = SchemaSerialization.extractSchema(schema); String transferredString = extractJson.toJSONString(); - JSONObject restoreJson = (JSONObject) JSONValue.parse(transferredString); + JSONObject restoreJson = JSONUtils.parse(transferredString); return SchemaSerialization.restoreSchema(restoreJson); } } diff --git a/execution/mapreduce/src/main/java/org/apache/sqoop/job/mr/MRConfigurationUtils.java b/execution/mapreduce/src/main/java/org/apache/sqoop/job/mr/MRConfigurationUtils.java index fb10e3ce..e07ddd70 100644 --- a/execution/mapreduce/src/main/java/org/apache/sqoop/job/mr/MRConfigurationUtils.java +++ b/execution/mapreduce/src/main/java/org/apache/sqoop/job/mr/MRConfigurationUtils.java @@ -24,12 +24,12 @@ import org.apache.log4j.PropertyConfigurator; import org.apache.sqoop.common.Direction; import org.apache.sqoop.job.MRJobConstants; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.json.util.SchemaSerialization; import org.apache.sqoop.model.ConfigUtils; import org.apache.sqoop.schema.Schema; import org.apache.sqoop.utils.ClassUtils; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import java.io.InputStream; import java.util.Properties; @@ -228,7 +228,7 @@ private static Schema getSchemaFromBytes(byte[] bytes) { return null; } - JSONObject jsonSchema = (JSONObject) JSONValue.parse(new String(bytes)); + JSONObject jsonSchema = JSONUtils.parse(new String(bytes)); return SchemaSerialization.restoreSchema(jsonSchema); } diff --git a/server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java b/server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java index 13129413..dc237b87 100644 --- a/server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java +++ b/server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java @@ -30,6 +30,7 @@ import org.apache.sqoop.connector.spi.SqoopConnector; import org.apache.sqoop.driver.Driver; import org.apache.sqoop.driver.JobManager; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.json.JobBean; import org.apache.sqoop.json.JobsBean; import org.apache.sqoop.json.JsonBean; @@ -51,7 +52,6 @@ import org.apache.sqoop.validation.ConfigValidationResult; import org.apache.sqoop.validation.Status; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; public class JobRequestHandler implements RequestHandler { @@ -156,7 +156,7 @@ private JsonBean createUpdateJob(RequestContext ctx, boolean create) { JobBean bean = new JobBean(); try { - JSONObject json = (JSONObject) JSONValue.parse(ctx.getRequest().getReader()); + JSONObject json = JSONUtils.parse(ctx.getRequest().getReader()); bean.restore(json); } catch (IOException e) { throw new SqoopException(ServerError.SERVER_0003, "Can't read request content", e); diff --git a/server/src/main/java/org/apache/sqoop/handler/LinkRequestHandler.java b/server/src/main/java/org/apache/sqoop/handler/LinkRequestHandler.java index d2d080bb..2e122175 100644 --- a/server/src/main/java/org/apache/sqoop/handler/LinkRequestHandler.java +++ b/server/src/main/java/org/apache/sqoop/handler/LinkRequestHandler.java @@ -27,6 +27,7 @@ import org.apache.sqoop.common.SqoopException; import org.apache.sqoop.connector.ConnectorManager; import org.apache.sqoop.connector.spi.SqoopConnector; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.json.JsonBean; import org.apache.sqoop.json.LinkBean; import org.apache.sqoop.json.LinksBean; @@ -42,7 +43,6 @@ import org.apache.sqoop.server.common.ServerError; import org.apache.sqoop.validation.ConfigValidationResult; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; public class LinkRequestHandler implements RequestHandler { @@ -110,7 +110,7 @@ private JsonBean createUpdateLink(RequestContext ctx, boolean create) { LinkBean linkBean = new LinkBean(); try { - JSONObject postData = (JSONObject) JSONValue.parse(ctx.getRequest().getReader()); + JSONObject postData = JSONUtils.parse(ctx.getRequest().getReader()); linkBean.restore(postData); } catch (IOException e) { throw new SqoopException(ServerError.SERVER_0003, "Can't read request content", e); diff --git a/tools/src/main/java/org/apache/sqoop/tools/tool/RepositoryLoadTool.java b/tools/src/main/java/org/apache/sqoop/tools/tool/RepositoryLoadTool.java index 64b08fc2..d0ebaed7 100644 --- a/tools/src/main/java/org/apache/sqoop/tools/tool/RepositoryLoadTool.java +++ b/tools/src/main/java/org/apache/sqoop/tools/tool/RepositoryLoadTool.java @@ -42,6 +42,7 @@ import org.apache.sqoop.connector.spi.SqoopConnector; import org.apache.sqoop.driver.Driver; import org.apache.sqoop.driver.DriverUpgrader; +import org.apache.sqoop.json.JSONUtils; import org.apache.sqoop.json.JobBean; import org.apache.sqoop.json.LinkBean; import org.apache.sqoop.json.SubmissionBean; @@ -66,7 +67,6 @@ import org.apache.sqoop.validation.Status; import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; /** * Load user-created content of Sqoop repository from a JSON formatted file The @@ -93,7 +93,7 @@ public boolean runToolWithConfiguration(String[] arguments) { LOG.info("Reading JSON from file" + inputFileName); InputStream input = new FileInputStream(inputFileName); String jsonTxt = IOUtils.toString(input, Charsets.UTF_8); - JSONObject json = (JSONObject) JSONValue.parse(jsonTxt); + JSONObject json = JSONUtils.parse(jsonTxt); boolean res = load(json); input.close(); return res;