]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/internal/DebugBrowserServer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / internal / DebugBrowserServer.java
1 /*******************************************************************************\r
2  * Copyright (c) 2016 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     THTH ry - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.debug.browser.internal;\r
13 \r
14 import java.net.MalformedURLException;\r
15 import java.net.URL;\r
16 \r
17 import org.eclipse.core.runtime.IStatus;\r
18 import org.eclipse.core.runtime.Platform;\r
19 import org.eclipse.core.runtime.Status;\r
20 import org.eclipse.jetty.server.Connector;\r
21 import org.eclipse.jetty.server.Handler;\r
22 import org.eclipse.jetty.server.Server;\r
23 import org.eclipse.jetty.server.ServerConnector;\r
24 import org.eclipse.jetty.server.handler.HandlerList;\r
25 import org.eclipse.jetty.server.handler.ResourceHandler;\r
26 import org.eclipse.jetty.servlet.ServletContextHandler;\r
27 import org.eclipse.jetty.servlet.ServletHandler;\r
28 import org.eclipse.jetty.servlet.ServletHolder;\r
29 import org.simantics.debug.browser.servlet.ResourceBrowserServlet;\r
30 \r
31 /**\r
32  * @author Jani Simomaa / Semantum Oy\r
33  * @since 1.22\r
34  */\r
35 public class DebugBrowserServer {\r
36 \r
37     private static final String CONTEXT_PATH = "/debug";\r
38     \r
39     private final Server server;\r
40     private final ServerConnector connector;\r
41 \r
42     public DebugBrowserServer(String resourcePath) throws Exception {\r
43         server = new Server();\r
44         \r
45         connector = new ServerConnector(server);\r
46         connector.setHost("localhost");\r
47         connector.setPort(0);\r
48         server.setConnectors(new Connector[] { connector } );\r
49         \r
50         ServletHandler handler = new ServletHandler();\r
51         \r
52         ServletHolder holder = new ServletHolder(ResourceBrowserServlet.class);\r
53         handler.addServletWithMapping(holder, "/*");\r
54         ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);\r
55         contextHandler.setContextPath(CONTEXT_PATH);\r
56         contextHandler.setHandler(handler);\r
57         \r
58         ResourceHandler resourceHandler = new ResourceHandler();\r
59         resourceHandler.setResourceBase(resourcePath);\r
60  \r
61         HandlerList handlers = new HandlerList();\r
62         handlers.setHandlers(new Handler[] { resourceHandler, contextHandler });\r
63         server.setHandler(handlers);\r
64     }\r
65     \r
66     public void start() throws Exception {\r
67         server.start();\r
68     }\r
69     \r
70     public void stop() throws Exception {\r
71         server.stop();\r
72     }\r
73     \r
74     public URL getURL() {\r
75         try {\r
76             return new URL("http", connector.getHost(), connector.getLocalPort(), CONTEXT_PATH);\r
77         } catch (MalformedURLException e) {\r
78             Platform.getLog(Activator.getContext().getBundle()).log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "URL construction failed", e));\r
79             throw new Error(e);\r
80         }\r
81     }\r
82     \r
83 }\r