diff src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java @ 71:326369d4bc4d

trying restlet 2.1. doesn't work yet.
author casties
date Fri, 07 Mar 2014 13:48:50 +0100
parents 2b1e6df5e21a
children 4c2cea836bc0
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java	Thu Mar 06 15:09:04 2014 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java	Fri Mar 07 13:48:50 2014 +0100
@@ -30,6 +30,7 @@
 import java.io.InputStream;
 import java.util.Hashtable;
 import java.util.Properties;
+import java.util.concurrent.ConcurrentMap;
 
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
@@ -50,6 +51,8 @@
 import org.neo4j.server.WrappingNeoServerBootstrapper;
 import org.restlet.Application;
 import org.restlet.Context;
+import org.restlet.Restlet;
+import org.restlet.data.LocalReference;
 
 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
 
@@ -61,14 +64,14 @@
      * Properties holding consumer keys and secrets.
      */
     protected Properties consumerKeys;
-    public String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property";
+    public String CONSUMER_KEYS_PATH = "consumerkeys.property";
     public static final String CONSUMERKEYS_KEY = "annotationmanager.consumerkeys";
 
     /**
      * Properties holding server config.
      */
     protected Properties serverConfig;
-    public String CONFIG_PROPS_PATH = "WEB-INF/serverconfig.property";
+    public String CONFIG_PROPS_PATH = "serverconfig.property";
     public static final String SERVERCONFIG_KEY = "annotationmanager.serverconfig";
 
     /** 
@@ -77,7 +80,7 @@
     protected GraphDatabaseService graphDb;
     public static final String GRAPHDB_KEY = "annotationmanager.graphdb";
     public static final String GRAPHDB_PATH_KEY = "annotationmanager.graphdb.path";
-    public String graphdbPath = "WEB-INF/neo4j-annotation-db";
+    public String graphdbPath = "neo4j-annotation-db";
 
     /**
      * database interface server instance.
@@ -133,29 +136,38 @@
      */
     public BaseRestlet(Context context) {
         super(context);
-        configure();
+        configure(context);
     }
 
     /**
      * Configures the restlet.
      * Reads serverConfig, consumerKeys and graphDb config from config files and starts graphDb.
      * Uses config from webapp context if already initialized. 
+     * @param context 
      */
-    protected void configure() {
-        ServletContext sc = (ServletContext) getContext().getServerDispatcher().getContext().getAttributes()
+    protected void configure(Context context) {
+        Context ctx = Context.getCurrent();
+        ConcurrentMap<String, Object> attrs = ctx.getAttributes();
+        ServletContext sc = null;
+        if (context != null) {
+            Restlet a = context.getServerDispatcher();
+            Context b = a.getContext();
+            ConcurrentMap<String, Object> c = b.getAttributes();
+            sc = (ServletContext) context.getServerDispatcher().getContext().getAttributes()
                 .get("org.restlet.ext.servlet.ServletContext");
-        if (sc != null) {
-            if (sc.getAttribute("annotationserver.log4j.configured") == null) {
+        }
+        if (attrs != null) {
+            if (attrs.get("annotationserver.log4j.configured") == null) {
                 // TODO: is this the right place to run the log4j configurator?
                 BasicConfigurator.configure();
-                sc.setAttribute("annotationserver.log4j.configured", "done");
+                attrs.put("annotationserver.log4j.configured", "done");
             }
             logger.info(getVersion() + " starting...");
 
             /*
              * read config from webapp
              */
-            serverConfig = (Properties) sc.getAttribute(SERVERCONFIG_KEY);
+            serverConfig = (Properties) attrs.get(SERVERCONFIG_KEY);
             if (serverConfig == null) {
                 serverConfig = new Properties();
                 InputStream ps = getResourceAsStream(sc, CONFIG_PROPS_PATH);
@@ -191,10 +203,10 @@
                     logger.error("Unable to get resource " + CONFIG_PROPS_PATH);
                 }
                 // store config
-                sc.setAttribute(SERVERCONFIG_KEY, serverConfig);
+                attrs.put(SERVERCONFIG_KEY, serverConfig);
             }
             // look for database service in context
-            graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY);
+            graphDb = (GraphDatabaseService) attrs.get(GRAPHDB_KEY);
             if (graphDb == null) {
                 /*
                  * open database
@@ -207,27 +219,27 @@
                     graphDb = graphDbBuilder.newGraphDatabase();
                     registerShutdownHook(graphDb);
                     // store in context
-                    sc.setAttribute(GRAPHDB_KEY, graphDb);
+                    attrs.put(GRAPHDB_KEY, graphDb);
                     // AnnotationStore
                     store = new AnnotationStore(graphDb);
-                    sc.setAttribute(ANNSTORE_KEY, store);
+                    attrs.put(ANNSTORE_KEY, store);
                     // admin server
                     srv = new WrappingNeoServerBootstrapper((GraphDatabaseAPI) graphDb);
                     logger.debug("Starting DB admin server...");
                     // store in context
-                    sc.setAttribute(GRAPHDBSRV_KEY, srv);
+                    attrs.put(GRAPHDBSRV_KEY, srv);
                     srv.start();
                 } else {
                     logger.error("Unable to get resource " + dbFn);
                 }
             } else {
                 // get existing AnnotationStore
-                store = (AnnotationStore) sc.getAttribute(ANNSTORE_KEY);
+                store = (AnnotationStore) attrs.get(ANNSTORE_KEY);
             }
             /*
              * read consumerKeys from webapp
              */
-            consumerKeys = (Properties) sc.getAttribute(CONSUMERKEYS_KEY);
+            consumerKeys = (Properties) attrs.get(CONSUMERKEYS_KEY);
             if (consumerKeys == null) {
                 consumerKeys = new Properties();
                 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH);
@@ -244,7 +256,7 @@
                     logger.error("Unable to get resource " + CONSUMER_KEYS_PATH);
                 }
                 // store config
-                sc.setAttribute(CONSUMERKEYS_KEY, consumerKeys);
+                attrs.put(CONSUMERKEYS_KEY, consumerKeys);
             }
         } else {
             logger.error("Unable to get ServletContext!");
@@ -341,16 +353,21 @@
      * @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);
+        InputStream ps = null;
+        if (sc == null) {
+            ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
+        } else {
+            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);
+                    }
                 }
             }
         }
@@ -370,7 +387,7 @@
     public static String getResourcePath(ServletContext sc, String filename) {
         File f = new File(filename);
         // is the filename absolute?
-        if (!f.isAbsolute()) {
+        if (!f.isAbsolute() && sc != null) {
             // relative path -> use getRealPath to resolve in webapp
             filename = sc.getRealPath(filename);
         }