View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
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   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.server.nio;
15  
16  import java.io.IOException;
17  import java.nio.channels.Channel;
18  import java.nio.channels.ServerSocketChannel;
19  
20  import org.eclipse.jetty.util.log.Log;
21  import org.eclipse.jetty.util.log.Logger;
22  
23  /**
24   * An implementation of the SelectChannelConnector which first tries to  
25   * inherit from a channel provided by the system. If there is no inherited
26   * channel available, or if the inherited channel provided not usable, then 
27   * it will fall back upon normal ServerSocketChannel creation.
28   * <p> 
29   * Note that System.inheritedChannel() is only available from Java 1.5 onwards.
30   * Trying to use this class under Java 1.4 will be the same as using a normal
31   * SelectChannelConnector. 
32   * <p> 
33   * Use it with xinetd/inetd, to launch an instance of Jetty on demand. The port
34   * used to access pages on the Jetty instance is the same as the port used to
35   * launch Jetty. 
36   * 
37   * @author athena
38   */
39  public class InheritedChannelConnector extends SelectChannelConnector
40  {
41      private static final Logger LOG = Log.getLogger(InheritedChannelConnector.class);
42  
43      /* ------------------------------------------------------------ */
44      @Override
45      public void open() throws IOException
46      {
47          synchronized(this)
48          {
49              try 
50              {
51                  Channel channel = System.inheritedChannel();
52                  if ( channel instanceof ServerSocketChannel )
53                      _acceptChannel = (ServerSocketChannel)channel;
54                  else
55                      LOG.warn("Unable to use System.inheritedChannel() [" +channel+ "]. Trying a new ServerSocketChannel at " + getHost() + ":" + getPort());
56                  
57                  if ( _acceptChannel != null )
58                      _acceptChannel.configureBlocking(true);
59              }
60              catch(NoSuchMethodError e)
61              {
62                  LOG.warn("Need at least Java 5 to use socket inherited from xinetd/inetd.");
63              }
64  
65              if (_acceptChannel == null)
66                  super.open();
67          }
68      }
69  
70  }