Skip to content

Commit be2e7f0

Browse files
committed
support templates for RegisteredType instances, using tags
1 parent c5af549 commit be2e7f0

5 files changed

Lines changed: 42 additions & 16 deletions

File tree

api/src/main/java/org/apache/brooklyn/api/objs/BrooklynObjectType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public static BrooklynObjectType of(Class<?> objectTypeOrSpecType) {
9595
}
9696

9797
public static BrooklynObjectType of(CatalogItemType t) {
98+
if (t==null) return null;
9899
switch (t) {
99100
case ENRICHER: return BrooklynObjectType.ENRICHER;
100101
case ENTITY: return BrooklynObjectType.ENTITY;

api/src/main/java/org/apache/brooklyn/api/typereg/BrooklynTypeRegistry.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public enum RegisteredTypeKind {
3838
/** a registered type which will create the java type described */
3939
BEAN,
4040
/** a partially registered type which requires subsequent validation and changing the kind;
41-
* until then, an item of this kind cannot be instantiated */
41+
* until then, an item of this kind cannot be instantiated.
42+
* items registered as templates (using a tag) may remain in this state
43+
* if they do not resolve. */
4244
UNRESOLVED
4345
// note: additional kinds should have the visitor in core/RegisteredTypeKindVisitor updated
4446
// to flush out all places which want to implement support for all kinds

core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@
4343
import org.apache.brooklyn.api.catalog.CatalogItem;
4444
import org.apache.brooklyn.api.catalog.CatalogItem.CatalogBundle;
4545
import org.apache.brooklyn.api.catalog.CatalogItem.CatalogItemType;
46+
import org.apache.brooklyn.api.entity.Application;
4647
import org.apache.brooklyn.api.internal.AbstractBrooklynObjectSpec;
47-
import org.apache.brooklyn.api.location.Location;
48-
import org.apache.brooklyn.api.location.LocationSpec;
4948
import org.apache.brooklyn.api.mgmt.ManagementContext;
5049
import org.apache.brooklyn.api.mgmt.classloading.BrooklynClassLoadingContext;
5150
import org.apache.brooklyn.api.objs.BrooklynObject;
@@ -56,7 +55,7 @@
5655
import org.apache.brooklyn.api.typereg.RegisteredType;
5756
import org.apache.brooklyn.core.catalog.CatalogPredicates;
5857
import org.apache.brooklyn.core.catalog.internal.CatalogClasspathDo.CatalogScanningModes;
59-
import org.apache.brooklyn.core.location.BasicLocationRegistry;
58+
import org.apache.brooklyn.core.mgmt.BrooklynTags;
6059
import org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult;
6160
import org.apache.brooklyn.core.mgmt.ha.OsgiManager;
6261
import org.apache.brooklyn.core.mgmt.internal.CampYamlParser;
@@ -869,22 +868,34 @@ private void collectCatalogItemsFromItemMetadataBlock(String sourceYaml, Managed
869868
// just need TODO to make sure we delete previously-persisted things which now come through this path.)
870869
// NB: when everything is a bundle and we've removed all scanning then this can be the _only_ path
871870
// and code can be massively simpler
871+
// TODO allow these to be set in catalog.bom ?
872+
List<String> aliases = MutableList.of();
873+
List<Object> tags = MutableList.of();
874+
Boolean catalogDisabled = null;
875+
876+
MutableList<Object> superTypes = MutableList.of();
877+
878+
if (itemType==CatalogItemType.TEMPLATE) {
879+
tags.add(BrooklynTags.CATALOG_TEMPLATE);
880+
itemType = CatalogItemType.ENTITY;
881+
superTypes.add(Application.class);
882+
}
872883

873884
if (resolutionError!=null) {
874-
if (requireValidation) {
875-
throw Exceptions.propagate(resolutionError);
885+
if (!tags.contains(BrooklynTags.CATALOG_TEMPLATE)) {
886+
if (requireValidation) {
887+
throw Exceptions.propagate(resolutionError);
888+
}
889+
// warn? add as "unresolved" ? just do nothing?
876890
}
877-
// warn? add as "unresolved" ? just do nothing?
878891
}
879-
String format = null; // could support specifying format
880-
// TODO if kind and supertype is known, set those here
881-
Class<?> javaType = null;
882-
List<Object> superTypes = MutableList.of().appendIfNotNull(javaType);
892+
String format = null; // could support specifying format?
883893

884-
// TODO allow these to be set in catalog.bom ?
885-
List<String> aliases = MutableList.of();
886-
List<Object> tags = MutableList.of();
887-
Boolean catalogDisabled = null;
894+
if (itemType!=null) {
895+
// if supertype is known, set it here;
896+
// we don't set kind (spec) because that is inferred from the supertype type
897+
superTypes.appendIfNotNull(BrooklynObjectType.of(itemType).getInterfaceType());
898+
}
888899

889900
if (version==null) {
890901
// use this as default version when nothing specified
@@ -1447,6 +1458,10 @@ public Map<RegisteredType,Collection<Throwable>> validateTypes(Iterable<Register
14471458
public Collection<Throwable> validateType(RegisteredType typeToValidate) {
14481459
ReferenceWithError<RegisteredType> result = resolve(typeToValidate);
14491460
if (result.hasError()) {
1461+
if (RegisteredTypes.isTemplate(typeToValidate)) {
1462+
// ignore for templates
1463+
return Collections.emptySet();
1464+
}
14501465
if (result.getError() instanceof CompoundRuntimeException) {
14511466
return ((CompoundRuntimeException)result.getError()).getAllCauses();
14521467
}
@@ -1456,7 +1471,7 @@ public Collection<Throwable> validateType(RegisteredType typeToValidate) {
14561471
((BasicBrooklynTypeRegistry) mgmt.getTypeRegistry()).addToLocalUnpersistedTypeRegistry(result.get(), true);
14571472
return Collections.emptySet();
14581473
}
1459-
1474+
14601475
/**
14611476
* Resolves the given object with respect to the catalog. Returns any errors found while trying to resolve.
14621477
* The argument may be changed (e.g. its kind set, supertypes set), and normal usage is to add

core/src/main/java/org/apache/brooklyn/core/mgmt/BrooklynTags.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class BrooklynTags {
3939
public static final String NOTES_KIND = "notes";
4040
public static final String OWNER_ENTITY_ID = "owner_entity_id";
4141
public static final String ICON_URL = "icon_url";
42+
/** tag on a registered type indicating that an item is intended to be used as a template,
43+
* and does not have to resolve */
44+
public static final Object CATALOG_TEMPLATE = "catalog_template";
4245

4346
public static class NamedStringTag implements Serializable {
4447
private static final long serialVersionUID = 7932098757009051348L;

core/src/main/java/org/apache/brooklyn/core/typereg/RegisteredTypes.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,4 +584,9 @@ public static RegisteredType changePlan(RegisteredType type, TypeImplementationP
584584
return type;
585585
}
586586

587+
public static boolean isTemplate(RegisteredType type) {
588+
if (type==null) return false;
589+
return type.getTags().contains(BrooklynTags.CATALOG_TEMPLATE);
590+
}
591+
587592
}

0 commit comments

Comments
 (0)