Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
4bfecc13
Commit
4bfecc13
authored
Jan 05, 2017
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.5.x'
parents
125d9d61
45d5b60f
Changes
23
Hide whitespace changes
Inline
Side-by-side
Showing
23 changed files
with
450 additions
and
284 deletions
+450
-284
pom.xml
spring-boot-cli/pom.xml
+4
-4
InitializrService.java
...ingframework/boot/cli/command/init/InitializrService.java
+2
-1
InitializrServiceMetadata.java
...work/boot/cli/command/init/InitializrServiceMetadata.java
+18
-11
AbstractHttpClientMockTests.java
...rk/boot/cli/command/init/AbstractHttpClientMockTests.java
+5
-3
InitializrServiceMetadataTests.java
...boot/cli/command/init/InitializrServiceMetadataTests.java
+7
-5
InitializrServiceTests.java
...amework/boot/cli/command/init/InitializrServiceTests.java
+11
-13
ProjectGenerationRequestTests.java
.../boot/cli/command/init/ProjectGenerationRequestTests.java
+10
-8
pom.xml
spring-boot-parent/pom.xml
+7
-1
pom.xml
spring-boot-tools/spring-boot-configuration-metadata/pom.xml
+2
-2
ConfigurationMetadataRepositoryJsonBuilder.java
...nmetadata/ConfigurationMetadataRepositoryJsonBuilder.java
+2
-9
JsonReader.java
...pringframework/boot/configurationmetadata/JsonReader.java
+28
-14
JsonReaderTests.java
...framework/boot/configurationmetadata/JsonReaderTests.java
+2
-1
pom.xml
...ng-boot-tools/spring-boot-configuration-processor/pom.xml
+35
-9
ConfigurationMetadataAnnotationProcessor.java
...onprocessor/ConfigurationMetadataAnnotationProcessor.java
+8
-9
MetadataStore.java
...gframework/boot/configurationprocessor/MetadataStore.java
+1
-3
ConfigurationMetadata.java
...onfigurationprocessor/metadata/ConfigurationMetadata.java
+30
-19
JSONOrderedObject.java
...ot/configurationprocessor/metadata/JSONOrderedObject.java
+48
-0
JsonConverter.java
...k/boot/configurationprocessor/metadata/JsonConverter.java
+161
-0
JsonMarshaller.java
.../boot/configurationprocessor/metadata/JsonMarshaller.java
+25
-159
ConfigurationMetadataAnnotationProcessorTests.java
...cessor/ConfigurationMetadataAnnotationProcessorTests.java
+16
-10
TestConfigurationMetadataAnnotationProcessor.java
...ocessor/TestConfigurationMetadataAnnotationProcessor.java
+1
-1
JsonMarshallerTests.java
.../configurationprocessor/metadata/JsonMarshallerTests.java
+1
-2
TestJsonConverter.java
...ot/configurationprocessor/metadata/TestJsonConverter.java
+26
-0
No files found.
spring-boot-cli/pom.xml
View file @
4bfecc13
...
...
@@ -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>
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java
View file @
4bfecc13
...
...
@@ -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
));
}
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrServiceMetadata.java
View file @
4bfecc13
...
...
@@ -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
);
...
...
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/AbstractHttpClientMockTests.java
View file @
4bfecc13
...
...
@@ -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
)
{
...
...
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceMetadataTests.java
View file @
4bfecc13
...
...
@@ -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
();
...
...
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java
View file @
4bfecc13
...
...
@@ -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
IO
Exception
{
public
void
loadMetadata
()
throws
Exception
{
mockSuccessfulMetadataGet
(
false
);
InitializrServiceMetadata
metadata
=
this
.
invoker
.
loadMetadata
(
"http://foo/bar"
);
assertThat
(
metadata
).
isNotNull
();
}
@Test
public
void
generateSimpleProject
()
throws
IO
Exception
{
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
IO
Exception
{
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
IO
Exception
{
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
IO
Exception
{
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
IO
Exception
{
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
IO
Exception
{
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
IO
Exception
{
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
IO
Exception
{
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
IO
Exception
{
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
IO
Exception
{
MockHttpProjectGenerationRequest
mockRequest
)
throws
Exception
{
mockSuccessfulProjectGeneration
(
mockRequest
);
ProjectGenerationResponse
entity
=
this
.
invoker
.
generate
(
request
);
assertThat
(
entity
.
getContent
()).
as
(
"wrong body content"
)
...
...
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java
View file @
4bfecc13
...
...
@@ -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
URISyntax
Exception
{
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
URISyntax
Exception
{
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"
);
...
...
spring-boot-parent/pom.xml
View file @
4bfecc13
<?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>
...
...
@@ -57,6 +58,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>
...
...
spring-boot-tools/spring-boot-configuration-metadata/pom.xml
View file @
4bfecc13
...
...
@@ -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>
spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilder.java
View file @
4bfecc13
...
...
@@ -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
);
}
}
...
...
spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/JsonReader.java
View file @
4bfecc13
...
...
@@ -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
IO
Exception
{
private
JSONObject
readJson
(
InputStream
in
,
Charset
charset
)
throws
Exception
{
try
{
StringBuilder
out
=
new
StringBuilder
();
InputStreamReader
reader
=
new
InputStreamReader
(
in
,
charset
);
...
...
spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java
View file @
4bfecc13
...
...
@@ -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
.
expect
Cause
(
CoreMatchers
.<
Throwable
>
instanceOf
(
JSONException
.
class
)
);
readFor
(
"invalid"
);
}
...
...
spring-boot-tools/spring-boot-configuration-processor/pom.xml
View file @
4bfecc13
<?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>
...
...
@@ -18,10 +19,11 @@
<main.basedir>
${basedir}/../..
</main.basedir>
</properties>
<dependencies>
<!-- Compile (should stick to the bare minimum) -->
<dependency>
<groupId>
org.json
</groupId>
<artifactId>
json
</artifactId>
<!-- Shaded -->
<dependency>
<groupId>
com.vaadin.external.google
</groupId>
<artifactId>
android-json
</artifactId>
<optional>
true
</optional>
</dependency>
<!-- Test -->
<dependency>
...
...
@@ -29,10 +31,6 @@
<artifactId>
lombok
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-core
</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
...
...
@@ -44,6 +42,34 @@
<proc>
none
</proc>
</configuration>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-shade-plugin
</artifactId>
<executions>
<execution>
<phase>
package
</phase>
<goals>
<goal>
shade
</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>
com.vaadin.external.google:android-json
</artifact>
</filter>
</filters>
<relocations>
<relocation>
<pattern>
org.json
</pattern>
<shadedPattern>
org.springframework.boot.configurationprocessor.json
</shadedPattern>
</relocation>
</relocations>
<createDependencyReducedPom>
false
</createDependencyReducedPom>
<createSourcesJar>
true
</createSourcesJar>
<shadeSourcesContent>
true
</shadeSourcesContent>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java
View file @
4bfecc13
...
...
@@ -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
;
...
...
spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataStore.java
View file @
4bfecc13
...
...
@@ -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
(
JSON
Exception
ex
)
{
catch
(
Exception
ex
)
{
throw
new
InvalidConfigurationMetadataException
(
"Invalid additional meta-data in '"
+
METADATA_PATH
+
"': "
+
ex
.
getMessage
(),
...
...
spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java
View file @
4bfecc13
...
...
@@ -20,15 +20,12 @@ import java.util.ArrayList;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.HashSet
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.ListIterator
;
import
java.util.Map
;
import
java.util.Set
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.ObjectUtils
;
/**
* Configuration meta-data.
*
...
...
@@ -46,18 +43,18 @@ public class ConfigurationMetadata {
SEPARATORS
=
Collections
.
unmodifiableSet
(
new
HashSet
<
Character
>(
chars
));
}
private
final
M
ultiValueMap
<
String
,
ItemMetadata
>
items
;
private
final
M
ap
<
String
,
List
<
ItemMetadata
>
>
items
;
private
final
M
ultiValueMap
<
String
,
ItemHint
>
hints
;
private
final
M
ap
<
String
,
List
<
ItemHint
>
>
hints
;
public
ConfigurationMetadata
()
{
this
.
items
=
new
Linked
MultiValueMap
<
String
,
ItemMetadata
>();
this
.
hints
=
new
Linked
MultiValueMap
<
String
,
ItemHint
>();
this
.
items
=
new
Linked
HashMap
<
String
,
List
<
ItemMetadata
>
>();
this
.
hints
=
new
Linked
HashMap
<
String
,
List
<
ItemHint
>
>();
}
public
ConfigurationMetadata
(
ConfigurationMetadata
metadata
)
{
this
.
items
=
new
Linked
MultiValueMap
<
String
,
ItemMetadata
>(
metadata
.
items
);
this
.
hints
=
new
Linked
MultiValueMap
<
String
,
ItemHint
>(
metadata
.
hints
);
this
.
items
=
new
Linked
HashMap
<
String
,
List
<
ItemMetadata
>
>(
metadata
.
items
);
this
.
hints
=
new
Linked
HashMap
<
String
,
List
<
ItemHint
>
>(
metadata
.
hints
);
}
/**
...
...
@@ -65,7 +62,7 @@ public class ConfigurationMetadata {
* @param itemMetadata the meta-data to add
*/
public
void
add
(
ItemMetadata
itemMetadata
)
{
this
.
items
.
add
(
itemMetadata
.
getName
(),
itemMetadata
);
add
(
this
.
items
,
itemMetadata
.
getName
(),
itemMetadata
);
}
/**
...
...
@@ -73,7 +70,7 @@ public class ConfigurationMetadata {
* @param itemHint the item hint to add
*/
public
void
add
(
ItemHint
itemHint
)
{
this
.
hints
.
add
(
itemHint
.
getName
(),
itemHint
);
add
(
this
.
hints
,
itemHint
.
getName
(),
itemHint
);
}
/**
...
...
@@ -131,13 +128,22 @@ public class ConfigurationMetadata {
}
}
else
{
this
.
items
.
add
(
metadata
.
getName
(),
metadata
);
add
(
this
.
items
,
metadata
.
getName
(),
metadata
);
}
}
private
<
K
,
V
>
void
add
(
Map
<
K
,
List
<
V
>>
map
,
K
key
,
V
value
)
{
List
<
V
>
values
=
map
.
get
(
key
);
if
(
values
==
null
)
{
values
=
new
ArrayList
<
V
>();
map
.
put
(
key
,
values
);
}
values
.
add
(
value
);
}
private
ItemMetadata
findMatchingItemMetadata
(
ItemMetadata
metadata
)
{
List
<
ItemMetadata
>
candidates
=
this
.
items
.
get
(
metadata
.
getName
());
if
(
CollectionUtils
.
isEmpty
(
candidates
))
{
if
(
candidates
==
null
||
candidates
.
isEmpty
(
))
{
return
null
;
}
ListIterator
<
ItemMetadata
>
it
=
candidates
.
listIterator
();
...
...
@@ -150,14 +156,20 @@ public class ConfigurationMetadata {
return
candidates
.
get
(
0
);
}
for
(
ItemMetadata
candidate
:
candidates
)
{
if
(
ObjectUtils
.
nullSafeEquals
(
candidate
.
getSourceType
(),
metadata
.
getSourceType
()))
{
if
(
nullSafeEquals
(
candidate
.
getSourceType
(),
metadata
.
getSourceType
()))
{
return
candidate
;
}
}
return
null
;
}
private
boolean
nullSafeEquals
(
Object
o1
,
Object
o2
)
{
if
(
o1
==
o2
)
{
return
true
;
}
return
o1
!=
null
&&
o2
!=
null
&&
o1
.
equals
(
o2
);
}
public
static
String
nestedPrefix
(
String
prefix
,
String
name
)
{
String
nestedPrefix
=
(
prefix
==
null
?
""
:
prefix
);
String
dashedName
=
toDashedCase
(
name
);
...
...
@@ -185,8 +197,7 @@ public class ConfigurationMetadata {
return
dashed
.
toString
().
toLowerCase
();
}
private
static
<
T
extends
Comparable
<
T
>>
List
<
T
>
flattenValues
(
MultiValueMap
<?,
T
>
map
)
{
private
static
<
T
extends
Comparable
<
T
>>
List
<
T
>
flattenValues
(
Map
<?,
List
<
T
>>
map
)
{
List
<
T
>
content
=
new
ArrayList
<
T
>();
for
(
List
<
T
>
values
:
map
.
values
())
{
content
.
addAll
(
values
);
...
...
spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JSONOrderedObject.java
0 → 100644
View file @
4bfecc13
/*
* 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
();
}
}
spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonConverter.java
0 → 100644
View file @
4bfecc13
/*
* 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
;
}
}
spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java
View file @
4bfecc13
...
...
@@ -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
));
}
}
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
(
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
()));
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
;
}
result
.
put
(
"parameters"
,
parameters
);
}
return
result
;
}
private
void
putIfPresent
(
JSONObject
jsonObject
,
String
name
,
Object
value
)
{
if
(
value
!=
null
)
{
jsonObject
.
put
(
name
,
value
);
}
}
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
));
if
(
ex
instanceof
RuntimeException
)
{
throw
(
RuntimeException
)
ex
;
}
defaultValue
=
array
;
throw
new
IllegalStateException
(
ex
);
}
return
defaultValue
;
}
public
ConfigurationMetadata
read
(
InputStream
inputStream
)
throws
IO
Exception
{
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
;
}
}
}
spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java
View file @
4bfecc13
...
...
@@ -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
IO
Exception
{
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
();
...
...
spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestConfigurationMetadataAnnotationProcessor.java
View file @
4bfecc13
...
...
@@ -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
,
...
...
spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java
View file @
4bfecc13
...
...
@@ -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
IO
Exception
{
public
void
marshallAndUnmarshal
()
throws
Exception
{
ConfigurationMetadata
metadata
=
new
ConfigurationMetadata
();
metadata
.
add
(
ItemMetadata
.
newProperty
(
"a"
,
"b"
,
StringBuffer
.
class
.
getName
(),
InputStream
.
class
.
getName
(),
"sourceMethod"
,
"desc"
,
"x"
,
...
...
spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/TestJsonConverter.java
0 → 100644
View file @
4bfecc13
/*
* 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
{
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment