diff src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java @ 72:4c2cea836bc0

restlet 2.1 works now. (it's the start() method, stupid!)
author casties
date Tue, 11 Mar 2014 17:43:00 +0100
parents 326369d4bc4d
children 4b8c909cabf3
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java	Fri Mar 07 13:48:50 2014 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java	Tue Mar 11 17:43:00 2014 +0100
@@ -129,14 +129,14 @@
 
     public static final String ANNOTATIONS_URI_KEY = "annotationmanager.uris.annotations";
 
-    /**
-     * constructor
-     * 
-     * @param context
+
+    /* (non-Javadoc)
+     * @see org.restlet.Application#start()
      */
-    public BaseRestlet(Context context) {
-        super(context);
-        configure(context);
+    @Override
+    public synchronized void start() throws Exception {
+        configure(getContext());
+        super.start();
     }
 
     /**
@@ -146,28 +146,20 @@
      * @param context 
      */
     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 (attrs != null) {
-            if (attrs.get("annotationserver.log4j.configured") == null) {
+        ConcurrentMap<String, Object> attrs = context.getAttributes();
+        ServletContext sc = (ServletContext) attrs.get("org.restlet.ext.servlet.ServletContext");
+        if (sc != null) {
+            if (sc.getAttribute("annotationserver.log4j.configured") == null) {
                 // TODO: is this the right place to run the log4j configurator?
                 BasicConfigurator.configure();
-                attrs.put("annotationserver.log4j.configured", "done");
+                sc.setAttribute("annotationserver.log4j.configured", "done");
             }
             logger.info(getVersion() + " starting...");
 
             /*
              * read config from webapp
              */
-            serverConfig = (Properties) attrs.get(SERVERCONFIG_KEY);
+            serverConfig = (Properties) sc.getAttribute(SERVERCONFIG_KEY);
             if (serverConfig == null) {
                 serverConfig = new Properties();
                 InputStream ps = getResourceAsStream(sc, CONFIG_PROPS_PATH);
@@ -203,10 +195,10 @@
                     logger.error("Unable to get resource " + CONFIG_PROPS_PATH);
                 }
                 // store config
-                attrs.put(SERVERCONFIG_KEY, serverConfig);
+                sc.setAttribute(SERVERCONFIG_KEY, serverConfig);
             }
             // look for database service in context
-            graphDb = (GraphDatabaseService) attrs.get(GRAPHDB_KEY);
+            graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY);
             if (graphDb == null) {
                 /*
                  * open database
@@ -219,27 +211,27 @@
                     graphDb = graphDbBuilder.newGraphDatabase();
                     registerShutdownHook(graphDb);
                     // store in context
-                    attrs.put(GRAPHDB_KEY, graphDb);
+                    sc.setAttribute(GRAPHDB_KEY, graphDb);
                     // AnnotationStore
                     store = new AnnotationStore(graphDb);
-                    attrs.put(ANNSTORE_KEY, store);
+                    sc.setAttribute(ANNSTORE_KEY, store);
                     // admin server
                     srv = new WrappingNeoServerBootstrapper((GraphDatabaseAPI) graphDb);
                     logger.debug("Starting DB admin server...");
                     // store in context
-                    attrs.put(GRAPHDBSRV_KEY, srv);
+                    sc.setAttribute(GRAPHDBSRV_KEY, srv);
                     srv.start();
                 } else {
                     logger.error("Unable to get resource " + dbFn);
                 }
             } else {
                 // get existing AnnotationStore
-                store = (AnnotationStore) attrs.get(ANNSTORE_KEY);
+                store = (AnnotationStore) sc.getAttribute(ANNSTORE_KEY);
             }
             /*
              * read consumerKeys from webapp
              */
-            consumerKeys = (Properties) attrs.get(CONSUMERKEYS_KEY);
+            consumerKeys = (Properties) sc.getAttribute(CONSUMERKEYS_KEY);
             if (consumerKeys == null) {
                 consumerKeys = new Properties();
                 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH);
@@ -256,7 +248,7 @@
                     logger.error("Unable to get resource " + CONSUMER_KEYS_PATH);
                 }
                 // store config
-                attrs.put(CONSUMERKEYS_KEY, consumerKeys);
+                sc.setAttribute(CONSUMERKEYS_KEY, consumerKeys);
             }
         } else {
             logger.error("Unable to get ServletContext!");
@@ -355,19 +347,27 @@
     protected InputStream getResourceAsStream(ServletContext sc, String path) {
         InputStream ps = null;
         if (sc == null) {
+            // no servlet context -> use class loader
             ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
+            /* Request request = new Request(Method.GET, "riap://component/ping");
+               Response response = getContext().getClientDispatcher().handle(request);
+               Representation repr = response.getEntity(); */
         } else {
+            // try path in webapp first
             ps = sc.getResourceAsStream(path);
             if (ps == null) {
                 // try as file
                 File pf = new File(sc.getRealPath(path));
-                if (pf != null) {
+                if (pf.canRead()) {
                     logger.debug("trying file for: " + pf);
                     try {
                         ps = new FileInputStream(pf);
                     } catch (FileNotFoundException e) {
                         logger.error(e);
                     }
+                } else {
+                    // use class loader
+                    ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);                    
                 }
             }
         }