brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [1/3] git commit: fix dto.entries repetition errors by improving the api for access to internal fields
Date Mon, 03 Nov 2014 20:25:49 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 83011fe09 -> b18b18948


fix dto.entries repetition errors by improving the api for access to internal fields

not as clean as it could be, but quick and safe, as this is needed in production systems


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

Branch: refs/heads/master
Commit: ed92b0e341ada0dc7ae277960c3de34632bd3b44
Parents: f3b967f
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Fri Oct 31 12:53:37 2014 -0500
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Fri Oct 31 12:53:37 2014 -0500

----------------------------------------------------------------------
 .../brooklyn/catalog/internal/CatalogDo.java    | 14 ++++----
 .../brooklyn/catalog/internal/CatalogDto.java   | 35 +++++++++++++++++++-
 .../catalog/internal/CatalogLoadTest.java       |  4 +--
 3 files changed, 42 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ed92b0e3/core/src/main/java/brooklyn/catalog/internal/CatalogDo.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/catalog/internal/CatalogDo.java b/core/src/main/java/brooklyn/catalog/internal/CatalogDo.java
index cb40839..6b7b52b 100644
--- a/core/src/main/java/brooklyn/catalog/internal/CatalogDo.java
+++ b/core/src/main/java/brooklyn/catalog/internal/CatalogDo.java
@@ -30,6 +30,7 @@ import org.slf4j.LoggerFactory;
 import brooklyn.catalog.internal.CatalogClasspathDo.CatalogScanningModes;
 import brooklyn.management.ManagementContext;
 import brooklyn.management.internal.ManagementContextInternal;
+import brooklyn.util.collections.MutableList;
 import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.javalang.AggregateClassLoader;
 import brooklyn.util.net.Urls;
