Cleanup for packaging

Change-Id: If1e7e7be9b178507f2ca34dfd3c966c0760a49bb
diff --git a/.gitignore b/.gitignore
index 38ccf9a..27469c6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,4 +20,5 @@
 /src/main/resources/server.properties
 /src/main/resources/krill.properties
 /src/main/resources/korap.conf
+/dependency-reduced-pom.xml
 /bin
diff --git a/pom.xml b/pom.xml
index e0eb994..2c68c10 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,17 +7,18 @@
       # Start the server with
       $ mvn clean compile exec:java
 
+      # Or after packaging
+      $ mvn clean package
+      # with
+      $ java -jar target/Krill-Server.jar
+
+
       ** Formatter
       # Format the code with
       $ mvn java-formatter:format
 
-      ** Indexer
-         (This is not the preferred way to index new data!)
-      # install the indexer
-      $ mvn clean compile assembly:single
-
-      # Then run e.g.
-      $ java -jar target/Krill-X.XX.jar
+      ** Indexer after packaging (see above)
+      $ java -jar target/Krill-Indexer.jar
              src/main/resources/krill.properties
              /data/hdd/lucene-new/WPD/
   -->
@@ -237,39 +238,47 @@
 
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-assembly-plugin</artifactId>
-        <version>2.2-beta-5</version>
-        <configuration>	    
-          <archive>
-            <manifest>
-              <addClasspath>true</addClasspath>
-              <mainClass>de.ids_mannheim.korap.index.Indexer</mainClass>
-            </manifest>
-          </archive>
-          <appendAssemblyId>false</appendAssemblyId>
-          <descriptorRefs>
-            <descriptorRef>jar-with-dependencies</descriptorRef>
-          </descriptorRefs>
-        </configuration>
+        <artifactId>maven-shade-plugin</artifactId>
+        <version>2.4.1</version>
         <executions>
           <execution>
-            <id>Indexer</id>
+	    <id>indexer</id>
             <phase>package</phase>
             <goals>
-              <goal>single</goal>
+              <goal>shade</goal>
             </goals>
             <configuration>
-              <finalName>Indexer</finalName>
-              <archive>
-                <manifest>
-		  <mainClass>de.ids_mannheim.korap.index.Indexer</mainClass>
-                </manifest>
-              </archive>
+              <transformers>
+                <transformer
+		    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                  <mainClass>de.ids_mannheim.korap.index.Indexer</mainClass>
+                </transformer>
+              </transformers>
+	      <finalName>${project.artifactId}-Indexer</finalName>
             </configuration>
           </execution>
-	</executions>
+          <execution>
+	    <id>server</id>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+            <configuration>
+              <transformers>
+                <transformer
+		    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                  <mainClass>de.ids_mannheim.korap.server.Node</mainClass>
+                </transformer>
+                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
+                  <resource>krill.properties</resource>
+                </transformer>
+              </transformers>
+	      <finalName>${project.name}-Server</finalName>
+            </configuration>
+          </execution>
+        </executions>
       </plugin>
- 
+
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
diff --git a/src/main/java/de/ids_mannheim/korap/KrillIndex.java b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
index ef34cfd..8a052b0 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillIndex.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
@@ -6,6 +6,7 @@
 import de.ids_mannheim.korap.response.*;
 import de.ids_mannheim.korap.query.SpanElementQuery;
 import de.ids_mannheim.korap.util.QueryException;
+import static de.ids_mannheim.korap.util.KrillProperties.*;
 
 // Lucene classes
 import org.apache.lucene.search.*;
@@ -120,7 +121,7 @@
     private int maxTermRelations = 100;
     private int autoCommit = 500;
     private String version = "unknown";
-    private String name = "Krill";
+    private String name = "Unknown";
 
     // Temp:
     private IndexReader reader;
@@ -144,47 +145,30 @@
             bbTerm = ByteBuffer.allocate(16);
 
     // Some initializations ...
