blob: 47f70090a29dbdf110049956f31652c1d25719fa [file] [log] [blame]
Nils Diewald32030a62014-09-03 20:16:50 +00001package de.ids_mannheim.korap;
2
Nils Diewaldf04e1002014-09-24 22:52:59 +00003import java.util.*;
4import java.io.*;
5
Nils Diewald32030a62014-09-03 20:16:50 +00006import org.glassfish.grizzly.http.server.HttpServer;
7import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
8import org.glassfish.jersey.server.ResourceConfig;
9
Nils Diewaldf6b351c2014-09-04 21:34:05 +000010import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
Nils Diewald32030a62014-09-03 20:16:50 +000013import java.net.URI;
Nils Diewaldf04e1002014-09-24 22:52:59 +000014import java.beans.PropertyVetoException;
Nils Diewald32030a62014-09-03 20:16:50 +000015
Nils Diewaldf6b351c2014-09-04 21:34:05 +000016import de.ids_mannheim.korap.KorapIndex;
17import org.apache.lucene.store.MMapDirectory;
18
Nils Diewaldf04e1002014-09-24 22:52:59 +000019import com.mchange.v2.c3p0.*;
Nils Diewaldf6b351c2014-09-04 21:34:05 +000020
Nils Diewald32030a62014-09-03 20:16:50 +000021/**
22 * Standalone REST-Service for the Lucene Search Backend.
23 *
24 * @author Nils Diewald
25 */
26public class KorapNode {
27
28 // Base URI the Grizzly HTTP server will listen on
Nils Diewaldf04e1002014-09-24 22:52:59 +000029 public static String BASE_URI = "http://localhost:8080/";
Nils Diewald32030a62014-09-03 20:16:50 +000030
Nils Diewaldf6b351c2014-09-04 21:34:05 +000031 // Logger
32 private final static Logger log = LoggerFactory.getLogger(KorapNode.class);
33
34 // Index
35 private static KorapIndex index;
Nils Diewaldf04e1002014-09-24 22:52:59 +000036 private static ComboPooledDataSource cpds;
37 private static String path;
38 private static String name = "unknown";
Nils Diewaldf6b351c2014-09-04 21:34:05 +000039
Nils Diewaldf04e1002014-09-24 22:52:59 +000040 private static String dbUser, dbPwd;
Nils Diewaldff6f7662014-09-21 15:08:52 +000041
Nils Diewaldf04e1002014-09-24 22:52:59 +000042 private static String dbClass = "org.sqlite.JDBC";
43 private static String dbURL = "jdbc:sqlite:";
Nils Diewaldf6b351c2014-09-04 21:34:05 +000044
Nils Diewald7cbcfe92014-09-22 22:01:51 +000045 /*
46 * Todo: Add shutdown hook,
Nils Diewaldf04e1002014-09-24 22:52:59 +000047 * Then also close cdps.close();
Nils Diewald7cbcfe92014-09-22 22:01:51 +000048 * see: https://10.0.10.12/trac/korap/browser/KorAP-modules/KorAP-REST/src/main/java/de/ids_mannheim/korap/web/Application.java
49 * https://10.0.10.12/trac/korap/browser/KorAP-modules/KorAP-REST/src/main/java/de/ids_mannheim/korap/web/ShutdownHook.java
50 */
51
Nils Diewald32030a62014-09-03 20:16:50 +000052 /**
53 * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
54 * @return Grizzly HTTP server.
55 */
56 public static HttpServer startServer() {
Nils Diewaldf6b351c2014-09-04 21:34:05 +000057
Nils Diewaldf04e1002014-09-24 22:52:59 +000058 // Load configuration
59 try {
60 InputStream file = new FileInputStream(
61 KorapNode.class.getClassLoader().getResource("server.properties").getFile()
62 );
63 Properties prop = new Properties();
64 prop.load(file);
65
66 // Node properties
67 path = prop.getProperty("lucene.indexDir", path);
68 name = prop.getProperty("lucene.node.name", name);
69 BASE_URI = prop.getProperty("lucene.node.baseURI", BASE_URI);
70
71 // Database properties
72 dbUser = prop.getProperty("lucene.db.user", dbUser);
73 dbPwd = prop.getProperty("lucene.db.pwd", dbPwd);
74 dbClass = prop.getProperty("lucene.db.class", dbClass);
75 dbURL = prop.getProperty("lucene.db.jdbcURL", dbURL);
76
77 }
78 catch (IOException e) {
79 log.error(e.getLocalizedMessage());
80 };
81
Nils Diewald32030a62014-09-03 20:16:50 +000082 // create a resource config that scans for JAX-RS resources and providers
83 // in de.ids_mannheim.korap.server package
Nils Diewaldf6b351c2014-09-04 21:34:05 +000084 final ResourceConfig rc =
85 new ResourceConfig().packages("de.ids_mannheim.korap.server");
Nils Diewald32030a62014-09-03 20:16:50 +000086
87 // create and start a new instance of grizzly http server
88 // exposing the Jersey application at BASE_URI
89 return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
Nils Diewaldff6f7662014-09-21 15:08:52 +000090 };
91
92
93 public static HttpServer startServer(String nodeName, String indexPath) {
94
95 // create a resource config that scans for JAX-RS resources and providers
96 // in de.ids_mannheim.korap.server package
97 final ResourceConfig rc =
98 new ResourceConfig().packages("de.ids_mannheim.korap.server");
99
100 name = nodeName;
101 path = indexPath;
102
103 // create and start a new instance of grizzly http server
104 // exposing the Jersey application at BASE_URI
105 return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
106 };
107
Nils Diewald32030a62014-09-03 20:16:50 +0000108
109 /**
110 * Main method.
111 * @param args
112 * @throws IOException
113 */
114 public static void main(String[] args) throws IOException {
Nils Diewald979b2fe2014-09-29 16:21:41 +0000115 // WADL available at BASE_URI + application.wadl
116
Nils Diewald32030a62014-09-03 20:16:50 +0000117 final HttpServer server = startServer();
Nils Diewald2f44c632014-09-29 19:46:55 +0000118
119 // Establish shutdown hook
120 Runtime.getRuntime().addShutdownHook(
121 new Thread(
122 new Runnable() {
123 @Override
124 public void run() {
125 log.info("Stup Server");
126 // staaahp!
127 server.stop();
128 }
129 },
130 "shutdownHook"
131 )
132 );
133
134 // Start server
135 try {
136 server.start();
137 log.info("You may kill me gently with Ctrl+C");
138 Thread.currentThread().join();
139 }
140 catch (Exception e) {
141 log.error("Unable to start server: {}", e.getLocalizedMessage());
142 };
Nils Diewaldf6b351c2014-09-04 21:34:05 +0000143 };
Nils Diewald32030a62014-09-03 20:16:50 +0000144
Nils Diewald979b2fe2014-09-29 16:21:41 +0000145
146 // What's the servers name?
Nils Diewaldff6f7662014-09-21 15:08:52 +0000147 public static String getName () {
148 return name;
149 };
150
Nils Diewald979b2fe2014-09-29 16:21:41 +0000151
152 // What is the server listening on?
Nils Diewaldf04e1002014-09-24 22:52:59 +0000153 public static String getListener () {
154 return BASE_URI;
155 };
156
Nils Diewald979b2fe2014-09-29 16:21:41 +0000157
158 // Get database pool
Nils Diewaldf04e1002014-09-24 22:52:59 +0000159 public static ComboPooledDataSource getDBPool () {
Nils Diewald979b2fe2014-09-29 16:21:41 +0000160
161 // Pool already initiated
Nils Diewaldf04e1002014-09-24 22:52:59 +0000162 if (cpds != null)
163 return cpds;
164
165 try {
Nils Diewald979b2fe2014-09-29 16:21:41 +0000166
167 // Parameters are defined in the property file
Nils Diewaldf04e1002014-09-24 22:52:59 +0000168 cpds = new ComboPooledDataSource();
169 cpds.setDriverClass(dbClass);
170 cpds.setJdbcUrl(dbURL);
171 if (dbUser != null)
172 cpds.setUser(dbUser);
173 if (dbPwd != null)
174 cpds.setPassword(dbPwd);
175 cpds.setMaxStatements(100);
176 return cpds;
177 }
178 catch (PropertyVetoException e) {
179 log.error(e.getLocalizedMessage());
180 };
181 return null;
182 };
183
184
Nils Diewald979b2fe2014-09-29 16:21:41 +0000185 // Get Lucene Index
Nils Diewaldf6b351c2014-09-04 21:34:05 +0000186 public static KorapIndex getIndex () {
Nils Diewald979b2fe2014-09-29 16:21:41 +0000187
188 // Index already instantiated
Nils Diewaldf6b351c2014-09-04 21:34:05 +0000189 if (index != null)
190 return index;
191
192 try {
Nils Diewald979b2fe2014-09-29 16:21:41 +0000193
194 // Get a temporary index
195 if (path == null)
Nils Diewaldff6f7662014-09-21 15:08:52 +0000196 // Temporary index
197 index = new KorapIndex();
Nils Diewaldf6b351c2014-09-04 21:34:05 +0000198
Nils Diewaldff6f7662014-09-21 15:08:52 +0000199 else {
200 File file = new File(path);
201
202 log.info("Loading index from {}", path);
203 if (!file.exists()) {
204 log.error("Index not found at {}", path);
205 return null;
206 };
207
Nils Diewald979b2fe2014-09-29 16:21:41 +0000208 // Set real index
Nils Diewaldff6f7662014-09-21 15:08:52 +0000209 index = new KorapIndex(new MMapDirectory(file));
Nils Diewaldf6b351c2014-09-04 21:34:05 +0000210 };
Nils Diewaldf6b351c2014-09-04 21:34:05 +0000211 return index;
Nils Diewaldf6b351c2014-09-04 21:34:05 +0000212 }
213 catch (IOException e) {
214 log.error("Index not loadable at {}: {}", path, e.getMessage());
215 };
216 return null;
217 };
218};