package org.apache.mesos.logstash.config;

import org.apache.mesos.logstash.common.network.NetworkUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.inject.Inject;

// TODO: Add unit test to assert default settings, URIs and commands

@Component
@ConfigurationProperties
public class FrameworkConfig {

    private static final String LOGSTASH_EXECUTOR_JAR   = "logstash-mesos-executor.jar";

    private static final String LOGSTASH_TARBALL = "logstash.tar.gz";

    private String javaHome = "";

    @Inject
    private NetworkUtils networkUtils;

    public String getJavaHome() {
        if (!javaHome.isEmpty()) {
            return javaHome.replaceAll("java$", "").replaceAll("/$", "") + "/";
        } else {
            return "";
        }
    }

    public void setJavaHome(String javaHome) {
        this.javaHome = javaHome;
    }

    public String getFrameworkFileServerAddress() {
        return networkUtils.addressToString(networkUtils.hostSocket(8080), true);
    }

    public String getLogstashTarballUri() {
        return getFrameworkFileServerAddress() + "/" + FrameworkConfig.LOGSTASH_TARBALL;
    }

    public String getLogstashExecutorUri() {
        return getFrameworkFileServerAddress() + "/" + FrameworkConfig.LOGSTASH_EXECUTOR_JAR;
    }

    public String getExecutorCommand() {
        return getJavaHome() + "java $JAVA_OPTS -jar ./" + FrameworkConfig.LOGSTASH_EXECUTOR_JAR;
    }

}

@@ -130,54 +130,264 @@
 rk.b
-oot.context.properties.ConfigurationProperties
+eans.factory.annotation.Autowired;%0Aimport org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;%0Aimport org.springframework.boot.context.properties.ConfigurationProperties;%0Aimport org.springframework.context.ApplicationListener
 ;%0Aim
@@ -601,12 +601,84 @@
 nfig
+ implements ApplicationListener%3CEmbeddedServletContainerInitializedEvent%3E
  %7B%0A
-%0A
     
@@ -921,16 +921,207 @@
 Utils;%0A%0A
+    int httpPort;%0A%0A    @Override%0A    public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) %7B%0A        httpPort = event.getEmbeddedServletContainer().getPort();%0A    %7D%0A%0A
     publ
@@ -1530,12 +1530,16 @@
 ket(
-8080
+httpPort
 ), t

Use server port when downloading from scheduler