brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [3/4] incubator-brooklyn git commit: Swap catalogue interfaces for catalogue traits
Date Tue, 24 Nov 2015 21:00:01 GMT
Swap catalogue interfaces for catalogue traits

"Interfaces" is too specific for a feature whose purpose is to guide
consumers about the capabilities of a catalogue item and does not lend
itself to the use or addition of other metadata.

Also: InterfacesTag maintains a List<String> rather than List<Class<?>>
and CatalogTransformer.makeTags handles null specs.


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/641247fd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/641247fd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/641247fd

Branch: refs/heads/master
Commit: 641247fd94b9aca2964aef9e016aabd8951816b7
Parents: d6eada1
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Tue Nov 24 14:38:24 2015 +0000
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Tue Nov 24 14:38:24 2015 +0000

----------------------------------------------------------------------
 .../catalog/internal/CatalogClasspathDo.java    |  2 +-
 .../apache/brooklyn/core/mgmt/BrooklynTags.java | 58 +++++++++++++-------
 .../rest/domain/CatalogEntitySummary.java       |  1 +
 .../rest/transform/CatalogTransformer.java      | 25 +++++----
 .../rest/resources/CatalogResourceTest.java     | 16 +++---
 5 files changed, 62 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/641247fd/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogClasspathDo.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogClasspathDo.java
b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogClasspathDo.java
index ea07e56..d183c7e 100644
--- a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogClasspathDo.java
+++ b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogClasspathDo.java
@@ -311,7 +311,7 @@ public class CatalogClasspathDo {
             item.setIconUrl(firstNonEmpty(catalogAnnotation.iconUrl()));
         }
         if (item instanceof CatalogEntityItemDto || item instanceof CatalogTemplateItemDto)
{
-            item.tags().addTag(BrooklynTags.newInterfacesTag(ClassUtils.getAllInterfaces(c)));
+            item.tags().addTag(BrooklynTags.newTraitsTag(ClassUtils.getAllInterfaces(c)));
         }
         if (log.isTraceEnabled())
             log.trace("adding to catalog: "+c+" (from catalog "+catalog+")");

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/641247fd/core/src/main/java/org/apache/brooklyn/core/mgmt/BrooklynTags.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/BrooklynTags.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/BrooklynTags.java
index f47171b..e8d7915 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/BrooklynTags.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/BrooklynTags.java
@@ -21,10 +21,13 @@ package org.apache.brooklyn.core.mgmt;
 import java.io.Serializable;
 import java.util.List;
 
+import org.codehaus.jackson.annotate.JsonIgnore;
 import org.codehaus.jackson.annotate.JsonProperty;
 
 import com.google.common.annotations.Beta;
+import com.google.common.base.Function;
 import com.google.common.base.Objects;
+import com.google.common.collect.Lists;
 
 /** @since 0.7.0 some strongly typed tags for reference; note these may migrate elsewhere!
*/
 @Beta
@@ -54,40 +57,57 @@ public class BrooklynTags {
         }
     }
 
