View Javadoc

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