Migrate to android-json

Migrate from `org.json:json` to the clean room Apache 2.0 licensed
version that was developed for Android.

Fixes gh-5929
This commit is contained in:
Phillip Webb 2017-01-04 21:18:08 -08:00
parent 4cb7d86aec
commit cc7c2ebb87
22 changed files with 389 additions and 259 deletions

View File

@ -38,6 +38,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader-tools</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</dependency>
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
@ -54,10 +58,6 @@
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>

View File

@ -233,7 +233,8 @@ class InitializrService {
return null;
}
private JSONObject getContentAsJson(HttpEntity entity) throws IOException {
private JSONObject getContentAsJson(HttpEntity entity)
throws IOException, JSONException {
return new JSONObject(getContent(entity));
}

View File

@ -23,6 +23,7 @@ import java.util.Iterator;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
@ -58,8 +59,9 @@ class InitializrServiceMetadata {
/**
* Creates a new instance using the specified root {@link JSONObject}.
* @param root the root JSONObject
* @throws JSONException on JSON parsing failure
*/
InitializrServiceMetadata(JSONObject root) {
InitializrServiceMetadata(JSONObject root) throws JSONException {
this.dependencies = parseDependencies(root);
this.projectTypes = parseProjectTypes(root);
this.defaults = Collections.unmodifiableMap(parseDefaults(root));
@ -124,7 +126,8 @@ class InitializrServiceMetadata {
return this.defaults;
}
private Map<String, Dependency> parseDependencies(JSONObject root) {
private Map<String, Dependency> parseDependencies(JSONObject root)
throws JSONException {
Map<String, Dependency> result = new HashMap<String, Dependency>();
if (!root.has(DEPENDENCIES_EL)) {
return result;
@ -138,7 +141,8 @@ class InitializrServiceMetadata {
return result;
}
private MetadataHolder<String, ProjectType> parseProjectTypes(JSONObject root) {
private MetadataHolder<String, ProjectType> parseProjectTypes(JSONObject root)
throws JSONException {
MetadataHolder<String, ProjectType> result = new MetadataHolder<String, ProjectType>();
if (!root.has(TYPE_EL)) {
return result;
@ -158,7 +162,7 @@ class InitializrServiceMetadata {
return result;
}
private Map<String, String> parseDefaults(JSONObject root) {
private Map<String, String> parseDefaults(JSONObject root) throws JSONException {
Map<String, String> result = new HashMap<String, String>();
Iterator<?> keys = root.keys();
while (keys.hasNext()) {
@ -174,7 +178,8 @@ class InitializrServiceMetadata {
return result;
}
private void parseGroup(JSONObject group, Map<String, Dependency> dependencies) {
private void parseGroup(JSONObject group, Map<String, Dependency> dependencies)
throws JSONException {
if (group.has(VALUES_EL)) {
JSONArray content = group.getJSONArray(VALUES_EL);
for (int i = 0; i < content.length(); i++) {
@ -184,14 +189,15 @@ class InitializrServiceMetadata {
}
}
private Dependency parseDependency(JSONObject object) {
private Dependency parseDependency(JSONObject object) throws JSONException {
String id = getStringValue(object, ID_ATTRIBUTE, null);
String name = getStringValue(object, NAME_ATTRIBUTE, null);
String description = getStringValue(object, DESCRIPTION_ATTRIBUTE, null);
return new Dependency(id, name, description);
}
private ProjectType parseType(JSONObject object, String defaultId) {
private ProjectType parseType(JSONObject object, String defaultId)
throws JSONException {
String id = getStringValue(object, ID_ATTRIBUTE, null);
String name = getStringValue(object, NAME_ATTRIBUTE, null);
String action = getStringValue(object, ACTION_ATTRIBUTE, null);
@ -204,14 +210,15 @@ class InitializrServiceMetadata {
return new ProjectType(id, name, action, defaultType, tags);
}
private String getStringValue(JSONObject object, String name, String defaultValue) {
private String getStringValue(JSONObject object, String name, String defaultValue)
throws JSONException {
return object.has(name) ? object.getString(name) : defaultValue;
}
private Map<String, String> parseStringItems(JSONObject json) {
private Map<String, String> parseStringItems(JSONObject json) throws JSONException {
Map<String, String> result = new HashMap<String, String>();
for (Object k : json.keySet()) {
String key = (String) k;
for (Iterator<?> iterator = json.keys(); iterator.hasNext();) {
String key = (String) iterator.next();
Object value = json.get(key);
if (value instanceof String) {
result.put(key, (String) value);

View File

@ -28,6 +28,7 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHeader;
import org.hamcrest.Matcher;
import org.json.JSONException;
import org.json.JSONObject;
import org.mockito.ArgumentMatcher;
@ -95,7 +96,7 @@ public abstract class AbstractHttpClientMockTests {
}
protected void mockProjectGenerationError(int status, String message)
throws IOException {
throws IOException, JSONException {
// Required for project generation as the metadata is read first
mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
@ -105,7 +106,8 @@ public abstract class AbstractHttpClientMockTests {
given(this.http.execute(isA(HttpGet.class))).willReturn(response);
}
protected void mockMetadataGetError(int status, String message) throws IOException {
protected void mockMetadataGetError(int status, String message)
throws IOException, JSONException {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, createJsonError(status, message).getBytes(),
"application/json");
@ -156,7 +158,7 @@ public abstract class AbstractHttpClientMockTests {
return "attachment; filename=\"" + fileName + "\"";
}
private String createJsonError(int status, String message) {
private String createJsonError(int status, String message) throws JSONException {
JSONObject json = new JSONObject();
json.put("status", status);
if (message != null) {

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
@ -37,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class InitializrServiceMetadataTests {
@Test
public void parseDefaults() {
public void parseDefaults() throws Exception {
InitializrServiceMetadata metadata = createInstance("2.0.0");
assertThat(metadata.getDefaults().get("bootVersion")).isEqualTo("1.1.8.RELEASE");
assertThat(metadata.getDefaults().get("javaVersion")).isEqualTo("1.7");
@ -55,7 +56,7 @@ public class InitializrServiceMetadataTests {
}
@Test
public void parseDependencies() {
public void parseDependencies() throws Exception {
InitializrServiceMetadata metadata = createInstance("2.0.0");
assertThat(metadata.getDependencies()).hasSize(5);
@ -70,7 +71,7 @@ public class InitializrServiceMetadataTests {
}
@Test
public void parseTypes() {
public void parseTypes() throws Exception {
InitializrServiceMetadata metadata = createInstance("2.0.0");
ProjectType projectType = metadata.getProjectTypes().get("maven-project");
assertThat(projectType).isNotNull();
@ -78,7 +79,8 @@ public class InitializrServiceMetadataTests {
assertThat(projectType.getTags().get("format")).isEqualTo("project");
}
private static InitializrServiceMetadata createInstance(String version) {
private static InitializrServiceMetadata createInstance(String version)
throws JSONException {
try {
return new InitializrServiceMetadata(readJson(version));
}
@ -87,7 +89,7 @@ public class InitializrServiceMetadataTests {
}
}
private static JSONObject readJson(String version) throws IOException {
private static JSONObject readJson(String version) throws IOException, JSONException {
Resource resource = new ClassPathResource(
"metadata/service-metadata-" + version + ".json");
InputStream stream = resource.getInputStream();

View File

@ -16,8 +16,6 @@
package org.springframework.boot.cli.command.init;
import java.io.IOException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.Rule;
@ -42,14 +40,14 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
private final InitializrService invoker = new InitializrService(this.http);
@Test
public void loadMetadata() throws IOException {
public void loadMetadata() throws Exception {
mockSuccessfulMetadataGet(false);
InitializrServiceMetadata metadata = this.invoker.loadMetadata("http://foo/bar");
assertThat(metadata).isNotNull();
}
@Test
public void generateSimpleProject() throws IOException {
public void generateSimpleProject() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest();
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/xml", "foo.zip");
@ -59,7 +57,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void generateProjectCustomTargetFilename() throws IOException {
public void generateProjectCustomTargetFilename() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest();
request.setOutput("bar.zip");
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
@ -69,7 +67,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void generateProjectNoDefaultFileName() throws IOException {
public void generateProjectNoDefaultFileName() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest();
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/xml", null);
@ -78,7 +76,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void generateProjectBadRequest() throws IOException {
public void generateProjectBadRequest() throws Exception {
String jsonMessage = "Unknown dependency foo:bar";
mockProjectGenerationError(400, jsonMessage);
ProjectGenerationRequest request = new ProjectGenerationRequest();
@ -89,7 +87,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void generateProjectBadRequestNoExtraMessage() throws IOException {
public void generateProjectBadRequestNoExtraMessage() throws Exception {
mockProjectGenerationError(400, null);
ProjectGenerationRequest request = new ProjectGenerationRequest();
this.thrown.expect(ReportableException.class);
@ -98,7 +96,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void generateProjectNoContent() throws IOException {
public void generateProjectNoContent() throws Exception {
mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockStatus(response, 500);
@ -110,7 +108,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void loadMetadataBadRequest() throws IOException {
public void loadMetadataBadRequest() throws Exception {
String jsonMessage = "whatever error on the server";
mockMetadataGetError(500, jsonMessage);
ProjectGenerationRequest request = new ProjectGenerationRequest();
@ -120,7 +118,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void loadMetadataInvalidJson() throws IOException {
public void loadMetadataInvalidJson() throws Exception {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, "Foo-Bar-Not-JSON".getBytes(), "application/json");
mockStatus(response, 200);
@ -132,7 +130,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void loadMetadataNoContent() throws IOException {
public void loadMetadataNoContent() throws Exception {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockStatus(response, 500);
given(this.http.execute(isA(HttpGet.class))).willReturn(response);
@ -143,7 +141,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
private ProjectGenerationResponse generateProject(ProjectGenerationRequest request,
MockHttpProjectGenerationRequest mockRequest) throws IOException {
MockHttpProjectGenerationRequest mockRequest) throws Exception {
mockSuccessfulProjectGeneration(mockRequest);
ProjectGenerationResponse entity = this.invoker.generate(request);
assertThat(entity.getContent()).as("wrong body content")

View File

@ -23,6 +23,7 @@ import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
@ -169,7 +170,7 @@ public class ProjectGenerationRequestTests {
}
@Test
public void buildNoMatch() {
public void buildNoMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("does-not-exist", null);
this.thrown.expect(ReportableException.class);
@ -178,7 +179,7 @@ public class ProjectGenerationRequestTests {
}
@Test
public void buildMultipleMatch() {
public void buildMultipleMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata("types-conflict");
setBuildAndFormat("gradle", null);
this.thrown.expect(ReportableException.class);
@ -188,7 +189,7 @@ public class ProjectGenerationRequestTests {
}
@Test
public void buildOneMatch() {
public void buildOneMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("gradle", null);
assertThat(this.request.generateUrl(metadata))
@ -196,7 +197,7 @@ public class ProjectGenerationRequestTests {
}
@Test
public void typeAndBuildAndFormat() {
public void typeAndBuildAndFormat() throws Exception {
InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("gradle", "project");
this.request.setType("maven-build");
@ -205,14 +206,14 @@ public class ProjectGenerationRequestTests {
}
@Test
public void invalidType() throws URISyntaxException {
public void invalidType() throws Exception {
this.request.setType("does-not-exist");
this.thrown.expect(ReportableException.class);
this.request.generateUrl(createDefaultMetadata());
}
@Test
public void noTypeAndNoDefault() throws URISyntaxException {
public void noTypeAndNoDefault() throws Exception {
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage("no default is defined");
this.request.generateUrl(readMetadata("types-conflict"));
@ -243,11 +244,12 @@ public class ProjectGenerationRequestTests {
return new InitializrServiceMetadata(projectType);
}
private static InitializrServiceMetadata readMetadata() {
private static InitializrServiceMetadata readMetadata() throws JSONException {
return readMetadata("2.0.0");
}
private static InitializrServiceMetadata readMetadata(String version) {
private static InitializrServiceMetadata readMetadata(String version)
throws JSONException {
try {
Resource resource = new ClassPathResource(
"metadata/service-metadata-" + version + ".json");

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
@ -62,6 +63,11 @@
<artifactId>okhttp</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>

View File

@ -20,8 +20,8 @@
<dependencies>
<!-- Compile (should stick to the bare minimum) -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -23,8 +23,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
/**
* Load a {@link ConfigurationMetadataRepository} from the content of arbitrary
* resource(s).
@ -104,13 +102,8 @@ public final class ConfigurationMetadataRepositoryJsonBuilder {
RawConfigurationMetadata metadata = this.reader.read(in, charset);
return create(metadata);
}
catch (IOException ex) {
throw new IllegalArgumentException(
"Failed to read configuration " + "metadata", ex);
}
catch (JSONException ex) {
throw new IllegalArgumentException(
"Invalid configuration " + "metadata document", ex);
catch (Exception ex) {
throw new IllegalStateException("Failed to read configuration metadata", ex);
}
}

View File

@ -41,14 +41,26 @@ class JsonReader {
public RawConfigurationMetadata read(InputStream in, Charset charset)
throws IOException {
JSONObject json = readJson(in, charset);
List<ConfigurationMetadataSource> groups = parseAllSources(json);
List<ConfigurationMetadataItem> items = parseAllItems(json);
List<ConfigurationMetadataHint> hints = parseAllHints(json);
return new RawConfigurationMetadata(groups, items, hints);
try {
JSONObject json = readJson(in, charset);
List<ConfigurationMetadataSource> groups = parseAllSources(json);
List<ConfigurationMetadataItem> items = parseAllItems(json);
List<ConfigurationMetadataHint> hints = parseAllHints(json);
return new RawConfigurationMetadata(groups, items, hints);
}
catch (Exception ex) {
if (ex instanceof IOException) {
throw (IOException) ex;
}
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
throw new IllegalStateException(ex);
}
}
private List<ConfigurationMetadataSource> parseAllSources(JSONObject root) {
private List<ConfigurationMetadataSource> parseAllSources(JSONObject root)
throws Exception {
List<ConfigurationMetadataSource> result = new ArrayList<ConfigurationMetadataSource>();
if (!root.has("groups")) {
return result;
@ -61,7 +73,8 @@ class JsonReader {
return result;
}
private List<ConfigurationMetadataItem> parseAllItems(JSONObject root) {
private List<ConfigurationMetadataItem> parseAllItems(JSONObject root)
throws Exception {
List<ConfigurationMetadataItem> result = new ArrayList<ConfigurationMetadataItem>();
if (!root.has("properties")) {
return result;
@ -74,7 +87,8 @@ class JsonReader {
return result;
}
private List<ConfigurationMetadataHint> parseAllHints(JSONObject root) {
private List<ConfigurationMetadataHint> parseAllHints(JSONObject root)
throws Exception {
List<ConfigurationMetadataHint> result = new ArrayList<ConfigurationMetadataHint>();
if (!root.has("hints")) {
return result;
@ -87,7 +101,7 @@ class JsonReader {
return result;
}
private ConfigurationMetadataSource parseSource(JSONObject json) {
private ConfigurationMetadataSource parseSource(JSONObject json) throws Exception {
ConfigurationMetadataSource source = new ConfigurationMetadataSource();
source.setGroupId(json.getString("name"));
source.setType(json.optString("type", null));
@ -100,7 +114,7 @@ class JsonReader {
return source;
}
private ConfigurationMetadataItem parseItem(JSONObject json) {
private ConfigurationMetadataItem parseItem(JSONObject json) throws Exception {
ConfigurationMetadataItem item = new ConfigurationMetadataItem();
item.setId(json.getString("name"));
item.setType(json.optString("type", null));
@ -115,7 +129,7 @@ class JsonReader {
return item;
}
private ConfigurationMetadataHint parseHint(JSONObject json) {
private ConfigurationMetadataHint parseHint(JSONObject json) throws Exception {
ConfigurationMetadataHint hint = new ConfigurationMetadataHint();
hint.setId(json.getString("name"));
if (json.has("values")) {
@ -152,7 +166,7 @@ class JsonReader {
return hint;
}
private Deprecation parseDeprecation(JSONObject object) {
private Deprecation parseDeprecation(JSONObject object) throws Exception {
if (object.has("deprecation")) {
JSONObject deprecationJsonObject = object.getJSONObject("deprecation");
Deprecation deprecation = new Deprecation();
@ -164,7 +178,7 @@ class JsonReader {
return (object.optBoolean("deprecated") ? new Deprecation() : null);
}
private Object readItemValue(Object value) {
private Object readItemValue(Object value) throws Exception {
if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value;
Object[] content = new Object[array.length()];
@ -176,7 +190,7 @@ class JsonReader {
return value;
}
private JSONObject readJson(InputStream in, Charset charset) throws IOException {
private JSONObject readJson(InputStream in, Charset charset) throws Exception {
try {
StringBuilder out = new StringBuilder();
InputStreamReader reader = new InputStreamReader(in, charset);

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import org.hamcrest.CoreMatchers;
import org.json.JSONException;
import org.junit.Test;
@ -45,7 +46,7 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
@Test
public void invalidMetadata() throws IOException {
this.thrown.expect(JSONException.class);
this.thrown.expectCause(CoreMatchers.<Throwable>instanceOf(JSONException.class));
readFor("invalid");
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
@ -20,8 +21,8 @@
<dependencies>
<!-- Compile (should stick to the bare minimum) -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</dependency>
<!-- Test -->
<dependency>

View File

@ -17,7 +17,6 @@
package org.springframework.boot.configurationprocessor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.LinkedHashMap;
@ -134,7 +133,12 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
}
}
if (roundEnv.processingOver()) {
writeMetaData();
try {
writeMetaData();
}
catch (Exception ex) {
throw new IllegalStateException("Failed to write metadata", ex);
}
}
return false;
}
@ -390,16 +394,11 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
return values;
}
protected ConfigurationMetadata writeMetaData() {
protected ConfigurationMetadata writeMetaData() throws Exception {
ConfigurationMetadata metadata = this.metadataCollector.getMetadata();
metadata = mergeAdditionalMetadata(metadata);
if (!metadata.getItems().isEmpty()) {
try {
this.metadataStore.writeMetadata(metadata);
}
catch (IOException ex) {
throw new IllegalStateException("Failed to write metadata", ex);
}
this.metadataStore.writeMetadata(metadata);
return metadata;
}
return null;

View File

@ -27,8 +27,6 @@ import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import org.json.JSONException;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException;
import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
@ -87,7 +85,7 @@ public class MetadataStore {
catch (IOException ex) {
return null;
}
catch (JSONException ex) {
catch (Exception ex) {
throw new InvalidConfigurationMetadataException(
"Invalid additional meta-data in '" + METADATA_PATH + "': "
+ ex.getMessage(),

View File

@ -0,0 +1,48 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed 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.springframework.boot.configurationprocessor.metadata;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Extension to {@link JSONObject} that remembers the order of inserts.
*
* @author Stephane Nicoll
* @author Phillip Webb
*/
@SuppressWarnings("rawtypes")
class JSONOrderedObject extends JSONObject {
private Set<String> keys = new LinkedHashSet<String>();
@Override
public JSONObject put(String key, Object value) throws JSONException {
this.keys.add(key);
return super.put(key, value);
}
@Override
public Iterator keys() {
return this.keys.iterator();
}
}

View File

@ -0,0 +1,161 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed 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.springframework.boot.configurationprocessor.metadata;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
/**
* Converter to change meta-data objects into JSON objects.
*
* @author Stephane Nicoll
* @author Phillip Webb
*/
class JsonConverter {
public JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType)
throws Exception {
JSONArray jsonArray = new JSONArray();
for (ItemMetadata item : metadata.getItems()) {
if (item.isOfItemType(itemType)) {
jsonArray.put(toJsonObject(item));
}
}
return jsonArray;
}
public JSONArray toJsonArray(Collection<ItemHint> hints) throws Exception {
JSONArray jsonArray = new JSONArray();
for (ItemHint hint : hints) {
jsonArray.put(toJsonObject(hint));
}
return jsonArray;
}
public JSONObject toJsonObject(ItemMetadata item) throws Exception {
JSONObject jsonObject = new JSONOrderedObject();
jsonObject.put("name", item.getName());
putIfPresent(jsonObject, "type", item.getType());
putIfPresent(jsonObject, "description", item.getDescription());
putIfPresent(jsonObject, "sourceType", item.getSourceType());
putIfPresent(jsonObject, "sourceMethod", item.getSourceMethod());
Object defaultValue = item.getDefaultValue();
if (defaultValue != null) {
putDefaultValue(jsonObject, defaultValue);
}
ItemDeprecation deprecation = item.getDeprecation();
if (deprecation != null) {
jsonObject.put("deprecated", true); // backward compatibility
JSONObject deprecationJsonObject = new JSONObject();
if (deprecation.getReason() != null) {
deprecationJsonObject.put("reason", deprecation.getReason());
}
if (deprecation.getReplacement() != null) {
deprecationJsonObject.put("replacement", deprecation.getReplacement());
}
jsonObject.put("deprecation", deprecationJsonObject);
}
return jsonObject;
}
private JSONObject toJsonObject(ItemHint hint) throws Exception {
JSONObject jsonObject = new JSONOrderedObject();
jsonObject.put("name", hint.getName());
if (!hint.getValues().isEmpty()) {
jsonObject.put("values", getItemHintValues(hint));
}
if (!hint.getProviders().isEmpty()) {
jsonObject.put("providers", getItemHintProviders(hint));
}
return jsonObject;
}
private JSONArray getItemHintValues(ItemHint hint) throws Exception {
JSONArray values = new JSONArray();
for (ItemHint.ValueHint value : hint.getValues()) {
values.put(getItemHintValue(value));
}
return values;
}
private JSONObject getItemHintValue(ItemHint.ValueHint value) throws Exception {
JSONObject result = new JSONOrderedObject();
putHintValue(result, value.getValue());
putIfPresent(result, "description", value.getDescription());
return result;
}
private JSONArray getItemHintProviders(ItemHint hint) throws Exception {
JSONArray providers = new JSONArray();
for (ItemHint.ValueProvider provider : hint.getProviders()) {
providers.put(getItemHintProvider(provider));
}
return providers;
}
private JSONObject getItemHintProvider(ItemHint.ValueProvider provider)
throws Exception {
JSONObject result = new JSONOrderedObject();
result.put("name", provider.getName());
if (provider.getParameters() != null && !provider.getParameters().isEmpty()) {
JSONObject parameters = new JSONOrderedObject();
for (Map.Entry<String, Object> entry : provider.getParameters().entrySet()) {
parameters.put(entry.getKey(), extractItemValue(entry.getValue()));
}
result.put("parameters", parameters);
}
return result;
}
private void putIfPresent(JSONObject jsonObject, String name, Object value)
throws Exception {
if (value != null) {
jsonObject.put(name, value);
}
}
private void putHintValue(JSONObject jsonObject, Object value) throws Exception {
Object hintValue = extractItemValue(value);
jsonObject.put("value", hintValue);
}
private void putDefaultValue(JSONObject jsonObject, Object value) throws Exception {
Object defaultValue = extractItemValue(value);
jsonObject.put("defaultValue", defaultValue);
}
private Object extractItemValue(Object value) {
Object defaultValue = value;
if (value.getClass().isArray()) {
JSONArray array = new JSONArray();
int length = Array.getLength(value);
for (int i = 0; i < length; i++) {
array.put(Array.get(value, i));
}
defaultValue = array;
}
return defaultValue;
}
}

View File

@ -20,18 +20,14 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
@ -51,136 +47,26 @@ public class JsonMarshaller {
public void write(ConfigurationMetadata metadata, OutputStream outputStream)
throws IOException {
JSONObject object = new JSONOrderedObject();
object.put("groups", toJsonArray(metadata, ItemType.GROUP));
object.put("properties", toJsonArray(metadata, ItemType.PROPERTY));
object.put("hints", toJsonArray(metadata.getHints()));
outputStream.write(object.toString(2).getBytes(UTF_8));
}
private JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) {
JSONArray jsonArray = new JSONArray();
for (ItemMetadata item : metadata.getItems()) {
if (item.isOfItemType(itemType)) {
jsonArray.put(toJsonObject(item));
try {
JSONObject object = new JSONOrderedObject();
JsonConverter converter = new JsonConverter();
object.put("groups", converter.toJsonArray(metadata, ItemType.GROUP));
object.put("properties", converter.toJsonArray(metadata, ItemType.PROPERTY));
object.put("hints", converter.toJsonArray(metadata.getHints()));
outputStream.write(object.toString(2).getBytes(UTF_8));
}
catch (Exception ex) {
if (ex instanceof IOException) {
throw (IOException) ex;
}
}
return jsonArray;
}
private JSONArray toJsonArray(Collection<ItemHint> hints) {
JSONArray jsonArray = new JSONArray();
for (ItemHint hint : hints) {
jsonArray.put(toJsonObject(hint));
}
return jsonArray;
}
private JSONObject toJsonObject(ItemMetadata item) {
JSONObject jsonObject = new JSONOrderedObject();
jsonObject.put("name", item.getName());
putIfPresent(jsonObject, "type", item.getType());
putIfPresent(jsonObject, "description", item.getDescription());
putIfPresent(jsonObject, "sourceType", item.getSourceType());
putIfPresent(jsonObject, "sourceMethod", item.getSourceMethod());
Object defaultValue = item.getDefaultValue();
if (defaultValue != null) {
putDefaultValue(jsonObject, defaultValue);
}
ItemDeprecation deprecation = item.getDeprecation();
if (deprecation != null) {
jsonObject.put("deprecated", true); // backward compatibility
JSONObject deprecationJsonObject = new JSONObject();
if (deprecation.getReason() != null) {
deprecationJsonObject.put("reason", deprecation.getReason());
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
if (deprecation.getReplacement() != null) {
deprecationJsonObject.put("replacement", deprecation.getReplacement());
}
jsonObject.put("deprecation", deprecationJsonObject);
}
return jsonObject;
}
private JSONObject toJsonObject(ItemHint hint) {
JSONObject jsonObject = new JSONOrderedObject();
jsonObject.put("name", hint.getName());
if (!hint.getValues().isEmpty()) {
jsonObject.put("values", getItemHintValues(hint));
}
if (!hint.getProviders().isEmpty()) {
jsonObject.put("providers", getItemHintProviders(hint));
}
return jsonObject;
}
private JSONArray getItemHintValues(ItemHint hint) {
JSONArray values = new JSONArray();
for (ItemHint.ValueHint value : hint.getValues()) {
values.put(getItemHintValue(value));
}
return values;
}
private JSONObject getItemHintValue(ItemHint.ValueHint value) {
JSONObject result = new JSONOrderedObject();
putHintValue(result, value.getValue());
putIfPresent(result, "description", value.getDescription());
return result;
}
private JSONArray getItemHintProviders(ItemHint hint) {
JSONArray providers = new JSONArray();
for (ItemHint.ValueProvider provider : hint.getProviders()) {
providers.put(getItemHintProvider(provider));
}
return providers;
}
private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) {
JSONObject result = new JSONOrderedObject();
result.put("name", provider.getName());
if (provider.getParameters() != null && !provider.getParameters().isEmpty()) {
JSONObject parameters = new JSONOrderedObject();
for (Map.Entry<String, Object> entry : provider.getParameters().entrySet()) {
parameters.put(entry.getKey(), extractItemValue(entry.getValue()));
}
result.put("parameters", parameters);
}
return result;
}
private void putIfPresent(JSONObject jsonObject, String name, Object value) {
if (value != null) {
jsonObject.put(name, value);
throw new IllegalStateException(ex);
}
}
private void putHintValue(JSONObject jsonObject, Object value) {
Object hintValue = extractItemValue(value);
jsonObject.put("value", hintValue);
}
private void putDefaultValue(JSONObject jsonObject, Object value) {
Object defaultValue = extractItemValue(value);
jsonObject.put("defaultValue", defaultValue);
}
private Object extractItemValue(Object value) {
Object defaultValue = value;
if (value.getClass().isArray()) {
JSONArray array = new JSONArray();
int length = Array.getLength(value);
for (int i = 0; i < length; i++) {
array.put(Array.get(value, i));
}
defaultValue = array;
}
return defaultValue;
}
public ConfigurationMetadata read(InputStream inputStream) throws IOException {
public ConfigurationMetadata read(InputStream inputStream) throws Exception {
ConfigurationMetadata metadata = new ConfigurationMetadata();
JSONObject object = new JSONObject(toString(inputStream));
JSONArray groups = object.optJSONArray("groups");
@ -205,7 +91,8 @@ public class JsonMarshaller {
return metadata;
}
private ItemMetadata toItemMetadata(JSONObject object, ItemType itemType) {
private ItemMetadata toItemMetadata(JSONObject object, ItemType itemType)
throws Exception {
String name = object.getString("name");
String type = object.optString("type", null);
String description = object.optString("description", null);
@ -217,7 +104,7 @@ public class JsonMarshaller {
description, defaultValue, deprecation);
}
private ItemDeprecation toItemDeprecation(JSONObject object) {
private ItemDeprecation toItemDeprecation(JSONObject object) throws Exception {
if (object.has("deprecation")) {
JSONObject deprecationJsonObject = object.getJSONObject("deprecation");
ItemDeprecation deprecation = new ItemDeprecation();
@ -229,7 +116,7 @@ public class JsonMarshaller {
return (object.optBoolean("deprecated") ? new ItemDeprecation() : null);
}
private ItemHint toItemHint(JSONObject object) {
private ItemHint toItemHint(JSONObject object) throws Exception {
String name = object.getString("name");
List<ItemHint.ValueHint> values = new ArrayList<ItemHint.ValueHint>();
if (object.has("values")) {
@ -248,19 +135,19 @@ public class JsonMarshaller {
return new ItemHint(name, values, providers);
}
private ItemHint.ValueHint toValueHint(JSONObject object) {
private ItemHint.ValueHint toValueHint(JSONObject object) throws Exception {
Object value = readItemValue(object.get("value"));
String description = object.optString("description", null);
return new ItemHint.ValueHint(value, description);
}
private ItemHint.ValueProvider toValueProvider(JSONObject object) {
private ItemHint.ValueProvider toValueProvider(JSONObject object) throws Exception {
String name = object.getString("name");
Map<String, Object> parameters = new HashMap<String, Object>();
if (object.has("parameters")) {
JSONObject parametersObject = object.getJSONObject("parameters");
for (Object k : parametersObject.keySet()) {
String key = (String) k;
for (Iterator<?> iterator = parametersObject.keys(); iterator.hasNext();) {
String key = (String) iterator.next();
Object value = readItemValue(parametersObject.get(key));
parameters.put(key, value);
}
@ -268,7 +155,7 @@ public class JsonMarshaller {
return new ItemHint.ValueProvider(name, parameters);
}
private Object readItemValue(Object value) {
private Object readItemValue(Object value) throws Exception {
if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value;
Object[] content = new Object[array.length()];
@ -291,25 +178,4 @@ public class JsonMarshaller {
return out.toString();
}
/**
* Extension to {@link JSONObject} that remembers the order of inserts.
*/
@SuppressWarnings("rawtypes")
private static class JSONOrderedObject extends JSONObject {
private Set<String> keys = new LinkedHashSet<String>();
@Override
public JSONObject put(String key, Object value) throws JSONException {
this.keys.add(key);
return super.put(key, value);
}
@Override
public Set keySet() {
return this.keys;
}
}
}

View File

@ -34,6 +34,7 @@ import org.springframework.boot.configurationprocessor.metadata.ConfigurationMet
import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
import org.springframework.boot.configurationprocessor.metadata.ItemHint;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
import org.springframework.boot.configurationprocessor.metadata.TestJsonConverter;
import org.springframework.boot.configurationsample.incremental.BarProperties;
import org.springframework.boot.configurationsample.incremental.FooProperties;
import org.springframework.boot.configurationsample.incremental.RenamedBarProperties;
@ -630,8 +631,9 @@ public class ConfigurationMetadataAnnotationProcessorTests {
JSONObject additionalMetadata = new JSONObject();
additionalMetadata.put("properties", properties);
FileWriter writer = new FileWriter(additionalMetadataFile);
additionalMetadata.write(writer);
writer.append(additionalMetadata.toString(2));
writer.flush();
writer.close();
ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata).has(Metadata.withProperty("simple.comparator"));
assertThat(metadata).has(Metadata.withProperty("foo", String.class)
@ -724,23 +726,28 @@ public class ConfigurationMetadataAnnotationProcessorTests {
return processor.getMetadata();
}
private void writeAdditionalMetadata(ItemMetadata... metadata) throws IOException {
private void writeAdditionalMetadata(ItemMetadata... metadata) throws Exception {
TestJsonConverter converter = new TestJsonConverter();
File additionalMetadataFile = createAdditionalMetadataFile();
JSONObject additionalMetadata = new JSONObject();
additionalMetadata.put("properties", metadata);
JSONArray properties = new JSONArray();
for (ItemMetadata itemMetadata : metadata) {
properties.put(converter.toJsonObject(itemMetadata));
}
additionalMetadata.put("properties", properties);
writeMetadata(additionalMetadataFile, additionalMetadata);
}
private void writeAdditionalHints(ItemHint... hints) throws IOException {
private void writeAdditionalHints(ItemHint... hints) throws Exception {
TestJsonConverter converter = new TestJsonConverter();
File additionalMetadataFile = createAdditionalMetadataFile();
JSONObject additionalMetadata = new JSONObject();
additionalMetadata.put("hints", hints);
additionalMetadata.put("hints", converter.toJsonArray(Arrays.asList(hints)));
writeMetadata(additionalMetadataFile, additionalMetadata);
}
private void writePropertyDeprecation(ItemMetadata... items) throws IOException {
private void writePropertyDeprecation(ItemMetadata... items) throws Exception {
File additionalMetadataFile = createAdditionalMetadataFile();
JSONArray propertiesArray = new JSONArray();
for (ItemMetadata item : items) {
JSONObject jsonObject = new JSONObject();
@ -776,11 +783,10 @@ public class ConfigurationMetadataAnnotationProcessorTests {
return additionalMetadataFile;
}
private void writeMetadata(File metadataFile, JSONObject metadata)
throws IOException {
private void writeMetadata(File metadataFile, JSONObject metadata) throws Exception {
FileWriter writer = new FileWriter(metadataFile);
try {
metadata.write(writer);
writer.append(metadata.toString(2));
}
finally {
writer.close();

View File

@ -70,7 +70,7 @@ public class TestConfigurationMetadataAnnotationProcessor
}
@Override
protected ConfigurationMetadata writeMetaData() {
protected ConfigurationMetadata writeMetaData() throws Exception {
super.writeMetaData();
try {
File metadataFile = new File(this.outputLocation,

View File

@ -18,7 +18,6 @@ package org.springframework.boot.configurationprocessor.metadata;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
@ -38,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class JsonMarshallerTests {
@Test
public void marshallAndUnmarshal() throws IOException {
public void marshallAndUnmarshal() throws Exception {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newProperty("a", "b", StringBuffer.class.getName(),
InputStream.class.getName(), "sourceMethod", "desc", "x",

View File

@ -0,0 +1,26 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed 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.springframework.boot.configurationprocessor.metadata;
/**
* {@link JsonConverter} for use in tests.
*
* @author Phillip Webb
*/
public class TestJsonConverter extends JsonConverter {
}