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
9fce630c
Commit
9fce630c
authored
Oct 28, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract lists in VCAP_APPLICATION (e.g. uris)
Fixes gh-1773
parent
6c3d490c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
33 deletions
+43
-33
VcapApplicationListener.java
...gframework/boot/cloudfoundry/VcapApplicationListener.java
+29
-30
VcapApplicationListenerTests.java
...ework/boot/cloudfoundry/VcapApplicationListenerTests.java
+14
-3
No files found.
spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java
View file @
9fce630c
...
...
@@ -18,7 +18,6 @@ package org.springframework.boot.cloudfoundry;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
...
...
@@ -71,7 +70,7 @@ import org.springframework.util.StringUtils;
* vcap.application.version: 0138c4a6-2a73-416b-aca0-572c09f7ca53
* vcap.application.name: foo
* vcap.application.uris[0]: foo.cfapps.io
*
*
* vcap.services.mysql.name: mysql
* vcap.services.mysql.label: rds-mysql-1.0
* vcap.services.mysql.credentials.name: d04fb13d27d964c62b267bbba1cffb9da
...
...
@@ -151,19 +150,7 @@ public class VcapApplicationListener implements
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
);
}
extractPropertiesFromApplication
(
properties
,
map
);
}
catch
(
Exception
ex
)
{
logger
.
error
(
"Could not parse VCAP_APPLICATION"
,
ex
);
...
...
@@ -176,21 +163,7 @@ public class VcapApplicationListener implements
try
{
Map
<
String
,
Object
>
map
=
this
.
parser
.
parseMap
(
environment
.
getProperty
(
VCAP_SERVICES
,
"{}"
));
if
(
map
!=
null
)
{
for
(
Object
services
:
map
.
values
())
{
@SuppressWarnings
(
"unchecked"
)
List
<
Object
>
list
=
(
List
<
Object
>)
services
;
for
(
Object
object
:
list
)
{
@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
);
}
}
}
extractPropertiesFromServices
(
properties
,
map
);
}
catch
(
Exception
ex
)
{
logger
.
error
(
"Could not parse VCAP_SERVICES"
,
ex
);
...
...
@@ -198,6 +171,32 @@ public class VcapApplicationListener implements
return
properties
;
}
private
void
extractPropertiesFromApplication
(
Properties
properties
,
Map
<
String
,
Object
>
map
)
{
if
(
map
!=
null
)
{
flatten
(
properties
,
map
,
""
);
}
}
private
void
extractPropertiesFromServices
(
Properties
properties
,
Map
<
String
,
Object
>
map
)
{
if
(
map
!=
null
)
{
for
(
Object
services
:
map
.
values
())
{
@SuppressWarnings
(
"unchecked"
)
List
<
Object
>
list
=
(
List
<
Object
>)
services
;
for
(
Object
object
:
list
)
{
@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
);
}
}
}
}
private
void
flatten
(
Properties
properties
,
Map
<
String
,
Object
>
input
,
String
path
)
{
for
(
Entry
<
String
,
Object
>
entry
:
input
.
entrySet
())
{
String
key
=
entry
.
getKey
();
...
...
spring-boot/src/test/java/org/springframework/boot/cloudfoundry/VcapApplicationListenerTests.java
View file @
9fce630c
...
...
@@ -16,6 +16,9 @@
package
org
.
springframework
.
boot
.
cloudfoundry
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
org.junit.Test
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
;
...
...
@@ -23,9 +26,6 @@ import org.springframework.boot.test.EnvironmentTestUtils;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
/**
* Tests for {@link VcapApplicationListener}.
*
...
...
@@ -49,6 +49,17 @@ public class VcapApplicationListenerTests {
.
getProperty
(
"vcap.application.instance_id"
));
}
@Test
public
void
testApplicationUris
()
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"VCAP_APPLICATION:{\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"uris\":[\"foo.cfapps.io\"]}"
);
this
.
initializer
.
onApplicationEvent
(
this
.
event
);
assertEquals
(
"foo.cfapps.io"
,
this
.
context
.
getEnvironment
().
getProperty
(
"vcap.application.uris[0]"
));
}
@Test
public
void
testUnparseableApplicationProperties
()
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"VCAP_APPLICATION:"
);
...
...
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