From baa41c5ff7322456946063d531d63230e45cfd13 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Mon, 5 Dec 2016 21:30:09 +0100 Subject: [PATCH] Add src/resources/config.properties to allow userdefined configuration --- .gitignore | 2 + README.md | 1 + .../de/hsh/inform/orientdb_project/Main.java | 12 +++-- .../util/ConfigPropertiesReader.java | 53 +++++++++++++++++++ src/resources/config.properties.sample | 9 ++++ 5 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/hsh/inform/orientdb_project/util/ConfigPropertiesReader.java create mode 100644 src/resources/config.properties.sample diff --git a/.gitignore b/.gitignore index 7d4b0bd..ed38738 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ .settings .project /target/ + +src/resources/config.properties diff --git a/README.md b/README.md index d180d29..9a9bb9d 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,4 @@ This is an application to import a tcpdump file into an orientdb server instance. In order to run this project, import it into ecplise and configure it to use maven to install the dependencies from `pom.xml` +Also make sure to make `src/resources` a source folder so that the configuration file `config.properties` is within the class path. (Use `config.properties.sample` to create your own `config.properties` file) diff --git a/src/main/java/de/hsh/inform/orientdb_project/Main.java b/src/main/java/de/hsh/inform/orientdb_project/Main.java index 25868a6..142218f 100644 --- a/src/main/java/de/hsh/inform/orientdb_project/Main.java +++ b/src/main/java/de/hsh/inform/orientdb_project/Main.java @@ -11,13 +11,15 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import de.hsh.inform.orientdb_project.netdata.AbstractNetdataImportService; import de.hsh.inform.orientdb_project.orientdb.NodeBasedImportService; import de.hsh.inform.orientdb_project.orientdb.OrientDbHelperService; +import de.hsh.inform.orientdb_project.util.ConfigPropertiesReader; public class Main { public static void main(String[] args) { - // TODO: Make this configurable or easy to exchange. - String filename = "/home/jpt/Temp/tcpdump_2"; - OrientDbHelperService odhs = new OrientDbHelperService("192.168.0.110", "hshtest", "root", "root"); + ConfigPropertiesReader config = new ConfigPropertiesReader(); + String filename = config.filename; + OrientDbHelperService odhs = new OrientDbHelperService(config.dbhost, config.dbname, config.dbuser, config.dbpass); + System.out.println("Using database: " + odhs.getDbUri(true)); // Clean up existing database and set up schema from scratch odhs.cleanUpServer(); @@ -26,8 +28,7 @@ public class Main { // Get "handle" for database to pass to import service OrientGraphNoTx ogf = odhs.getOrientGraphNoTx(); - //AbstractNetdataImportService importService = new DummyImportService(filename); - //AbstractNetdataImportService importService = new LowPerformanceOrientDbNetdataImportService(filename, ogf); + //AbstractNetdataImportService importService = new DummyImportService(filename); // Only for comparison reasons AbstractNetdataImportService importService = new NodeBasedImportService(filename, ogf); // Go go gadget import service! @@ -38,6 +39,7 @@ public class Main { } catch (EOFException | PcapNativeException | TimeoutException | NotOpenException e) { e.printStackTrace(); } + // Done odhs.close(); System.out.println(System.currentTimeMillis()/1000L + ": End of program."); diff --git a/src/main/java/de/hsh/inform/orientdb_project/util/ConfigPropertiesReader.java b/src/main/java/de/hsh/inform/orientdb_project/util/ConfigPropertiesReader.java new file mode 100644 index 0000000..61b07c0 --- /dev/null +++ b/src/main/java/de/hsh/inform/orientdb_project/util/ConfigPropertiesReader.java @@ -0,0 +1,53 @@ +package de.hsh.inform.orientdb_project.util; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class ConfigPropertiesReader { + + public String dbuser; + public String dbpass; + public String dbname; + public String dbhost; + public String filename; + public boolean limitedImport; + public int importLimit; + + public ConfigPropertiesReader() { + Properties props = null; + try { + props = this.readProperties(); + } catch (IOException e) { + e.printStackTrace(); + } + this.dbuser = props.getProperty("dbuser"); + this.dbpass = props.getProperty("dbpass"); + this.dbname = props.getProperty("dbname"); + this.dbhost = props.getProperty("dbhost"); + this.filename = props.getProperty("filename"); + this.limitedImport = Boolean.valueOf(props.getProperty("limitedImport")); + this.importLimit = Integer.valueOf(props.getProperty("importLimit")); + } + + private Properties readProperties() throws IOException { + String propFileName = "config.properties"; + Properties properties = null; + InputStream inputStream = null; + try { + properties = new Properties(); + inputStream = getClass().getClassLoader().getResourceAsStream(propFileName); + if(inputStream != null) { + properties.load(inputStream); + } else { + throw new FileNotFoundException("Property file '" + propFileName + "' not found in the classpath! Make sure to use src/resources as source folder!"); + } + } catch (Exception e) { + System.err.println("Exception: " + e); + } finally { + inputStream.close(); + } + return properties; + } +} diff --git a/src/resources/config.properties.sample b/src/resources/config.properties.sample new file mode 100644 index 0000000..1b46a81 --- /dev/null +++ b/src/resources/config.properties.sample @@ -0,0 +1,9 @@ +#OrientDb config settings +dbuser=root +dbpass=root +dbname=root +dbhost=127.0.0.1 +filename=/home/jpt/Temp/tcpdump_2 +limitedImport=true +importLimit=10000 +