1 /*******************************************************************************
2 * Copyright (c) 2016 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * THTH ry - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.debug.browser.internal;
14 import java.net.MalformedURLException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.jetty.server.Connector;
21 import org.eclipse.jetty.server.Handler;
22 import org.eclipse.jetty.server.Server;
23 import org.eclipse.jetty.server.ServerConnector;
24 import org.eclipse.jetty.server.handler.HandlerList;
25 import org.eclipse.jetty.server.handler.ResourceHandler;
26 import org.eclipse.jetty.servlet.ServletContextHandler;
27 import org.eclipse.jetty.servlet.ServletHandler;
28 import org.eclipse.jetty.servlet.ServletHolder;
29 import org.simantics.debug.browser.servlet.ResourceBrowserServlet;
32 * @author Jani Simomaa / Semantum Oy
35 public class DebugBrowserServer {
37 private static final String CONTEXT_PATH = "/debug";
39 private final Server server;
40 private final ServerConnector connector;
42 public DebugBrowserServer(String resourcePath) throws Exception {
43 server = new Server();
45 connector = new ServerConnector(server);
46 connector.setHost("localhost");
48 server.setConnectors(new Connector[] { connector } );
50 ServletHandler handler = new ServletHandler();
52 ServletHolder holder = new ServletHolder(ResourceBrowserServlet.class);
53 handler.addServletWithMapping(holder, "/*");
54 ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
55 contextHandler.setContextPath(CONTEXT_PATH);
56 contextHandler.setHandler(handler);
58 ResourceHandler resourceHandler = new ResourceHandler();
59 resourceHandler.setResourceBase(resourcePath);
61 HandlerList handlers = new HandlerList();
62 handlers.setHandlers(new Handler[] { resourceHandler, contextHandler });
63 server.setHandler(handlers);
66 public void start() throws Exception {
70 public void stop() throws Exception {
76 return new URL("http", connector.getHost(), connector.getLocalPort(), CONTEXT_PATH);
77 } catch (MalformedURLException e) {
78 Platform.getLog(Activator.getContext().getBundle()).log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "URL construction failed", e));