-    public static class InterfacesTag {
-        @JsonProperty
-        final List<Class<?>> interfaces;
+    public static class ListTag<T> {
+        @JsonIgnore
+        final List<T> list;
 
-        public InterfacesTag(List<Class<?>> interfaces) {
-            this.interfaces = interfaces;
+        public ListTag(List<T> list) {
+            this.list = list;
         }
 
-        public List<Class<?>> getInterfaces() {
-            return interfaces;
+        public List<T> getList() {
+            return list;
         }
 
         @Override
         public boolean equals(Object o) {
             if (this == o) return true;
             if (o == null || getClass() != o.getClass()) return false;
-
-            InterfacesTag that = (InterfacesTag) o;
-
-            return interfaces == null
-                    ? that.interfaces == null
-                    : interfaces.equals(that.interfaces);
+            ListTag that = (ListTag) o;
+            return list == null ? that.list == null : list.equals(that.list);
         }
 
         @Override
         public int hashCode() {
-            return Objects.hashCode(interfaces);
+            return Objects.hashCode(list);
         }
     }
-    
-    public static NamedStringTag newYamlSpecTag(String contents) { return new NamedStringTag(YAML_SPEC_KIND,
contents); }
-    public static NamedStringTag newNotesTag(String contents) { return new NamedStringTag(NOTES_KIND,
contents); }
-    public static InterfacesTag newInterfacesTag(List<Class<?>> interfaces) {
-        return new InterfacesTag(interfaces);
+
+    public static class TraitsTag extends ListTag<String> {
+        public TraitsTag(List<Class<?>> interfaces) {
+            super(Lists.transform(interfaces, new Function<Class<?>, String>()
{
+                @Override public String apply(Class<?> input) {
+                    return input.getName();
+                }
+            }));
+        }
+
+        @JsonProperty("traits")
+        public List<String> getTraits() {
+            return super.list;
+        }
+    }
+
+    public static NamedStringTag newYamlSpecTag(String contents) {
+        return new NamedStringTag(YAML_SPEC_KIND, contents);
+    }
+
+    public static NamedStringTag newNotesTag(String contents) {
+        return new NamedStringTag(NOTES_KIND, contents);
+    }
+
+    public static TraitsTag newTraitsTag(List<Class<?>> interfaces) {
+        return new TraitsTag(interfaces);
     }
     
     public static NamedStringTag findFirst(String kind, Iterable<Object> tags) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/641247fd/usage/rest-api/src/main/java/org/apache/brooklyn/rest/domain/CatalogEntitySummary.java
----------------------------------------------------------------------
diff --git a/usage/rest-api/src/main/java/org/apache/brooklyn/rest/domain/CatalogEntitySummary.java
b/usage/rest-api/src/main/java/org/apache/brooklyn/rest/domain/CatalogEntitySummary.java
index 4d39ed0..48e49cb 100644
--- a/usage/rest-api/src/main/java/org/apache/brooklyn/rest/domain/CatalogEntitySummary.java
+++ b/usage/rest-api/src/main/java/org/apache/brooklyn/rest/domain/CatalogEntitySummary.java
@@ -35,6 +35,7 @@ public class CatalogEntitySummary extends CatalogItemSummary {
     
     @JsonSerialize(include=Inclusion.NON_EMPTY)
     private final Set<SensorSummary> sensors;
+
     @JsonSerialize(include=Inclusion.NON_EMPTY)
     private final Set<EffectorSummary> effectors;
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/641247fd/usage/rest-server/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java
b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java
index 332132b..0f710bc 100644
--- a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java
+++ b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java
@@ -51,7 +51,7 @@ import org.apache.brooklyn.rest.util.BrooklynRestResourceUtils;
 import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.exceptions.Exceptions;
-import org.apache.commons.lang.ClassUtils;
+import org.apache.brooklyn.util.javalang.Reflections;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.ImmutableSet;
@@ -165,17 +165,20 @@ public class CatalogTransformer {
     }
 
     private static Set<Object> makeTags(EntitySpec<?> spec, CatalogItem<?,
?> item) {
-        Class<?> type = null;
-        if (spec.getImplementation() != null) {
-            type = spec.getImplementation();
-        } else {
-            type = spec.getType();
+        // Combine tags on item with an InterfacesTag.
+        Set<Object> tags = MutableSet.copyOf(item.tags().getTags());
+        if (spec != null) {
+            Class<?> type;
+            if (spec.getImplementation() != null) {
+                type = spec.getImplementation();
+            } else {
+                type = spec.getType();
+            }
+            if (type != null) {
+                tags.add(new BrooklynTags.TraitsTag(Reflections.getAllInterfaces(type)));
+            }
         }
-
-        return MutableSet.builder()
-                .addAll(item.tags().getTags())
-                .add(new BrooklynTags.InterfacesTag(ClassUtils.getAllInterfaces(type)))
-                .build();
+        return tags;
     }
     
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/641247fd/usage/rest-server/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourceTest.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourceTest.java
b/usage/rest-server/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourceTest.java
index 3e0eec2..25d16e7 100644
--- a/usage/rest-server/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourceTest.java
+++ b/usage/rest-server/src/test/java/org/apache/brooklyn/rest/resources/CatalogResourceTest.java
@@ -44,7 +44,7 @@ import org.apache.brooklyn.rest.domain.CatalogLocationSummary;
 import org.apache.brooklyn.rest.domain.CatalogPolicySummary;
 import org.apache.brooklyn.rest.testing.BrooklynRestResourceTest;
 import org.apache.brooklyn.test.support.TestResourceUnavailableException;
-import org.apache.commons.lang3.ClassUtils;
+import org.apache.brooklyn.util.javalang.Reflections;
 import org.apache.http.HttpHeaders;
 import org.apache.http.entity.ContentType;
 import org.eclipse.jetty.http.HttpStatus;
@@ -128,14 +128,12 @@ public class CatalogResourceTest extends BrooklynRestResourceTest {
 
         // an InterfacesTag should be created for every catalog item
         assertEquals(entityItem.getTags().size(), 1);
-        for (Object tag : entityItem.getTags()) {
-            // As tags are treated as object, an InterfacesTag is return as a Map<String,
List<String>> to match what the API expose
-            List<String> actualInterfaces = ((Map<String, List<String>>)
tag).get("interfaces");
-            List<Class<?>> expectedInterfaces = ClassUtils.getAllInterfaces(TestEntity.class);
-            assertEquals(actualInterfaces.size(), expectedInterfaces.size());
-            for (Class<?> expectedInterface : expectedInterfaces) {
-                assertTrue(actualInterfaces.contains(expectedInterface.getName()));
-            }
+        Object tag = entityItem.getTags().iterator().next();
+        List<String> actualInterfaces = ((Map<String, List<String>>) tag).get("traits");
+        List<Class<?>> expectedInterfaces = Reflections.getAllInterfaces(TestEntity.class);
+        assertEquals(actualInterfaces.size(), expectedInterfaces.size());
+        for (Class<?> expectedInterface : expectedInterfaces) {
+            assertTrue(actualInterfaces.contains(expectedInterface.getName()));
         }
 
         byte[] iconData = client().resource("/v1/catalog/icon/" + symbolicName + "/" + TEST_VERSION).get(byte[].class);


Mime
View raw message