brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From neykov <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Flesh out type creation using the...
Date Thu, 12 Nov 2015 10:45:22 GMT
Github user neykov commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/1017#discussion_r44643336
  
    --- Diff: core/src/main/java/org/apache/brooklyn/core/typereg/RegisteredTypes.java ---
    @@ -66,180 +94,131 @@ public static RegisteredType of(CatalogItem<?, ?> item) {
             type.deprecated = item.isDeprecated();
     
             // TODO
    -        // javaType, specType, registeredTypeName ...
    -        // tags ?
    +        // probably not: javaType, specType, registeredTypeName ...
    +        // maybe: tags ?
             return type;
         }
     
    -    /** Visitor adapter which can be used to ensure all kinds are supported */
    -    public static abstract class RegisteredTypeKindVisitor<T> {
    -        public T visit(RegisteredType type) {
    -            if (type==null) throw new NullPointerException("Registered type must not
be null");
    -            if (type instanceof RegisteredSpecType) {
    -                return visitSpec((RegisteredSpecType)type);
    -            }
    -            // others go here
    -            throw new IllegalStateException("Unexpected registered type: "+type.getClass());
    -        }
    -
    -        protected abstract T visitSpec(RegisteredSpecType type);
    -        
    -        // TODO beans, others
    +    /** 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);
         }
         
    -    public static RegisteredTypeKind getKindOf(RegisteredType type) {
    -        return new RegisteredTypeKindVisitor<RegisteredTypeKind>() {
    -            @Override protected RegisteredTypeKind visitSpec(RegisteredSpecType type)
{ return RegisteredTypeKind.SPEC; }
    -        }.visit(type);
    +    public static RegisteredType spec(String symbolicName, String version, TypeImplementationPlan
plan, @Nullable Class<?> superType) {
    +        return addSuperType(new BasicRegisteredType(RegisteredTypeKind.SPEC, symbolicName,
version, plan), superType);
         }
    -    
    -    public abstract static class AbstractRegisteredType implements RegisteredType {
     
    -        final String symbolicName;
    -        final String version;
    +    /** returns the {@link Class} object corresponding to the given java type name,
    +     * using the cache on the type and the loader defined on the type
    +     * @param mgmt */
    +    @Beta
    +    // TODO should this be on the AbstractTypePlanTransformer ?
    +    public static Class<?> loadActualJavaType(String javaTypeName, ManagementContext
mgmt, RegisteredType type, RegisteredTypeLoadingContext context) throws Exception {
    +        Class<?> result = ((BasicRegisteredType)type).getCache().get(ACTUAL_JAVA_TYPE);
    +        if (result!=null) return result;
             
    -        List<OsgiBundleWithUrl> bundles;
    -        String displayName;
    -        String description;
    -        String iconUrl;
    -        boolean deprecated;
    -        boolean disabled;
    -
    -        // TODO ensure this is re-populated on rebind
    -        transient Class<?> javaType;
    +        result = CatalogUtils.newClassLoadingContext(mgmt, type, context==null ? null
: context.getLoader()).loadClass( javaTypeName );
    +        Preconditions.checkNotNull(result, "Could not load class "+javaTypeName+"; returned
null (should have thrown a different exception!)");
             
    -        public AbstractRegisteredType(String symbolicName, String version, Class<?>
javaType) {
    -            this.symbolicName = symbolicName;
    -            this.version = version;
    -            this.javaType = javaType;
    -        }
    -
    -        @Override
    -        public String getId() {
    -            return symbolicName + (version!=null ? ":"+version : "");
    -        }
    +        ((BasicRegisteredType)type).getCache().put(ACTUAL_JAVA_TYPE, result);
    +        return result;
    +    }
     
    -        @Override
    -        public String getSymbolicName() {
    -            return symbolicName;
    +    @Beta
    +    public static RegisteredType addSuperType(RegisteredType type, @Nullable Class<?>
superType) {
    +        if (superType!=null) {
    +            ((BasicRegisteredType)type).superTypes.add(superType);
             }
    +        return type;
    +    }
     
    -        @Override
    -        public String getVersion() {
    -            return version;
    -        }
    -        
    -        @Override
    -        public Collection<OsgiBundleWithUrl> getLibraries() {
    -            return bundles;
    +    @Beta
    +    public static RegisteredType addSuperType(RegisteredType type, @Nullable RegisteredType
superType) {
    +        if (superType!=null) {
    +            if (isSubTypeOf(superType, type)) {
    +                throw new IllegalStateException(superType+" declares "+type+" as a supertype;
cannot set "+superType+" as a supertype of "+type);
    +            }
    +            ((BasicRegisteredType)type).superTypes.add(superType);
             }
    +        return type;
    +    }
     
    -        @Override
    -        public String getDisplayName() {
    -            return displayName;
    -        }
    +    /** returns the implementation data for a spec if it is a string (e.g. plan yaml
or java class name); else false */
    +    @Beta
    +    public static String getImplementationDataStringForSpec(RegisteredType item) {
    +        if (item==null || item.getPlan()==null) return null;
    +        Object data = item.getPlan().getPlanData();
    +        if (!(data instanceof String)) return null;
    +        return (String)data;
    +    }
     
    -        @Override
    -        public String getDescription() {
    -            return description;
    -        }
    +    /** returns an implementation of the spec class corresponding to the given target
type;
    +     * for use in {@link BrooklynTypePlanTransformer#create(RegisteredType, RegisteredTypeLoadingContext)}

    +     * implementations when dealing with a spec; returns null if none found
    +     * @param mgmt */
    +    @Beta
    +    public static AbstractBrooklynObjectSpec<?,?> newSpecInstance(ManagementContext
mgmt, Class<? extends BrooklynObject> targetType) throws Exception {
    +        Class<? extends AbstractBrooklynObjectSpec<?, ?>> specType = RegisteredTypeLoadingContexts.lookupSpecTypeForTarget(targetType);
    +        if (specType==null) return null;
    +        Method createMethod = specType.getMethod("create", Class.class);
    +        return (AbstractBrooklynObjectSpec<?, ?>) createMethod.invoke(null, targetType);
    +    }
     
    -        @Override
    -        public String getIconUrl() {
    -            return iconUrl;
    -        }
    -        
    -        @Override
    -        public boolean isDisabled() {
    -            return disabled;
    -        }
    -        
    -        @Override
    -        public boolean isDeprecated() {
    -            return deprecated;
    -        }
    -        
    -        @Override
    -        public Class<?> getJavaType() {
    -            return javaType;
    -        }
    -        
    -        @Override
    -        public String toString() {
    -            return JavaClassNames.simpleClassName(this)+"["+getId()+
    -                (isDisabled() ? ";DISABLED" : "")+
    -                (isDeprecated() ? ";deprecated" : "")+
    -                "]";
    -        }
    +    /** Returns a wrapped map, if the object is YAML which parses as a map; 
    +     * otherwise returns absent capable of throwing an error with more details */
    +    @SuppressWarnings("unchecked")
    +    public static Maybe<Map<Object,Object>> getAsYamlMap(Object planData)
{
    --- End diff --
    
    Return type should be `Maybe<Map<?,?>>` (it's a read-only container), callers
are not supposed to mutate it.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message