| Nils Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap; |
| 2 | |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 3 | import java.util.*; |
| 4 | import java.io.*; |
| 5 | |
| Nils Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 6 | import org.glassfish.grizzly.http.server.HttpServer; |
| 7 | import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; |
| 8 | import org.glassfish.jersey.server.ResourceConfig; |
| 9 | |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 10 | import org.slf4j.Logger; |
| 11 | import org.slf4j.LoggerFactory; |
| 12 | |
| Nils Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 13 | import java.net.URI; |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 14 | import java.beans.PropertyVetoException; |
| Nils Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 15 | |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 16 | import de.ids_mannheim.korap.KorapIndex; |
| 17 | import org.apache.lucene.store.MMapDirectory; |
| 18 | |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 19 | import com.mchange.v2.c3p0.*; |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 20 | |
| Nils Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 21 | /** |
| 22 | * Standalone REST-Service for the Lucene Search Backend. |
| 23 | * |
| 24 | * @author Nils Diewald |
| 25 | */ |
| 26 | public class KorapNode { |
| 27 | |
| 28 | // Base URI the Grizzly HTTP server will listen on |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 29 | public static String BASE_URI = "http://localhost:8080/"; |
| Nils Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 30 | |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 31 | // Logger |
| 32 | private final static Logger log = LoggerFactory.getLogger(KorapNode.class); |
| 33 | |
| 34 | // Index |
| 35 | private static KorapIndex index; |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 36 | private static ComboPooledDataSource cpds; |
| 37 | private static String path; |
| 38 | private static String name = "unknown"; |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 39 | |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 40 | private static String dbUser, dbPwd; |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 41 | |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 42 | private static String dbClass = "org.sqlite.JDBC"; |
| 43 | private static String dbURL = "jdbc:sqlite:"; |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 44 | |
| Nils Diewald | 7cbcfe9 | 2014-09-22 22:01:51 +0000 | [diff] [blame] | 45 | /* |
| 46 | * Todo: Add shutdown hook, |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 47 | * Then also close cdps.close(); |
| Nils Diewald | 7cbcfe9 | 2014-09-22 22:01:51 +0000 | [diff] [blame] | 48 | * 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 Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 52 | /** |
| 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 Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 57 | |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 58 | // 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 Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 82 | // create a resource config that scans for JAX-RS resources and providers |
| 83 | // in de.ids_mannheim.korap.server package |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 84 | final ResourceConfig rc = |
| 85 | new ResourceConfig().packages("de.ids_mannheim.korap.server"); |
| Nils Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 86 | |
| 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 Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 90 | }; |
| 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 Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Main method. |
| 111 | * @param args |
| 112 | * @throws IOException |
| 113 | */ |
| 114 | public static void main(String[] args) throws IOException { |
| Nils Diewald | 979b2fe | 2014-09-29 16:21:41 +0000 | [diff] [blame] | 115 | // WADL available at BASE_URI + application.wadl |
| 116 | |
| Nils Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 117 | final HttpServer server = startServer(); |
| Nils Diewald | 2f44c63 | 2014-09-29 19:46:55 +0000 | [diff] [blame] | 118 | |
| 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 Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 143 | }; |
| Nils Diewald | 32030a6 | 2014-09-03 20:16:50 +0000 | [diff] [blame] | 144 | |
| Nils Diewald | 979b2fe | 2014-09-29 16:21:41 +0000 | [diff] [blame] | 145 | |
| 146 | // What's the servers name? |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 147 | public static String getName () { |
| 148 | return name; |
| 149 | }; |
| 150 | |
| Nils Diewald | 979b2fe | 2014-09-29 16:21:41 +0000 | [diff] [blame] | 151 | |
| 152 | // What is the server listening on? |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 153 | public static String getListener () { |
| 154 | return BASE_URI; |
| 155 | }; |
| 156 | |
| Nils Diewald | 979b2fe | 2014-09-29 16:21:41 +0000 | [diff] [blame] | 157 | |
| 158 | // Get database pool |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 159 | public static ComboPooledDataSource getDBPool () { |
| Nils Diewald | 979b2fe | 2014-09-29 16:21:41 +0000 | [diff] [blame] | 160 | |
| 161 | // Pool already initiated |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 162 | if (cpds != null) |
| 163 | return cpds; |
| 164 | |
| 165 | try { |
| Nils Diewald | 979b2fe | 2014-09-29 16:21:41 +0000 | [diff] [blame] | 166 | |
| 167 | // Parameters are defined in the property file |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 168 | 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 Diewald | 979b2fe | 2014-09-29 16:21:41 +0000 | [diff] [blame] | 185 | // Get Lucene Index |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 186 | public static KorapIndex getIndex () { |
| Nils Diewald | 979b2fe | 2014-09-29 16:21:41 +0000 | [diff] [blame] | 187 | |
| 188 | // Index already instantiated |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 189 | if (index != null) |
| 190 | return index; |
| 191 | |
| 192 | try { |
| Nils Diewald | 979b2fe | 2014-09-29 16:21:41 +0000 | [diff] [blame] | 193 | |
| 194 | // Get a temporary index |
| 195 | if (path == null) |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 196 | // Temporary index |
| 197 | index = new KorapIndex(); |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 198 | |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 199 | 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 Diewald | 979b2fe | 2014-09-29 16:21:41 +0000 | [diff] [blame] | 208 | // Set real index |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 209 | index = new KorapIndex(new MMapDirectory(file)); |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 210 | }; |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 211 | return index; |
| Nils Diewald | f6b351c | 2014-09-04 21:34:05 +0000 | [diff] [blame] | 212 | } |
| 213 | catch (IOException e) { |
| 214 | log.error("Index not loadable at {}: {}", path, e.getMessage()); |
| 215 | }; |
| 216 | return null; |
| 217 | }; |
| 218 | }; |