All Downloads are FREE. Search and download functionalities are using the official Maven repository.

brooklyn.entity.basic.AbstractEffector Maven / Gradle / Ivy

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.entity.basic;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import brooklyn.entity.Effector;
import brooklyn.entity.Entity;
import brooklyn.entity.ParameterType;
import brooklyn.entity.effector.EffectorBase;
import brooklyn.entity.effector.EffectorWithBody;
import brooklyn.entity.effector.EffectorTasks.EffectorTaskFactory;
import brooklyn.management.Task;
import brooklyn.management.internal.EffectorUtils;
import brooklyn.util.config.ConfigBag;
import brooklyn.util.task.BasicTask;

import com.google.common.collect.ImmutableMap;

/**
 * The abstract {@link Effector} implementation.
 * 
 * The concrete subclass (often anonymous) will supply the {@link #call(Entity, Map)} implementation,
 * and the fields in the constructor.
 */
public abstract class AbstractEffector extends EffectorBase implements EffectorWithBody {

    private static final long serialVersionUID = 1832435915652457843L;
    /** @deprecated since 0.6.0 shouldn't be public */
    public static final Logger LOG = LoggerFactory.getLogger(AbstractEffector.class);

    public AbstractEffector(String name, Class returnType, List> parameters, String description) {
        super(name, returnType, parameters, description);
    }

    public abstract T call(Entity entity, @SuppressWarnings("rawtypes") Map parameters);

    /** Convenience for named-parameter syntax (needs map in first argument) */
    public T call(Entity entity) { return call(ImmutableMap.of(), entity); }

    /** Convenience for named-parameter syntax (needs map in first argument) */
    public T call(@SuppressWarnings("rawtypes") Map parameters, Entity entity) { return call(entity, parameters); }

    /** subclasses may override to add additional flags, but they should include the flags returned here 
     * unless there is very good reason not to */
    protected Map getFlagsForTaskInvocationAt(Entity entity) {
        return EffectorUtils.getTaskFlagsForEffectorInvocation(entity, this);
    }
    
    /** not meant for overriding; subclasses should override the abstract {@link #call(Entity, Map)} method in this class */
    @Override
    public final EffectorTaskFactory getBody() {
        return new EffectorTaskFactory() {
            @Override
            public Task newTask(final Entity entity, final Effector effector, final ConfigBag parameters) {
                return new BasicTask(
                        getFlagsForTaskInvocationAt(entity),
                new Callable() {
                    @Override public T call() {
                        return AbstractEffector.this.call(parameters.getAllConfig(), entity);
                    }
                });
            }
        };
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy