1 package org.simantics.history.rest;
3 import java.util.EnumSet;
4 import java.util.concurrent.Semaphore;
6 import javax.servlet.DispatcherType;
8 import org.eclipse.jetty.server.Connector;
9 import org.eclipse.jetty.server.Server;
10 import org.eclipse.jetty.server.ServerConnector;
11 import org.eclipse.jetty.servlet.FilterHolder;
12 import org.eclipse.jetty.servlet.ServletContextHandler;
13 import org.eclipse.jetty.servlet.ServletHolder;
14 import org.glassfish.jersey.jackson.JacksonFeature;
15 import org.glassfish.jersey.server.ResourceConfig;
16 import org.glassfish.jersey.servlet.ServletContainer;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public class HistoryRestServer {
21 private static final Logger LOGGER = LoggerFactory.getLogger(HistoryRestServer.class);
23 private static HistoryRestServer INSTANCE = null;
24 private static Server server;
25 private static ServiceServerThread serverThread;
27 private static String allowOriginUrl = null;
29 private static String address = null;
31 private HistoryRestServer(int port) {
32 ResourceConfig config = new ResourceConfig();
33 // JSON serialization/deserialization
34 config.register(JacksonFeature.class);
36 config.register(HistoryRestApi.class);
38 ServletHolder holder = new ServletHolder(new ServletContainer(config));
40 server = new Server();
41 ServerConnector connector = new ServerConnector(server);
42 connector.setPort(port);
44 server.setConnectors(new Connector[] { connector });
46 ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
47 context.addServlet(holder, "/*");
49 if (allowOriginUrl != null){
50 org.eclipse.jetty.servlets.CrossOriginFilter cfilter = new org.eclipse.jetty.servlets.CrossOriginFilter();
51 FilterHolder filterHolder = new FilterHolder(cfilter);
52 filterHolder.setInitParameter(org.eclipse.jetty.servlets.CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, allowOriginUrl);
53 context.addFilter(filterHolder, "/*", EnumSet.allOf(DispatcherType.class));
58 private static class ServiceServerThread extends Thread {
65 address = server.getURI().toString()+"history/";
69 } catch (Exception e) {
70 LOGGER.error("Could not start server ", e);
74 public void startAndWait() {
79 } catch (InterruptedException e) {};
83 private static synchronized HistoryRestServer getInstance(int port) {
85 if (INSTANCE == null) {
86 INSTANCE = new HistoryRestServer(port);
88 } catch (Exception e) {
89 LOGGER.error("Could not initialize SCL REST server", e);
94 public static HistoryRestServer getRunningInstance() {
98 public static void setAllowOrigin(String url) {
102 public static synchronized void startAsync(int port) throws Exception {
103 // Ensure that an instance is created
105 if (serverThread == null && server != null) {
106 serverThread = new ServiceServerThread();
107 serverThread.start();
111 public static synchronized void start(int port) throws Exception {
112 // Ensure that an instance is created
114 if (serverThread == null && server != null) {
115 serverThread = new ServiceServerThread();
116 serverThread.startAndWait();
120 public static synchronized void stop() throws Exception {
129 public static synchronized boolean running() {
130 return INSTANCE != null;
133 public static synchronized String address() {