package eu.socialsensor.sfc.streams;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;


import eu.socialsensor.framework.streams.StreamException;
import eu.socialsensor.sfc.builder.InputConfiguration;
import eu.socialsensor.sfc.streams.management.StreamsManager;

/**
 * Main class for the execution of StreamManager
 * defining the appropriate configuration file.
 * 
 * @author manosetro
 * @email  manosetro@iti.gr
 *
 */
public class StreamCollector {

	public static void main(String[] args) {
	
		Logger logger = Logger.getLogger(StreamCollector.class);
		
		File streamConfigFile;
		File inputConfigFile;
		
		if(args.length != 2 ) {
			streamConfigFile = new File("./conf/streams.conf.xml");
			inputConfigFile = new File("./conf/input.conf.xml");
		}
		else {
			streamConfigFile = new File(args[0]);
			inputConfigFile = new File(args[1]);
		}
		
		try {
			StreamsManagerConfiguration config = StreamsManagerConfiguration.readFromFile(streamConfigFile);		
			InputConfiguration input_config = InputConfiguration.readFromFile(inputConfigFile);		
	        
			StreamsManager manager = new StreamsManager(config,input_config);
			manager.open();
			
			waiting();
			
			//close manager
			logger.info("Close Stream manager.");
			manager.close();
			
		} catch (ParserConfigurationException e) {
			logger.error(e.getMessage());
		} catch (SAXException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		} catch (StreamException e) {
			logger.error(e.getMessage());
		}
		
	}
	
	private static void waiting() {
		try {
			InetAddress inet = InetAddress.getByName(null);
			ServerSocket shutdownSocket = new ServerSocket(11111, 0, inet);
			shutdownSocket.accept();
			
			shutdownSocket.close();
		} catch (IOException e1) {
			System.out.println("Problems connecting to shutdown port");
		}
	}
}

@@ -228,16 +228,62 @@
 Logger;%0A
+import org.apache.log4j.PropertyConfigurator;%0A
 import o
@@ -308,17 +308,16 @@
 ption;%0A%0A
-%0A
 import e
@@ -677,16 +677,20 @@
 ctor %7B%0A%0A
+%09%0A%09%0A
 %09public 
@@ -724,16 +724,118 @@
 rgs) %7B%0A%09
+%09%0A%09%09if(new File(%22log4j.properties%22).exists()) %0A%09%09%09PropertyConfigurator.configure(%22log4j.properties%22);%0A
 %0A%09%09Logge
@@ -916,16 +916,9 @@
 File
-;%0A%09%09File
+,
  inp
@@ -931,19 +931,16 @@
 igFile;%0A
-%09%09%0A
 %09%09if(arg
@@ -1173,16 +1173,49 @@
 %0A%09%09%7D%0A%09%09%0A
+%09%09StreamsManager manager = null;%0A
 %09%09try %7B%0A
@@ -1418,31 +1418,16 @@
     %0A%09%09%09
-StreamsManager 
 manager 
@@ -1454,16 +1454,17 @@
 (config,
+ 
 input_co
@@ -1517,120 +1517,133 @@
 %09%0A%09%09
-%09//close manager%0A%09%09%09logger.info(%22Close Stream manager.%22);%0A%09%09%09manager.close();%0A%09%09%09%0A%09%09%7D catch (ParserConfiguration
+%7D catch (ParserConfigurationException e) %7B%0A%09%09%09logger.error(e);%0A%09%09%7D catch (SAXException e) %7B%0A%09%09%09logger.error(e);%0A%09%09%7D catch (IO
 Exce
@@ -1666,37 +1666,24 @@
 gger.error(e
-.getMessage()
 );%0A%09%09%7D catch
@@ -1677,34 +1677,37 @@
 e);%0A%09%09%7D catch (S
-AX
+tream
 Exception e) %7B%0A%09
@@ -1718,37 +1718,24 @@
 gger.error(e
-.getMessage()
 );%0A%09%09%7D catch
@@ -1728,34 +1728,32 @@
 (e);%0A%09%09%7D catch (
-IO
 Exception e) %7B%0A%09
@@ -1768,32 +1768,152 @@
 .error(e
-.getMessage()
+);%0A%09%09%7D%0A%09%09finally %7B%0A%09%09%09//close manager%0A%09%09%09logger.info(%22Close Stream manager.%22);%0A%09%09%09if(manager != null)%0A%09%09%09%09try %7B%0A%09%09%09%09%09manager.close(
 );%0A
+%09%09
 %09%09%7D catc
@@ -1931,32 +1931,34 @@
 ception e) %7B%0A%09%09%09
+%09%09
 logger.error(e.g
@@ -1959,23 +1959,16 @@
 or(e
-.getMessage());
+);%0A%09%09%09%09%7D
 %0A%09%09%7D
@@ -2006,16 +2006,33 @@
 aiting()
+ throws Exception
  %7B%0A%09%09try
@@ -2239,79 +2239,67 @@
 on e
-1
 ) %7B%0A%09%09%09
-System.out.println(%22Problems connecting to shutdown port%22
+e.printStackTrace();%0A%09%09%09throw new Exception(e
 );%0A%09%09%7D
+ 
 %0A%09%7D%0A

Fix waiting() method