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
10 * THTH ry - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.debug.browser.internal;
\r
14 import java.net.MalformedURLException;
\r
15 import java.net.URL;
\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
32 * @author Jani Simomaa / Semantum Oy
\r
35 public class DebugBrowserServer {
\r
37 private static final String CONTEXT_PATH = "/debug";
\r
39 private final Server server;
\r
40 private final ServerConnector connector;
\r
42 public DebugBrowserServer(String resourcePath) throws Exception {
\r
43 server = new Server();
\r
45 connector = new ServerConnector(server);
\r
46 connector.setHost("localhost");
\r
47 connector.setPort(0);
\r
48 server.setConnectors(new Connector[] { connector } );
\r
50 ServletHandler handler = new ServletHandler();
\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
58 ResourceHandler resourceHandler = new ResourceHandler();
\r
59 resourceHandler.setResourceBase(resourcePath);
\r
61 HandlerList handlers = new HandlerList();
\r
62 handlers.setHandlers(new Handler[] { resourceHandler, contextHandler });
\r
63 server.setHandler(handlers);
\r
66 public void start() throws Exception {
\r
70 public void stop() throws Exception {
\r
74 public URL getURL() {
\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