brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [08/12] incubator-brooklyn git commit: resolve the curious Maybe.Present(null) making use of new Absent.isNull()
Date Mon, 21 Dec 2015 12:10:58 GMT
resolve the curious Maybe.Present(null) making use of new Absent.isNull()


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

Branch: refs/heads/master
Commit: 3900e19d6df17bf1f605508b62dbcea98b336467
Parents: 0b9b2e3
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Thu Dec 17 13:59:48 2015 +0000
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Thu Dec 17 14:23:02 2015 +0000

----------------------------------------------------------------------
 .../core/catalog/internal/CatalogUtils.java     |  2 +-
 .../typereg/AbstractTypePlanTransformer.java    | 15 ++++-------
 .../core/typereg/BasicBrooklynTypeRegistry.java | 11 ++++----
 .../brooklyn/core/typereg/RegisteredTypes.java  | 27 +++++++++++---------
 .../BrooklynEntityDecorationResolver.java       |  3 ++-
 .../rest/resources/ApplicationResource.java     | 24 +++++++++++------
 .../rest/resources/CatalogResource.java         | 16 ++++++++----
 7 files changed, 55 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3900e19d/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUtils.java
b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUtils.java
index fbf34f7..a1b2040 100644
--- a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUtils.java
+++ b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/CatalogUtils.java
@@ -87,7 +87,7 @@ public class CatalogUtils {
         String catId = entity.getCatalogItemId();
         if (Strings.isBlank(catId)) return JavaBrooklynClassLoadingContext.create(mgmt);
         Maybe<RegisteredType> cat = RegisteredTypes.tryValidate(mgmt.getTypeRegistry().get(catId),
RegisteredTypeLoadingContexts.spec(Entity.class));
-        if (cat.get()==null) {
+        if (cat.isNull()) {
             log.warn("Cannot load "+catId+" to get classloader for "+entity+"; will try with
standard loader, but might fail subsequently");
             return JavaBrooklynClassLoadingContext.create(mgmt);
         }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3900e19d/core/src/main/java/org/apache/brooklyn/core/typereg/AbstractTypePlanTransformer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/typereg/AbstractTypePlanTransformer.java
b/core/src/main/java/org/apache/brooklyn/core/typereg/AbstractTypePlanTransformer.java
index 7b91529..8f671f2 100644
--- a/core/src/main/java/org/apache/brooklyn/core/typereg/AbstractTypePlanTransformer.java
+++ b/core/src/main/java/org/apache/brooklyn/core/typereg/AbstractTypePlanTransformer.java
@@ -23,6 +23,7 @@ import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.api.typereg.RegisteredType;
 import org.apache.brooklyn.api.typereg.RegisteredTypeLoadingContext;
 import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.javalang.JavaClassNames;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -98,7 +99,7 @@ public abstract class AbstractTypePlanTransformer implements BrooklynTypePlanTra
     @Override
     public Object create(final RegisteredType type, final RegisteredTypeLoadingContext context)
{
         try {
-            return validate(new RegisteredTypeKindVisitor<Object>() {
+            return tryValidate(new RegisteredTypeKindVisitor<Object>() {
                 @Override protected Object visitSpec() {
                     try { 
                         AbstractBrooklynObjectSpec<?, ?> result = createSpec(type,
context);
@@ -112,7 +113,7 @@ public abstract class AbstractTypePlanTransformer implements BrooklynTypePlanTra
                     } catch (Exception e) { throw Exceptions.propagate(e); }
                 }
                 
-            }.visit(type.getKind()), type, context);
+            }.visit(type.getKind()), type, context).get();
         } catch (Exception e) {
             Exceptions.propagateIfFatal(e);
             if (!(e instanceof UnsupportedTypePlanException)) {
@@ -125,14 +126,8 @@ public abstract class AbstractTypePlanTransformer implements BrooklynTypePlanTra
     /** Validates the object. Subclasses may do further validation based on the context.

      * @throw UnsupportedTypePlanException if we want to quietly abandon this, any other
exception to report the problem, when validation fails
      * @return the created object for fluent usage */
-    protected <T> T validate(T createdObject, RegisteredType type, RegisteredTypeLoadingContext
constraint) {
-        if (createdObject==null) return null;
-        try {
-            return RegisteredTypes.tryValidate(createdObject, type, constraint).get();
-        } catch (Exception e) {
-            Exceptions.propagateIfFatal(e);
-            throw new IllegalStateException("Created incompatible object: "+Exceptions.collapseText(e),
e);
-        }
+    protected <T> Maybe<T> tryValidate(T createdObject, RegisteredType type,
RegisteredTypeLoadingContext constraint) {
+        return RegisteredTypes.tryValidate(createdObject, type, constraint);
     }
 
     protected abstract AbstractBrooklynObjectSpec<?,?> createSpec(RegisteredType type,
RegisteredTypeLoadingContext context) throws Exception;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3900e19d/core/src/main/java/org/apache/brooklyn/core/typereg/BasicBrooklynTypeRegistry.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/typereg/BasicBrooklynTypeRegistry.java
b/core/src/main/java/org/apache/brooklyn/core/typereg/BasicBrooklynTypeRegistry.java
index 496c028..1b47648 100644
--- a/core/src/main/java/org/apache/brooklyn/core/typereg/BasicBrooklynTypeRegistry.java
+++ b/core/src/main/java/org/apache/brooklyn/core/typereg/BasicBrooklynTypeRegistry.java
@@ -71,10 +71,10 @@ public class BasicBrooklynTypeRegistry implements BrooklynTypeRegistry
{
         return Iterables.filter(localRegisteredTypes.values(), filter);
     }
 
-    private RegisteredType getExactWithoutLegacyCatalog(String symbolicName, String version,
RegisteredTypeLoadingContext constraint) {
+    private Maybe<RegisteredType> getExactWithoutLegacyCatalog(String symbolicName,
String version, RegisteredTypeLoadingContext constraint) {
         // TODO look in any nested/private registries
         RegisteredType item = localRegisteredTypes.get(symbolicName+":"+version);
-        return RegisteredTypes.tryValidate(item, constraint).orNull();
+        return RegisteredTypes.tryValidate(item, constraint);
     }
 
     @SuppressWarnings("deprecation")
@@ -93,12 +93,11 @@ public class BasicBrooklynTypeRegistry implements BrooklynTypeRegistry
{
         String version = versionFinal;
         if (version==null) version = BrooklynCatalog.DEFAULT_VERSION;
 
-        RegisteredType type;
         if (!BrooklynCatalog.DEFAULT_VERSION.equals(version)) {
             // normal code path when version is supplied
             
-            type = getExactWithoutLegacyCatalog(symbolicNameOrAliasIfNoVersion, version,
context);
-            if (type!=null) return Maybe.of(type);
+            Maybe<RegisteredType> type = getExactWithoutLegacyCatalog(symbolicNameOrAliasIfNoVersion,
version, context);
+            if (type.isPresent()) return type;
         }
 
         if (BrooklynCatalog.DEFAULT_VERSION.equals(version)) {
@@ -123,7 +122,7 @@ public class BasicBrooklynTypeRegistry implements BrooklynTypeRegistry
{
                 }
             }
             if (!Iterables.isEmpty(types)) {
-                type = RegisteredTypes.getBestVersion(types);
+                RegisteredType type = RegisteredTypes.getBestVersion(types);
                 if (type!=null) return Maybe.of(type);
             }
         }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3900e19d/core/src/main/java/org/apache/brooklyn/core/typereg/RegisteredTypes.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/typereg/RegisteredTypes.java b/core/src/main/java/org/apache/brooklyn/core/typereg/RegisteredTypes.java
index 2f792f0..0c7b09b 100644
--- a/core/src/main/java/org/apache/brooklyn/core/typereg/RegisteredTypes.java
+++ b/core/src/main/java/org/apache/brooklyn/core/typereg/RegisteredTypes.java
@@ -24,7 +24,6 @@ import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 
-import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 import org.apache.brooklyn.api.catalog.CatalogItem;
@@ -42,6 +41,7 @@ import org.apache.brooklyn.core.objs.BrooklynObjectInternal;
 import org.apache.brooklyn.core.typereg.JavaClassNameTypePlanTransformer.JavaClassNameTypeImplementationPlan;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.guava.Maybe.Absent;
 import org.apache.brooklyn.util.text.NaturalOrderComparator;
 import org.apache.brooklyn.util.text.VersionComparator;
 import org.apache.brooklyn.util.yaml.Yamls;
@@ -284,13 +284,15 @@ public class RegisteredTypes {
     }
 
     /** Validates that the given type matches the context (if supplied);
-     * returns a Maybe(null) if the type is null. */
+     * if not satisfied. returns an {@link Absent} if failed with details of the error,
+     * with {@link Absent#isNull()} true if the object is null. */
     public static Maybe<RegisteredType> tryValidate(RegisteredType item, final RegisteredTypeLoadingContext
constraint) {
         // kept as a Maybe in case someone wants a wrapper around item validity;
         // unclear what the contract should be, as this can return Maybe.Present(null)
         // which is suprising, but it is more natural to callers otherwise they'll likely
do a separate null check on the item
         // (often handling null different to errors) so the Maybe.get() is redundant as they
have an object for the input anyway.
-        if (item==null || constraint==null) return Maybe.of(item);
+        
+        if (item==null || constraint==null) return Maybe.ofDisallowingNull(item);
         if (constraint.getExpectedKind()!=null && !constraint.getExpectedKind().equals(item.getKind()))
             return Maybe.absent(item+" is not the expected kind "+constraint.getExpectedKind());
         if (constraint.getExpectedJavaSuperType()!=null) {
@@ -336,9 +338,10 @@ public class RegisteredTypes {
 
     /** validates that the given object (required) satisfies the constraints implied by the
given
      * type and context object, using {@link Maybe} as the result set absent containing the
error(s)
-     * if not satisfied */
-    public static <T> Maybe<T> tryValidate(@Nonnull final T object, @Nullable
final RegisteredType type, @Nullable final RegisteredTypeLoadingContext context) {
-        if (object==null) return Maybe.absent("object is null");
+     * if not satisfied. returns an {@link Absent} if failed with details of the error,
+     * with {@link Absent#isNull()} true if the object is null. */
+    public static <T> Maybe<T> tryValidate(final T object, @Nullable final RegisteredType
type, @Nullable final RegisteredTypeLoadingContext context) {
+        if (object==null) return Maybe.absentNull("object is null");
         
         RegisteredTypeKind kind = type!=null ? type.getKind() : context!=null ? context.getExpectedKind()
: null;
         if (kind==null) {
@@ -348,18 +351,18 @@ public class RegisteredTypes {
         return new RegisteredTypeKindVisitor<Maybe<T>>() {
             @Override
             protected Maybe<T> visitSpec() {
-                return validateSpec(object, type, context);
+                return tryValidateSpec(object, type, context);
             }
 
             @Override
             protected Maybe<T> visitBean() {
-                return validateBean(object, type, context);
+                return tryValidateBean(object, type, context);
             }
         }.visit(kind);
     }
 
-    private static <T> Maybe<T> validateBean(T object, RegisteredType type, final
RegisteredTypeLoadingContext context) {
-        if (object==null) return Maybe.absent("object is null");
+    private static <T> Maybe<T> tryValidateBean(T object, RegisteredType type,
final RegisteredTypeLoadingContext context) {
+        if (object==null) return Maybe.absentNull("object is null");
         
         if (type!=null) {
             if (type.getKind()!=RegisteredTypeKind.BEAN)
@@ -378,8 +381,8 @@ public class RegisteredTypes {
         return Maybe.of(object);
     }
 
-    private static <T> Maybe<T> validateSpec(T object, RegisteredType rType,
final RegisteredTypeLoadingContext constraint) {
-        if (object==null) return Maybe.absent("object is null");
+    private static <T> Maybe<T> tryValidateSpec(T object, RegisteredType rType,
final RegisteredTypeLoadingContext constraint) {
+        if (object==null) return Maybe.absentNull("object is null");
         
         if (!(object instanceof AbstractBrooklynObjectSpec)) {
             Maybe.absent("Found "+object+" when expecting a spec");

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3900e19d/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityDecorationResolver.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityDecorationResolver.java
b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityDecorationResolver.java
index e4f500d..6734875 100644
--- a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityDecorationResolver.java
+++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityDecorationResolver.java
@@ -118,7 +118,8 @@ public abstract class BrooklynEntityDecorationResolver<DT> {
             
             Maybe<RegisteredType> item = RegisteredTypes.tryValidate(mgmt.getTypeRegistry().get(policyType),
RegisteredTypeLoadingContexts.spec(Policy.class));
             PolicySpec<?> spec;
-            if (item.get()!=null) {
+            if (!item.isNull()) {
+                // throw error if absent for any reason other than null
                 spec = mgmt.getTypeRegistry().createSpec(item.get(), null, PolicySpec.class);
             } else {
                 Class<? extends Policy> type = decoLoader.getType(Policy.class);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3900e19d/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/ApplicationResource.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/ApplicationResource.java
b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/ApplicationResource.java
index 83c6423..22a4502 100644
--- a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/ApplicationResource.java
+++ b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/ApplicationResource.java
@@ -45,6 +45,7 @@ import org.apache.brooklyn.api.mgmt.Task;
 import org.apache.brooklyn.api.objs.BrooklynObject;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.api.sensor.Sensor;
+import org.apache.brooklyn.api.typereg.RegisteredType;
 import org.apache.brooklyn.core.config.ConstraintViolationException;
 import org.apache.brooklyn.core.entity.Attributes;
 import org.apache.brooklyn.core.entity.EntityPredicates;
@@ -75,6 +76,7 @@ import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.core.ResourceUtils;
 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.javalang.JavaClassNames;
 import org.apache.brooklyn.util.text.Strings;
 import org.codehaus.jackson.JsonNode;
@@ -402,15 +404,21 @@ public class ApplicationResource extends AbstractBrooklynRestResource
implements
     }
 
     private void checkSpecTypeIsValid(String type, Class<? extends BrooklynObject>
subType) {
-        if (RegisteredTypes.tryValidate(mgmt().getTypeRegistry().get(type), RegisteredTypeLoadingContexts.spec(subType))
== null) {
-            try {
-                brooklyn().getCatalogClassLoader().loadClass(type);
-            } catch (ClassNotFoundException e) {
-                log.debug("Class not found for type '" + type + "'; reporting 404", e);
-                throw WebResourceUtils.notFound("Undefined type '%s'", type);
-            }
-            log.info(JavaClassNames.simpleClassName(subType)+" type '{}' not defined in catalog
but is on classpath; continuing", type);
+        Maybe<RegisteredType> typeV = RegisteredTypes.tryValidate(mgmt().getTypeRegistry().get(type),
RegisteredTypeLoadingContexts.spec(subType));
+        if (!typeV.isNull()) {
+            // found, throw if any problem
+            typeV.get();
+            return;
+        }
+        
+        // not found, try classloading
+        try {
+            brooklyn().getCatalogClassLoader().loadClass(type);
+        } catch (ClassNotFoundException e) {
+            log.debug("Class not found for type '" + type + "'; reporting 404", e);
+            throw WebResourceUtils.notFound("Undefined type '%s'", type);
         }
+        log.info(JavaClassNames.simpleClassName(subType)+" type '{}' not defined in catalog
but is on classpath; continuing", type);
     }
     
     private void checkEntityTypeIsValid(String type) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3900e19d/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
index 0993f16..01bf992 100644
--- a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
+++ b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
@@ -65,6 +65,7 @@ import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.core.ResourceUtils;
 import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.stream.Streams;
 import org.apache.brooklyn.util.text.StringPredicates;
 import org.apache.brooklyn.util.text.Strings;
@@ -149,15 +150,20 @@ public class CatalogResource extends AbstractBrooklynRestResource implements
Cat
                 Entitlements.getEntitlementContext().user());
         }
         try {
-            RegisteredType item = mgmt().getTypeRegistry().get(entityId);
-            if (item==null) {
+            Maybe<RegisteredType> item = RegisteredTypes.tryValidate(mgmt().getTypeRegistry().get(entityId),
RegisteredTypeLoadingContexts.spec(Entity.class));
+            if (item.isNull()) {
                 throw WebResourceUtils.notFound("Entity with id '%s' not found", entityId);
             }
-            RegisteredTypes.tryValidate(item, RegisteredTypeLoadingContexts.spec(Entity.class));
+            if (item.isAbsent()) {
+                throw WebResourceUtils.notFound("Item with id '%s' is not an entity", entityId);
+            }
+            
+            brooklyn().getCatalog().deleteCatalogItem(item.get().getSymbolicName(), item.get().getVersion());
             
-            brooklyn().getCatalog().deleteCatalogItem(item.getSymbolicName(), item.getVersion());
         } catch (NoSuchElementException e) {
-            throw WebResourceUtils.notFound("Entity with id '%s' not found", entityId);
+            // shouldn't come here
+            throw WebResourceUtils.notFound("Entity with id '%s' could not be deleted", entityId);
+            
         }
     }
 


Mime
View raw message