-    // TODO: This should probably happen at a more central point
     {
-        Properties prop = new Properties();
-        URL file = getClass().getClassLoader().getResource("krill.properties");
+        Properties prop = loadProperties();
+        this.version = prop.getProperty("krill.version");
+        this.name = prop.getProperty("krill.name");
 
-        // File found
-        if (file != null) {
-            String f = file.getFile();
-            // Read property file
+        // Check for auto commit value
+        String stringProp = prop.getProperty("krill.index.commit.auto");
+        if (stringProp != null) {
             try {
-                InputStream fr = new FileInputStream(f);
-                prop.load(fr);
-                this.version = prop.getProperty("krill.version");
-                this.name = prop.getProperty("krill.name");
-
-                // Check for auto commit value
-                String stringProp = prop.getProperty("krill.index.commit.auto");
-                if (stringProp != null) {
-                    try {
-                        this.autoCommit = Integer.parseInt(stringProp);
-                    }
-                    catch (NumberFormatException e) {
-                        log.error("krill.index.commit.auto expected to be a numerical value");
-                    };
-                };
-
-                // Check for maximum term relations
-                stringProp = prop.getProperty("krill.index.relations.max");
-                if (stringProp != null) {
-                    try {
-                        this.maxTermRelations = Integer.parseInt(stringProp);
-                    }
-                    catch (NumberFormatException e) {
-                        log.error("krill.index.commit.auto expected to be a numerical value");
-                    };
-                };
+                this.autoCommit = Integer.parseInt(stringProp);
             }
+            catch (NumberFormatException e) {
+                log.error("krill.index.commit.auto expected to be a numerical value");
+            };
+        };
 
-            // Unable to read property file
-            catch (FileNotFoundException e) {
-                log.warn(e.getLocalizedMessage());
+        // Check for maximum term relations
+        stringProp = prop.getProperty("krill.index.relations.max");
+        if (stringProp != null) {
+            try {
+                this.maxTermRelations = Integer.parseInt(stringProp);
+            }
+            catch (NumberFormatException e) {
+                log.error("krill.index.commit.auto expected to be a numerical value");
             };
         };
     };
@@ -1288,11 +1272,12 @@
                  * by using OpenBitSet - but this may not be as fast as I think.
                  */
                 final Bits bitset = collection.bits(atomic);
-                final PositionsToOffset pto = new PositionsToOffset(atomic, field);
+                final PositionsToOffset pto = new PositionsToOffset(atomic,
+                        field);
 
                 // Spans spans = NearSpansOrdered();
                 final Spans spans = query.getSpans(atomic, (Bits) bitset,
-                                                   termContexts);
+                        termContexts);
 
                 final IndexReader lreader = atomic.reader();
                 int localDocID, docID;
@@ -1349,8 +1334,8 @@
                             localDocID, fields) : lreader.document(localDocID);
 
                     // Create new Match
-                    final Match match = new Match(pto, localDocID, spans.start(),
-                            spans.end());
+                    final Match match = new Match(pto, localDocID,
+                            spans.start(), spans.end());
                     match.setContext(kr.getContext());
 
                     // Add match to Result
