diff src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java @ 4:3599b29c393f

store seems to work now :-)
author casties
date Mon, 02 Jul 2012 22:39:46 +0200
parents 47b53ae385d1
children bbf0cc5bee29
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java	Fri Jun 29 20:38:27 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java	Mon Jul 02 22:39:46 2012 +0200
@@ -27,15 +27,12 @@
 import org.restlet.Context;
 import org.restlet.Restlet;
 import org.restlet.routing.Router;
-import org.restlet.security.ChallengeAuthenticator;
 
-import scala.sys.process.ProcessBuilderImpl.Dummy;
+import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
 
 public class RestServer extends Application {
 
-    public static Logger logger = Logger.getRootLogger();
-
-    private ChallengeAuthenticator authenticator;
+    public static Logger logger = Logger.getLogger(RestServer.class);
 
     /**
      * Properties holding consumer keys and secrets
@@ -47,6 +44,12 @@
     public static final String GRAPHDB_KEY = "annotationmanager.graphdb";
     public String DB_PATH = "WEB-INF/neo4j-annotation-db";
 
+    private WrappingNeoServerBootstrapper srv;
+    public static final String GRAPHDBSRV_KEY = "annotationmanager.graphdb.srv";
+    
+    private AnnotationStore store;
+    public static final String ANNSTORE_KEY = "annotationmanager.store";
+
     /**
      * constructor
      * 
@@ -70,10 +73,17 @@
                 if (dbFn != null) {
                     logger.debug("opening DB " + dbFn);
                     graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(dbFn);
+                    registerShutdownHook(graphDb);
                     // store in context
                     sc.setAttribute(GRAPHDB_KEY, graphDb);
-                    WrappingNeoServerBootstrapper srv = new WrappingNeoServerBootstrapper((AbstractGraphDatabase) graphDb);
+                    // AnnotationStore
+                    store = new AnnotationStore(graphDb);
+                    sc.setAttribute(ANNSTORE_KEY, store);
+                    // admin server
+                    srv = new WrappingNeoServerBootstrapper((AbstractGraphDatabase) graphDb);
                     logger.debug("Starting DB admin server...");
+                    // store in context
+                    sc.setAttribute(GRAPHDBSRV_KEY, srv);
                     srv.start();
                 } else {
                     logger.error("Unable to get resource " + DB_PATH);
@@ -126,7 +136,7 @@
 
         router.attach("/annotator/annotations", AnnotatorAnnotations.class);
         router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class);
-        // router.attach("/annotator/search", AnnotatorSearch.class);
+        router.attach("/annotator/search", AnnotatorSearch.class);
 
         // router.attach("",redirector); router.attach("/annotator",
         // ExtendedAnnotationInput.class);
@@ -240,4 +250,38 @@
         return filename;
     }
 
+    /* (non-Javadoc)
+     * @see org.restlet.Application#stop()
+     */
+    @Override
+    public synchronized void stop() throws Exception {
+        /*
+         * trying to clean up databases, nur sure if this is the right way...
+         */
+        if (srv != null) {
+            logger.debug("Stopping DB admin server...");
+            srv.stop();
+            srv = null;
+        }
+        if (graphDb != null) {
+            logger.debug("Stopping DB...");
+            graphDb.shutdown();
+            graphDb = null;
+        }
+        super.stop();
+    }
+
+    private static void registerShutdownHook(final GraphDatabaseService graphDb) {
+        // Registers a shutdown hook for the Neo4j instance so that it
+        // shuts down nicely when the VM exits (even if you "Ctrl-C" the
+        // running example before it's completed)
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                graphDb.shutdown();
+            }
+        });
+    }
+   
+    
 }