public Builder rawDataTransformer(RawDataTransformer val) { for (BrooklynObjectType type : BrooklynObjectType.values()) { rawDataTransformer(type, val); } return this; } public Builder rawDataTransformer(BrooklynObjectType type, RawDataTransformer val) {
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; }
/** 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 */
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; }
/** 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(); }
if (name.equals("deletions")) { Set<String> validKeys = Sets.newLinkedHashSet(); for (BrooklynObjectType type : BrooklynObjectType.values()) { String key = type.getSubPathName(); validKeys.add(key);
/** 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); }
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) {
@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; } switch (type) { case CATALOG_ITEM: return rebindContext.getCatalogItem(id); case MANAGED_BUNDLE: return rebindContext.getBundle(id); case ENRICHER: return rebindContext.getEnricher(id); case ENTITY: return rebindContext.getEntity(id); case FEED: return rebindContext.getFeed(id); case LOCATION: return rebindContext.getLocation(id); case POLICY: return rebindContext.getPolicy(id); case UNKNOWN: return null; } throw new IllegalStateException("Unexpected type "+type+" / id "+id); }
@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; }}); }