View Javadoc

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