@@ -120,7 +121,7 @@ public class CatalogDo {
     }
 
     private void loadCatalogItems() {
-        List<CatalogItemDtoAbstract<?, ?>> entries = dto.entries;
+        Iterable<CatalogItemDtoAbstract<?, ?>> entries = dto.getUniqueEntries();
         if (entries!=null) {
             for (CatalogItemDtoAbstract<?,?> entry : entries) {
                 CatalogUtils.installLibraries(mgmt, entry.getLibrariesDto());
@@ -190,8 +191,8 @@ public class CatalogDo {
                 cache.putAll(child.getIdCache());
             }
         }
-        if (dto.entries!=null) {
-            List<CatalogItemDtoAbstract<?,?>> entriesReversed = new ArrayList<CatalogItemDtoAbstract<?,?>>(dto.entries);
+        if (dto.getUniqueEntries()!=null) {
+            List<CatalogItemDtoAbstract<?,?>> entriesReversed = MutableList.copyOf(dto.getUniqueEntries());
             Collections.reverse(entriesReversed);
             for (CatalogItemDtoAbstract<?,?> entry: entriesReversed)
                 cache.put(entry.getId(), new CatalogItemDo(this, entry));
@@ -221,9 +222,7 @@ public class CatalogDo {
      */
     @SuppressWarnings({ "unchecked", "rawtypes" })
     public synchronized void addEntry(CatalogItemDtoAbstract<?,?> entry) {
-        if (dto.entries == null)
-            dto.entries = new ArrayList<CatalogItemDtoAbstract<?,?>>();
-        dto.entries.add(entry);
+        dto.addEntry(entry);
         if (cacheById != null) {
             CatalogItemDo<?, ?> cdo = new CatalogItemDo(this, entry);
             cacheById.put(entry.getId(), cdo);
@@ -238,8 +237,7 @@ public class CatalogDo {
      * Removes the given entry from the catalog.
      */
     public synchronized void deleteEntry(CatalogItemDtoAbstract<?, ?> entry) {
-        if (dto.entries != null)
-            dto.entries.remove(entry);
+        dto.removeEntry(entry);
         if (cacheById != null) {
             cacheById.remove(entry.getId());
             cacheByRegisteredTypeName.remove(entry.getRegisteredTypeName());

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ed92b0e3/core/src/main/java/brooklyn/catalog/internal/CatalogDto.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/catalog/internal/CatalogDto.java b/core/src/main/java/brooklyn/catalog/internal/CatalogDto.java
index fd74122..1ea79ce 100644
--- a/core/src/main/java/brooklyn/catalog/internal/CatalogDto.java
+++ b/core/src/main/java/brooklyn/catalog/internal/CatalogDto.java
@@ -22,12 +22,15 @@ import java.io.InputStream;
 import java.io.StringReader;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import brooklyn.catalog.CatalogItem;
 import brooklyn.util.ResourceUtils;
+import brooklyn.util.collections.MutableList;
+import brooklyn.util.collections.MutableMap;
 import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.exceptions.PropagatedRuntimeException;
 import brooklyn.util.stream.Streams;
@@ -49,7 +52,8 @@ public class CatalogDto {
     String name;
     String description;
     CatalogClasspathDto classpath;
-    List<CatalogItemDtoAbstract<?,?>> entries = null;
+    private List<CatalogItemDtoAbstract<?,?>> entries = null;
+//    Map<String,CatalogItemDtoAbstract<?,?>> entries = null;
     
     // for thread-safety, any dynamic additions to this should be handled by a method 
     // in this class which does copy-on-write
@@ -178,4 +182,33 @@ public class CatalogDto {
                 .toString();
     }
 
+    // temporary fix for issue where entries might not be unique
+    Iterable<CatalogItemDtoAbstract<?, ?>> getUniqueEntries() {
+        if (entries==null) return null;
+        Map<String, CatalogItemDtoAbstract<?, ?>> result = getEntriesMap();
+        return result.values();
+    }
+
+    private Map<String, CatalogItemDtoAbstract<?, ?>> getEntriesMap() {
+        if (entries==null) return null;
+        Map<String,CatalogItemDtoAbstract<?, ?>> result = MutableMap.of();
+        for (CatalogItemDtoAbstract<?,?> entry: entries) {
+            result.put(entry.getId(), entry);
+        }
+        return result;
+    }
+
+    void removeEntry(CatalogItemDtoAbstract<?, ?> entry) {
+        if (entries!=null)
+            entries.remove(entry);
+    }
+
+    void addEntry(CatalogItemDtoAbstract<?, ?> entry) {
+        if (entries == null) entries = MutableList.of();
+        CatalogItemDtoAbstract<?, ?> oldEntry = getEntriesMap().get(entry.getId());
+        entries.add(entry);
+        if (oldEntry!=null)
+            removeEntry(oldEntry);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ed92b0e3/core/src/test/java/brooklyn/catalog/internal/CatalogLoadTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/catalog/internal/CatalogLoadTest.java b/core/src/test/java/brooklyn/catalog/internal/CatalogLoadTest.java
index d8cd0b8..e9102c2 100644
--- a/core/src/test/java/brooklyn/catalog/internal/CatalogLoadTest.java
+++ b/core/src/test/java/brooklyn/catalog/internal/CatalogLoadTest.java
@@ -49,9 +49,9 @@ public class CatalogLoadTest {
                 loadFile("classpath://brooklyn/catalog/internal/osgi-catalog.xml"));
         assertNotNull(catalog);
         assertEquals(catalog.name, "OSGi catalogue");
-        assertEquals(catalog.entries.size(), 1, "Catalog entries = " + Joiner.on(", ").join(catalog.entries));
+        assertEquals(Iterables.size(catalog.getUniqueEntries()), 1, "Catalog entries = "
+ Joiner.on(", ").join(catalog.getUniqueEntries()));
 
-        CatalogItemDtoAbstract<?,?> template = Iterables.getOnlyElement(catalog.entries);
+        CatalogItemDtoAbstract<?,?> template = Iterables.getOnlyElement(catalog.getUniqueEntries());
         assertEquals(template.getDisplayName(), "Entity name");
         assertEquals(template.getVersion(), "9.1.3");
         assertEquals(template.getJavaType(), "com.example.ExampleApp");


Mime
View raw message