org.apache.brooklyn.core.enricher.AbstractEnricher Java Examples

The following examples show how to use org.apache.brooklyn.core.enricher.AbstractEnricher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractEntity.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public void add(Enricher enricher) {
    Enricher old = findApparentlyEqualAndWarnIfNotSameUniqueTag(enrichersInternal, enricher);
    if (old!=null) {
        LOG.debug("Removing "+old+" when adding "+enricher+" to "+AbstractEntity.this);
        remove(old);
    }
    
    CatalogUtils.setCatalogItemIdOnAddition(AbstractEntity.this, enricher);
    enrichersInternal.add((AbstractEnricher) enricher);
    ((AbstractEnricher)enricher).setEntity(AbstractEntity.this);
    ConfigConstraints.assertValid(enricher);
    
    getManagementSupport().getEntityChangeListener().onEnricherAdded(enricher);
    // TODO Could add equivalent of AbstractEntity.POLICY_ADDED for enrichers; no use-case for that yet
}
 
Example #2
Source File: MementosGenerators.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/**
 * Given an enricher, extracts its state for serialization.
 * @deprecated since 0.7.0, see {@link #newBasicMemento(BrooklynObject)}
 */
@Deprecated
public static EnricherMemento newEnricherMemento(Enricher enricher) {
    BasicEnricherMemento.Builder builder = BasicEnricherMemento.builder();
    populateBrooklynObjectMementoBuilder(enricher, builder);
    
    // TODO persist config keys as well? Or only support those defined on policy class;
    // current code will lose the ConfigKey type on rebind for anything not defined on class.
    // Whereas entities support that.
    // TODO Do we need the "nonPersistableFlagNames" that locations use?
    Map<ConfigKey<?>, Object> config = ((AbstractEnricher)enricher).config().getInternalConfigMap().getAllConfigLocalRaw();
    for (Map.Entry<ConfigKey<?>, Object> entry : config.entrySet()) {
        ConfigKey<?> key = checkNotNull(entry.getKey(), "config=%s", config);
        Object value = configValueToPersistable(entry.getValue(), enricher, key.getName());
        builder.config.put(key.getName(), value); 
    }
    
    Map<String, Object> persistableFlags = MutableMap.<String, Object>builder()
            .putAll(FlagUtils.getFieldsWithFlagsExcludingModifiers(enricher, Modifier.STATIC ^ Modifier.TRANSIENT))
            .remove("id")
            .remove("name")
            .build();
    builder.config.putAll(persistableFlags);

    return builder.build();
}
 
Example #3
Source File: BasicEntityRebindSupport.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@Override
public void addEnrichers(RebindContext rebindContext, EntityMemento memento) {
    for (String enricherId : memento.getEnrichers()) {
        AbstractEnricher enricher = (AbstractEnricher) rebindContext.lookup().lookupEnricher(enricherId);
        if (enricher != null) {
            try {
                entity.enrichers().add(enricher);
            } catch (Exception e) {
                rebindContext.getExceptionHandler().onAddEnricherFailed(entity, enricher, e);
            }
        } else {
            LOG.warn("Enricher not found; discarding enricher {} of entity {}({})",
                    new Object[] {enricherId, memento.getType(), memento.getId()});
        }
    }
}
 
Example #4
Source File: Enrichers.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected EnricherSpec<? extends Enricher> build() {
    EnricherSpec<? extends Enricher> spec = EnricherSpec.create(enricherType);
    
    String uniqueTag2 = uniqueTag;
    if (uniqueTag2==null)
        uniqueTag2 = getDefaultUniqueTag();
    if (uniqueTag2!=null)
        spec.uniqueTag(uniqueTag2);
    
    if (!tags.isEmpty()) spec.tags(tags);
    if (suppressDuplicates!=null)
        spec.configure(AbstractEnricher.SUPPRESS_DUPLICATES, suppressDuplicates);
    
    return spec;
}
 
Example #5
Source File: AbstractEntity.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public boolean remove(Enricher enricher) {
    ((AbstractEnricher)enricher).destroy();
    boolean changed = enrichersInternal.remove(enricher);
    
    if (changed) {
        getManagementSupport().getEntityChangeListener().onEnricherRemoved(enricher);
    }
    return changed;

}
 
