brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Adds EntityInitializers for http ...
Date Thu, 17 Jul 2014 09:45:56 GMT
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/48#discussion_r15048602
  
    --- Diff: software/base/src/main/java/brooklyn/entity/software/http/HttpRequestSensor.java
---
    @@ -0,0 +1,60 @@
    +package brooklyn.entity.software.http;
    +
    +import brooklyn.config.ConfigKey;
    +import brooklyn.entity.basic.ConfigKeys;
    +import brooklyn.entity.basic.EntityLocal;
    +import brooklyn.entity.effector.AddSensor;
    +import brooklyn.event.AttributeSensor;
    +import brooklyn.event.feed.http.HttpFeed;
    +import brooklyn.event.feed.http.HttpPollConfig;
    +import brooklyn.event.feed.http.HttpValueFunctions;
    +import brooklyn.util.config.ConfigBag;
    +import brooklyn.util.time.Duration;
    +import com.google.common.base.Functions;
    +import com.google.common.base.Preconditions;
    +import org.slf4j.LoggerFactory;
    +
    +import java.util.Map;
    +
    +/**
    + * Configurable {@link brooklyn.entity.proxying.EntityInitializer} which adds an HTTP
sensor feed to retrieve the
    + * <code>JSONObject</code> from a JSON response in order to populate the
sensor with the indicated <code>name</code>.
    + */
    +public final class HttpRequestSensor<T> extends AddSensor<T, AttributeSensor<T>>
{
    +
    +    private static final org.slf4j.Logger log = LoggerFactory.getLogger(HttpRequestSensor.class);
    +
    +    public static final ConfigKey<String> JSON_PATH = ConfigKeys.newStringConfigKey("jsonPath");
    +    public static final ConfigKey<String> SENSOR_URI = ConfigKeys.newStringConfigKey("uri");
    +
    +    private String jsonPath;
    +    private String uri;
    +
    +    public HttpRequestSensor(ConfigBag params) {
    +        super(AddSensor.<T>newSensor(params));
    +        jsonPath = Preconditions.checkNotNull(params.get(JSON_PATH), JSON_PATH);
    +        uri = Preconditions.checkNotNull(params.get(SENSOR_URI), SENSOR_URI);
    +    }
    +
    +    @Override
    +    public void apply(final EntityLocal entity) {
    +        super.apply(entity);
    +
    +        Duration period = Duration.ONE_SECOND;
    +
    +        HttpPollConfig<T> pollConfig = new HttpPollConfig<T>(sensor)
    +                .checkSuccess(HttpValueFunctions.responseCodeEquals(200))
    +                .onFailureOrException(Functions.constant((T) null))
    +                .onSuccess(HttpValueFunctions.<T>jsonContentsFromPath(jsonPath))
    +                .period(period);
    +
    +        HttpFeed.builder().entity(entity)
    +                .baseUri(uri)
    +                .poll(pollConfig)
    +                .build();
    +    }
    +
    +    public HttpRequestSensor(Map<String, String> params) {
    --- End diff --
    
    Minor preference for constructors to all go at the top together; but don't worry about
it.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message