public static BrooklynObjectType of(Class<?> objectTypeOrSpecType) { if (objectTypeOrSpecType!=null) { for (BrooklynObjectType t: values()) { if (t.getInterfaceType()!=null && t.getInterfaceType().isAssignableFrom(objectTypeOrSpecType)) return t; if (t.getSpecType()!=null && t.getSpecType().isAssignableFrom(objectTypeOrSpecType)) return t; } } return UNKNOWN; }
public void add(BrooklynObject instance) { BrooklynObjectType type = BrooklynObjectType.of(instance); getUnsafeCollectionOfType(type).add(instance); if (type==BrooklynObjectType.CATALOG_ITEM) { removedCatalogItemIds.remove(instance.getId()); } }
@Override public void onNotFound(BrooklynObjectType type, String id) { if (creationFailedIds.contains(id)) { // already know about this; ignore } else { String errmsg = type.toCamelCase()+" '"+id+"' not found"; exceptions.add(new IllegalStateException(errmsg)); onErrorImpl(errmsg); } }
public static BrooklynObjectType of(BrooklynObject instance) { if (instance!=null) { for (BrooklynObjectType t: values()) { if (t.getInterfaceType()!=null && t.getInterfaceType().isInstance(instance)) return t; } } return UNKNOWN; }
public Builder rawDataTransformer(RawDataTransformer val) { for (BrooklynObjectType type : BrooklynObjectType.values()) { rawDataTransformer(type, val); } return this; } public Builder rawDataTransformer(BrooklynObjectType type, RawDataTransformer val) {
if (name.equals("deletions")) { Set<String> validKeys = Sets.newLinkedHashSet(); for (BrooklynObjectType type : BrooklynObjectType.values()) { String key = type.getSubPathName(); validKeys.add(key); List<String> ids = (List<String>) args.get(key);
for (Object superI: supers) { BrooklynObjectType boTypeI = null; if (superI instanceof BrooklynObject) boTypeI = BrooklynObjectType.of((BrooklynObject)superI); else if (superI instanceof Class) boTypeI = BrooklynObjectType.of((Class<?>)superI); if (boTypeI!=null && boTypeI!=BrooklynObjectType.UNKNOWN) { if (boType==null) boType = boTypeI; resultO = ((BasicBrooklynTypeRegistry)mgmt.getTypeRegistry()).createSpec(resultT, constraint, boType.getSpecType()); } catch (Exception e) { Exceptions.propagateIfFatal(e); if (boType==null) { boType = BrooklynObjectType.of(ciType); if (boType!=null) { supers = MutableSet.copyOf(supers); supers.add(boType.getInterfaceType()); } else if (Objects.equal(boType, BrooklynObjectType.of(ciType))) { if (specError==null) { throw new IllegalStateException("Guesser resolved but TypeRegistry couldn't create"); supers.add(BrooklynObjectType.of(resultO.getClass()).getInterfaceType()); RegisteredTypes.addSuperTypes(resultT, supers);
private String idFromPath(BrooklynObjectType type, String path) { // the replace underscore with colon below handles file names of catalog items like "catalog/my.catalog.app.id.load_0.1.0" return path.substring(type.getSubPathName().length()+1).replace('_', ':'); }
superTypes.appendIfNotNull(BrooklynObjectType.of(itemType).getInterfaceType());
public String toCamelCase() { return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, this.name()); }
/** registers the given XSLT code to be applied to all persisted {@link BrooklynObjectType}s */ public Builder xsltTransformer(String xslt) { XsltTransformer xsltTransformer = new XsltTransformer(xslt); for (BrooklynObjectType type : BrooklynObjectType.values()) { rawDataTransformer(type, xsltTransformer); } return this; } /** registers the given XSLT code to be applied to the given persisted {@link BrooklynObjectType}s */
@Override public Void apply(BrooklynMementoPersister input) { try { BrooklynMementoRawData transformed = transformer.transform(input, RebindExceptionHandlerImpl.builder().build()); PersistenceObjectStore objectStore = ((BrooklynMementoPersisterToObjectStore)input).getObjectStore(); for (BrooklynObjectType type : BrooklynObjectType.values()) { final List<String> contents = objectStore.listContentsWithSubPath(type.getSubPathName()); for (String path : contents) { if (path.endsWith(".jar")) { // don't apply transformers to JARs continue; } StoreObjectAccessor accessor = objectStore.newAccessor(path); String memento = checkNotNull(accessor.get(), path); String replacement = transformed.getObjectsOfType(type).get(idFromPath(type, path)); getLogger().trace("Replacing {} with {}", memento, replacement); accessor.put(replacement); } } } catch (Exception e) { Exceptions.propagateIfFatal(e); getLogger().warn(Strings.join(new Object[]{ "Caught'", e.getMessage(), "' when transforming '", input.getBackingStoreDescription() }, ""), e); } return null; }}); }
for (Map.Entry<String, String> entry : newMemento.getObjectsOfType(type).entrySet()) { addPersistContentIfManagedBundle(type, entry.getKey(), futures, exceptionHandler); futures.add(asyncPersist(type.getSubPathName(), type, entry.getKey(), entry.getValue(), exceptionHandler));
/** given a {@link BrooklynObject}, returns the spec class which would generate it, for instance returns {@link EntitySpec} given {@link Entity}, * or null if not known */ static <BO extends BrooklynObject> Class<? extends AbstractBrooklynObjectSpec<?,?>> lookupSpecTypeForTarget(Class<BO> targetSuperType) { if (targetSuperType==null) return null; BrooklynObjectType best = null; for (BrooklynObjectType t: BrooklynObjectType.values()) { if (t.getInterfaceType()==null) continue; if (!t.getInterfaceType().isAssignableFrom(targetSuperType)) continue; // on equality, exit immediately if (t.getInterfaceType().equals(targetSuperType)) return t.getSpecType(); // else pick which is best if (best==null) { best = t; continue; } // if t is more specific, it is better (handles case when e.g. a Policy is a subclass of Entity) if (best.getInterfaceType().isAssignableFrom(t.getInterfaceType())) { best = t; continue; } } if (best==null) { log.warn("Unexpected target supertype ("+targetSuperType+"); unable to infer spec type"); return null; } // the spec is more specific, but we're not familiar with it here; return the best return best.getSpecType(); }
@Override public BrooklynObject peek(BrooklynObjectType type, String id) { if (type==null) { for (BrooklynObjectType typeX: BrooklynObjectType.values()) { BrooklynObject result = peek(typeX, id); if (result!=null) return result; } return null; } Map<String, ? extends BrooklynObject> map = getMapFor(type); if (map==null) return null; return map.get(id); }
public Builder putType(BrooklynObjectType type, String id, String javaType) { switch (type) { case ENTITY: throw new IllegalArgumentException(type.toCamelCase()+" requires additional parameters"); case LOCATION: return location(id, javaType); case POLICY: return policy(id, javaType); case ENRICHER: return enricher(id, javaType); case FEED: return feed(id, javaType); case CATALOG_ITEM: case MANAGED_BUNDLE: throw new IllegalArgumentException(type.toCamelCase()+" requires different parameters"); case UNKNOWN: default: throw new IllegalArgumentException(type.toCamelCase()+" not supported"); } }
public void addIfNotRemoved(BrooklynObject instance) { BrooklynObjectType type = BrooklynObjectType.of(instance); if (!getRemovedIdsOfType(type).contains(instance.getId())) { getUnsafeCollectionOfType(type).add(instance); } }
if (!deletedIds.contains(item.getId())) { addPersistContentIfManagedBundle(type, item.getId(), futures, exceptionHandler); futures.add(asyncPersist(type.getSubPathName(), item, exceptionHandler)); futures.add(asyncDelete(type.getSubPathName(), id, exceptionHandler)); if (type==BrooklynObjectType.MANAGED_BUNDLE) { futures.add(asyncDelete(type.getSubPathName(), id+".jar", exceptionHandler));
/** given a spec, returns the class of the item it targets, for instance returns {@link Entity} given {@link EntitySpec}; * see also {@link #lookupSpecTypeForTarget(Class)}, * If given null, returns {@link BrooklynObject} */ static <T extends AbstractBrooklynObjectSpec<?,?>> Class<? extends BrooklynObject> lookupTargetTypeForSpec(@Nullable Class<T> specSuperType) { if (specSuperType==null) return BrooklynObject.class; BrooklynObjectType best = null; for (BrooklynObjectType t: BrooklynObjectType.values()) { if (t.getSpecType()==null) continue; if (!t.getSpecType().isAssignableFrom(specSuperType)) continue; // on equality, exit immediately if (t.getSpecType().equals(specSuperType)) return t.getInterfaceType(); // else pick which is best if (best==null) { best = t; continue; } // if t is more specific, it is better (handles case when e.g. a Policy is a subclass of Entity) if (best.getSpecType().isAssignableFrom(t.getSpecType())) { best = t; continue; } } if (best==null) { log.warn("Unexpected spec supertype ("+specSuperType+"); treating as any "+BrooklynObject.class, new Throwable("Trace for unexpected spec supertype")); return BrooklynObject.class; } // the spec is more specific, but we're not familiar with it here; return the best return best.getInterfaceType(); }
for (BrooklynObjectType type : BrooklynObjectType.values()) { Set<String> itemsToDelete = ImmutableSet.copyOf(deletions.get(type)); Set<String> missing; for (BrooklynObjectType type : BrooklynObjectType.values()) { Collection<RawDataTransformer> transformers = rawDataTransformers.get(type); for (RawDataTransformer transformer : transformers) {