package org.squirrelframework.cloud;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.squirrelframework.cloud.conf.JsonFlattenConverter;
import org.squirrelframework.cloud.utils.CloudConfigCommon;

import static org.squirrelframework.cloud.utils.CloudConfigCommon.ZK_CONNECT_STRING_KEY;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    private Environment environment;

    @Bean
    public CuratorFramework zkClient() {
        String connectionString = resolveConnectionString();
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(connectionString)
                .retryPolicy(retryPolicy)
                .canBeReadOnly(false)
                .build();
        client.start();
        return client;
    }

    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }

    @Bean
    public JsonFlattenConverter jsonFlattenConverter() {
        JsonFlattenConverter jsonFlattenConverter = new JsonFlattenConverter();
        jsonFlattenConverter.setAllowOverride(false);
        return jsonFlattenConverter;
    }

    private String resolveConnectionString() {
        if(environment!=null && environment.containsProperty(ZK_CONNECT_STRING_KEY)) {
            return environment.getProperty(ZK_CONNECT_STRING_KEY);
        }
        return "127.0.0.1:2181";
    }
}

@@ -705,68 +705,8 @@
 ter;
-%0Aimport org.squirrelframework.cloud.utils.CloudConfigCommon;
 %0A%0Aim
@@ -885,24 +885,56 @@
 g%5B%5D args) %7B%0A
+        processArguments(args);%0A
         Spri
@@ -980,24 +980,920 @@
 gs);%0A    %7D%0A%0A
+    private static void processArguments(String%5B%5D args) %7B%0A        for (String arg : args) %7B%0A            if (arg.startsWith(%22--%22)) %7B%0A                String optionText = arg.substring(2, arg.length());%0A                String optionName;%0A                String optionValue = null;%0A                if (optionText.contains(%22=%22)) %7B%0A                    optionName = optionText.substring(0, optionText.indexOf(%22=%22));%0A                    optionValue = optionText.substring(optionText.indexOf(%22=%22)+1, optionText.length());%0A                %7D else %7B%0A                    optionName = optionText;%0A                %7D%0A                if (optionName.isEmpty() %7C%7C (optionValue != null && optionValue.isEmpty())) %7B%0A                    throw new IllegalArgumentException(%22Invalid argument syntax: %22 + arg);%0A                %7D%0A                System.setProperty(optionName, optionValue);%0A            %7D%0A        %7D%0A    %7D%0A%0A
     @Autowir

enable application argument settings