blob: 1d09d8a6ededf4a80bf7d75b069fbe497582eefe [file] [log] [blame]
hebastae2bd6e42020-03-03 19:07:20 +01001package de.ids_mannheim.korap.plkexport;
2
hebasta68a5bdd2020-03-07 13:44:17 +01003import java.io.File;
4import java.io.FileInputStream;
5import java.io.InputStream;
6import java.util.Properties;
7
hebasta97c0a042020-03-24 21:26:44 +01008import org.eclipse.jetty.server.Handler;
hebastae2bd6e42020-03-03 19:07:20 +01009import org.eclipse.jetty.server.Server;
hebasta68a5bdd2020-03-07 13:44:17 +010010import org.eclipse.jetty.server.ServerConnector;
hebasta97c0a042020-03-24 21:26:44 +010011import org.eclipse.jetty.server.handler.ContextHandler;
12import org.eclipse.jetty.server.handler.DefaultHandler;
13import org.eclipse.jetty.server.handler.HandlerList;
14import org.eclipse.jetty.server.handler.ResourceHandler;
hebastae2bd6e42020-03-03 19:07:20 +010015import org.eclipse.jetty.servlet.ServletContextHandler;
16import org.eclipse.jetty.servlet.ServletHolder;
17
hebastae2bd6e42020-03-03 19:07:20 +010018public class PluginServer {
19 public static void main (String[] args) throws Exception {
20
21
22 ServletContextHandler contextHandler = new ServletContextHandler(
23 ServletContextHandler.NO_SESSIONS);
24 contextHandler.setContextPath("/");
25
hebasta68a5bdd2020-03-07 13:44:17 +010026 Properties properties = new Properties();
27 File f = new File("exportPlugin.conf");
28 InputStream in = null;
29
30 if (!f.exists()) {
31 String rootPath = Thread.currentThread().getContextClassLoader()
32 .getResource("").getPath();
33 String appConfigPath = rootPath + "exportPlugin.conf";
34 in = new FileInputStream(appConfigPath);
35 }
36 else {
37 in = new FileInputStream(f);
38 }
hebasta97c0a042020-03-24 21:26:44 +010039
40
hebasta68a5bdd2020-03-07 13:44:17 +010041 properties.load(in);
42 in.close();
43
44 //Default: Server is available under http://localhost:7070/
45 String portStr = properties.getProperty("server.port", "7070");
46 String host = properties.getProperty("server.host", "localhost");
47 int port = Integer.parseInt(portStr);
48
49 Server jettyServer = new Server();
50 ServerConnector connector = new ServerConnector(jettyServer);
51 connector.setPort(port);
52 connector.setHost(host);
53 connector.setIdleTimeout(60000);
54 jettyServer.addConnector(connector);
hebasta97c0a042020-03-24 21:26:44 +010055
56 ResourceHandler resourceHandler= new ResourceHandler();
57 String resourceBase ="templates";
58 //If server is started as jar-file in target directory
59 if(!new File("templates").exists()) {
60 resourceBase = "../" + resourceBase;
61 }
hebasta68a5bdd2020-03-07 13:44:17 +010062
hebasta97c0a042020-03-24 21:26:44 +010063 resourceHandler.setResourceBase(resourceBase);
64 //enable directory listing
65 resourceHandler.setDirectoriesListed(true);
66 ContextHandler contextHandRes= new ContextHandler("/res");
67 contextHandRes.setHandler(resourceHandler);
68
69 HandlerList handlers = new HandlerList();
70 handlers.setHandlers(new Handler[] { contextHandRes, contextHandler, new DefaultHandler()});
71 jettyServer.setHandler(handlers);
72
hebastae2bd6e42020-03-03 19:07:20 +010073 ServletHolder servletHolder = contextHandler.addServlet(
74 org.glassfish.jersey.servlet.ServletContainer.class, "/*");
75 servletHolder.setInitOrder(0);
76
77 // Tells the Jersey Servlet which REST service/class to load.
78 servletHolder.setInitParameter(
79 "jersey.config.server.provider.classnames",
80 IdsExportService.class.getCanonicalName());
81
82 try {
83 jettyServer.start();
hebasta68a5bdd2020-03-07 13:44:17 +010084 System.out.println("PluginServer available under: http://" + host+ ":" + portStr);
hebastae2bd6e42020-03-03 19:07:20 +010085 jettyServer.join();
86 }
87 finally {
88 jettyServer.destroy();
89 }
90 }
91}