annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java @ 4:3599b29c393f

store seems to work now :-)
author casties
date Mon, 02 Jul 2012 22:39:46 +0200
parents 47b53ae385d1
children bbf0cc5bee29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
1 package de.mpiwg.itgroup.annotations.restlet;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
2
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
3 import java.io.File;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
4 import java.io.FileInputStream;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
5 import java.io.FileNotFoundException;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
6 import java.io.IOException;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
7 import java.io.InputStream;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
8 import java.util.Hashtable;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
9 import java.util.Properties;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
10
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
11 import javax.naming.NamingEnumeration;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
12 import javax.naming.NamingException;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
13 import javax.naming.directory.Attribute;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
14 import javax.naming.directory.DirContext;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
15 import javax.naming.directory.InitialDirContext;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
16 import javax.naming.directory.SearchControls;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
17 import javax.naming.directory.SearchResult;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
18 import javax.servlet.ServletContext;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
19
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
20 import org.apache.log4j.BasicConfigurator;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
21 import org.apache.log4j.Logger;
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
22 import org.neo4j.graphdb.GraphDatabaseService;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
23 import org.neo4j.graphdb.factory.GraphDatabaseFactory;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
24 import org.neo4j.kernel.AbstractGraphDatabase;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
25 import org.neo4j.server.WrappingNeoServerBootstrapper;
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
26 import org.restlet.Application;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
27 import org.restlet.Context;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
28 import org.restlet.Restlet;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
29 import org.restlet.routing.Router;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
30
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
31 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
32
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
33 public class RestServer extends Application {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
34
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
35 public static Logger logger = Logger.getLogger(RestServer.class);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
36
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
37 /**
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
38 * Properties holding consumer keys and secrets
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
39 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
40 private Properties consumerKeys;
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
41 public String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property";
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
42
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
43 private GraphDatabaseService graphDb;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
44 public static final String GRAPHDB_KEY = "annotationmanager.graphdb";
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
45 public String DB_PATH = "WEB-INF/neo4j-annotation-db";
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
46
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
47 private WrappingNeoServerBootstrapper srv;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
48 public static final String GRAPHDBSRV_KEY = "annotationmanager.graphdb.srv";
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
49
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
50 private AnnotationStore store;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
51 public static final String ANNSTORE_KEY = "annotationmanager.store";
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
52
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
53 /**
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
54 * constructor
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
55 *
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
56 * @param parentContext
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
57 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
58 public RestServer(Context parentContext) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
59 super(parentContext);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
60 // TODO: is this the right place to run the log4j configurator?
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
61 BasicConfigurator.configure();
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
62
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
63 ServletContext sc = (ServletContext) getContext().getServerDispatcher().getContext().getAttributes()
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
64 .get("org.restlet.ext.servlet.ServletContext");
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
65 if (sc != null) {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
66 // look for database service in context
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
67 graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
68 if (graphDb == null) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
69 /*
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
70 * open database
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
71 */
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
72 String dbFn = getResourcePath(sc, DB_PATH);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
73 if (dbFn != null) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
74 logger.debug("opening DB " + dbFn);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
75 graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(dbFn);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
76 registerShutdownHook(graphDb);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
77 // store in context
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
78 sc.setAttribute(GRAPHDB_KEY, graphDb);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
79 // AnnotationStore
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
80 store = new AnnotationStore(graphDb);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
81 sc.setAttribute(ANNSTORE_KEY, store);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
82 // admin server
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
83 srv = new WrappingNeoServerBootstrapper((AbstractGraphDatabase) graphDb);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
84 logger.debug("Starting DB admin server...");
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
85 // store in context
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
86 sc.setAttribute(GRAPHDBSRV_KEY, srv);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
87 srv.start();
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
88 } else {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
89 logger.error("Unable to get resource " + DB_PATH);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
90 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
91 }
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
92 /*
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
93 * read consumerKeys from webapp
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
94 */
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
95 consumerKeys = new Properties();
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
96 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
97 if (ps != null) {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
98 logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
99 try {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
100 consumerKeys.load(ps);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
101 } catch (IOException e) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
102 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
103 e.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
104 }
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
105 logger.debug("consumer keys: " + consumerKeys);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
106 } else {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
107 logger.error("Unable to get resource " + CONSUMER_KEYS_PATH);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
108 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
109 } else {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
110 logger.error("Unable to get ServletContext!");
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
111 }
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
112
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
113 }
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
114
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
115 /**
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
116 * returns consumer secret for consumer key. returns null if consumer key
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
117 * doesn't exist.
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
118 *
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
119 * @param consumerKey
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
120 * @return
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
121 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
122 public String getConsumerSecret(String consumerKey) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
123 return consumerKeys.getProperty(consumerKey);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
124 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
125
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
126 /*
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
127 * (non-Javadoc)
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
128 *
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
129 * @see org.restlet.Application#createInboundRoot()
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
130 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
131 @Override
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
132 public Restlet createInboundRoot() {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
133 // this.authenticator = createAuthenticator();
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
134
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
135 Router router = new Router(getContext());
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
136
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
137 router.attach("/annotator/annotations", AnnotatorAnnotations.class);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
138 router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
139 router.attach("/annotator/search", AnnotatorSearch.class);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
140
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
141 // router.attach("",redirector); router.attach("/annotator",
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
142 // ExtendedAnnotationInput.class);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
143
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
144 router.attach("/", AnnotatorInfo.class);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
145 // authenticator.setNext(router);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
146 // return authenticator;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
147
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
148 return router;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
149 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
150
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
151 /**
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
152 * Hole den vollen Benutzernamen aus dem LDAP
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
153 *
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
154 * @param creator
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
155 * @return
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
156 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
157 public String getUserNameFromLdap(String creator) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
158 String retString = creator; // falls nichts gefunden wird einfach den
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
159 // creator zurueckgeben
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
160 Hashtable<String, String> env = new Hashtable<String, String>();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
161 String sp = "com.sun.jndi.ldap.LdapCtxFactory";
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
162 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
163
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
164 // TODO: should go into config file
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
165 String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de";
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
166 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
167
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
168 DirContext dctx;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
169 try {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
170 dctx = new InitialDirContext(env);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
171 } catch (NamingException e1) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
172 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
173 e1.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
174 return retString;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
175 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
176
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
177 String base = "ou=People";
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
178
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
179 SearchControls sc = new SearchControls();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
180 String[] attributeFilter = { "cn", "mail" };
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
181 sc.setReturningAttributes(attributeFilter);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
182 sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
183
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
184 String filter = "(uid=" + creator + ")";
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
185
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
186 try {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
187 NamingEnumeration<SearchResult> results = dctx.search(base, filter, sc);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
188 while (results.hasMore()) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
189 SearchResult sr = (SearchResult) results.next();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
190 javax.naming.directory.Attributes attrs = sr.getAttributes();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
191
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
192 Attribute attr = attrs.get("cn");
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
193 retString = (String) attr.get();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
194 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
195 } catch (NamingException e) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
196 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
197 e.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
198 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
199
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
200 try {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
201 dctx.close();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
202 } catch (NamingException e) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
203 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
204 e.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
205 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
206 return retString;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
207 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
208
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
209 /**
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
210 * returns resource from path (in webapp) as InputStream.
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
211 *
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
212 * @param sc
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
213 * @param path
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
214 * @return
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
215 */
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
216 protected InputStream getResourceAsStream(ServletContext sc, String path) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
217 InputStream ps = sc.getResourceAsStream(path);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
218 if (ps == null) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
219 // try as file
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
220 File pf = new File(sc.getRealPath(path));
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
221 if (pf != null) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
222 logger.debug("trying file for: " + pf);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
223 try {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
224 ps = new FileInputStream(pf);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
225 } catch (FileNotFoundException e) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
226 logger.error(e);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
227 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
228 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
229 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
230 return ps;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
231 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
232
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
233 /**
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
234 * get a real file name for a web app file pathname.
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
235 *
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
236 * If filename starts with "/" its treated as absolute else the path is
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
237 * appended to the base directory of the web-app.
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
238 *
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
239 * @param filename
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
240 * @param sc
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
241 * @return
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
242 */
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
243 public static String getResourcePath(ServletContext sc, String filename) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
244 File f = new File(filename);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
245 // is the filename absolute?
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
246 if (!f.isAbsolute()) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
247 // relative path -> use getRealPath to resolve in webapp
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
248 filename = sc.getRealPath(filename);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
249 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
250 return filename;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
251 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
252
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
253 /* (non-Javadoc)
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
254 * @see org.restlet.Application#stop()
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
255 */
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
256 @Override
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
257 public synchronized void stop() throws Exception {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
258 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
259 * trying to clean up databases, nur sure if this is the right way...
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
260 */
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
261 if (srv != null) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
262 logger.debug("Stopping DB admin server...");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
263 srv.stop();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
264 srv = null;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
265 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
266 if (graphDb != null) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
267 logger.debug("Stopping DB...");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
268 graphDb.shutdown();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
269 graphDb = null;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
270 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
271 super.stop();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
272 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
273
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
274 private static void registerShutdownHook(final GraphDatabaseService graphDb) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
275 // Registers a shutdown hook for the Neo4j instance so that it
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
276 // shuts down nicely when the VM exits (even if you "Ctrl-C" the
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
277 // running example before it's completed)
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
278 Runtime.getRuntime().addShutdownHook(new Thread() {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
279 @Override
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
280 public void run() {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
281 graphDb.shutdown();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
282 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
283 });
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
284 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
285
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
286
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
287 }