]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/internal/DebugBrowserServer.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / internal / DebugBrowserServer.java
1 /*******************************************************************************
2  * Copyright (c) 2016 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     THTH ry - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.debug.browser.internal;
13
14 import java.net.MalformedURLException;
15 import java.net.URL;
16
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;
30
31 /**
32  * @author Jani Simomaa / Semantum Oy
33  * @since 1.22
34  */
35 public class DebugBrowserServer {
36
37     private static final String CONTEXT_PATH = "/debug";
38     
39     private final Server server;
40     private final ServerConnector connector;
41
42     public DebugBrowserServer(String resourcePath) throws Exception {
43         server = new Server();
44         
45         connector = new ServerConnector(server);
46         connector.setHost("localhost");
47         connector.setPort(0);
48         server.setConnectors(new Connector[] { connector } );
49         
50         ServletHandler handler = new ServletHandler();
51         
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);
57         
58         ResourceHandler resourceHandler = new ResourceHandler();
59         resourceHandler.setResourceBase(resourcePath);
60  
61         HandlerList handlers = new HandlerList();
62         handlers.setHandlers(new Handler[] { resourceHandler, contextHandler });
63         server.setHandler(handlers);
64     }
65     
66     public void start() throws Exception {
67         server.start();
68     }
69     
70     public void stop() throws Exception {
71         server.stop();
72     }
73     
74     public URL getURL() {
75         try {
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));
79             throw new Error(e);
80         }
81     }
82     
83 }