View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.DefaultHandler;
27  import org.eclipse.jetty.server.handler.HandlerCollection;
28  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
29  
30  import com.sun.net.httpserver.HttpServer;
31  import com.sun.net.httpserver.HttpsServer;
32  import com.sun.net.httpserver.spi.HttpServerProvider;
33  
34  /**
35   * Jetty implementation of <a href="http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/index.html">Java HTTP Server SPI</a>
36   */
37  public class JettyHttpServerProvider extends HttpServerProvider
38  {
39  
40      private static Server _server;
41  
42      public static void setServer(Server server)
43      {
44      	_server = server;
45      }
46  
47      @Override
48      public HttpServer createHttpServer(InetSocketAddress addr, int backlog)
49              throws IOException
50      {
51          Server server = _server;
52      	boolean shared = true;
53  
54          if (server == null)
55          {
56          	server = new Server();
57          	
58          	HandlerCollection handlerCollection = new HandlerCollection();
59          	handlerCollection.setHandlers(new Handler[] {new ContextHandlerCollection(), new DefaultHandler()});
60  			server.setHandler(handlerCollection);
61  
62              shared = false;
63          }
64  
65          JettyHttpServer jettyHttpServer = new JettyHttpServer(server, shared);
66          jettyHttpServer.bind(addr, backlog);
67          return jettyHttpServer;
68      }
69  
70      @Override
71      public HttpsServer createHttpsServer(InetSocketAddress addr, int backlog) throws IOException
72      {
73          throw new UnsupportedOperationException();
74      }
75  
76  }