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  
22  /**
23   * An implementation of the SelectChannelConnector which first tries to  
24   * inherit from a channel provided by the system. If there is no inherited
25   * channel available, or if the inherited channel provided not usable, then 
26   * it will fall back upon normal ServerSocketChannel creation.
27   * <p> 
28   * Note that System.inheritedChannel() is only available from Java 1.5 onwards.
29   * Trying to use this class under Java 1.4 will be the same as using a normal
30   * SelectChannelConnector. 
31   * <p> 
32   * Use it with xinetd/inetd, to launch an instance of Jetty on demand. The port
33   * used to access pages on the Jetty instance is the same as the port used to
34   * launch Jetty. 
35   * 
36   * @author athena
37   */
38  public class InheritedChannelConnector extends SelectChannelConnector
39  {
40      /* ------------------------------------------------------------ */
41      @Override
42      public void open() throws IOException
43      {
44          synchronized(this)
45          {
46              try 
47              {
48                  Channel channel = System.inheritedChannel();
49                  if ( channel instanceof ServerSocketChannel )
50                      _acceptChannel = (ServerSocketChannel)channel;
51                  else
52                      Log.warn("Unable to use System.inheritedChannel() [" +channel+ "]. Trying a new ServerSocketChannel at " + getHost() + ":" + getPort());
53                  
54                  if ( _acceptChannel != null )
55                      _acceptChannel.configureBlocking(false);
56              }
57              catch(NoSuchMethodError e)
58              {
59                  Log.warn("Need at least Java 5 to use socket inherited from xinetd/inetd.");
60              }
61  
62              if (_acceptChannel == null)
63                  super.open();
64          }
65      }
66  
67  }