package com.github.dreambrother.hackernews;

import com.github.dreambrother.hackernews.client.HackernewsClientImpl;
import com.github.dreambrother.hackernews.client.VkClientImpl;
import com.github.dreambrother.hackernews.dao.PublishedItemsDaoImpl;
import com.github.dreambrother.hackernews.http.PingServer;
import com.github.dreambrother.hackernews.wd.WatchdogPingHandler;
import com.github.dreambrother.hackernews.worker.Publicator;
import com.github.dreambrother.hackernews.worker.ScheduledPublicator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class HackernewsVkApp {

    private static final Logger log = LoggerFactory.getLogger(HackernewsVkApp.class);

    public static void main(String[] args) throws InterruptedException {
        log.info("Start hackernews-vk publicator application");

        HackernewsVkApp app = new HackernewsVkApp();
        Properties properties = app.getProperties();

        VkClientImpl vkClient = new VkClientImpl();
        vkClient.setCommunityId(properties.getProperty("vk.communityId"));
        vkClient.setToken(properties.getProperty("vk.token"));

        HackernewsClientImpl hackernewsClient = new HackernewsClientImpl();

        PublishedItemsDaoImpl publishedItemsDao = new PublishedItemsDaoImpl();
        publishedItemsDao.setDbFile(new File(properties.getProperty("db.file")));

        Publicator publicator = new Publicator();
        publicator.setVkClient(vkClient);
        publicator.setPublishedItemsDao(publishedItemsDao);
        publicator.setHackernewsClient(hackernewsClient);
        publicator.setPostingDelayMillis(Integer.parseInt("publicator.delayBetweenPostingMillis"));

        ScheduledPublicator scheduledPublicator = new ScheduledPublicator();

        app.registerShutdownHook(scheduledPublicator);
        app.startWdListener(Integer.parseInt(properties.getProperty("wd.ping.port")));

        scheduledPublicator.start(publicator, Integer.parseInt(properties.getProperty("publicator.schedulerDelayMillis")));

        log.info("Application started");
    }

    private void startWdListener(int port) {
        PingServer server = new PingServer();
        server.setHandler(new WatchdogPingHandler());
        server.setPort(port);

        log.info("Start PingServer...");
        server.start();
    }

    private void registerShutdownHook(final ScheduledPublicator scheduledPublicator) {
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            public void run() {
                log.info("Just before shutdown the programm");
                scheduledPublicator.shutdown();
            }
        }));
    }

    public Properties getProperties() {
        try {
            Properties properties = new Properties();
            if (System.getProperty("os.name").equals("Ubuntu")) {
                log.info("Start with PROD properties");
                properties.load(new FileInputStream("/etc/app.properties"));
            } else {
                log.info("Start with DEV properties");
                properties.load(getClass().getResourceAsStream("/app.properties"));
            }
            return properties;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

@@ -296,24 +296,92 @@
 PingServer;%0A
+import com.github.dreambrother.hackernews.properties.AppProperties;%0A
 import com.g
@@ -647,69 +647,8 @@
 ile;
-%0Aimport java.io.FileInputStream;%0Aimport java.util.Properties;
 %0A%0Apu
@@ -955,32 +955,35 @@
 kApp();%0A        
+App
 Properties prope
@@ -1102,32 +1102,38 @@
 d(properties.get
+String
 Property(%22vk.com
@@ -1177,32 +1177,38 @@
 n(properties.get
+String
 Property(%22vk.tok
@@ -1422,32 +1422,38 @@
 e(properties.get
+String
 Property(%22db.fil
@@ -1711,32 +1711,41 @@
 yMillis(
-Integer.parseInt
+properties.getIntProperty
 (%22public
@@ -1941,33 +1941,16 @@
 istener(
-Integer.parseInt(
 properti
@@ -1947,32 +1947,35 @@
 r(properties.get
+Int
 Property(%22wd.pin
@@ -1983,17 +1983,16 @@
 .port%22))
-)
 ;%0A%0A     
@@ -2036,25 +2036,8 @@
 or, 
-Integer.parseInt(
 prop
@@ -2042,24 +2042,27 @@
 operties.get
+Int
 Property(%22pu
@@ -2093,17 +2093,16 @@
 illis%22))
-)
 ;%0A%0A     
@@ -2740,16 +2740,19 @@
  public 
+App
 Properti
@@ -2776,80 +2776,8 @@
 ) %7B%0A
-        try %7B%0A            Properties properties = new Properties();%0A    
     
@@ -2838,36 +2838,32 @@
 ) %7B%0A            
- 
-   
 log.info(%22Start 
@@ -2902,47 +2902,32 @@
     
-    properties.load(new FileInputStream
+return new AppProperties
 (%22/e
@@ -2937,33 +2937,32 @@
 app.properties%22)
-)
 ;%0A            %7D 
@@ -2955,20 +2955,16 @@
         
-  
-  
 %7D else %7B
@@ -2964,20 +2964,16 @@
  else %7B%0A
-    
         
@@ -3031,199 +3031,60 @@
     
-    properties.load(getClass().getResourceAsStream(%22/app.properties%22));%0A            %7D%0A            return properties;%0A        %7D catch (Exception ex) %7B%0A            throw new RuntimeException(ex
+return new AppProperties(%22classpath:/app.properties%22
 );%0A 

use custom Properties