Return-Path: X-Original-To: apmail-brooklyn-commits-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2D88F104E4 for ; Fri, 11 Jul 2014 04:14:35 +0000 (UTC) Received: (qmail 60063 invoked by uid 500); 11 Jul 2014 04:14:35 -0000 Delivered-To: apmail-brooklyn-commits-archive@brooklyn.apache.org Received: (qmail 60044 invoked by uid 500); 11 Jul 2014 04:14:35 -0000 Mailing-List: contact commits-help@brooklyn.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.incubator.apache.org Delivered-To: mailing list commits@brooklyn.incubator.apache.org Received: (qmail 60034 invoked by uid 99); 11 Jul 2014 04:14:34 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Jul 2014 04:14:34 +0000 X-ASF-Spam-Status: No, hits=-2000.7 required=5.0 tests=ALL_TRUSTED,RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Fri, 11 Jul 2014 04:14:35 +0000 Received: (qmail 59651 invoked by uid 99); 11 Jul 2014 04:14:09 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Jul 2014 04:14:09 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id E69EA9AC721; Fri, 11 Jul 2014 04:14:08 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: heneveld@apache.org To: commits@brooklyn.incubator.apache.org Date: Fri, 11 Jul 2014 04:14:09 -0000 Message-Id: <490dc82317da4ccf8e4e6b156106a88e@git.apache.org> In-Reply-To: <95ed02c9d1a04dfda8602df3f1076ebb@git.apache.org> References: <95ed02c9d1a04dfda8602df3f1076ebb@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [02/10] git commit: Rest API sorts locations by name X-Virus-Checked: Checked by ClamAV on apache.org 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 Authored: Thu Jul 3 17:42:55 2014 +0100 Committer: Sam Corbett 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 specsWarnedOnException = Sets.newConcurrentHashSet(); - + @Override - public List list() { - return Lists.newArrayList(Iterables.filter(Iterables.transform(brooklyn().getLocationRegistry().getDefinedLocations().values(), - new Function() { - @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 list() { + Function transformer = new Function() { + @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 nameComparator = new Comparator() { + @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> getLocatedLocations() { + public Map> getLocatedLocations() { Map> result = new LinkedHashMap>(); Map counts = new EntityLocationUtils(mgmt()).countLeafEntitiesByLocatedLocations(); for (Map.Entry 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);