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/11 06:14:09 UTC

[02/10] git commit: Rest API sorts locations by name

Rest API sorts locations by name


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

Branch: refs/heads/master
Commit: f8756e27f8781b96ab515dcc2ea0692c3717851c
Parents: 73dcb1d
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Thu Jul 3 17:42:55 2014 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Wed Jul 9 14:28:40 2014 +0100

----------------------------------------------------------------------
 .../rest/resources/LocationResource.java        | 70 ++++++++++++--------
 1 file changed, 41 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f8756e27/usage/rest-server/src/main/java/brooklyn/rest/resources/LocationResource.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/brooklyn/rest/resources/LocationResource.java b/usage/rest-server/src/main/java/brooklyn/rest/resources/LocationResource.java
index b2dc7e7..5ff2955 100644
--- a/usage/rest-server/src/main/java/brooklyn/rest/resources/LocationResource.java
+++ b/usage/rest-server/src/main/java/brooklyn/rest/resources/LocationResource.java
@@ -1,6 +1,7 @@
 package brooklyn.rest.resources;
 
 import java.net.URI;
+import java.util.Comparator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -27,6 +28,7 @@ import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.text.Identifiers;
 
 import com.google.common.base.Function;
+import com.google.common.collect.FluentIterable;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
@@ -34,35 +36,45 @@ import com.google.common.collect.Sets;
 public class LocationResource extends AbstractBrooklynRestResource implements LocationApi {
 
     private static final Logger log = LoggerFactory.getLogger(LocationResource.class);
-    
+
     private final Set<String> specsWarnedOnException = Sets.newConcurrentHashSet();
-    
+
     @Override
-  public List<LocationSummary> list() {
-    return Lists.newArrayList(Iterables.filter(Iterables.transform(brooklyn().getLocationRegistry().getDefinedLocations().values(),
-        new Function<LocationDefinition, LocationSummary>() {
-          @Override
-          public LocationSummary apply(LocationDefinition l) {
-              try {
-                  return LocationTransformer.newInstance(mgmt(), l, LocationDetailLevel.LOCAL_EXCLUDING_SECRET);
-              } catch (Exception e) {
-                  Exceptions.propagateIfFatal(e);
-                  String spec = l.getSpec();
-                  if (spec == null || specsWarnedOnException.add(spec)) {
-                      log.warn("Unable to find details of location {} in REST call to list (ignoring location): {}", l, e);
-                      if (log.isDebugEnabled()) log.debug("Error details for location "+l, e);
-                  } else {
-                      if (log.isTraceEnabled()) log.trace("Unable again to find details of location {} in REST call to list (ignoring location): {}", l, e);
-                  }
-                  return null;
-              }
-          }
-        }), LocationSummary.class));
-  }
+    public List<LocationSummary> list() {
+        Function<LocationDefinition, LocationSummary> transformer = new Function<LocationDefinition, LocationSummary>() {
+            @Override
+            public LocationSummary apply(LocationDefinition l) {
+                try {
+                    return LocationTransformer.newInstance(mgmt(), l, LocationDetailLevel.LOCAL_EXCLUDING_SECRET);
+                } catch (Exception e) {
+                    Exceptions.propagateIfFatal(e);
+                    String spec = l.getSpec();
+                    if (spec == null || specsWarnedOnException.add(spec)) {
+                        log.warn("Unable to find details of location {} in REST call to list (ignoring location): {}", l, e);
+                        if (log.isDebugEnabled()) log.debug("Error details for location " + l, e);
+                    } else {
+                        if (log.isTraceEnabled())
+                            log.trace("Unable again to find details of location {} in REST call to list (ignoring location): {}", l, e);
+                    }
+                    return null;
+                }
+            }
+        };
+        Comparator<LocationSummary> nameComparator = new Comparator<LocationSummary>() {
+            @Override
+            public int compare(LocationSummary o1, LocationSummary o2) {
+                return o1.getName().compareTo(o2.getName());
+            }
+        };
+        return FluentIterable.from(brooklyn().getLocationRegistry().getDefinedLocations().values())
+                .transform(transformer)
+                .filter(LocationSummary.class)
+                .toSortedList(nameComparator);
+    }
 
-  // this is here to support the web GUI's circles
+    // this is here to support the web GUI's circles
     @Override
-  public Map<String,Map<String,Object>> getLocatedLocations() {
+    public Map<String,Map<String,Object>> getLocatedLocations() {
       Map<String,Map<String,Object>> result = new LinkedHashMap<String,Map<String,Object>>();
       Map<Location, Integer> counts = new EntityLocationUtils(mgmt()).countLeafEntitiesByLocatedLocations();
       for (Map.Entry<Location,Integer> count: counts.entrySet()) {
@@ -77,25 +89,25 @@ public class LocationResource extends AbstractBrooklynRestResource implements Lo
           result.put(l.getId(), m);
       }
       return result;
-  }
+    }
 
   /** @deprecated since 0.7.0; REST call now handled by below (optional query parameter added) */
   public LocationSummary get(String locationId) {
       return get(locationId, false);
   }
-  
+
   @Override
   public LocationSummary get(String locationId, String fullConfig) {
       return get(locationId, Boolean.valueOf(fullConfig));
   }
-  
+
   public LocationSummary get(String locationId, boolean fullConfig) {
       LocationDetailLevel configLevel = fullConfig ? LocationDetailLevel.FULL_EXCLUDING_SECRET : LocationDetailLevel.LOCAL_EXCLUDING_SECRET;
       Location l1 = mgmt().getLocationManager().getLocation(locationId);
       if (l1!=null) {
         return LocationTransformer.newInstance(mgmt(), l1, configLevel);
     }
-      
+
       LocationDefinition l2 = brooklyn().getLocationRegistry().getDefinedLocationById(locationId);
       if (l2==null) throw WebResourceUtils.notFound("No location matching %s", locationId);
       return LocationTransformer.newInstance(mgmt(), l2, configLevel);