comparison 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
comparison
equal deleted inserted replaced
71:326369d4bc4d 72:4c2cea836bc0
127 public static String GROUPS_URI_PREFIX = ""; 127 public static String GROUPS_URI_PREFIX = "";
128 public static final String GROUPS_URI_KEY = "annotationmanager.uris.groups"; 128 public static final String GROUPS_URI_KEY = "annotationmanager.uris.groups";
129 129
130 public static final String ANNOTATIONS_URI_KEY = "annotationmanager.uris.annotations"; 130 public static final String ANNOTATIONS_URI_KEY = "annotationmanager.uris.annotations";
131 131
132 /** 132
133 * constructor 133 /* (non-Javadoc)
134 * 134 * @see org.restlet.Application#start()
135 * @param context 135 */
136 */ 136 @Override
137 public BaseRestlet(Context context) { 137 public synchronized void start() throws Exception {
138 super(context); 138 configure(getContext());
139 configure(context); 139 super.start();
140 } 140 }
141 141
142 /** 142 /**
143 * Configures the restlet. 143 * Configures the restlet.
144 * 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.
145 * Uses config from webapp context if already initialized. 145 * Uses config from webapp context if already initialized.
146 * @param context 146 * @param context
147 */ 147 */
148 protected void configure(Context context) { 148 protected void configure(Context context) {
149 Context ctx = Context.getCurrent(); 149 ConcurrentMap<String, Object> attrs = context.getAttributes();
150 ConcurrentMap<String, Object> attrs = ctx.getAttributes(); 150 ServletContext sc = (ServletContext) attrs.get("org.restlet.ext.servlet.ServletContext");
151 ServletContext sc = null; 151 if (sc != null) {
152 if (context != null) { 152 if (sc.getAttribute("annotationserver.log4j.configured") == 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()
157 .get("org.restlet.ext.servlet.ServletContext");
158 }
159 if (attrs != null) {
160 if (attrs.get("annotationserver.log4j.configured") == null) {
161 // TODO: is this the right place to run the log4j configurator? 153 // TODO: is this the right place to run the log4j configurator?
162 BasicConfigurator.configure(); 154 BasicConfigurator.configure();
163 attrs.put("annotationserver.log4j.configured", "done"); 155 sc.setAttribute("annotationserver.log4j.configured", "done");
164 } 156 }
165 logger.info(getVersion() + " starting..."); 157 logger.info(getVersion() + " starting...");
166 158
167 /* 159 /*
168 * read config from webapp 160 * read config from webapp
169 */ 161 */
170 serverConfig = (Properties) attrs.get(SERVERCONFIG_KEY); 162 serverConfig = (Properties) sc.getAttribute(SERVERCONFIG_KEY);
171 if (serverConfig == null) { 163 if (serverConfig == null) {
172 serverConfig = new Properties(); 164 serverConfig = new Properties();
173 InputStream ps = getResourceAsStream(sc, CONFIG_PROPS_PATH); 165 InputStream ps = getResourceAsStream(sc, CONFIG_PROPS_PATH);
174 if (ps != null) { 166 if (ps != null) {
175 logger.debug("loading config from " + CONFIG_PROPS_PATH); 167 logger.debug("loading config from " + CONFIG_PROPS_PATH);
201 logger.debug("config: " + serverConfig); 193 logger.debug("config: " + serverConfig);
202 } else { 194 } else {
203 logger.error("Unable to get resource " + CONFIG_PROPS_PATH); 195 logger.error("Unable to get resource " + CONFIG_PROPS_PATH);
204 } 196 }
205 // store config 197 // store config
206 attrs.put(SERVERCONFIG_KEY, serverConfig); 198 sc.setAttribute(SERVERCONFIG_KEY, serverConfig);
207 } 199 }
208 // look for database service in context 200 // look for database service in context
209 graphDb = (GraphDatabaseService) attrs.get(GRAPHDB_KEY); 201 graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY);
210 if (graphDb == null) { 202 if (graphDb == null) {
211 /* 203 /*
212 * open database 204 * open database
213 */ 205 */
214 String dbFn = getResourcePath(sc, graphdbPath); 206 String dbFn = getResourcePath(sc, graphdbPath);
217 GraphDatabaseBuilder graphDbBuilder = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(dbFn); 209 GraphDatabaseBuilder graphDbBuilder = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(dbFn);
218 graphDbBuilder.setConfig(GraphDatabaseSettings.allow_store_upgrade, "true"); 210 graphDbBuilder.setConfig(GraphDatabaseSettings.allow_store_upgrade, "true");
219 graphDb = graphDbBuilder.newGraphDatabase(); 211 graphDb = graphDbBuilder.newGraphDatabase();
220 registerShutdownHook(graphDb); 212 registerShutdownHook(graphDb);
221 // store in context 213 // store in context
222 attrs.put(GRAPHDB_KEY, graphDb); 214 sc.setAttribute(GRAPHDB_KEY, graphDb);
223 // AnnotationStore 215 // AnnotationStore
224 store = new AnnotationStore(graphDb); 216 store = new AnnotationStore(graphDb);
225 attrs.put(ANNSTORE_KEY, store); 217 sc.setAttribute(ANNSTORE_KEY, store);
226 // admin server 218 // admin server
227 srv = new WrappingNeoServerBootstrapper((GraphDatabaseAPI) graphDb); 219 srv = new WrappingNeoServerBootstrapper((GraphDatabaseAPI) graphDb);
228 logger.debug("Starting DB admin server..."); 220 logger.debug("Starting DB admin server...");
229 // store in context 221 // store in context
230 attrs.put(GRAPHDBSRV_KEY, srv); 222 sc.setAttribute(GRAPHDBSRV_KEY, srv);
231 srv.start(); 223 srv.start();
232 } else { 224 } else {
233 logger.error("Unable to get resource " + dbFn); 225 logger.error("Unable to get resource " + dbFn);
234 } 226 }
235 } else { 227 } else {
236 // get existing AnnotationStore 228 // get existing AnnotationStore
237 store = (AnnotationStore) attrs.get(ANNSTORE_KEY); 229 store = (AnnotationStore) sc.getAttribute(ANNSTORE_KEY);
238 } 230 }
239 /* 231 /*
240 * read consumerKeys from webapp 232 * read consumerKeys from webapp
241 */ 233 */
242 consumerKeys = (Properties) attrs.get(CONSUMERKEYS_KEY); 234 consumerKeys = (Properties) sc.getAttribute(CONSUMERKEYS_KEY);
243 if (consumerKeys == null) { 235 if (consumerKeys == null) {
244 consumerKeys = new Properties(); 236 consumerKeys = new Properties();
245 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH); 237 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH);
246 if (ps != null) { 238 if (ps != null) {
247 logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH); 239 logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH);
254 logger.debug("consumer keys: " + consumerKeys); 246 logger.debug("consumer keys: " + consumerKeys);
255 } else { 247 } else {
256 logger.error("Unable to get resource " + CONSUMER_KEYS_PATH); 248 logger.error("Unable to get resource " + CONSUMER_KEYS_PATH);
257 } 249 }
258 // store config 250 // store config
259 attrs.put(CONSUMERKEYS_KEY, consumerKeys); 251 sc.setAttribute(CONSUMERKEYS_KEY, consumerKeys);
260 } 252 }
261 } else { 253 } else {
262 logger.error("Unable to get ServletContext!"); 254 logger.error("Unable to get ServletContext!");
263 } 255 }
264 } 256 }
353 * @return 345 * @return
354 */ 346 */
355 protected InputStream getResourceAsStream(ServletContext sc, String path) { 347 protected InputStream getResourceAsStream(ServletContext sc, String path) {
356 InputStream ps = null; 348 InputStream ps = null;
357 if (sc == null) { 349 if (sc == null) {
350 // no servlet context -> use class loader
358 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); 351 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
352 /* Request request = new Request(Method.GET, "riap://component/ping");
353 Response response = getContext().getClientDispatcher().handle(request);
354 Representation repr = response.getEntity(); */
359 } else { 355 } else {
356 // try path in webapp first
360 ps = sc.getResourceAsStream(path); 357 ps = sc.getResourceAsStream(path);
361 if (ps == null) { 358 if (ps == null) {
362 // try as file 359 // try as file
363 File pf = new File(sc.getRealPath(path)); 360 File pf = new File(sc.getRealPath(path));
364 if (pf != null) { 361 if (pf.canRead()) {
365 logger.debug("trying file for: " + pf); 362 logger.debug("trying file for: " + pf);
366 try { 363 try {
367 ps = new FileInputStream(pf); 364 ps = new FileInputStream(pf);
368 } catch (FileNotFoundException e) { 365 } catch (FileNotFoundException e) {
369 logger.error(e); 366 logger.error(e);
370 } 367 }
368 } else {
369 // use class loader
370 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
371 } 371 }
372 } 372 }
373 } 373 }
374 return ps; 374 return ps;
375 } 375 }