You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2014/07/04 11:51:17 UTC
[35/45] git commit: Simplify custom attributes in
BrooklynEntityMatcher
Simplify custom attributes in BrooklynEntityMatcher
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/24c78d5e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/24c78d5e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/24c78d5e
Branch: refs/heads/master
Commit: 24c78d5e71b0c979ce0077416e5aac6f4793d17e
Parents: e96af92
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Thu Jul 3 15:04:19 2014 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Thu Jul 3 16:18:54 2014 +0100
----------------------------------------------------------------------
.../spi/creation/BrooklynEntityMatcher.java | 69 ++++++++------------
1 file changed, 27 insertions(+), 42 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/24c78d5e/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityMatcher.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityMatcher.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityMatcher.java
index fd58bac..d8e8fc6 100644
--- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityMatcher.java
+++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityMatcher.java
@@ -110,48 +110,12 @@ public class BrooklynEntityMatcher implements PdpMatcher {
// (any other brooklyn config goes here)
if (!brooklynConfig.isEmpty())
builder.customAttribute("brooklyn.config", brooklynConfig);
-
- List<Object> brooklynPolicies = Lists.newArrayList();
- Object origBrooklynPolicies = attrs.remove("brooklyn.policies");
- if (origBrooklynPolicies != null) {
- if (!(origBrooklynPolicies instanceof List))
- throw new IllegalArgumentException("brooklyn.policies must be a list of brooklyn policy definitions");
- brooklynPolicies.addAll((List<?>)origBrooklynPolicies);
- }
- if (!brooklynPolicies.isEmpty())
- builder.customAttribute("brooklyn.policies", brooklynPolicies);
-
- List<Object> brooklynEnrichers = Lists.newArrayList();
- Object origBrooklynEnrichers = attrs.remove("brooklyn.enrichers");
- if (origBrooklynEnrichers != null) {
- if (!(origBrooklynEnrichers instanceof List))
- throw new IllegalArgumentException("brooklyn.enrichers must be a list of brooklyn enricher definitions");
- brooklynEnrichers.addAll((List<?>)origBrooklynEnrichers);
- }
- if (!brooklynEnrichers.isEmpty())
- builder.customAttribute("brooklyn.enrichers", brooklynEnrichers);
-
- List<Object> brooklynInitializers = Lists.newArrayList();
- Object origBrooklynInitializers = attrs.remove("brooklyn.initializers");
- if (origBrooklynInitializers != null) {
- if (!(origBrooklynInitializers instanceof List))
- throw new IllegalArgumentException("brooklyn.initializers must be a list of brooklyn initializer definitions");
- brooklynInitializers.addAll((List<?>)origBrooklynInitializers);
- }
- if (!brooklynInitializers.isEmpty())
- builder.customAttribute("brooklyn.initializers", brooklynInitializers);
-
- List<Object> brooklynChildren = Lists.newArrayList();
- Object origBrooklynChildren = attrs.remove("brooklyn.children");
- if (origBrooklynChildren != null) {
- if (!(origBrooklynChildren instanceof List))
- throw new IllegalArgumentException("brooklyn.children must be a list of brooklyn entity definitions");
- brooklynChildren.addAll((List<?>)origBrooklynChildren);
- }
-
- if (!brooklynChildren.isEmpty())
- builder.customAttribute("brooklyn.children", brooklynChildren);
-
+
+ addCustomListAttributeIfNonNull(builder, attrs, "brooklyn.policies");
+ addCustomListAttributeIfNonNull(builder, attrs, "brooklyn.enrichers");
+ addCustomListAttributeIfNonNull(builder, attrs, "brooklyn.initializers");
+ addCustomListAttributeIfNonNull(builder, attrs, "brooklyn.children");
+
if (!attrs.isEmpty()) {
log.warn("Ignoring PDP attributes on "+deploymentPlanItem+": "+attrs);
}
@@ -161,6 +125,27 @@ public class BrooklynEntityMatcher implements PdpMatcher {
return true;
}
+ /**
+ * Looks for the given key in the map of attributes and adds it to the given builder
+ * as a custom attribute with type List.
+ * @throws java.lang.IllegalArgumentException if map[key] is not an instance of List
+ */
+ private void addCustomListAttributeIfNonNull(Builder<? extends PlatformComponentTemplate> builder, Map attrs, String key) {
+ Object items = attrs.remove(key);
+ if (items != null) {
+ if (items instanceof List) {
+ List<?> itemList = (List<?>) items;
+ if (!itemList.isEmpty()) {
+ builder.customAttribute(key, Lists.newArrayList(itemList));
+ }
+ } else {
+ throw new IllegalArgumentException(key + " must be a list, is: " + items.getClass().getName());
+ }
+ }
+ }
+ }
+ }
+
/** finds flags and keys on the given typeName which are present in the given map;
* returns those (using the config key name), and removes them from attrs
*/