diff --git a/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java b/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java
index 0510437..7e428a4 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java
@@ -339,7 +339,8 @@
             throws IOException {
 
         for (int i = 0; i < spanQueries.size(); i++) {
-            final SpanQuery query = (SpanQuery) spanQueries.get(i).rewrite(reader);
+            final SpanQuery query = (SpanQuery) spanQueries.get(i).rewrite(
+                    reader);
             if (!query.equals(spanQueries.get(i))) {
                 if (clone == null)
                     clone = clone();
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
index 30515d5..0e4768d 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
@@ -34,9 +34,10 @@
     private final Logger log = LoggerFactory.getLogger(ElementSpans.class);
     // This advices the java compiler to ignore all loggings
     public static final boolean DEBUG = false;
-    
+
     private byte[] b = new byte[8];
 
+
     /**
      * Constructs ElementSpans for the given {@link SpanElementQuery}.
      * 
diff --git a/src/main/java/de/ids_mannheim/korap/response/Response.java b/src/main/java/de/ids_mannheim/korap/response/Response.java
index d5846fb..a1f72a4 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Response.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Response.java
@@ -49,8 +49,8 @@
     private String benchmark;
     private boolean timeExceeded = false;
 
-    private static final String KORAL_VERSION =
-        "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld";
+    private static final String KORAL_VERSION = "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld";
+
 
     /**
      * Construct a new Response object.
diff --git a/src/main/java/de/ids_mannheim/korap/server/Node.java b/src/main/java/de/ids_mannheim/korap/server/Node.java
index 3e406f5..78d46db 100644
--- a/src/main/java/de/ids_mannheim/korap/server/Node.java
+++ b/src/main/java/de/ids_mannheim/korap/server/Node.java
@@ -19,6 +19,8 @@
 import de.ids_mannheim.korap.KrillIndex;
 import org.apache.lucene.store.MMapDirectory;
 
+import static de.ids_mannheim.korap.util.KrillProperties.*;
+
 import com.mchange.v2.c3p0.*;
 
 /**
@@ -65,7 +67,18 @@
      * @return Grizzly HTTP server.
      */
     public static HttpServer startServer () {
-        _loadResourceProperties();
+        Properties prop = loadProperties(propFile);
+
+        // Node properties
+        path = prop.getProperty("krill.indexDir", path);
+        name = prop.getProperty("krill.server.name", name);
+        BASE_URI = prop.getProperty("krill.server.baseURI", BASE_URI);
+
+        // Database properties
+        dbUser = prop.getProperty("krill.db.user", dbUser);
+        dbPwd = prop.getProperty("krill.db.pwd", dbPwd);
+        dbClass = prop.getProperty("krill.db.class", dbClass);
+        dbURL = prop.getProperty("krill.db.jdbcURL", dbURL);
 
         // create a resource config that scans for JAX-RS resources and providers
         // in de.ids_mannheim.korap.server package
@@ -251,46 +264,4 @@
         };
         return null;
     };
-
-
-    // Load properties from file
-    private static Properties _loadProperties (String propFile) {
-        try {
-            InputStream file = new FileInputStream(propFile);
-            Properties prop = new Properties();
-            prop.load(file);
-
-            // Node properties
-            path = prop.getProperty("krill.indexDir", path);
-            name = prop.getProperty("krill.server.name", name);
-            BASE_URI = prop.getProperty("krill.server.baseURI", BASE_URI);
-
-            // Database properties
-            dbUser = prop.getProperty("krill.db.user", dbUser);
-            dbPwd = prop.getProperty("krill.db.pwd", dbPwd);
-            dbClass = prop.getProperty("krill.db.class", dbClass);
-            dbURL = prop.getProperty("krill.db.jdbcURL", dbURL);
-            return prop;
-        }
-        catch (IOException e) {
-            log.error(e.getLocalizedMessage());
-        };
-        return null;
-    };
-
-
-    // Load properties from resource file
-    private static Properties _loadResourceProperties () {
-
-        // Load configuration
-        URL resUrl = Node.class.getClassLoader().getResource(propFile);
-        if (resUrl == null) {
-            log.error(
-                    "Cannot find {}. Please create it using \"{}.info\" as template.",
-                    propFile, propFile);
-            return null;
-        };
-
-        return _loadProperties(resUrl.getFile());
-    };
 };
diff --git a/src/main/java/de/ids_mannheim/korap/server/Resource.java b/src/main/java/de/ids_mannheim/korap/server/Resource.java
index 52bfa11..83f69b5 100644
--- a/src/main/java/de/ids_mannheim/korap/server/Resource.java
+++ b/src/main/java/de/ids_mannheim/korap/server/Resource.java
@@ -66,9 +66,9 @@
     public final static boolean DEBUG = false;
 
     // Slightly based on String::BooleanSimple
-    private final static Pattern p =
-        Pattern.compile("\\s*(?i:false|no|inactive|disabled|"
-            + "off|n|neg(?:ative)?|not|null|undef)\\s*");
+    private final static Pattern p = Pattern
+            .compile("\\s*(?i:false|no|inactive|disabled|"
+                    + "off|n|neg(?:ative)?|not|null|undef)\\s*");
 
     private KrillIndex index;
 
@@ -133,11 +133,8 @@
         };
 
         // Set HTTP to 200
