/*
 * Copyright 2017 flow.ci
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flow.platform.api.config;

import com.flow.platform.api.domain.CmdQueueItem;
import com.flow.platform.api.util.PlatformURL;
import com.flow.platform.core.config.AppConfigBase;
import com.flow.platform.core.config.DatabaseConfig;
import com.flow.platform.core.sysinfo.PropertySystemInfo;
import com.flow.platform.core.sysinfo.SystemInfo;
import com.flow.platform.core.sysinfo.SystemInfo.Status;
import com.flow.platform.core.util.ThreadUtil;
import com.flow.platform.util.DateUtil;
import com.flow.platform.util.Logger;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.context.support.StandardServletEnvironment;

/**
 * @author yang
 */
@Configuration
@Import({DatabaseConfig.class})
public class AppConfig extends AppConfigBase {

    public final static String NAME = "flow-api";

    public final static String VERSION = "alpha-0.1";

    public final static String DEFAULT_YML_FILE = ".flow.yml";

    public final static Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    private final static Logger LOGGER = new Logger(AppConfig.class);

    private final static int ASYNC_POOL_SIZE = 50;

    private final static String THREAD_NAME_PREFIX = "async-task-";

    public final static String ALLOW_SUFFIX = "p12,mobileprovision,jks,pem";

    public final static long ALLOW_SIZE = 2 * 1024 * 1024;


    @Value("${api.workspace}")
    private String workspace;

    @Value("${platform.url}")
    private String platFormBaseURL;

    @Bean
    public Path workspace() {
        try {
            Path dir = Files.createDirectories(Paths.get(workspace));
            LOGGER.trace("flow.ci working dir been created : %s", dir);
            return dir;
        } catch (IOException e) {
            throw new RuntimeException("Fail to create flow.ci api working dir", e);
        }
    }

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        return ThreadUtil.createTaskExecutor(ASYNC_POOL_SIZE, ASYNC_POOL_SIZE / 10, 100, THREAD_NAME_PREFIX);
    }

    @Bean
    public BlockingQueue<CmdQueueItem> cmdBaseBlockingQueue() {
        return new LinkedBlockingQueue<>(50);
    }

    @Override
    protected String getName() {
        return NAME;
    }

    @Override
    protected String getVersion() {
        return VERSION;
    }

    @Bean
    public PlatformURL platformURL(){
        return new PlatformURL(platFormBaseURL);
    }
}

@@ -859,224 +859,19 @@
 ore.
-sysinfo.PropertySystemInfo;%0Aimport com.flow.platform.core.sysinfo.SystemInfo;%0Aimport com.flow.platform.core.sysinfo.SystemInfo.Status;%0Aimport com.flow.platform.core.util.ThreadUtil;%0Aimport com.flow.platform.util.Date
+util.Thread
 Util
@@ -1058,30 +1058,8 @@
 hs;%0A
-import java.util.Map;%0A
 impo
@@ -1150,71 +1150,8 @@
 ue;%0A
-import org.springframework.beans.factory.annotation.Autowired;%0A
 impo
@@ -1403,295 +1403,52 @@
 ork.
-core.env.Environment;%0Aimport org.springframework.core.env.PropertySource;%0Aimport org.springframework.core.io.support.ResourcePropertySource;%0Aimport org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;%0Aimport org.springframework.web.context.support.StandardServletEnvironment
+scheduling.concurrent.ThreadPoolTaskExecutor
 ;%0A%0A/
@@ -3135,16 +3135,17 @@
 ormURL()
+ 
 %7B%0A      

delete no use code