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
0e798a32
Commit
0e798a32
authored
Aug 09, 2013
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix issue with new format of VCAP_APPLICATION
parent
08e67975
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
26 deletions
+80
-26
SimpleJsonParser.java
...ava/org/springframework/boot/config/SimpleJsonParser.java
+16
-10
VcapApplicationContextInitializer.java
...ontext/initializer/VcapApplicationContextInitializer.java
+45
-15
VcapApplicationContextInitializerTests.java
...t/initializer/VcapApplicationContextInitializerTests.java
+19
-1
No files found.
spring-boot/src/main/java/org/springframework/boot/config/SimpleJsonParser.java
View file @
0e798a32
...
@@ -37,22 +37,28 @@ public class SimpleJsonParser implements JsonParser {
...
@@ -37,22 +37,28 @@ public class SimpleJsonParser implements JsonParser {
@Override
@Override
public
Map
<
String
,
Object
>
parseMap
(
String
json
)
{
public
Map
<
String
,
Object
>
parseMap
(
String
json
)
{
if
(
json
.
startsWith
(
"{"
))
{
if
(
json
!=
null
)
{
return
parseMapInternal
(
json
);
json
=
json
.
trim
();
}
if
(
json
.
startsWith
(
"{"
))
{
else
if
(
json
.
trim
().
equals
(
""
))
{
return
parseMapInternal
(
json
);
return
new
HashMap
<
String
,
Object
>();
}
else
if
(
json
.
equals
(
""
))
{
return
new
HashMap
<
String
,
Object
>();
}
}
}
return
null
;
return
null
;
}
}
@Override
@Override
public
List
<
Object
>
parseList
(
String
json
)
{
public
List
<
Object
>
parseList
(
String
json
)
{
if
(
json
.
startsWith
(
"["
))
{
if
(
json
!=
null
)
{
return
parseListInternal
(
json
);
json
=
json
.
trim
();
}
if
(
json
.
startsWith
(
"["
))
{
else
if
(
json
.
trim
().
equals
(
""
))
{
return
parseListInternal
(
json
);
return
new
ArrayList
<
Object
>();
}
else
if
(
json
.
trim
().
equals
(
""
))
{
return
new
ArrayList
<
Object
>();
}
}
}
return
null
;
return
null
;
}
}
...
...
spring-boot/src/main/java/org/springframework/boot/context/initializer/VcapApplicationContextInitializer.java
View file @
0e798a32
...
@@ -18,11 +18,14 @@ package org.springframework.boot.context.initializer;
...
@@ -18,11 +18,14 @@ package org.springframework.boot.context.initializer;
import
java.util.Collection
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.Map.Entry
;
import
java.util.Properties
;
import
java.util.Properties
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.boot.config.JsonParser
;
import
org.springframework.boot.config.JsonParser
;
import
org.springframework.boot.config.JsonParserFactory
;
import
org.springframework.boot.config.JsonParserFactory
;
import
org.springframework.context.ApplicationContextInitializer
;
import
org.springframework.context.ApplicationContextInitializer
;
...
@@ -83,6 +86,9 @@ import org.springframework.util.StringUtils;
...
@@ -83,6 +86,9 @@ import org.springframework.util.StringUtils;
public
class
VcapApplicationContextInitializer
implements
public
class
VcapApplicationContextInitializer
implements
ApplicationContextInitializer
<
ConfigurableApplicationContext
>,
Ordered
{
ApplicationContextInitializer
<
ConfigurableApplicationContext
>,
Ordered
{
private
static
final
Log
logger
=
LogFactory
.
getLog
(
VcapApplicationContextInitializer
.
class
);
private
static
final
String
VCAP_APPLICATION
=
"VCAP_APPLICATION"
;
private
static
final
String
VCAP_APPLICATION
=
"VCAP_APPLICATION"
;
private
static
final
String
VCAP_SERVICES
=
"VCAP_SERVICES"
;
private
static
final
String
VCAP_SERVICES
=
"VCAP_SERVICES"
;
...
@@ -136,30 +142,54 @@ public class VcapApplicationContextInitializer implements
...
@@ -136,30 +142,54 @@ public class VcapApplicationContextInitializer implements
}
}
private
Properties
getPropertiesFromApplication
(
Environment
environment
)
{
private
Properties
getPropertiesFromApplication
(
Environment
environment
)
{
Map
<
String
,
Object
>
map
=
this
.
parser
.
parseMap
(
environment
.
getProperty
(
VCAP_APPLICATION
,
"{}"
));
Properties
properties
=
new
Properties
();
Properties
properties
=
new
Properties
();
properties
.
putAll
(
map
);
try
{
Map
<
String
,
Object
>
map
=
this
.
parser
.
parseMap
(
environment
.
getProperty
(
VCAP_APPLICATION
,
"{}"
));
if
(
map
!=
null
)
{
map
=
new
LinkedHashMap
<
String
,
Object
>(
map
);
for
(
String
key
:
map
.
keySet
())
{
Object
value
=
map
.
get
(
key
);
if
(!(
value
instanceof
String
))
{
if
(
value
==
null
)
{
value
=
""
;
}
map
.
put
(
key
,
value
.
toString
());
}
}
properties
.
putAll
(
map
);
}
}
catch
(
IllegalArgumentException
e
)
{
logger
.
error
(
"Could not parse VCAP_APPLICATION"
,
e
);
}
return
properties
;
return
properties
;
}
}
private
Properties
getPropertiesFromServices
(
Environment
environment
)
{
private
Properties
getPropertiesFromServices
(
Environment
environment
)
{
Map
<
String
,
Object
>
map
=
this
.
parser
.
parseMap
(
environment
.
getProperty
(
VCAP_SERVICES
,
"{}"
));
Properties
properties
=
new
Properties
();
Properties
properties
=
new
Properties
();
for
(
Object
services
:
map
.
values
())
{
try
{
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
map
=
this
.
parser
.
parseMap
(
environment
.
getProperty
(
List
<
Object
>
list
=
(
List
<
Object
>)
services
;
VCAP_SERVICES
,
"{}"
));
for
(
Object
object
:
list
)
{
if
(
map
!=
null
)
{
@SuppressWarnings
(
"unchecked"
)
for
(
Object
services
:
map
.
values
())
{
Map
<
String
,
Object
>
service
=
(
Map
<
String
,
Object
>)
object
;
@SuppressWarnings
(
"unchecked"
)
String
key
=
(
String
)
service
.
get
(
"name"
);
List
<
Object
>
list
=
(
List
<
Object
>)
services
;
if
(
key
==
null
)
{
for
(
Object
object
:
list
)
{
key
=
(
String
)
service
.
get
(
"label"
);
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
service
=
(
Map
<
String
,
Object
>)
object
;
String
key
=
(
String
)
service
.
get
(
"name"
);
if
(
key
==
null
)
{
key
=
(
String
)
service
.
get
(
"label"
);
}
flatten
(
properties
,
service
,
key
);
}
}
}
flatten
(
properties
,
service
,
key
);
}
}
}
}
catch
(
IllegalArgumentException
e
)
{
logger
.
error
(
"Could not parse VCAP_APPLICATION"
,
e
);
}
return
properties
;
return
properties
;
}
}
...
...
spring-boot/src/test/java/org/springframework/boot/context/initializer/VcapApplicationContextInitializerTests.java
View file @
0e798a32
...
@@ -18,7 +18,6 @@ package org.springframework.boot.context.initializer;
...
@@ -18,7 +18,6 @@ package org.springframework.boot.context.initializer;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.TestUtils
;
import
org.springframework.boot.TestUtils
;
import
org.springframework.boot.context.initializer.VcapApplicationContextInitializer
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
...
@@ -45,6 +44,25 @@ public class VcapApplicationContextInitializerTests {
...
@@ -45,6 +44,25 @@ public class VcapApplicationContextInitializerTests {
.
getProperty
(
"vcap.application.instance_id"
));
.
getProperty
(
"vcap.application.instance_id"
));
}
}
@Test
public
void
testUnparseableApplicationProperties
()
{
ConfigurableApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
TestUtils
.
addEnviroment
(
context
,
"VCAP_APPLICATION:"
);
this
.
initializer
.
initialize
(
context
);
assertEquals
(
null
,
context
.
getEnvironment
().
getProperty
(
"vcap"
));
}
@Test
public
void
testNullApplicationProperties
()
{
ConfigurableApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
TestUtils
.
addEnviroment
(
context
,
"VCAP_APPLICATION:{\"application_users\":null,\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\",\"name\":\"foo\",\"uris\":[\"foo.cfapps.io\"],\"started_at\":\"2013-05-29 02:37:59 +0000\",\"started_at_timestamp\":1369795079,\"host\":\"0.0.0.0\",\"port\":61034,\"limits\":{\"mem\":128,\"disk\":1024,\"fds\":16384},\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\",\"name\":\"dsyerenv\",\"uris\":[\"dsyerenv.cfapps.io\"],\"users\":[],\"start\":\"2013-05-29 02:37:59 +0000\",\"state_timestamp\":1369795079}"
);
this
.
initializer
.
initialize
(
context
);
assertEquals
(
null
,
context
.
getEnvironment
().
getProperty
(
"vcap"
));
}
@Test
@Test
public
void
testServiceProperties
()
{
public
void
testServiceProperties
()
{
ConfigurableApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
ConfigurableApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
...
...
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