-        kresp.addMessage(
-            681,
-            "Document was added successfully",
-            fd.getID() != null ? fd.getID() : "Unknown"
-        );
+        kresp.addMessage(681, "Document was added successfully",
+                fd.getID() != null ? fd.getID() : "Unknown");
 
         return kresp.toJsonString();
     };
@@ -195,9 +192,8 @@
         final MultivaluedMap<String, String> qp = uri.getQueryParameters();
 
         if (qp.get("uid") == null) {
-            kresp.addError(610,
-                           "Missing request parameters",
-                           "No unique IDs were given");
+            kresp.addError(610, "Missing request parameters",
+                    "No unique IDs were given");
             return kresp.toJsonString();
         };
 
@@ -237,7 +233,8 @@
 
         // Get the database
         try {
-            final MatchCollectorDB mc = new MatchCollectorDB(1000, "Res_" + resultID);
+            final MatchCollectorDB mc = new MatchCollectorDB(1000, "Res_"
+                    + resultID);
             final ComboPooledDataSource pool = Node.getDBPool();
             mc.setDBPool("mysql", pool, pool.getConnection());
 
@@ -331,9 +328,9 @@
 
         try {
             // Get match info
-            return index.getMatchInfo(id, "tokens", info, foundries,
-                                      layers, includeSpans, includeHighlights,
-                                      extendToSentence).toJsonString();
+            return index.getMatchInfo(id, "tokens", info, foundries, layers,
+                    includeSpans, includeHighlights, extendToSentence)
+                    .toJsonString();
         }
 
         // Nothing found
@@ -417,6 +414,7 @@
         return kresp;
     };
 
+
     // Check if a string is meant to represent null
     private static boolean _isNull (String value) {
         if (value == null)
diff --git a/src/main/java/de/ids_mannheim/korap/util/KrillProperties.java b/src/main/java/de/ids_mannheim/korap/util/KrillProperties.java
new file mode 100644
index 0000000..551fc0c
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/util/KrillProperties.java
@@ -0,0 +1,53 @@
+package de.ids_mannheim.korap.util;
+
+import java.util.*;
+import java.io.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import de.ids_mannheim.korap.Krill;
+
+// Todo: Properties may be loaded twice - althogh Java may cache automatically
+public class KrillProperties {
+
+    // Logger
+    private final static Logger log = LoggerFactory.getLogger(Krill.class);
+
+
+    // Load properties from file
+    public static Properties loadProperties () {
+        return loadProperties("krill.properties");
+    };
+
+
+    // Load properties from file
+    public static Properties loadProperties (String propFile) {
+        InputStream file;
+        Properties prop;
+        try {
+            file = new FileInputStream(propFile);
+            prop = new Properties();
+            prop.load(file);
+        }
+        catch (IOException t) {
+            try {
+                file = KrillProperties.class.getClassLoader()
+                        .getResourceAsStream(propFile);
+
+                if (file == null) {
+                    log.error(
+                            "Cannot find {}. Please create it using \"{}.info\" as template.",
+                            propFile, propFile);
+                    return null;
+                };
+
+                prop = new Properties();
+                prop.load(file);
+            }
+            catch (IOException e) {
+                log.error(e.getLocalizedMessage());
+                return null;
+            };
+        };
+        return prop;
+    };
+};
diff --git a/src/test/java/de/ids_mannheim/korap/benchmark/TestBenchmarkSamples.java b/src/test/java/de/ids_mannheim/korap/benchmark/TestBenchmarkSamples.java
index 69e8503..535b1ce 100644
--- a/src/test/java/de/ids_mannheim/korap/benchmark/TestBenchmarkSamples.java
+++ b/src/test/java/de/ids_mannheim/korap/benchmark/TestBenchmarkSamples.java
@@ -28,6 +28,7 @@
     private final int rounds = 1000;
     private long t1 = 0, t2 = 0;
 
+
     @Test
     public void simpleSegmentQuery () throws Exception {
         // Construct index
@@ -36,10 +37,10 @@
 
         // Indexing test files
         for (String i : new String[] { "00001", "00002", "00003", "00004",
-                                           "00005", "00006", "02439" }) {
+                "00005", "00006", "02439" }) {
             ki.addDoc(
-                      getClass().getResourceAsStream("/wiki/" + i + ".json.gz"),
-                      true);
+                    getClass().getResourceAsStream("/wiki/" + i + ".json.gz"),
+                    true);
         };
         ki.commit();
 
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
index 6c2b2f9..e8fa3f8 100644
--- a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
@@ -104,6 +104,7 @@
         assertEquals("-author:goethe", ks.getCollection().toString());
     };
 
