brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/3] brooklyn-server git commit: background clarification on type registry and related utils
Date Mon, 26 Sep 2016 16:11:56 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master dfcd8857a -> 57fabdf8c


background clarification on type registry and related utils


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

Branch: refs/heads/master
Commit: b8467b225d64e2bbf6502c7919f557cfc17be4ad
Parents: 4e76170
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Wed Sep 7 15:47:45 2016 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Wed Sep 7 15:58:59 2016 +0100

----------------------------------------------------------------------
 .../brooklyn/api/typereg/RegisteredType.java    |  9 +++-
 .../core/typereg/BasicBrooklynTypeRegistry.java |  4 +-
 .../brooklyn/core/typereg/RegisteredTypes.java  | 45 +++++++++++++++-----
 .../core/typereg/TypePlanTransformers.java      |  4 +-
 .../JavaClassNameTypePlanTransformerTest.java   |  2 +-
 .../brooklyn/util/exceptions/Exceptions.java    |  2 +
 .../org/apache/brooklyn/util/guava/Maybe.java   |  2 +-
 7 files changed, 51 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b8467b22/api/src/main/java/org/apache/brooklyn/api/typereg/RegisteredType.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/brooklyn/api/typereg/RegisteredType.java b/api/src/main/java/org/apache/brooklyn/api/typereg/RegisteredType.java
