brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [3/5] git commit: cleans up some of the mess in LocationRegistry around testing-vs-resolving and managed-vs-unmanaged, deprecating many methods (and removing their calls)
Date Thu, 16 Oct 2014 16:44:23 GMT
cleans up some of the mess in LocationRegistry around testing-vs-resolving and managed-vs-unmanaged,
deprecating many methods (and removing their calls)


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

Branch: refs/heads/master
Commit: d5c11385bcc77def199e33a6e52fccb38daaee78
Parents: 752f161
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Wed Oct 8 21:23:44 2014 -0700
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Thu Oct 16 17:42:52 2014 +0100

----------------------------------------------------------------------
 .../brooklyn/location/LocationRegistry.java     | 43 ++++++++---
 .../location/basic/BasicLocationRegistry.java   | 76 ++++++++++++--------
 .../location/basic/HostLocationResolver.java    |  9 ++-
 .../basic/SingleMachineLocationResolver.java    |  9 ++-
 .../internal/LocalLocationManager.java          |  1 +
 .../basic/LocalhostLocationResolverTest.java    |  9 ++-
 .../SshMachineLocationIntegrationTest.java      |  2 +-
 .../brooklyn/entity/pool/ServerPoolImpl.java    |  2 +-
 .../entity/chef/ChefLiveTestSupport.java        |  4 +-
 .../creation/BrooklynYamlLocationResolver.java  | 46 ++++--------
 .../rest/transform/LocationTransformer.java     | 17 ++---
 .../main/java/brooklyn/util/guava/Maybe.java    |  6 +-
 12 files changed, 131 insertions(+), 93 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/api/src/main/java/brooklyn/location/LocationRegistry.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/location/LocationRegistry.java b/api/src/main/java/brooklyn/location/LocationRegistry.java
index e9db3f2..e5b8cf3 100644
--- a/api/src/main/java/brooklyn/location/LocationRegistry.java
+++ b/api/src/main/java/brooklyn/location/LocationRegistry.java
@@ -22,6 +22,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
 
+import brooklyn.util.guava.Maybe;
+
 import com.google.common.annotations.Beta;
 
 /**
@@ -49,35 +51,54 @@ public interface LocationRegistry {
     /** removes the defined location from the registry (applications running there are unaffected)
*/
     public void removeDefinedLocation(String id);
 
-    /** returns fully populated (config etc) location from the given definition, 
-     * currently by creating it but TODO creation can be a leak so all current 'resolve'
methods should be carefully used! */
+    /** Returns a fully populated (config etc) location from the given definition, with optional
add'l flags.
+     * the location will be managed by default, unless the manage parameter is false or the
CREATE_UNMANAGED flag is set.
+     * @since 0.7.0, but beta and likely to change as the semantics of this class are tuned
*/
+    @Beta
+    public Maybe<Location> resolve(LocationDefinition ld, Boolean manage, Map locationFlags);
+    
+    /** As {@link #resolve(LocationDefinition, Boolean, Map), with the location managed,
and no additional flags,
+     * unwrapping the result (throwing if not resolvable) */
     public Location resolve(LocationDefinition l);
 