+
     @Test
     public void nocollectiontypegiven () {
         String metaQuery = _getJSONString("multiterm_rewrite_collection.jsonld");
@@ -157,7 +158,6 @@
 
 
 
-
     // Legacy collections reflect old tests, that were adopted to the new scheme
     @Test
     public void metaQuery1Legacy () {
diff --git a/src/test/java/de/ids_mannheim/korap/response/TestResponse.java b/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
index 99bc0a5..1ab89fe 100644
--- a/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
+++ b/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
@@ -25,8 +25,9 @@
     public void testResponse () throws IOException {
         Response resp = new Response();
         JsonNode respJson = mapper.readTree(resp.toJsonString());
-        assertEquals("http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
-                     respJson.at("/@context").asText());
+        assertEquals(
+                "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+                respJson.at("/@context").asText());
         assertEquals("", respJson.at("/meta").asText());
 
         resp.setVersion("0.24");
@@ -59,8 +60,9 @@
     public void testResponseNotifications () throws IOException {
         Response resp = new Response();
         JsonNode respJson = mapper.readTree(resp.toJsonString());
-        assertEquals("http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
-                     respJson.at("/@context").asText());
+        assertEquals(
+                "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+                respJson.at("/@context").asText());
         assertEquals("", respJson.at("/meta").asText());
         resp.setVersion("0.24");
         resp.setNode("Tanja");
diff --git a/src/test/java/de/ids_mannheim/korap/server/TestResource.java b/src/test/java/de/ids_mannheim/korap/server/TestResource.java
index 4d4dd48..79de7d3 100644
--- a/src/test/java/de/ids_mannheim/korap/server/TestResource.java
+++ b/src/test/java/de/ids_mannheim/korap/server/TestResource.java
@@ -75,8 +75,8 @@
         Node.closeDBPool();
         t4 = System.nanoTime();
 
-        double startup  = (double) (t2 - t1) / 1000000000.0;
-        double action   = (double) (t3 - t2) / 1000000000.0;
+        double startup = (double) (t2 - t1) / 1000000000.0;
+        double action = (double) (t3 - t2) / 1000000000.0;
         double shutdown = (double) (t4 - t3) / 1000000000.0;
 
         /*
@@ -107,13 +107,13 @@
         assertEquals(680, res.at("/messages/0/0").asInt());
     };
 
+
     @Test
     public void testIndexing () throws IOException {
         String resp;
         JsonNode res;
 
-        for (String i : new String[] {
-                "00001", "00002", "00003", "00004",
+        for (String i : new String[] { "00001", "00002", "00003", "00004",
                 "00005", "00006", "02439" }) {
 
             String json = StringfromFile(getClass().getResource(