View Javadoc

1   package org.mortbay.jetty.nio;
2   
3   import java.io.IOException;
4   import java.nio.channels.Channel;
5   import java.nio.channels.ServerSocketChannel;
6   
7   import org.mortbay.log.Log;
8   
9   /**
10   * An implementation of the SelectChannelConnector which first tries to  
11   * inherit from a channel provided by the system. If there is no inherited
12   * channel available, or if the inherited channel provided not usable, then 
13   * it will fall back upon normal ServerSocketChannel creation.
14   * <p> 
15   * Note that System.inheritedChannel() is only available from Java 1.5 onwards.
16   * Trying to use this class under Java 1.4 will be the same as using a normal
17   * SelectChannelConnector. 
18   * <p> 
19   * Use it with xinetd/inetd, to launch an instance of Jetty on demand. The port
20   * used to access pages on the Jetty instance is the same as the port used to
21   * launch Jetty. 
22   * 
23   * @author athena
24   */
25  public class InheritedChannelConnector extends SelectChannelConnector
26  {
27      /* ------------------------------------------------------------ */
28      public void open() throws IOException
29      {
30          synchronized(this)
31          {
32              try 
33              {
34                  Channel channel = System.inheritedChannel();
35                  if ( channel instanceof ServerSocketChannel )
36                      _acceptChannel = (ServerSocketChannel)channel;
37                  else
38                      Log.warn("Unable to use System.inheritedChannel() [" +channel+ "]. Trying a new ServerSocketChannel at " + getHost() + ":" + getPort());
39                  
40                  if ( _acceptChannel != null )
41                      _acceptChannel.configureBlocking(false);
42              }
43              catch(NoSuchMethodError e)
44              {
45                  Log.warn("Need at least Java 5 to use socket inherited from xinetd/inetd.");
46              }
47  
48              if (_acceptChannel == null)
49                  super.open();
50          }
51      }
52  
53  }