+    /** Returns a location created from the given spec, which might correspond to a definition,
or created on-the-fly.
+     * Optional flags can be passed through to underlying the location. 
+     * @since 0.7.0, but beta and likely to change as the semantics of this class are tuned
*/
+    @Beta
+    public Maybe<Location> resolve(String spec, Boolean manage, Map locationFlags);
+    
     /** efficiently returns for inspection only a fully populated (config etc) location from
the given definition; 
      * the value might be unmanaged so it is not meant for any use other than inspection,
      * but callers should prefer this when they don't wish to create a new location which
will be managed in perpetuity!
      * 
-     * @since 0.7.0, but beta and likely to change as the semantics of this class are tuned
*/
-    @Beta   // see impl for notes
+     * @deprecated since 0.7.0, use {@link #resolve(LocationDefinition, Boolean, Map)} */
+    @Deprecated
     public Location resolveForPeeking(LocationDefinition l);
 
-    /** returns fully populated (config etc) location from the given definition */
+    /** returns fully populated (config etc) location from the given definition, with overrides;
+     * @deprecated since 0.7.0, use {@link #resolve(LocationDefinition, Boolean, Map)} */
+    @Deprecated
     public Location resolve(LocationDefinition l, Map<?,?> locationFlags);
     
-    /** See {@link #resolve(String, Map)} (with no options) */
+    /** See {@link #resolve(String, Boolean, Map)}; asks for the location to be managed,
and supplies no additional flags,
+     * and unwraps the result (throwing if the spec cannot be resolve) */
     public Location resolve(String spec);
     
     /** Returns true/false depending whether spec seems like a valid location,
-     * that is it has a chance of being resolved (depending on the spec) but NOT guaranteed;
-     * see {@link #resolveIfPossible(String)} which has stronger guarantees */
+     * that is it has a chance of being resolved (depending on the spec) but NOT guaranteed,
+     * as it is not passed to the spec;
+     * see {@link #resolve(String, Boolean, Map)} which has stronger guarantees 
+     * @deprecated since 0.7.0, not really needed, and semantics are weak; use {@link #resolve(String,
Boolean, Map)} */
+    @Deprecated
     public boolean canMaybeResolve(String spec);
     
-    /** Returns a location created from the given spec, which might correspond to a definition,
or created on-the-fly.
-     * Optional flags can be passed through to underlying the location. 
+    /** As {@link #resolve(String, Boolean, Map)}, but unwrapped
      * @throws NoSuchElementException if the spec cannot be resolved */
     public Location resolve(String spec, Map locationFlags);
     
-    /** as {@link #resolve(String)} but returning null (never throwing) */
+    /** as {@link #resolve(String)} but returning null (never throwing)
+     * @deprecated since 0.7.0 use {@link #resolve(String, Boolean, Map)} */
+    @Deprecated
     public Location resolveIfPossible(String spec);
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/core/src/main/java/brooklyn/location/basic/BasicLocationRegistry.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/basic/BasicLocationRegistry.java b/core/src/main/java/brooklyn/location/basic/BasicLocationRegistry.java
index 6fd63b4..618bec0 100644
--- a/core/src/main/java/brooklyn/location/basic/BasicLocationRegistry.java
+++ b/core/src/main/java/brooklyn/location/basic/BasicLocationRegistry.java
@@ -45,6 +45,9 @@ import brooklyn.management.ManagementContext;
 import brooklyn.management.internal.LocalLocationManager;
 import brooklyn.util.collections.MutableMap;
 import brooklyn.util.config.ConfigBag;
+import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.guava.Maybe;
+import brooklyn.util.guava.Maybe.Absent;
 import brooklyn.util.javalang.JavaClassNames;
 import brooklyn.util.text.Identifiers;
 import brooklyn.util.text.StringEscapes.JavaStringEscapes;
@@ -52,6 +55,7 @@ import brooklyn.util.text.WildcardGlobs;
 import brooklyn.util.text.WildcardGlobs.PhraseTreatment;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Suppliers;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Sets;
 
@@ -190,52 +194,58 @@ public class BasicLocationRegistry implements LocationRegistry {
         // defining the format and file etc)
     }
 
-    BasicLocationDefinition localhost(String id) {
+    static BasicLocationDefinition localhost(String id) {
         return new BasicLocationDefinition(id, "localhost", "localhost", null);
     }
     
     /** to catch circular references */
     protected ThreadLocal<Set<String>> specsSeen = new ThreadLocal<Set<String>>();
     
-    @Override
+    @Override @Deprecated
     public boolean canMaybeResolve(String spec) {
         return getSpecResolver(spec) != null;
     }
 
     @Override
     public final Location resolve(String spec) {
-        return resolve(spec, new MutableMap());
+        return resolve(spec, true, null).get();
     }
     
-    @Override
+    @Override @Deprecated
     public final Location resolveIfPossible(String spec) {
         if (!canMaybeResolve(spec)) return null;
-        try {
-            return resolve(spec);
-        } catch (Exception e) {
-            if (log.isTraceEnabled())
-                log.trace("Unable to resolve "+spec+": "+e, e);
-            // can't resolve
-            return null;
-        }
+        return resolve(spec, null, null).orNull();
     }
-
-    @Override
-    public Location resolve(String spec, Map locationFlags) {
+    
+    @Deprecated /** since 0.7.0 not used */
+    public Maybe<Location> resolve(String spec, boolean manage) {
+        return resolve(spec, manage, MutableMap.of());
+    }
+    
+    public Maybe<Location> resolve(String spec, Boolean manage, Map locationFlags)
{
         try {
+            if (locationFlags==null) locationFlags = MutableMap.of();
+            if (manage!=null) {
+                locationFlags.put(LocalLocationManager.CREATE_UNMANAGED, !manage);
+            }
+            
             Set<String> seenSoFar = specsSeen.get();
             if (seenSoFar==null) {
                 seenSoFar = new LinkedHashSet<String>();
                 specsSeen.set(seenSoFar);
             }
             if (seenSoFar.contains(spec))
-                throw new IllegalStateException("Circular reference in definition of location
'"+spec+"' ("+seenSoFar+")");
+                return Maybe.absent(Suppliers.ofInstance(new IllegalStateException("Circular
reference in definition of location '"+spec+"' ("+seenSoFar+")")));
             seenSoFar.add(spec);
             
             LocationResolver resolver = getSpecResolver(spec);
 
             if (resolver != null) {
-                return resolver.newLocationFromString(locationFlags, spec, this);
+                try {
+                    return Maybe.of(resolver.newLocationFromString(locationFlags, spec, this));
+                } catch (RuntimeException e) {
+                    return Maybe.absent(Suppliers.ofInstance(e));
+                }
             }
 
             // problem: but let's ensure that classpath is sane to give better errors in
common IDE bogus case;
@@ -263,13 +273,18 @@ public class BasicLocationRegistry implements LocationRegistry {
                         + "either this location is not recognised or there is a problem with
location resolver configuration.";
             }
 
-            throw new NoSuchElementException(errmsg);
+            return Maybe.absent(Suppliers.ofInstance(new NoSuchElementException(errmsg)));
 
         } finally {
             specsSeen.remove();
         }
     }
 
+    @Override
+    public Location resolve(String spec, Map locationFlags) {
+        return resolve(spec, null, locationFlags).get();
+    }
+
     protected LocationResolver getSpecResolver(String spec) {
         int colonIndex = spec.indexOf(':');
         int bracketIndex = spec.indexOf("(");
@@ -331,10 +346,10 @@ public class BasicLocationRegistry implements LocationRegistry {
     
     @Override
     public Location resolve(LocationDefinition ld) {
-        return resolve(ld, Collections.emptyMap());
+        return resolve(ld, null, null).get();
     }
 
-    @Override
+    @Override @Deprecated
     public Location resolveForPeeking(LocationDefinition ld) {
         // TODO should clean up how locations are stored, figuring out whether they are shared
or not;
         // or maybe better, the API calls to this might just want to get the LocationSpec
objects back
@@ -343,22 +358,27 @@ public class BasicLocationRegistry implements LocationRegistry {
         return resolve(ld, ConfigBag.newInstance().configure(LocalLocationManager.CREATE_UNMANAGED,
true).getAllConfig());
     }
 
-    @Override
+    @Override @Deprecated
     public Location resolve(LocationDefinition ld, Map<?,?> flags) {
         return resolveLocationDefinition(ld, flags, null);
     }
     
+    /** @deprecated since 0.7.0 not used (and optionalName was ignored anyway) */
+    @Deprecated
     public Location resolveLocationDefinition(LocationDefinition ld, Map locationFlags, String
optionalName) {
+        return resolve(ld, null, locationFlags).get();
+    }
+    
+    public Maybe<Location> resolve(LocationDefinition ld, Boolean manage, Map locationFlags)
{
         ConfigBag newLocationFlags = ConfigBag.newInstance(ld.getConfig())
             .putAll(locationFlags)
             .putIfAbsentAndNotNull(LocationInternal.NAMED_SPEC_NAME, ld.getName())
             .putIfAbsentAndNotNull(LocationInternal.ORIGINAL_SPEC, ld.getName());
-        try {
-            return resolve(ld.getSpec(), newLocationFlags.getAllConfig());
-        } catch (Exception e) {
-            throw new IllegalStateException("Cannot instantiate location '"+
-                (optionalName!=null ? optionalName : ld)+"' pointing at "+ld.getSpec()+":
"+e, e);
-        }
+        Maybe<Location> result = resolve(ld.getSpec(), manage, newLocationFlags.getAllConfig());
+        if (result.isPresent()) 
+            return result;
+        throw new IllegalStateException("Cannot instantiate location '"+ld+"' pointing at
"+ld.getSpec()+": "+
+            Exceptions.collapseText( ((Absent<?>)result).getException() ));
     }
 
     @Override
@@ -371,7 +391,7 @@ public class BasicLocationRegistry implements LocationRegistry {
         // ensure localhost is added (even on windows)
         LocationDefinition l = mgmt.getLocationRegistry().getDefinedLocationByName("localhost");
         if (l==null) mgmt.getLocationRegistry().updateDefinedLocation(
-                ((BasicLocationRegistry)mgmt.getLocationRegistry()).localhost(Identifiers.makeRandomId(8)));
+                BasicLocationRegistry.localhost(Identifiers.makeRandomId(8)) );
         
         ((BasicLocationRegistry)mgmt.getLocationRegistry()).disablePersistence();
     }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/core/src/main/java/brooklyn/location/basic/HostLocationResolver.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/basic/HostLocationResolver.java b/core/src/main/java/brooklyn/location/basic/HostLocationResolver.java
index 5f6783e..6bd70c1 100644
--- a/core/src/main/java/brooklyn/location/basic/HostLocationResolver.java
+++ b/core/src/main/java/brooklyn/location/basic/HostLocationResolver.java
@@ -25,6 +25,9 @@ import brooklyn.location.LocationRegistry;
 import brooklyn.location.LocationSpec;
 import brooklyn.location.basic.AbstractLocationResolver.SpecParser.ParsedSpec;
 import brooklyn.util.config.ConfigBag;
+import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.guava.Maybe;
+import brooklyn.util.guava.Maybe.Absent;
 import brooklyn.util.text.KeyValueParser;
 
 import com.google.common.collect.ImmutableMap;
@@ -58,8 +61,10 @@ public class HostLocationResolver extends AbstractLocationResolver {
 
         // Generate target spec
         String target = "byon("+KeyValueParser.toLine(argsMap)+")";
-        if (!managementContext.getLocationRegistry().canMaybeResolve(target)) {
-            throw new IllegalArgumentException("Invalid target location '" + target + "';
must be resolvable location");
+        Maybe<Location> testResolve = managementContext.getLocationRegistry().resolve(target,
false, null);
+        if (!testResolve.isPresent()) {
+            throw new IllegalArgumentException("Invalid target location '" + target + "'
for location '"+HOST+"': "+
+                Exceptions.collapseText( ((Absent<?>)testResolve).getException() ));
         }
         
         return managementContext.getLocationManager().createLocation(LocationSpec.create(SingleMachineProvisioningLocation.class)

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/core/src/main/java/brooklyn/location/basic/SingleMachineLocationResolver.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/basic/SingleMachineLocationResolver.java
b/core/src/main/java/brooklyn/location/basic/SingleMachineLocationResolver.java
index 5024cfc..7b2dc25 100644
--- a/core/src/main/java/brooklyn/location/basic/SingleMachineLocationResolver.java
+++ b/core/src/main/java/brooklyn/location/basic/SingleMachineLocationResolver.java
@@ -24,6 +24,9 @@ import brooklyn.location.Location;
 import brooklyn.location.LocationRegistry;
 import brooklyn.location.LocationSpec;
 import brooklyn.util.config.ConfigBag;
+import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.guava.Maybe;
+import brooklyn.util.guava.Maybe.Absent;
 
 public class SingleMachineLocationResolver extends AbstractLocationResolver {
     
@@ -45,8 +48,10 @@ public class SingleMachineLocationResolver extends AbstractLocationResolver
{
         }
         String target = config.getStringKey("target").toString();
         config.remove("target");
-        if (!managementContext.getLocationRegistry().canMaybeResolve(target)) {
-            throw new IllegalArgumentException("Invalid target location '" + target + "';
must be resolvable location");
+        Maybe<Location> testResolve = managementContext.getLocationRegistry().resolve(target,
false, null);
+        if (!testResolve.isPresent()) {
+            throw new IllegalArgumentException("Invalid target location '" + target + "'
for location '"+SINGLE+"': "+
+                Exceptions.collapseText( ((Absent<?>)testResolve).getException() ));
         }
         
         return managementContext.getLocationManager().createLocation(LocationSpec.create(SingleMachineProvisioningLocation.class)

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/core/src/main/java/brooklyn/management/internal/LocalLocationManager.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/internal/LocalLocationManager.java b/core/src/main/java/brooklyn/management/internal/LocalLocationManager.java
index 59415fe..f57b5df 100644
--- a/core/src/main/java/brooklyn/management/internal/LocalLocationManager.java
+++ b/core/src/main/java/brooklyn/management/internal/LocalLocationManager.java
@@ -83,6 +83,7 @@ public class LocalLocationManager implements LocationManagerInternal {
     public InternalLocationFactory getLocationFactory() {
         if (!isRunning()) throw new IllegalStateException("Management context no longer running");
         return locationFactory;
+        
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/core/src/test/java/brooklyn/location/basic/LocalhostLocationResolverTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/location/basic/LocalhostLocationResolverTest.java
b/core/src/test/java/brooklyn/location/basic/LocalhostLocationResolverTest.java
index 5efeb60..c09e3a7 100644
--- a/core/src/test/java/brooklyn/location/basic/LocalhostLocationResolverTest.java
+++ b/core/src/test/java/brooklyn/location/basic/LocalhostLocationResolverTest.java
@@ -36,6 +36,7 @@ import brooklyn.entity.basic.Entities;
 import brooklyn.location.Location;
 import brooklyn.location.NoMachinesAvailableException;
 import brooklyn.management.internal.LocalManagementContext;
+import brooklyn.test.entity.LocalManagementContextForTests;
 import brooklyn.util.text.StringEscapes.JavaStringEscapes;
 
 import com.google.common.collect.ImmutableList;
@@ -47,7 +48,7 @@ public class LocalhostLocationResolverTest {
 
     @BeforeMethod(alwaysRun=true)
     public void setUp() throws Exception {
-        managementContext = new LocalManagementContext(BrooklynProperties.Factory.newEmpty());
+        managementContext = LocalManagementContextForTests.newInstance();
         brooklynProperties = managementContext.getBrooklynProperties();
     }
     
@@ -245,6 +246,9 @@ public class LocalhostLocationResolverTest {
         } catch (NoSuchElementException e) {
             // success
         }
+
+        // and check the long form returns an Absent (not throwing)
+        Assert.assertTrue(managementContext.getLocationRegistry().resolve(val, false, null).isAbsent());
     }
     
     private void assertThrowsIllegalArgument(String val) {
@@ -254,5 +258,8 @@ public class LocalhostLocationResolverTest {
         } catch (IllegalArgumentException e) {
             // success
         }
+        
+        // and check the long form returns an Absent (not throwing)
+        Assert.assertTrue(managementContext.getLocationRegistry().resolve(val, false, null).isAbsent());
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/core/src/test/java/brooklyn/location/basic/SshMachineLocationIntegrationTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/location/basic/SshMachineLocationIntegrationTest.java
b/core/src/test/java/brooklyn/location/basic/SshMachineLocationIntegrationTest.java
index 9f743f5..21f1e77 100644
--- a/core/src/test/java/brooklyn/location/basic/SshMachineLocationIntegrationTest.java
+++ b/core/src/test/java/brooklyn/location/basic/SshMachineLocationIntegrationTest.java
@@ -57,7 +57,7 @@ public class SshMachineLocationIntegrationTest {
     // Note: requires `named:localhost-passphrase` set up with a key whose passphrase is
"localhost"    
     @Test(groups = "Integration")
     public void testExtractingConnectablePassphraselessKey() throws Exception {
-        LocalhostMachineProvisioningLocation lhp = (LocalhostMachineProvisioningLocation)
mgmt.getLocationRegistry().resolveIfPossible("named:localhost-passphrase");
+        LocalhostMachineProvisioningLocation lhp = (LocalhostMachineProvisioningLocation)
mgmt.getLocationRegistry().resolve("named:localhost-passphrase", true, null).orNull();
         Preconditions.checkNotNull(lhp, "This test requires a localhost named location called
'localhost-passphrase' (which should have a passphrase set)");
         SshMachineLocation sm = lhp.obtain();
         

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/software/base/src/main/java/brooklyn/entity/pool/ServerPoolImpl.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/pool/ServerPoolImpl.java b/software/base/src/main/java/brooklyn/entity/pool/ServerPoolImpl.java
index 7e13591..9ca8f74 100644
--- a/software/base/src/main/java/brooklyn/entity/pool/ServerPoolImpl.java
+++ b/software/base/src/main/java/brooklyn/entity/pool/ServerPoolImpl.java
@@ -253,7 +253,7 @@ public class ServerPoolImpl extends DynamicClusterImpl implements ServerPool
{
 
     @Override
     public Collection<Entity> addExistingMachinesFromSpec(String spec) {
-        Location location = getManagementContext().getLocationRegistry().resolveIfPossible(spec);
+        Location location = getManagementContext().getLocationRegistry().resolve(spec, true,
null).orNull();
         List<Entity> additions = Lists.newLinkedList();
         if (location == null) {
             LOG.warn("Spec was unresolvable: {}", spec);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/software/base/src/test/java/brooklyn/entity/chef/ChefLiveTestSupport.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/brooklyn/entity/chef/ChefLiveTestSupport.java b/software/base/src/test/java/brooklyn/entity/chef/ChefLiveTestSupport.java
index 62a471e..fba1ac3 100644
--- a/software/base/src/test/java/brooklyn/entity/chef/ChefLiveTestSupport.java
+++ b/software/base/src/test/java/brooklyn/entity/chef/ChefLiveTestSupport.java
@@ -64,10 +64,10 @@ public class ChefLiveTestSupport extends BrooklynAppLiveTestSupport {
      **/
     @SuppressWarnings("unchecked")
     public static MachineProvisioningLocation<? extends SshMachineLocation> createLocation(ManagementContext
mgmt) {
-        Location bestLocation = mgmt.getLocationRegistry().resolveIfPossible("named:ChefTests");
+        Location bestLocation = mgmt.getLocationRegistry().resolve("named:ChefTests", true,
null).orNull();
         if (bestLocation==null) {
             log.info("using AWS for chef tests because named:ChefTests does not exist");
-            bestLocation = mgmt.getLocationRegistry().resolveIfPossible("jclouds:aws-ec2:us-east-1");
+            bestLocation = mgmt.getLocationRegistry().resolve("jclouds:aws-ec2:us-east-1");
         }
         if (bestLocation==null) {
             throw new IllegalStateException("Need a location called named:ChefTests or AWS
configured for these tests");

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlLocationResolver.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlLocationResolver.java
b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlLocationResolver.java
index 598be4c..d9f7a65 100644
--- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlLocationResolver.java
+++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlLocationResolver.java
@@ -26,7 +26,10 @@ import brooklyn.location.Location;
 import brooklyn.location.LocationDefinition;
 import brooklyn.management.ManagementContext;
 import brooklyn.util.collections.MutableList;
+import brooklyn.util.collections.MutableMap;
 import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.guava.Maybe;
+import brooklyn.util.guava.Maybe.Absent;
 import brooklyn.util.text.Strings;
 
 import com.google.common.collect.Iterables;
@@ -103,28 +106,13 @@ public class BrooklynYamlLocationResolver {
     /** resolves the location from the given spec string, either "Named Location", or "named:Named
Location" format;
      * returns null if input is blank (or null); otherwise guaranteed to resolve or throw
error */
     public Location resolveLocationFromString(String location) {
-        if (Strings.isBlank(location))
-            return null;
-        
-        LocationDefinition ldef = mgmt.getLocationRegistry().getDefinedLocationByName((String)location);
-        if (ldef!=null)
-            // found it as a named location
-            return mgmt.getLocationRegistry().resolve(ldef);
-        
-        if (mgmt.getLocationRegistry().canMaybeResolve((String)location)) {
-            try {
-                return mgmt.getLocationRegistry().resolve((String)location);
-            } catch (Exception e) {
-                Exceptions.propagateIfFatal(e);
-                throw new IllegalStateException("Illegal parameter for 'location' ("+location+");
not resolvable: "+e, e);
-            }
-        }
-        throw new IllegalStateException("Illegal parameter for 'location' ("+location+");
not resolvable");
+        if (Strings.isBlank(location)) return null;
+        return resolveLocation(location, MutableMap.of());
     }
 
     public Location resolveLocationFromMap(Map<?,?> location) {
         if (location.size() > 1) {
-            throw new IllegalStateException("Illegal parameter for 'location'; not resolvable
("+location+")");
+            throw new IllegalStateException("Illegal parameter for 'location'; expected a
single entry in map ("+location+")");
         }
         Object key = Iterables.getOnlyElement(location.keySet());
         Object value = location.get(key);
@@ -135,22 +123,18 @@ public class BrooklynYamlLocationResolver {
         if (!(value instanceof Map)) {
             throw new IllegalStateException("Illegal parameter for 'location'; expected config
map ("+location+")");
         }
-        String spec = (String) key;
-        Map<?,?> flags = (Map<?, ?>) value;
-        
+        return resolveLocation((String)key, (Map<?,?>)value);
+    }
+    
+    protected Location resolveLocation(String spec, Map<?,?> flags) {
         LocationDefinition ldef = mgmt.getLocationRegistry().getDefinedLocationByName((String)spec);
         if (ldef!=null)
             // found it as a named location
-            return mgmt.getLocationRegistry().resolve(ldef, flags);
+            return mgmt.getLocationRegistry().resolve(ldef, null, flags).get();
         
-        if (mgmt.getLocationRegistry().canMaybeResolve(spec)) {
-            try {
-                return mgmt.getLocationRegistry().resolve(spec, flags);
-            } catch (Exception e) {
-                Exceptions.propagateIfFatal(e);
-                throw new IllegalStateException("Illegal parameter for 'location'; not resolvable
("+location+"): "+e, e);
-            }
-        }
-        throw new IllegalStateException("Illegal parameter for 'location'; not resolvable
("+location+")");
+        Maybe<Location> l = mgmt.getLocationRegistry().resolve(spec, null, flags);
+        if (l.isPresent()) return l.get();
+        throw new IllegalStateException("Illegal parameter for 'location' ("+spec+"); not
resolvable: "+
+            Exceptions.collapseText( ((Absent<?>)l).getException() ));
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/usage/rest-server/src/main/java/brooklyn/rest/transform/LocationTransformer.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/brooklyn/rest/transform/LocationTransformer.java
b/usage/rest-server/src/main/java/brooklyn/rest/transform/LocationTransformer.java
index 980b931..fdf837a 100644
--- a/usage/rest-server/src/main/java/brooklyn/rest/transform/LocationTransformer.java
+++ b/usage/rest-server/src/main/java/brooklyn/rest/transform/LocationTransformer.java
@@ -36,7 +36,6 @@ import brooklyn.rest.domain.LocationSummary;
 import brooklyn.rest.util.WebResourceUtils;
 import brooklyn.util.collections.MutableMap;
 import brooklyn.util.config.ConfigBag;
-import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.text.Strings;
 
 import com.google.common.collect.ImmutableMap;
@@ -57,13 +56,13 @@ public class LocationTransformer {
         Map<String, ?> config = locationSpec.getConfig();
         if (mgmt != null && (level==LocationDetailLevel.FULL_EXCLUDING_SECRET ||
level==LocationDetailLevel.FULL_INCLUDING_SECRET)) {
             LocationDefinition ld = new BasicLocationDefinition(id, locationSpec.getName(),
locationSpec.getSpec(), locationSpec.getConfig());
-            Location ll = mgmt.getLocationRegistry().resolveForPeeking(ld);
+            Location ll = mgmt.getLocationRegistry().resolve(ld, false, null).orNull();
             if (ll!=null) config = ll.getAllConfig(true);
         } else if (level==LocationDetailLevel.LOCAL_EXCLUDING_SECRET) {
             // get displayName
             if (!config.containsKey(LocationConfigKeys.DISPLAY_NAME.getName()) &&
mgmt!=null) {
                 LocationDefinition ld = new BasicLocationDefinition(id, locationSpec.getName(),
locationSpec.getSpec(), locationSpec.getConfig());
-                Location ll = mgmt.getLocationRegistry().resolveForPeeking(ld);
+                Location ll = mgmt.getLocationRegistry().resolve(ld, false, null).orNull();
                 if (ll!=null) {
                     Map<String, Object> configExtra = ll.getAllConfig(true);
                     if (configExtra.containsKey(LocationConfigKeys.DISPLAY_NAME.getName()))
{
@@ -93,12 +92,12 @@ public class LocationTransformer {
         // TODO: Can remove null checks on mgmt when newInstance(LocationDefinition) is deleted
         Map<String, Object> config = l.getConfig();
         if (mgmt != null && (level==LocationDetailLevel.FULL_EXCLUDING_SECRET ||
level==LocationDetailLevel.FULL_INCLUDING_SECRET)) {
-            Location ll = mgmt.getLocationRegistry().resolveForPeeking(l);
+            Location ll = mgmt.getLocationRegistry().resolve(l, false, null).orNull();
             if (ll!=null) config = ll.getAllConfig(true);
         } else if (level==LocationDetailLevel.LOCAL_EXCLUDING_SECRET) {
             // get displayName
             if (mgmt != null && !config.containsKey(LocationConfigKeys.DISPLAY_NAME.getName()))
{
-                Location ll = mgmt.getLocationRegistry().resolveForPeeking(l);
+                Location ll = mgmt.getLocationRegistry().resolve(l, false, null).orNull();
                 if (ll!=null) {
                     Map<String, Object> configExtra = ll.getAllConfig(true);
                     if (configExtra.containsKey(LocationConfigKeys.DISPLAY_NAME.getName()))
{
@@ -156,12 +155,8 @@ public class LocationTransformer {
         }
         if (specId==null && spec!=null) {
             // fall back to attempting to lookup it
-            try {
-                Location ll = mgmt.getLocationRegistry().resolveForPeeking(new BasicLocationDefinition(null,
spec, null));
-                if (ll!=null) specId = ll.getId();
-            } catch (Exception e) {
-                Exceptions.propagateIfFatal(e);
-            }
+            Location ll = mgmt.getLocationRegistry().resolve(spec, false, null).orNull();
+            if (ll!=null) specId = ll.getId();
         }
         
         Map<String, Object> configOrig = l.getAllConfig(level!=LocationDetailLevel.LOCAL_EXCLUDING_SECRET);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d5c11385/utils/common/src/main/java/brooklyn/util/guava/Maybe.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/guava/Maybe.java b/utils/common/src/main/java/brooklyn/util/guava/Maybe.java
index 41d7a06..86c82fc 100644
--- a/utils/common/src/main/java/brooklyn/util/guava/Maybe.java
+++ b/utils/common/src/main/java/brooklyn/util/guava/Maybe.java
@@ -67,7 +67,7 @@ public abstract class Maybe<T> implements Serializable, Supplier<T>
{
     }
     
     /** Creates an absent whose get throws an {@link RuntimeException} generated on demand
from the given supplier */
-    public static <T> Maybe<T> absent(final Supplier<RuntimeException>
exceptionSupplier) {
+    public static <T> Maybe<T> absent(final Supplier<? extends RuntimeException>
exceptionSupplier) {
         return new Absent<T>(Preconditions.checkNotNull(exceptionSupplier));
     }
     
@@ -196,11 +196,11 @@ public abstract class Maybe<T> implements Serializable, Supplier<T>
{
     
     public static class Absent<T> extends Maybe<T> {
         private static final long serialVersionUID = -757170462010887057L;
-        private final Supplier<RuntimeException> exception;
+        private final Supplier<? extends RuntimeException> exception;
         public Absent() {
             this(IllegalStateExceptionSupplier.EMPTY_EXCEPTION);
         }
-        public Absent(Supplier<RuntimeException> exception) {
+        public Absent(Supplier<? extends RuntimeException> exception) {
             this.exception = exception;
         }
         @Override


Mime
View raw message