package launcher;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;

import launcher.beans.ComponentBean;

public class Downloader {

	private Logging logging;
	private List<ComponentBean> remoteConfigs;
	
	public Downloader(Logging logging, List<ComponentBean> remoteConfigs) {
		super();
		this.logging = logging;
		this.remoteConfigs = remoteConfigs;
	}

	public void run() {
		logging.logEmptyLine();
		logging.logDebug("BEGIN DOWNLOAD");
		for (ComponentBean cfg : remoteConfigs) {
			logging.logDebug("CHECK DOWNLOAD '" + cfg.getName() + "'");
			File sourceComparisonFile = cfg.getCompare() != null ? cfg
					.getCompare() : cfg.getTarget();
			logging.logDebug("  COMPARE= '"
					+ sourceComparisonFile.getAbsolutePath() + "'");
			if (cfg.getSource().lastModified() > sourceComparisonFile
					.lastModified())
				download(cfg);
			else {
				logging.logDebug("  SKIP ALREADY UPDATE");
				logging.logDebug("  '" + cfg.getSource().getAbsolutePath() + "'" + " -> "
						+ "'" + cfg.getTarget().getAbsolutePath() + "'");
			}
		}
		logging.logDebug("DONE!");
	}

	/**
	 * Copy files using
	 * {@link Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)}
	 * 
	 * @param cfg
	 */
	private void download(ComponentBean cfg) {
		try {
			logging.logInfo("  DOWNLOADING ...");
			logging.logInfo("  '" + cfg.getSource().getAbsolutePath() + "'" + " -> " + "'"
					+ cfg.getTarget().getAbsolutePath() + "'");
			cfg.getTarget().mkdirs();
			Files.copy(cfg.getSource().toPath(), cfg.getTarget().toPath(),
					StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException e) {
			logging.printException(e);
		}
	}
}

@@ -274,29 +274,29 @@
 ntBean%3E 
-remoteConfigs
+componentList
 ;%0A%09%0A%09pub
@@ -347,29 +347,29 @@
 ntBean%3E 
-remoteConfigs
+componentList
 ) %7B%0A%09%09su
@@ -412,63 +412,182 @@
 his.
-remoteConfigs = remoteConfigs;%0A%09%7D%0A%0A%09public void run() %7B
+componentList = componentList;%0A%09%7D%0A%0A%09public void run() %7B%0A%09%09if (componentList == null %7C%7C componentList.isEmpty()) %7B%0A%09%09%09logging.logDebug(%22componentList == null%22);%0A%09%09%09return;%0A%09%09%7D
 %0A%09%09l
@@ -678,21 +678,21 @@
 g : 
-remoteConfigs
+componentList
 ) %7B%0A
@@ -1413,18 +1413,24 @@
 @param c
-fg
+omponent
 %0A%09 */%0A%09p
@@ -1465,18 +1465,24 @@
 ntBean c
-fg
+omponent
 ) %7B%0A%09%09tr
@@ -1519,16 +1519,26 @@
 LOADING 
+COMPONENT 
 ...%22);%0A%09
@@ -1556,34 +1556,40 @@
 ogInfo(%22  '%22 + c
-fg
+omponent
 .getSource().get
@@ -1632,18 +1632,24 @@
 %09%09%09%09%09+ c
-fg
+omponent
 .getTarg
@@ -1683,18 +1683,24 @@
 %22);%0A%09%09%09c
-fg
+omponent
 .getTarg
@@ -1729,18 +1729,24 @@
 s.copy(c
-fg
+omponent
 .getSour
@@ -1761,18 +1761,24 @@
 ath(), c
-fg
+omponent
 .getTarg

Exit downloader if invalid components passed