<commit_before>/*
 * Project: ironvas
 * Package: main.java.de.fhhannover.inform.trust.ironvas
 * File:    Ironvas.java
 *
 * Copyright (C) 2011-2012 <NAME>
 * <NAME>adtweg 118, 30459 Hannover, Germany 
 *
 * 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 de.fhhannover.inform.trust.ironvas;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import javax.net.ssl.KeyManager;
import javax.net.ssl.TrustManager;

import de.fhhannover.inform.trust.ifmapj.IfmapJHelper;
import de.fhhannover.inform.trust.ifmapj.channel.SSRC;
import de.fhhannover.inform.trust.ifmapj.exception.InitializationException;
import de.fhhannover.inform.trust.ironvas.converter.Converter;
import de.fhhannover.inform.trust.ironvas.converter.FilterEventUpdateConverter;
import de.fhhannover.inform.trust.ironvas.converter.FilterParser;
import de.fhhannover.inform.trust.ironvas.ifmap.Keepalive;
import de.fhhannover.inform.trust.ironvas.ifmap.ThreadSafeSsrc;
import de.fhhannover.inform.trust.ironvas.omp.OmpConnection;
import de.fhhannover.inform.trust.ironvas.omp.VulnerabilityFetcher;
import de.fhhannover.inform.trust.ironvas.subscriber.Subscriber;

/**
 * Ironvas is an IF-MAP client which integrates OpenVAS into an IF-MAP
 * environment.
 * 
 * @author <NAME>
 *
 */
public class Ironvas {
	
	private static final Logger logger =
			Logger.getLogger(Ironvas.class.getName());
	private static final String LOGGING_CONFIG_FILE = "/logging.properties";
	
	public static void main(String[] args) {
		setupLogging();
		Configuration.init();
		
		// TODO command line parser
		// overwrite configuration with command line arguments

		
		// begin initialization ------------------------------------------------
		
		
		if (Configuration.publisherEnable().equals("false") &&
				Configuration.subscriberEnable().equals("false")) {
			logger.warning("nothing to do, shutting down ...");
		}
		else {
			ShutdownHook hook = new ShutdownHook();
			Runtime.getRuntime().addShutdownHook(new Thread(hook));
			ThreadInterruptionWatcher watcher = new ThreadInterruptionWatcher();

			SSRC ssrc = initIfmap();

			try {
				ssrc.newSession();
				ssrc.purgePublisher();
			} catch (Exception e) {
				System.err.println("could not connect to ifmap server: " + e);
				System.exit(1);
			}
			
			// TODO introduce Executor for thread handling
			Thread ssrcKeepaliveThread = new Thread(new Keepalive(ssrc,
							Integer.parseInt(Configuration.ifmapKeepalive())),
					"ssrc-keepalive-thread");
			
			hook.add(ssrcKeepaliveThread);
			watcher.add(ssrcKeepaliveThread);
			ssrcKeepaliveThread.start();
			
			
			
			if (Configuration.publisherEnable().equals("true")) {
				logger.info("activate publisher ...");
				runPublisher(ssrc, hook, watcher);
			}
			if (Configuration.subscriberEnable().equals("true")) {
				logger.info("activate subscriber ...");
				runSubscriber(ssrc, hook, watcher);
			}
			
			watcher.run(); // returns if one thread is not alive anymore
			hook.run();
			
			System.exit(1); // there is no way to exit with 0 at the moment
		}
	}
	
	/**
	 * Creates and starts the publisher part of ironvas based on the values
	 * in {@link Configuration}.<commit_msg>ironvas: add exit-switch to main thread<commit_after>/*
 * Project: ironvas
 * Package: main.java.de.fhhannover.inform.trust.ironvas
 * File:    Ironvas.java
 *
 * Copyright (C) 2011-2012 <NAME>
 * <NAME> 118, 30459 Hannover, Germany 
 *
 * 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 de.fhhannover.inform.trust.ironvas;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import javax.net.ssl.KeyManager;
import javax.net.ssl.TrustManager;

import de.fhhannover.inform.trust.ifmapj.IfmapJHelper;
import de.fhhannover.inform.trust.ifmapj.channel.SSRC;
import de.fhhannover.inform.trust.ifmapj.exception.InitializationException;
import de.fhhannover.inform.trust.ironvas.converter.Converter;
import de.fhhannover.inform.trust.ironvas.converter.FilterEventUpdateConverter;
import de.fhhannover.inform.trust.ironvas.converter.FilterParser;
import de.fhhannover.inform.trust.ironvas.ifmap.Keepalive;
import de.fhhannover.inform.trust.ironvas.ifmap.ThreadSafeSsrc;
import de.fhhannover.inform.trust.ironvas.omp.OmpConnection;
import de.fhhannover.inform.trust.ironvas.omp.VulnerabilityFetcher;
import de.fhhannover.inform.trust.ironvas.subscriber.Subscriber;

/**
 * Ironvas is an IF-MAP client which integrates OpenVAS into an IF-MAP
 * environment.
 * 
 * @author <NAME>
 *
 */
public class Ironvas {
	
	private static final Logger logger =
			Logger.getLogger(Ironvas.class.getName());
	private static final String LOGGING_CONFIG_FILE = "/logging.properties";
	
	public static void main(String[] args) {
		setupLogging();
		Configuration.init();
		
		// TODO command line parser
		// overwrite configuration with command line arguments

		
		// begin initialization ------------------------------------------------
		
		
		if (Configuration.publisherEnable().equals("false") &&
				Configuration.subscriberEnable().equals("false")) {
			logger.warning("nothing to do, shutting down ...");
		}
		else {
			ShutdownHook hook = new ShutdownHook();
			Runtime.getRuntime().addShutdownHook(new Thread(hook));
			ThreadInterruptionWatcher watcher = new ThreadInterruptionWatcher();

			SSRC ssrc = initIfmap();

			try {
				ssrc.newSession();
				ssrc.purgePublisher();
			} catch (Exception e) {
				System.err.println("could not connect to ifmap server: " + e);
				System.exit(1);
			}
			
			// TODO introduce Executor for thread handling
			Thread ssrcKeepaliveThread = new Thread(new Keepalive(ssrc,
							Integer.parseInt(Configuration.ifmapKeepalive())),
					"ssrc-keepalive-thread");
			
			hook.add(ssrcKeepaliveThread);
			watcher.add(ssrcKeepaliveThread);
			ssrcKeepaliveThread.start();
			
			
			
			if (Configuration.publisherEnable().equals("true")) {
				logger.info("activate publisher ...");
				runPublisher(ssrc, hook, watcher);
			}
			if (Configuration.subscriberEnable().equals("true")) {
				logger.info("activate subscriber ...");
				runSubscriber(ssrc, hook, watcher);
			}
			
			
			Scanner s = new Scanner(System.in);
			do {
				System.out.print("enter 'exit' to shutdown: ");
			} while (!s.nextLine().equals("exit"));
			
			hook.run();
			System.exit(0);
		}
	}
	
	/**
	 * Creates and starts the publisher part of ironvas based on the values
	 * in {@link Configuration}.
{'fraction_non_alphanumeric': 0.12392077196546471, 'fraction_numerical': 0.005713560182833925, 'mean_word_length': 3.81479217603912, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4370402', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2324, 'n_words': 579}