index 29b64d3..1eb7391 100644
--- a/api/src/main/java/org/apache/brooklyn/api/typereg/RegisteredType.java
+++ b/api/src/main/java/org/apache/brooklyn/api/typereg/RegisteredType.java
@@ -47,18 +47,23 @@ public interface RegisteredType extends Identifiable {
     /** @return all declared supertypes or super-interfaces of this registered type,
      * consisting of a collection of {@link Class} or {@link RegisteredType}
      * <p>
-     * This should normally include at least one {@link Class} object:
+     * This should normally include at least one {@link Class} object for filtering purposes:
      * For beans, this should include the java type that the {@link BrooklynTypeRegistry}
will create. 
      * For specs, this should refer to the {@link BrooklynObject} type that the created spec
will point at 
      * (e.g. the concrete {@link Entity}, not the {@link EntitySpec}).
      * <p>
-     * This may not necessarily return the most specific java class or classes;
+     * This will normally not return all ancestor classes,
+     * and it is not required even to return the most specific java class or classes:
      * such as if the concrete type is private and callers should know only about a particular
public interface,
      * or if precise type details are unavailable and all that is known at creation is some
higher level interface/supertype
      * (e.g. this may return {@link Entity} even though the spec points at a specific subclass,
      * for instance because the YAML has not yet been parsed or OSGi bundles downloaded).
      * <p>
      * This may include other registered types such as marker interfaces.
+     * <p>
+     * It may even include multiple interfaces but exclude the concrete subclass which implements
them all
+     * (for instance if that concrete implementation is an internal private class). 
+     * However it must be possible for the corresponding transformer to instantiate that
type at runtime. 
      */
     @Beta
     Set<Object> getSuperTypes();

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b8467b22/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 c112779..75697f8 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
@@ -240,7 +240,7 @@ public class BasicBrooklynTypeRegistry implements BrooklynTypeRegistry
{
 
     @Override
     public <SpecT extends AbstractBrooklynObjectSpec<?, ?>> SpecT createSpecFromPlan(String
planFormat, Object planData, RegisteredTypeLoadingContext optionalConstraint, Class<SpecT>
optionalSpecSuperType) {
-        return createSpec(RegisteredTypes.spec(null, null, new BasicTypeImplementationPlan(planFormat,
planData), null),
+        return createSpec(RegisteredTypes.anonymousRegisteredType(RegisteredTypeKind.SPEC,
new BasicTypeImplementationPlan(planFormat, planData)),
             optionalConstraint, optionalSpecSuperType);
     }
 
@@ -269,7 +269,7 @@ public class BasicBrooklynTypeRegistry implements BrooklynTypeRegistry
{
 
     @Override
     public <T> T createBeanFromPlan(String planFormat, Object planData, RegisteredTypeLoadingContext
optionalConstraint, Class<T> optionalSuperType) {
-        return createBean(RegisteredTypes.bean(null, null, new BasicTypeImplementationPlan(planFormat,
planData), null),
+        return createBean(RegisteredTypes.anonymousRegisteredType(RegisteredTypeKind.BEAN,
new BasicTypeImplementationPlan(planFormat, planData)),
             optionalConstraint, optionalSuperType);
     }
     

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b8467b22/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 72e05ea..cfc7ec4 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,6 +24,7 @@ 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;
@@ -45,6 +46,8 @@ 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;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Function;
@@ -57,7 +60,7 @@ import com.google.common.reflect.TypeToken;
 /**
  * Utility and preferred creation mechanisms for working with {@link RegisteredType} instances.
  * <p>
- * Use {@link #bean(String, String, TypeImplementationPlan, Class)} and {@link #spec(String,
String, TypeImplementationPlan, Class)}
+ * Use {@link #bean(String, String, TypeImplementationPlan)} and {@link #spec(String, String,
TypeImplementationPlan)}
  * to create {@link RegisteredType} instances.
  * <p>
  * See {@link #isSubtypeOf(RegisteredType, Class)} or {@link #isSubtypeOf(RegisteredType,
RegisteredType)} to 
@@ -65,6 +68,8 @@ import com.google.common.reflect.TypeToken;
  */
 public class RegisteredTypes {
 
+    private static final Logger log = LoggerFactory.getLogger(RegisteredTypes.class);
+    
     @SuppressWarnings("serial")
     static ConfigKey<Class<?>> ACTUAL_JAVA_TYPE = ConfigKeys.newConfigKey(new
TypeToken<Class<?>>() {}, "java.type.actual",
         "The actual Java type which will be instantiated (bean) or pointed at (spec)");
@@ -106,18 +111,38 @@ public class RegisteredTypes {
         return type;
     }
 
-    /** Preferred mechanism for defining a bean {@link RegisteredType}. */
-    public static RegisteredType bean(String symbolicName, String version, TypeImplementationPlan
plan, @Nullable Class<?> superType) {
-        return addSuperType(new BasicRegisteredType(RegisteredTypeKind.BEAN, symbolicName,
version, plan), superType);
+    /** Preferred mechanism for defining a bean {@link RegisteredType}. 
+     * Callers should also {@link #addSuperTypes(RegisteredType, Iterable)} on the result.*/
+    public static RegisteredType bean(@Nonnull String symbolicName, @Nonnull String version,
@Nonnull TypeImplementationPlan plan) {
+        if (symbolicName==null || version==null) log.warn("Deprecated use of RegisteredTypes
API passing null name/version", new Exception("Location of deprecated use, wrt "+plan));
+        return new BasicRegisteredType(RegisteredTypeKind.BEAN, symbolicName, version, plan);
+    }
+    /** Convenience for {@link #bean(String, String, TypeImplementationPlan)} when there
is a single known java signature/super type */
+    public static RegisteredType bean(@Nonnull String symbolicName, @Nonnull String version,
@Nonnull TypeImplementationPlan plan, @Nonnull Class<?> superType) {
+        if (superType==null) log.warn("Deprecated use of RegisteredTypes API passing null
name/version", new Exception("Location of deprecated use, wrt "+symbolicName+":"+version+"
"+plan));
+        return addSuperType(bean(symbolicName, version, plan), superType);
     }
     
-    /** Preferred mechanism for defining a spec {@link RegisteredType}. */
-    // TODO we currently allow symbolicName and version to be null for the purposes of creation,
internal only in BasicBrooklynTypeRegistry.createSpec
-    // (ideally the API in TypePlanTransformer can be changed so even that is not needed)
-    public static RegisteredType spec(String symbolicName, String version, TypeImplementationPlan
plan, @Nullable Class<?> superType) {
-        return addSuperType(new BasicRegisteredType(RegisteredTypeKind.SPEC, symbolicName,
version, plan), superType);
+    /** Preferred mechanism for defining a spec {@link RegisteredType}.
+     * Callers should also {@link #addSuperTypes(RegisteredType, Iterable)} on the result.*/
+    public static RegisteredType spec(@Nonnull String symbolicName, @Nonnull String version,
@Nonnull TypeImplementationPlan plan) {
+        if (symbolicName==null || version==null) log.warn("Deprecated use of RegisteredTypes
API passing null name/version", new Exception("Location of deprecated use, wrt "+plan));
+        return new BasicRegisteredType(RegisteredTypeKind.SPEC, symbolicName, version, plan);
+    }
+    /** Convenience for {@link #cpec(String, String, TypeImplementationPlan)} when there
is a single known java signature/super type */
+    public static RegisteredType spec(@Nonnull String symbolicName, @Nonnull String version,
@Nonnull TypeImplementationPlan plan, @Nonnull Class<?> superType) {
+        if (superType==null) log.warn("Deprecated use of RegisteredTypes API passing null
name/version", new Exception("Location of deprecated use, wrt "+symbolicName+":"+version+"
"+plan));
+        return addSuperType(spec(symbolicName, version, plan), superType);
     }
 
+    /** Creates an anonymous {@link RegisteredType} for plan-instantiation-only use. */
+    @Beta
+    // internal only in *TypeRegistry.create methods; uses null for name and version,
+    // which is ignored in the TypePlanTransformer; might be better to clean up the API to
avoid this
+    public static RegisteredType anonymousRegisteredType(RegisteredTypeKind kind, TypeImplementationPlan
plan) {
+        return new BasicRegisteredType(kind, null, null, plan);
+    }
+    
     /** returns the {@link Class} object corresponding to the given java type name and registered
type,
      * using the cache on the type in the first instance, falling back to the loader defined
the type and context. */
     @Beta
@@ -156,7 +181,7 @@ public class RegisteredTypes {
         return type;
     }
     @Beta
-    public static RegisteredType addSuperTypes(RegisteredType type, Iterable<Object>
superTypesAsClassOrRegisteredType) {
+    public static RegisteredType addSuperTypes(RegisteredType type, Iterable<? extends
Object> superTypesAsClassOrRegisteredType) {
         if (superTypesAsClassOrRegisteredType!=null) {
             for (Object superType: superTypesAsClassOrRegisteredType) {
                 if (superType==null) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b8467b22/core/src/main/java/org/apache/brooklyn/core/typereg/TypePlanTransformers.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/typereg/TypePlanTransformers.java
b/core/src/main/java/org/apache/brooklyn/core/typereg/TypePlanTransformers.java
index f799d98..bd2a1ab 100644
--- a/core/src/main/java/org/apache/brooklyn/core/typereg/TypePlanTransformers.java
+++ b/core/src/main/java/org/apache/brooklyn/core/typereg/TypePlanTransformers.java
@@ -130,7 +130,9 @@ public class TypePlanTransformers {
                     (Strings.isNonBlank(e.getMessage()) ? " ("+e.getMessage()+")" : ""));
             } catch (Throwable e) {
                 Exceptions.propagateIfFatal(e);
-                failuresFromTransformers.add(new PropagatedRuntimeException("Transformer
for "+t.getFormatCode()+" gave an error creating this plan: "+
+                failuresFromTransformers.add(new PropagatedRuntimeException(
+                    (type.getSymbolicName()!=null ? "Error in definition of "+type.getId()
: 
+                        "Transformer for "+t.getFormatCode()+" gave an error creating this
plan") + ": "+
                     Exceptions.collapseText(e), e));
             }
         }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b8467b22/core/src/test/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerTest.java
b/core/src/test/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerTest.java
index 55b5d3b..a18dcfa 100644
--- a/core/src/test/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/typereg/JavaClassNameTypePlanTransformerTest.java
@@ -42,7 +42,7 @@ public class JavaClassNameTypePlanTransformerTest extends BrooklynMgmtUnitTestSu
     }
     
     protected RegisteredType newNoArgRegisteredType(String format) {
-        return RegisteredTypes.bean("no-arg", "1.0", new BasicTypeImplementationPlan(format,
NoArg.class.getName()), null);
+        return RegisteredTypes.bean("no-arg", "1.0", new BasicTypeImplementationPlan(format,
NoArg.class.getName()));
     }
     
     protected BrooklynTypePlanTransformer newTransformer() {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b8467b22/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
b/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
index e4c9980..279d22c 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/exceptions/Exceptions.java
@@ -351,6 +351,8 @@ public class Exceptions {
         return collapseText(t, false);
     }
     
+    /** as {@link #collapseText(Throwable)} but skipping any throwables which implement {@link
CanSkipInContext}
+     * and indicate they should be skipped any of the given contexts */
     public static String collapseTextInContext(Throwable t, Object ...contexts) {
         return collapseText(t, false, ImmutableSet.<Throwable>of(), contexts);
     }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b8467b22/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 73c9761..70a5432 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
@@ -99,7 +99,7 @@ public abstract class Maybe<T> implements Serializable, Supplier<T>
{
 
     /** as {@link #absentNull(String)} but with a generic message */
     public static <T> Maybe<T> absentNull() {
-        return absentNull("value is null");
+        return absentNull("disallowed null value");
     }
     
     /** like {@link #absent(String)} but {@link #isNull()} will return true on the result.
*/


Mime
View raw message