Improved predefined vc caching (issue #28)

Change-Id: I85a3778c8bb79ed21ef4667d20eb20a2b2dc7bdc
diff --git a/core/pom.xml b/core/pom.xml
index f5cfa3e..869823c 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -8,7 +8,7 @@
 	<properties>
 		<java.version>1.8</java.version>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-		<spring-framework.version>5.1.1.RELEASE</spring-framework.version>
+		<spring.version>5.1.1.RELEASE</spring.version>
 		<jersey.version>1.19.4</jersey.version>
 		<jetty.version>9.4.12.v20180830</jetty.version>
 		<hibernate.version>5.3.7.Final</hibernate.version>
@@ -307,12 +307,12 @@
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-core</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-web</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 		<dependency>
 			<groupId>org.springframework</groupId>
@@ -322,44 +322,44 @@
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-orm</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-jdbc</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-tx</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 		<dependency>
 			<groupId>org.springframework.security</groupId>
 			<artifactId>spring-security-core</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 		<dependency>
 			<groupId>org.springframework.security</groupId>
 			<artifactId>spring-security-web</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 		<!-- EM: done -->
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-context</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-context-support</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-test</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 			<scope>compile</scope>
 		</dependency>
 
@@ -368,12 +368,12 @@
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-aop</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-aspects</artifactId>
-			<version>${spring-framework.version}</version>
+			<version>${spring.version}</version>
 		</dependency>
 
 		<!-- end copy -->
diff --git a/full/Changes b/full/Changes
index 3532fcd..2cfe740 100644
--- a/full/Changes
+++ b/full/Changes
@@ -3,6 +3,8 @@
    - Added a search timeout test (margaretha)
 18/12/2018
    - Updated tests using BeanConfigTest to use SpringJerseyTest (margaretha)
+08/01/2019
+   - Improved predefined vc caching (issue #28, margaretha)
 
 # version 0.61.4
 14/11/2018
diff --git a/full/pom.xml b/full/pom.xml
index 9d0a1d9..3278b7b 100644
--- a/full/pom.xml
+++ b/full/pom.xml
@@ -7,7 +7,7 @@
 	<properties>
 		<java.version>1.8</java.version>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-		<spring-framework.version>5.1.1.RELEASE</spring-framework.version>
+		<spring.version>5.1.1.RELEASE</spring.version>
 		<jetty.version>9.4.12.v20180830</jetty.version>
 		<jersey.version>1.19.4</jersey.version>
 		<hibernate.version>5.3.7.Final</hibernate.version>
diff --git a/full/src/main/java/de/ids_mannheim/de/init/InitializatorImpl.java b/full/src/main/java/de/ids_mannheim/de/init/InitializatorImpl.java
index 9f0e27d..1b49c17 100644
--- a/full/src/main/java/de/ids_mannheim/de/init/InitializatorImpl.java
+++ b/full/src/main/java/de/ids_mannheim/de/init/InitializatorImpl.java
@@ -25,7 +25,7 @@
     @Autowired
     private AccessScopeDao accessScopeDao;
     @Autowired
-    private NamedVCLoader loader;
+    private NamedVCLoader vcLoader;
     @Autowired
     private AnnotationParser annotationParser;
     @Autowired
@@ -39,15 +39,17 @@
     @Override
     public void init () throws IOException, QueryException, KustvaktException {
         setInitialAccessScope();
-        loader.loadVCToCache();
+        Thread t = new Thread(vcLoader);
+        t.start();
     }
 
     public void initAnnotation ()
             throws IOException, QueryException, KustvaktException {
         setInitialAccessScope();
-        loader.loadVCToCache();
         annotationParser.run();
         resourceParser.run();
+        Thread t = new Thread(vcLoader);
+        t.start();
     }
 
     /* (non-Javadoc)
diff --git a/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java b/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
index f1242e5..4d710ef 100644
--- a/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
+++ b/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
@@ -6,6 +6,8 @@
 import java.io.InputStream;
 import java.util.zip.GZIPInputStream;
 
+import javax.management.RuntimeErrorException;
+
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -23,7 +25,7 @@
 import de.ids_mannheim.korap.web.SearchKrill;
 
 @Component
-public class NamedVCLoader {
+public class NamedVCLoader implements Runnable{
     @Autowired
     private FullConfiguration config;
     @Autowired
@@ -34,6 +36,17 @@
     public static Logger jlog = LogManager.getLogger(NamedVCLoader.class);
     public static boolean DEBUG = false;
 
+    @Override
+    public void run () {
+        try {
+            loadVCToCache();
+        }
+        catch (IOException | QueryException | KustvaktException e) {
+//            e.printStackTrace();
+            throw new RuntimeErrorException(new Error(e.getMessage(), e.getCause()));
+        }
+    }
+    
     public void loadVCToCache (String filename, String filePath)
             throws IOException, QueryException, KustvaktException {
 
@@ -118,23 +131,21 @@
 
     private void cacheVC (String json, String filename)
             throws IOException, QueryException {
-        jlog.info("Create KrillCollection: " + filename);
         long start, end;
         start = System.currentTimeMillis();
 
         KrillCollection collection = new KrillCollection(json);
         collection.setIndex(searchKrill.getIndex());
 
-        jlog.info("Store vc in cache " + filename);
+        jlog.info("Store {} in cache ", filename);
         if (collection != null) {
             collection.storeInCache(filename);
         }
         end = System.currentTimeMillis();
-        jlog.info(filename + " caching duration: " + (end - start));
+        jlog.info("{} caching duration: {}", filename, (end - start));
         if (DEBUG) {
             jlog.debug("memory cache: "
                     + KrillCollection.cache.calculateInMemorySize());
         }
     }
-
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionRewrite.java b/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionRewrite.java
index 806069b..9d3d952 100644
--- a/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionRewrite.java
+++ b/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionRewrite.java
@@ -186,7 +186,9 @@
         }
 
         node = node.at("/collection");
-        jlog.debug("REWRITES: " + node.toString());
+        if (DEBUG) { 
+            jlog.debug("REWRITES: " + node.toString());
+        }
         
         return node;
     }
diff --git a/full/src/main/java/de/ids_mannheim/korap/server/KustvaktLiteServer.java b/full/src/main/java/de/ids_mannheim/korap/server/KustvaktLiteServer.java
deleted file mode 100644
index b2510ec..0000000
--- a/full/src/main/java/de/ids_mannheim/korap/server/KustvaktLiteServer.java
+++ /dev/null
@@ -1,41 +0,0 @@
-package de.ids_mannheim.korap.server;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
-import java.util.Properties;
-
-import de.ids_mannheim.korap.config.KustvaktConfiguration;
-
-public class KustvaktLiteServer extends KustvaktBaseServer {
-
-    public static void main (String[] args) throws Exception {
-        KustvaktLiteServer server = new KustvaktLiteServer();
-        kargs = server.readAttributes(args);
-
-        if (kargs == null) System.exit(0);
-
-        File f = new File("kustvakt-lite.conf");
-        Properties properties = new Properties();
-        InputStream in = null;
-
-        if (!f.exists()) {
-            in = KustvaktLiteServer.class.getClassLoader()
-                    .getResourceAsStream("kustvakt-lite.conf");
-        }
-        else {
-            in = new FileInputStream(f);
-        }
-
-        properties.load(in);
-        in.close();
-        config = new KustvaktConfiguration(properties);
-
-        kargs.setSpringConfig("lite-config.xml");
-        rootPackages =
-                "de.ids_mannheim.korap.web";
-
-        server.start();
-    }
-
-}
diff --git a/full/src/main/resources/default-config.xml b/full/src/main/resources/default-config.xml
index 948b2f7..2fbd0ae 100644
--- a/full/src/main/resources/default-config.xml
+++ b/full/src/main/resources/default-config.xml
@@ -172,8 +172,6 @@
 
 	<!-- Data access objects -->
 	<bean id="adminDao" class="de.ids_mannheim.korap.dao.AdminDaoImpl" />
-	<bean id="resourceDao" class="de.ids_mannheim.korap.dao.ResourceDao" />
-	<bean id="accessScopeDao" class="de.ids_mannheim.korap.oauth2.dao.AccessScopeDao" />
 	<bean id="authorizationDao" class="de.ids_mannheim.korap.oauth2.dao.CachedAuthorizationDaoImpl" />
 
 	<!-- Filters -->