diff src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java @ 3:47b53ae385d1

merging old code
author casties
date Fri, 29 Jun 2012 20:38:27 +0200
parents 6556943c4fb9
children 3599b29c393f
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java	Thu Jun 28 19:34:32 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java	Fri Jun 29 20:38:27 2012 +0200
@@ -19,6 +19,10 @@
 
 import org.apache.log4j.BasicConfigurator;
 import org.apache.log4j.Logger;
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.factory.GraphDatabaseFactory;
+import org.neo4j.kernel.AbstractGraphDatabase;
+import org.neo4j.server.WrappingNeoServerBootstrapper;
 import org.restlet.Application;
 import org.restlet.Context;
 import org.restlet.Restlet;
@@ -30,14 +34,18 @@
 public class RestServer extends Application {
 
     public static Logger logger = Logger.getRootLogger();
-    
+
     private ChallengeAuthenticator authenticator;
 
     /**
      * Properties holding consumer keys and secrets
      */
     private Properties consumerKeys;
-    public final String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property";
+    public String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property";
+
+    private GraphDatabaseService graphDb;
+    public static final String GRAPHDB_KEY = "annotationmanager.graphdb";
+    public String DB_PATH = "WEB-INF/neo4j-annotation-db";
 
     /**
      * constructor
@@ -46,50 +54,58 @@
      */
     public RestServer(Context parentContext) {
         super(parentContext);
-
-        Logger rl = Logger.getRootLogger();
+        // TODO: is this the right place to run the log4j configurator?
         BasicConfigurator.configure();
-        // read consumerKeys from webapp
-        consumerKeys = new Properties();
-        ServletContext sc = (ServletContext) getContext().getServerDispatcher()
-                .getContext().getAttributes()
+
+        ServletContext sc = (ServletContext) getContext().getServerDispatcher().getContext().getAttributes()
                 .get("org.restlet.ext.servlet.ServletContext");
         if (sc != null) {
-            InputStream ps = sc.getResourceAsStream(CONSUMER_KEYS_PATH);
-            if (ps == null) {
-                // try as file
-                File pf = new File(sc.getRealPath(CONSUMER_KEYS_PATH));
-                if (pf != null) {
-                    rl.debug("trying file for consumer keys: "+pf);
-                    try {
-                        ps = new FileInputStream(pf);
-                    } catch (FileNotFoundException e) {
-                        // TODO Auto-generated catch block
-                        e.printStackTrace();
-                    }
+            // look for database service in context
+            graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY);
+            if (graphDb == null) {
+                /*
+                 * open database
+                 */
+                String dbFn = getResourcePath(sc, DB_PATH);
+                if (dbFn != null) {
+                    logger.debug("opening DB " + dbFn);
+                    graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(dbFn);
+                    // store in context
+                    sc.setAttribute(GRAPHDB_KEY, graphDb);
+                    WrappingNeoServerBootstrapper srv = new WrappingNeoServerBootstrapper((AbstractGraphDatabase) graphDb);
+                    logger.debug("Starting DB admin server...");
+                    srv.start();
+                } else {
+                    logger.error("Unable to get resource " + DB_PATH);
                 }
             }
+            /*
+             * read consumerKeys from webapp
+             */
+            consumerKeys = new Properties();
+            InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH);
             if (ps != null) {
-                rl.debug("loading consumer keys from "+CONSUMER_KEYS_PATH);
+                logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH);
                 try {
                     consumerKeys.load(ps);
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
-                rl.debug("consumer keys: "+consumerKeys);
+                logger.debug("consumer keys: " + consumerKeys);
             } else {
-                rl.error("Unable to get resource "+CONSUMER_KEYS_PATH);
+                logger.error("Unable to get resource " + CONSUMER_KEYS_PATH);
             }
         } else {
-            rl.error("Unable to get ServletContext!");
+            logger.error("Unable to get ServletContext!");
         }
-        
+
     }
-    
+
     /**
-     * returns consumer secret for consumer key.
-     * returns null if consumer key doesn't exist.
+     * returns consumer secret for consumer key. returns null if consumer key
+     * doesn't exist.
+     * 
      * @param consumerKey
      * @return
      */
@@ -97,7 +113,6 @@
         return consumerKeys.getProperty(consumerKey);
     }
 
-
     /*
      * (non-Javadoc)
      * 
@@ -105,26 +120,21 @@
      */
     @Override
     public Restlet createInboundRoot() {
-        //this.authenticator = createAuthenticator();
-
-        // String target = "{rh}/{rf}/XX";
-        // Redirector redirector = new
-        // Redirector(getContext().createChildContext(), target,
-        // Redirector.MODE_CLIENT_SEE_OTHER);
+        // this.authenticator = createAuthenticator();
 
         Router router = new Router(getContext());
-        /*
+
         router.attach("/annotator/annotations", AnnotatorAnnotations.class);
         router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class);
-        router.attach("/annotator/search", AnnotatorSearch.class);
-        
-        // router.attach("",redirector);
-        router.attach("/annotator", ExtendedAnnotationInput.class);
-        */
+        // router.attach("/annotator/search", AnnotatorSearch.class);
+
+        // router.attach("",redirector); router.attach("/annotator",
+        // ExtendedAnnotationInput.class);
+
         router.attach("/", AnnotatorInfo.class);
-        //authenticator.setNext(router);
-        //return authenticator;
-        
+        // authenticator.setNext(router);
+        // return authenticator;
+
         return router;
     }
 
@@ -141,7 +151,8 @@
         String sp = "com.sun.jndi.ldap.LdapCtxFactory";
         env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
 
-        String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de"; // TODO should go into config file
+        // TODO: should go into config file
+        String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de"; 
         env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
 
         DirContext dctx;
@@ -163,8 +174,7 @@
         String filter = "(uid=" + creator + ")";
 
         try {
-            NamingEnumeration<SearchResult> results = dctx.search(base, filter,
-                    sc);
+            NamingEnumeration<SearchResult> results = dctx.search(base, filter, sc);
             while (results.hasMore()) {
                 SearchResult sr = (SearchResult) results.next();
                 javax.naming.directory.Attributes attrs = sr.getAttributes();
@@ -186,4 +196,48 @@
         return retString;
     }
 
+    /**
+     * returns resource from path (in webapp) as InputStream.
+     * 
+     * @param sc
+     * @param path
+     * @return
+     */
+    protected InputStream getResourceAsStream(ServletContext sc, String path) {
+        InputStream ps = sc.getResourceAsStream(path);
+        if (ps == null) {
+            // try as file
+            File pf = new File(sc.getRealPath(path));
+            if (pf != null) {
+                logger.debug("trying file for: " + pf);
+                try {
+                    ps = new FileInputStream(pf);
+                } catch (FileNotFoundException e) {
+                    logger.error(e);
+                }
+            }
+        }
+        return ps;
+    }
+
+    /**
+     * get a real file name for a web app file pathname.
+     * 
+     * If filename starts with "/" its treated as absolute else the path is
+     * appended to the base directory of the web-app.
+     * 
+     * @param filename
+     * @param sc
+     * @return
+     */
+    public static String getResourcePath(ServletContext sc, String filename) {
+        File f = new File(filename);
+        // is the filename absolute?
+        if (!f.isAbsolute()) {
+            // relative path -> use getRealPath to resolve in webapp
+            filename = sc.getRealPath(filename);
+        }
+        return filename;
+    }
+
 }