View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.http.spi;
20  
21  import java.io.IOException;
22  import java.net.InetSocketAddress;
23  
24  import org.eclipse.jetty.server.Handler;
25  import org.eclipse.jetty.server.Server;
26  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
27  import org.eclipse.jetty.server.handler.DefaultHandler;
28  import org.eclipse.jetty.server.handler.HandlerCollection;
29  import org.eclipse.jetty.util.thread.QueuedThreadPool;
30  import org.eclipse.jetty.util.thread.ThreadPool;
31  
32  import com.sun.net.httpserver.HttpServer;
33  import com.sun.net.httpserver.HttpsServer;
34  import com.sun.net.httpserver.spi.HttpServerProvider;
35  
36  /**
37   * Jetty implementation of <a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/index.html">Java HTTP Server SPI</a>
38   */
39  public class JettyHttpServerProvider extends HttpServerProvider
40  {
41  
42      private static Server _server;
43  
44      public static void setServer(Server server)
45      {
46          _server = server;
47      }
48  
49      @Override
50      public HttpServer createHttpServer(InetSocketAddress addr, int backlog)
51              throws IOException
52      {
53          Server server = _server;
54          boolean shared = true;
55  
56          if (server == null)
57          {
58              ThreadPool threadPool = new DelegatingThreadPool(new QueuedThreadPool());
59              server = new Server(threadPool);
60  
61              HandlerCollection handlerCollection = new HandlerCollection();
62              handlerCollection.setHandlers(new Handler[] {new ContextHandlerCollection(), new DefaultHandler()});
63              server.setHandler(handlerCollection);
64  
65              shared = false;
66          }
67  
68          JettyHttpServer jettyHttpServer = new JettyHttpServer(server, shared);
69          jettyHttpServer.bind(addr, backlog);
70          return jettyHttpServer;
71      }
72  
73      @Override
74      public HttpsServer createHttpsServer(InetSocketAddress addr, int backlog) throws IOException
75      {
76          throw new UnsupportedOperationException();
77      }
78  
79  }