brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/5] brooklyn-server git commit: add static Maybe.getException(maybe) to avoid messy casts to Absent, and Asserts methods
Date Mon, 31 Oct 2016 12:24:25 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master d7edfadaa -> f9637f3d1


add static Maybe.getException(maybe) to avoid messy casts to Absent, and Asserts methods


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

Branch: refs/heads/master
Commit: 9c040018d242373ce7270bf65302f89de8c0c742
Parents: aa3f183
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Wed Sep 28 15:34:34 2016 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Wed Sep 28 15:36:58 2016 +0100

----------------------------------------------------------------------
 .../spi/creation/BrooklynYamlLocationResolver.java    |  3 +--
 .../brooklyn/core/location/BasicLocationRegistry.java |  2 +-
 .../BrooklynClassLoadingContextSequential.java        |  4 ++--
 .../org/apache/brooklyn/core/mgmt/ha/OsgiManager.java |  2 +-
 .../core/resolve/entity/JavaEntitySpecResolver.java   |  2 +-
 .../brooklyn/location/byon/HostLocationResolver.java  |  2 +-
 .../location/byon/SingleMachineLocationResolver.java  |  2 +-
 .../main/java/org/apache/brooklyn/test/Asserts.java   | 14 ++++++++++++++
 .../java/org/apache/brooklyn/util/guava/Maybe.java    |  6 +++++-
 9 files changed, 27 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9c040018/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlLocationResolver.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlLocationResolver.java
b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlLocationResolver.java
index 390d784..b19e62e 100644
--- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlLocationResolver.java
+++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlLocationResolver.java
@@ -32,7 +32,6 @@ import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.exceptions.UserFacingException;
 import org.apache.brooklyn.util.guava.Maybe;
-import org.apache.brooklyn.util.guava.Maybe.Absent;
 import org.apache.brooklyn.util.text.Strings;
 
 import com.google.common.collect.Iterables;
@@ -138,7 +137,7 @@ public class BrooklynYamlLocationResolver {
         Maybe<LocationSpec<?>> l = mgmt.getLocationRegistry().getLocationSpec(spec,
flags);
         if (l.isPresent()) return l.get();
         
-        RuntimeException exception = ((Absent<?>)l).getException();
+        RuntimeException exception = Maybe.getException(l);
         if (exception instanceof NoSuchElementException && 
                 exception.getMessage().contains("Unknown location")) {
             // common case

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9c040018/core/src/main/java/org/apache/brooklyn/core/location/BasicLocationRegistry.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/location/BasicLocationRegistry.java
b/core/src/main/java/org/apache/brooklyn/core/location/BasicLocationRegistry.java
index 850f3d5..d4d7f7e 100644
--- a/core/src/main/java/org/apache/brooklyn/core/location/BasicLocationRegistry.java
+++ b/core/src/main/java/org/apache/brooklyn/core/location/BasicLocationRegistry.java
@@ -508,7 +508,7 @@ public class BasicLocationRegistry implements LocationRegistry {
         if (result.isPresent()) 
             return result;
         throw new IllegalStateException("Cannot instantiate location '"+ld+"' pointing at
"+ld.getSpec(), 
-            ((Absent<?>)result).getException() );
+            Maybe.getException(result) );
     }
     
     @Override

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9c040018/core/src/main/java/org/apache/brooklyn/core/mgmt/classloading/BrooklynClassLoadingContextSequential.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/classloading/BrooklynClassLoadingContextSequential.java
b/core/src/main/java/org/apache/brooklyn/core/mgmt/classloading/BrooklynClassLoadingContextSequential.java
index 530039a..19aa743 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/classloading/BrooklynClassLoadingContextSequential.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/classloading/BrooklynClassLoadingContextSequential.java
@@ -74,7 +74,7 @@ public final class BrooklynClassLoadingContextSequential extends AbstractBrookly
             Maybe<Class<?>> clazz = target.tryLoadClass(className);
             if (clazz.isPresent())
                 return clazz;
-            errors.add( ((Maybe.Absent<?>)clazz).getException() );
+            errors.add( Maybe.getException(clazz) );
         }
         boolean noPrimaryErrors = errors.isEmpty();
         for (BrooklynClassLoadingContext target: secondaries) {
@@ -82,7 +82,7 @@ public final class BrooklynClassLoadingContextSequential extends AbstractBrookly
             if (clazz.isPresent())
                 return clazz;
             if (noPrimaryErrors)
-                errors.add( ((Maybe.Absent<?>)clazz).getException() );
+                errors.add( Maybe.getException(clazz) );
         }
 
         return Maybe.absent(Exceptions.create("Unable to load "+className+" from "+primaries,
errors));

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9c040018/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java
index 2c68c8a..56d98a0 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java
@@ -195,7 +195,7 @@ public class OsgiManager {
                     clazz = SystemFrameworkLoader.get().loadClassFromBundle(type, b);
                     return Maybe.of(clazz);
                 } else {
-                    bundleProblems.put(osgiBundle, ((Maybe.Absent<?>)bundle).getException());
+                    bundleProblems.put(osgiBundle, Maybe.getException(bundle));
                 }
                 
             } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9c040018/core/src/main/java/org/apache/brooklyn/core/resolve/entity/JavaEntitySpecResolver.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/resolve/entity/JavaEntitySpecResolver.java
