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

com.makitoo.Makitoggle Maven / Gradle / Ivy

The newest version!
package com.makitoo;

import com.makitoo.internal.*;

import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by nicolas on 23/01/17.
 */
public class Makitoggle {

    private static final Logger LOGGER = Logger.getLogger(Makitoggle.class.getName());

    private final String appId;
    private String environment;
    private final Properties properties;
    private ConfigurationProvider configurationProvider;
    private EventBuilder eventBuilder;
    private EventSender eventSender;

    public Makitoggle(Properties properties, String appId) {
        this.properties = properties;
        this.appId = appId;
    }

    public Makitoggle(Properties properties, String appId, String environment) {
        this.properties = properties;
        this.appId = appId;
        this.environment = environment;
    }

    public Makitoggle(String appId) {
        properties = new Properties();
        this.appId = appId;
    }

    EventBuilder getEventBuilder() {
        if (eventBuilder == null) {
            synchronized (this) {
                if (eventBuilder == null) {
                    eventBuilder = new EventBuilder();
                }
            }
        }
        return eventBuilder;
    }

    EventSender getEventSender() {
        if (eventSender == null) {
            synchronized (this) {
                if (eventSender == null) {
                    eventSender = new EventSender(properties, appId, environment);
                }
            }
        }
        return eventSender;
    }

    ConfigurationProvider getConfigurationProvider() {
        if (configurationProvider == null) {
            synchronized (this) {
                if (configurationProvider == null) {
                    configurationProvider = new CacheConfigurationProvider(properties, new HttpConfigurationProvider(getEventBuilder(), getEventSender()));
                }
            }
        }
        return configurationProvider;
    }

    public boolean isActive(Feature feature, User user) {
        Boolean result = null;
        try {
            result = getConfigurationProvider().getUserConfiguration(user).isActive(feature.getName());
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            LOGGER.log(Level.FINE, e.getMessage(), e);
        }
        if (result == null) {
            result = feature.getDefaultValue();
        }

        getEventSender().sendAsyncEvent(Boolean.TRUE.equals(result) ?
                getEventBuilder().createSeenEvent(feature, user) :
                getEventBuilder().createSkipEvent(feature, user)
        );

        return result;
    }

    public void success(Feature feature, User user) {
        getEventSender().sendAsyncEvent(
                getEventBuilder().createSuccessEvent(feature, user));
    }

    public void track(Event event, User user) {
        getEventSender().sendAsyncEvent(
                getEventBuilder().createTrackEvent(event, user));
    }

    public void waitTermination(int timeout, TimeUnit timeUnit) throws InterruptedException {
        getEventSender().shutdown(timeout, timeUnit);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy