package com.github.dreambrother.hackernews;

import com.github.dreambrother.hackernews.http.PingServer;
import com.github.dreambrother.hackernews.wd.WatchdogPingHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HackernewsVkApp {

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

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

        HackernewsVkApp app = new HackernewsVkApp();
        app.startWdListener();
        app.registerShutdownHook();
        app.startApplication();
    }

    private void startApplication() {
        // TODO: stub implementation
        while (true) {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

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

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

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

@@ -84,146 +84,563 @@
 ews.
-http.PingServer;%0Aimport com.github.dreambrother.hackernews.wd.WatchdogPingHandler;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
+client.HackernewsClientImpl;%0Aimport com.github.dreambrother.hackernews.client.VkClientImpl;%0Aimport com.github.dreambrother.hackernews.dao.PublishedItemsDaoImpl;%0Aimport com.github.dreambrother.hackernews.http.PingServer;%0Aimport com.github.dreambrother.hackernews.wd.WatchdogPingHandler;%0Aimport com.github.dreambrother.hackernews.worker.Publicator;%0Aimport com.github.dreambrother.hackernews.worker.ScheduledPublicator;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.util.Properties
 ;%0A%0Ap
@@ -799,16 +799,44 @@
 %5B%5D args)
+ throws InterruptedException
  %7B%0A     
@@ -960,351 +960,1024 @@
     
-app.startWdListener();%0A        app.registerShutdownHook();%0A        app.startApplication(
+Properties properties = app.getProperties();%0A%0A        VkClientImpl vkClient = new VkClientImpl();%0A        vkClient.setCommunityId(properties.getProperty(%22vk.communityId%22));%0A        vkClient.setToken(properties.getProperty(%22vk.token%22)
 );%0A
+%0A
     
-%7D%0A%0A
     
-private void startApplication() %7B%0A        // TODO: stub implementation%0A        while (true) %7B%0A            try %7B%0A                Thread.sleep(1000L);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace(
+HackernewsClientImpl hackernewsClient = new HackernewsClientImpl();%0A%0A        PublishedItemsDaoImpl publishedItemsDao = new PublishedItemsDaoImpl();%0A        publishedItemsDao.setDbFile(new File(properties.getProperty(%22db.file%22)));%0A%0A        Publicator publicator = new Publicator();%0A        publicator.setVkClient(vkClient);%0A        publicator.setPublishedItemsDao(publishedItemsDao);%0A        publicator.setHackernewsClient(hackernewsClient);%0A        publicator.setPostingDelayMillis(Integer.parseInt(%22publicator.delayBetweenPostingMillis%22));%0A%0A        ScheduledPublicator scheduledPublicator = new ScheduledPublicator();%0A%0A        app.registerShutdownHook(scheduledPublicator);%0A        app.startWdListener(Integer.parseInt(properties.getProperty(%22wd.ping.port%22))
 );%0A
+%0A
         
     
@@ -1972,31 +1972,173 @@
         
-    %7D%0A        %7D
+scheduledPublicator.start(publicator, Integer.parseInt(properties.getProperty(%22publicator.schedulerDelayMillis%22)));%0A%0A        log.info(%22Application started%22);
 %0A    %7D%0A%0A
@@ -2170,16 +2170,24 @@
 istener(
+int port
 ) %7B%0A    
@@ -2309,13 +2309,12 @@
 ort(
-10000
+port
 );%0A%0A
@@ -2423,16 +2423,61 @@
 ownHook(
+final ScheduledPublicator scheduledPublicator
 ) %7B%0A    
@@ -2656,22 +2656,676 @@
     
-%7D%0A        %7D));
+    scheduledPublicator.shutdown();%0A            %7D%0A        %7D));%0A    %7D%0A%0A    public Properties getProperties() %7B%0A        try %7B%0A            Properties properties = new Properties();%0A            if (System.getProperty(%22os.name%22).equals(%22Ubuntu%22)) %7B%0A                log.info(%22Start with PROD properties%22);%0A                properties.load(new FileInputStream(%22/etc/app.properties%22));%0A            %7D else %7B%0A                log.info(%22Start with DEV properties%22);%0A                properties.load(getClass().getResourceAsStream(%22/app.properties%22));%0A            %7D%0A            return properties;%0A        %7D catch (Exception ex) %7B%0A            throw new RuntimeException(ex);%0A        %7D
 %0A   

add main