b/core/src/main/java/org/apache/brooklyn/core/resolve/entity/JavaEntitySpecResolver.java
index 55cda9f..f600a30 100644
--- a/core/src/main/java/org/apache/brooklyn/core/resolve/entity/JavaEntitySpecResolver.java
+++ b/core/src/main/java/org/apache/brooklyn/core/resolve/entity/JavaEntitySpecResolver.java
@@ -70,7 +70,7 @@ public class JavaEntitySpecResolver extends AbstractEntitySpecResolver{
     private EntitySpec<?> resolveInternal(String localType, BrooklynClassLoadingContext
loader) {
         Maybe<Class<? extends Entity>> javaTypeMaybe = tryLoadJavaType(localType,
loader);
         if (javaTypeMaybe.isAbsent())
-            throw new IllegalStateException("Could not find "+localType, ((Maybe.Absent<?>)javaTypeMaybe).getException());
+            throw new IllegalStateException("Could not find "+localType, Maybe.getException(javaTypeMaybe));
         Class<? extends Entity> javaType = javaTypeMaybe.get();
 
         EntitySpec<? extends Entity> spec;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9c040018/core/src/main/java/org/apache/brooklyn/location/byon/HostLocationResolver.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/location/byon/HostLocationResolver.java
b/core/src/main/java/org/apache/brooklyn/location/byon/HostLocationResolver.java
index f2d45ab..ab41c85 100644
--- a/core/src/main/java/org/apache/brooklyn/location/byon/HostLocationResolver.java
+++ b/core/src/main/java/org/apache/brooklyn/location/byon/HostLocationResolver.java
@@ -66,7 +66,7 @@ public class HostLocationResolver extends AbstractLocationResolver {
         Maybe<LocationSpec<?>> testResolve = managementContext.getLocationRegistry().getLocationSpec(target);
         if (!testResolve.isPresent()) {
             throw new IllegalArgumentException("Invalid target location '" + target + "'
for location '"+HOST+"': "+
-                Exceptions.collapseText( ((Absent<?>)testResolve).getException() ),
((Absent<?>)testResolve).getException());
+                Exceptions.collapseText( Maybe.getException(testResolve) ), Maybe.getException(testResolve));
         }
         
         return LocationSpec.create(SingleMachineProvisioningLocation.class)

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9c040018/core/src/main/java/org/apache/brooklyn/location/byon/SingleMachineLocationResolver.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/location/byon/SingleMachineLocationResolver.java
b/core/src/main/java/org/apache/brooklyn/location/byon/SingleMachineLocationResolver.java
index 9c238eb..fa30b53 100644
--- a/core/src/main/java/org/apache/brooklyn/location/byon/SingleMachineLocationResolver.java
+++ b/core/src/main/java/org/apache/brooklyn/location/byon/SingleMachineLocationResolver.java
@@ -55,7 +55,7 @@ public class SingleMachineLocationResolver extends AbstractLocationResolver
{
         Maybe<LocationSpec<?>> testResolve = managementContext.getLocationRegistry().getLocationSpec(target);
         if (!testResolve.isPresent()) {
             throw new IllegalArgumentException("Invalid target location '" + target + "'
for location '"+SINGLE+"': "+
-                Exceptions.collapseText( ((Absent<?>)testResolve).getException() ));
+                Exceptions.collapseText( Maybe.getException(testResolve) ), Maybe.getException(testResolve));
         }
         
         return LocationSpec.create(SingleMachineProvisioningLocation.class)

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9c040018/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java b/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
index 23624d9..a4f8747 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/test/Asserts.java
@@ -1381,4 +1381,18 @@ public class Asserts {
         if (Iterables.size(list)!=expectedSize) fail("List has wrong size "+Iterables.size(list)+"
(expected "+expectedSize+"): "+list);
     }
 
+    public static void assertInstanceOf(Object obj, Class<?> type) {
+        assertThat(obj, Predicates.instanceOf(type));
+    }
+
+    public static <T> void assertPresent(Maybe<T> candidate) {
+        if (candidate.isPresent()) return;
+        fail( Maybe.getException(candidate) );
+    }
+
+    public static <T> void assertNotPresent(Maybe<T> candidate) {
+        if (candidate.isAbsent()) return;
+        fail("Expected absent value; instead got: "+candidate.get());
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/9c040018/utils/common/src/main/java/org/apache/brooklyn/util/guava/Maybe.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/guava/Maybe.java b/utils/common/src/main/java/org/apache/brooklyn/util/guava/Maybe.java
index a53425f..37aa7c9 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/guava/Maybe.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/guava/Maybe.java
@@ -450,5 +450,9 @@ public abstract class Maybe<T> implements Serializable, Supplier<T>
{
             return !other.isPresent();
         return Objects.equal(get(), other.get());
     }
-    
+
+    /** Finds the {@link Absent#getException()} if {@link #isAbsent()}, or null */
+    public static RuntimeException getException(Maybe<?> t) {
+        return ((Maybe.Absent<?>)t).getException();
+    }
 }


Mime
View raw message