comparison 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
comparison
equal deleted inserted replaced
70:2b1e6df5e21a 71:326369d4bc4d
28 import java.io.FileNotFoundException; 28 import java.io.FileNotFoundException;
29 import java.io.IOException; 29 import java.io.IOException;
30 import java.io.InputStream; 30 import java.io.InputStream;
31 import java.util.Hashtable; 31 import java.util.Hashtable;
32 import java.util.Properties; 32 import java.util.Properties;
33 import java.util.concurrent.ConcurrentMap;
33 34
34 import javax.naming.NamingEnumeration; 35 import javax.naming.NamingEnumeration;
35 import javax.naming.NamingException; 36 import javax.naming.NamingException;
36 import javax.naming.directory.Attribute; 37 import javax.naming.directory.Attribute;
37 import javax.naming.directory.DirContext; 38 import javax.naming.directory.DirContext;
48 import org.neo4j.graphdb.factory.GraphDatabaseSettings; 49 import org.neo4j.graphdb.factory.GraphDatabaseSettings;
49 import org.neo4j.kernel.GraphDatabaseAPI; 50 import org.neo4j.kernel.GraphDatabaseAPI;
50 import org.neo4j.server.WrappingNeoServerBootstrapper; 51 import org.neo4j.server.WrappingNeoServerBootstrapper;
51 import org.restlet.Application; 52 import org.restlet.Application;
52 import org.restlet.Context; 53 import org.restlet.Context;
54 import org.restlet.Restlet;
55 import org.restlet.data.LocalReference;
53 56
54 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 57 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
55 58
56 public abstract class BaseRestlet extends Application { 59 public abstract class BaseRestlet extends Application {
57 60
59 62
60 /** 63 /**
61 * Properties holding consumer keys and secrets. 64 * Properties holding consumer keys and secrets.
62 */ 65 */
63 protected Properties consumerKeys; 66 protected Properties consumerKeys;
64 public String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property"; 67 public String CONSUMER_KEYS_PATH = "consumerkeys.property";
65 public static final String CONSUMERKEYS_KEY = "annotationmanager.consumerkeys"; 68 public static final String CONSUMERKEYS_KEY = "annotationmanager.consumerkeys";
66 69
67 /** 70 /**
68 * Properties holding server config. 71 * Properties holding server config.
69 */ 72 */
70 protected Properties serverConfig; 73 protected Properties serverConfig;
71 public String CONFIG_PROPS_PATH = "WEB-INF/serverconfig.property"; 74 public String CONFIG_PROPS_PATH = "serverconfig.property";
72 public static final String SERVERCONFIG_KEY = "annotationmanager.serverconfig"; 75 public static final String SERVERCONFIG_KEY = "annotationmanager.serverconfig";
73 76
74 /** 77 /**
75 * database instance; 78 * database instance;
76 */ 79 */
77 protected GraphDatabaseService graphDb; 80 protected GraphDatabaseService graphDb;
78 public static final String GRAPHDB_KEY = "annotationmanager.graphdb"; 81 public static final String GRAPHDB_KEY = "annotationmanager.graphdb";
79 public static final String GRAPHDB_PATH_KEY = "annotationmanager.graphdb.path"; 82 public static final String GRAPHDB_PATH_KEY = "annotationmanager.graphdb.path";
80 public String graphdbPath = "WEB-INF/neo4j-annotation-db"; 83 public String graphdbPath = "neo4j-annotation-db";
81 84
82 /** 85 /**
83 * database interface server instance. 86 * database interface server instance.
84 */ 87 */
85 protected WrappingNeoServerBootstrapper srv; 88 protected WrappingNeoServerBootstrapper srv;
131 * 134 *
132 * @param context 135 * @param context
133 */ 136 */
134 public BaseRestlet(Context context) { 137 public BaseRestlet(Context context) {
135 super(context); 138 super(context);
136 configure(); 139 configure(context);
137 } 140 }
138 141
139 /** 142 /**
140 * Configures the restlet. 143 * Configures the restlet.
141 * Reads serverConfig, consumerKeys and graphDb config from config files and starts graphDb. 144 * Reads serverConfig, consumerKeys and graphDb config from config files and starts graphDb.
142 * Uses config from webapp context if already initialized. 145 * Uses config from webapp context if already initialized.
143 */ 146 * @param context
144 protected void configure() { 147 */
145 ServletContext sc = (ServletContext) getContext().getServerDispatcher().getContext().getAttributes() 148 protected void configure(Context context) {
149 Context ctx = Context.getCurrent();
150 ConcurrentMap<String, Object> attrs = ctx.getAttributes();
151 ServletContext sc = null;
152 if (context != null) {
153 Restlet a = context.getServerDispatcher();
154 Context b = a.getContext();
155 ConcurrentMap<String, Object> c = b.getAttributes();
156 sc = (ServletContext) context.getServerDispatcher().getContext().getAttributes()
146 .get("org.restlet.ext.servlet.ServletContext"); 157 .get("org.restlet.ext.servlet.ServletContext");
147 if (sc != null) { 158 }
148 if (sc.getAttribute("annotationserver.log4j.configured") == null) { 159 if (attrs != null) {
160 if (attrs.get("annotationserver.log4j.configured") == null) {
149 // TODO: is this the right place to run the log4j configurator? 161 // TODO: is this the right place to run the log4j configurator?
150 BasicConfigurator.configure(); 162 BasicConfigurator.configure();
151 sc.setAttribute("annotationserver.log4j.configured", "done"); 163 attrs.put("annotationserver.log4j.configured", "done");
152 } 164 }
153 logger.info(getVersion() + " starting..."); 165 logger.info(getVersion() + " starting...");
154 166
155 /* 167 /*
156 * read config from webapp 168 * read config from webapp
157 */ 169 */
158 serverConfig = (Properties) sc.getAttribute(SERVERCONFIG_KEY); 170 serverConfig = (Properties) attrs.get(SERVERCONFIG_KEY);
159 if (serverConfig == null) { 171 if (serverConfig == null) {
160 serverConfig = new Properties(); 172 serverConfig = new Properties();
161 InputStream ps = getResourceAsStream(sc, CONFIG_PROPS_PATH); 173 InputStream ps = getResourceAsStream(sc, CONFIG_PROPS_PATH);
162 if (ps != null) { 174 if (ps != null) {
163 logger.debug("loading config from " + CONFIG_PROPS_PATH); 175 logger.debug("loading config from " + CONFIG_PROPS_PATH);
189 logger.debug("config: " + serverConfig); 201 logger.debug("config: " + serverConfig);
190 } else { 202 } else {
191 logger.error("Unable to get resource " + CONFIG_PROPS_PATH); 203 logger.error("Unable to get resource " + CONFIG_PROPS_PATH);
192 } 204 }
193 // store config 205 // store config
194 sc.setAttribute(SERVERCONFIG_KEY, serverConfig); 206 attrs.put(SERVERCONFIG_KEY, serverConfig);
195 } 207 }
196 // look for database service in context 208 // look for database service in context
197 graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY); 209 graphDb = (GraphDatabaseService) attrs.get(GRAPHDB_KEY);
198 if (graphDb == null) { 210 if (graphDb == null) {
199 /* 211 /*
200 * open database 212 * open database
201 */ 213 */
202 String dbFn = getResourcePath(sc, graphdbPath); 214 String dbFn = getResourcePath(sc, graphdbPath);
205 GraphDatabaseBuilder graphDbBuilder = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(dbFn); 217 GraphDatabaseBuilder graphDbBuilder = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(dbFn);
206 graphDbBuilder.setConfig(GraphDatabaseSettings.allow_store_upgrade, "true"); 218 graphDbBuilder.setConfig(GraphDatabaseSettings.allow_store_upgrade, "true");
207 graphDb = graphDbBuilder.newGraphDatabase(); 219 graphDb = graphDbBuilder.newGraphDatabase();
208 registerShutdownHook(graphDb); 220 registerShutdownHook(graphDb);
209 // store in context 221 // store in context
210 sc.setAttribute(GRAPHDB_KEY, graphDb); 222 attrs.put(GRAPHDB_KEY, graphDb);
211 // AnnotationStore 223 // AnnotationStore
212 store = new AnnotationStore(graphDb); 224 store = new AnnotationStore(graphDb);
213 sc.setAttribute(ANNSTORE_KEY, store); 225 attrs.put(ANNSTORE_KEY, store);
214 // admin server 226 // admin server
215 srv = new WrappingNeoServerBootstrapper((GraphDatabaseAPI) graphDb); 227 srv = new WrappingNeoServerBootstrapper((GraphDatabaseAPI) graphDb);
216 logger.debug("Starting DB admin server..."); 228 logger.debug("Starting DB admin server...");
217 // store in context 229 // store in context
218 sc.setAttribute(GRAPHDBSRV_KEY, srv); 230 attrs.put(GRAPHDBSRV_KEY, srv);
219 srv.start(); 231 srv.start();
220 } else { 232 } else {
221 logger.error("Unable to get resource " + dbFn); 233 logger.error("Unable to get resource " + dbFn);
222 } 234 }
223 } else { 235 } else {
224 // get existing AnnotationStore 236 // get existing AnnotationStore
225 store = (AnnotationStore) sc.getAttribute(ANNSTORE_KEY); 237 store = (AnnotationStore) attrs.get(ANNSTORE_KEY);
226 } 238 }
227 /* 239 /*
228 * read consumerKeys from webapp 240 * read consumerKeys from webapp
229 */ 241 */
230 consumerKeys = (Properties) sc.getAttribute(CONSUMERKEYS_KEY); 242 consumerKeys = (Properties) attrs.get(CONSUMERKEYS_KEY);
231 if (consumerKeys == null) { 243 if (consumerKeys == null) {
232 consumerKeys = new Properties(); 244 consumerKeys = new Properties();
233 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH); 245 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH);
234 if (ps != null) { 246 if (ps != null) {
235 logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH); 247 logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH);
242 logger.debug("consumer keys: " + consumerKeys); 254 logger.debug("consumer keys: " + consumerKeys);
243 } else { 255 } else {
244 logger.error("Unable to get resource " + CONSUMER_KEYS_PATH); 256 logger.error("Unable to get resource " + CONSUMER_KEYS_PATH);
245 } 257 }
246 // store config 258 // store config
247 sc.setAttribute(CONSUMERKEYS_KEY, consumerKeys); 259 attrs.put(CONSUMERKEYS_KEY, consumerKeys);
248 } 260 }
249 } else { 261 } else {
250 logger.error("Unable to get ServletContext!"); 262 logger.error("Unable to get ServletContext!");
251 } 263 }
252 } 264 }
339 * @param sc 351 * @param sc
340 * @param path 352 * @param path
341 * @return 353 * @return
342 */ 354 */
343 protected InputStream getResourceAsStream(ServletContext sc, String path) { 355 protected InputStream getResourceAsStream(ServletContext sc, String path) {
344 InputStream ps = sc.getResourceAsStream(path); 356 InputStream ps = null;
345 if (ps == null) { 357 if (sc == null) {
346 // try as file 358 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
347 File pf = new File(sc.getRealPath(path)); 359 } else {
348 if (pf != null) { 360 ps = sc.getResourceAsStream(path);
349 logger.debug("trying file for: " + pf); 361 if (ps == null) {
350 try { 362 // try as file
351 ps = new FileInputStream(pf); 363 File pf = new File(sc.getRealPath(path));
352 } catch (FileNotFoundException e) { 364 if (pf != null) {
353 logger.error(e); 365 logger.debug("trying file for: " + pf);
366 try {
367 ps = new FileInputStream(pf);
368 } catch (FileNotFoundException e) {
369 logger.error(e);
370 }
354 } 371 }
355 } 372 }
356 } 373 }
357 return ps; 374 return ps;
358 } 375 }
368 * @return 385 * @return
369 */ 386 */
370 public static String getResourcePath(ServletContext sc, String filename) { 387 public static String getResourcePath(ServletContext sc, String filename) {
371 File f = new File(filename); 388 File f = new File(filename);
372 // is the filename absolute? 389 // is the filename absolute?
373 if (!f.isAbsolute()) { 390 if (!f.isAbsolute() && sc != null) {
374 // relative path -> use getRealPath to resolve in webapp 391 // relative path -> use getRealPath to resolve in webapp
375 filename = sc.getRealPath(filename); 392 filename = sc.getRealPath(filename);
376 } 393 }
377 return filename; 394 return filename;
378 } 395 }