Example #6
Source File: AbstractEntity.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public boolean removeAll() {
    boolean changed = false;
    for (AbstractEnricher enricher : enrichersInternal) {
        changed = remove(enricher) || changed;
    }
    return changed;
}
 
Example #7
Source File: RebindIteration.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new enricher, passing to its constructor the enricher id and all of memento.getConfig().
 */
protected Enricher newEnricher(EnricherMemento memento) {
    String id = memento.getId();
    LoadedClass<? extends Enricher> loaded = load(Enricher.class, memento);
    Class<? extends Enricher> enricherClazz = loaded.clazz;

    Enricher enricher;
    if (InternalFactory.isNewStyle(enricherClazz)) {
        InternalPolicyFactory policyFactory = managementContext.getPolicyFactory();
        enricher = policyFactory.constructEnricher(enricherClazz);
        FlagUtils.setFieldsFromFlags(ImmutableMap.of("id", id), enricher);
        ((AbstractEnricher)enricher).setManagementContext(managementContext);

    } else {
        LOG.warn("Deprecated rebind of enricher without no-arg constructor; " +
            "this may not be supported in future versions: id=" + id + "; type="+enricherClazz);

        // There are several possibilities for the constructor; find one that works.
        // Prefer passing in the flags because required for Application to set the management context
        // TODO Feels very hacky!
        Map<String, Object> flags = MutableMap.<String, Object>of(
            "id", id, 
            "deferConstructionChecks", true,
            "noConstructionInit", true);
        flags.putAll(memento.getConfig());

        enricher = invokeConstructor(reflections, enricherClazz, new Object[] {flags});
    }
    
    setCatalogItemIds(enricher, loaded.catalogItemId, loaded.searchPath);
    return enricher;
}
 
Example #8
Source File: EnricherTypeTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetConfig() throws Exception {
    EnricherType enricherType = enricher.getEnricherType();
    assertEquals(enricherType.getConfigKeys(), ImmutableSet.of(MyEnricher.CONF1, MyEnricher.CONF2, AbstractEnricher.SUPPRESS_DUPLICATES));
    assertEquals(enricherType.getName(), MyEnricher.class.getCanonicalName());
    assertEquals(enricherType.getConfigKey("test.conf1"), MyEnricher.CONF1);
    assertEquals(enricherType.getConfigKey("test.conf2"), MyEnricher.CONF2);
}
 
Example #9
Source File: InternalPolicyFactory.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T extends Enricher> T createEnricher(EnricherSpec<T> spec) {
    if (spec.getFlags().containsKey("parent")) {
        throw new IllegalArgumentException("Spec's flags must not contain parent; use spec.parent() instead for "+spec);
    }
    
    try {
        Class<? extends T> clazz = spec.getType();
        
        T enricher = construct(clazz, spec, null);

        final AbstractEnricher theEnricher = (AbstractEnricher) enricher;
        if (spec.getDisplayName()!=null)
            theEnricher.setDisplayName(spec.getDisplayName());
        
        if (spec.getCatalogItemId()!=null) {
            theEnricher.setCatalogItemIdAndSearchPath(spec.getCatalogItemId(), spec.getCatalogItemIdSearchPath());
        }
        
        enricher.tags().addTags(spec.getTags());
        
        if (isNewStyle(clazz)) {
            theEnricher.setManagementContext(managementContext);
            Map<String, Object> config = ConfigBag.newInstance().putAll(spec.getFlags()).putAll(spec.getConfig()).getAllConfig();
            theEnricher.configure(MutableMap.copyOf(config)); // TODO AbstractEnricher.configure modifies the map
        }
        
        // TODO Can we avoid this for "new-style policies"? Should we just trust the configure() method, 
        // which the user may have overridden? 
        // Also see InternalLocationFactory for same issue, which this code is based on.
        for (Map.Entry<ConfigKey<?>, Object> entry : spec.getConfig().entrySet()) {
            enricher.config().set((ConfigKey)entry.getKey(), entry.getValue());
        }
        theEnricher.init();
        
        return enricher;
        
    } catch (Exception e) {
        throw Exceptions.propagate(e);
    }
}
 
Example #10
Source File: BasicEnricherRebindSupport.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public BasicEnricherRebindSupport(AbstractEnricher enricher) {
    super(enricher);
    this.enricher = enricher;
}