View Javadoc

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