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

io.mstream.trader.commons.test.zookeeper.ZookeeperBootstrap Maven / Gradle / Ivy

There is a newer version: 1.14
Show newest version
package io.mstream.trader.commons.test.zookeeper;

import io.mstream.trader.commons.config.Application;
import io.mstream.trader.commons.config.ApplicationName;
import io.mstream.trader.commons.config.model.Version;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.data.Stat;

import javax.inject.Inject;
import java.io.InputStream;

import static com.google.common.io.ByteStreams.toByteArray;
import static java.lang.String.format;


public class ZookeeperBootstrap {

    private final CuratorFramework curator;
    private final Version version;
    private final String appName;

    @Inject
    public ZookeeperBootstrap(
            CuratorFramework curator,
            @Application Version version,
            @ApplicationName String appName
    ) {
        this.curator = curator;
        this.version = version;
        this.appName = appName;
        init();
    }

    private void init() {
        try {
            InputStream configInputStream =
                    getClass().getResourceAsStream("/config.json");

            if (configInputStream == null) {
                throw new IllegalStateException("no config.json file found in the classpath");
            }

            byte[] configBytes = toByteArray(configInputStream);

            String path = format(
                    "/config/%s/%s",
                    appName,
                    version.getValue()
            );

            Stat stat = curator
                    .checkExists()
                    .forPath(path);

            if (stat == null) {
                curator
                        .create()
                        .creatingParentsIfNeeded()
                        .forPath(
                                path,
                                configBytes
                        );
            } else {
                curator
                        .setData()
                        .forPath(
                                path,
                                configBytes
                        );
            }
        } catch (Exception e) {
            throw new RuntimeException("can't create a config", e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy