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 public void open() throws IOException
42 {
43 synchronized(this)
44 {
45 try
46 {
47 Channel channel = System.inheritedChannel();
48 if ( channel instanceof ServerSocketChannel )
49 _acceptChannel = (ServerSocketChannel)channel;
50 else
51 Log.warn("Unable to use System.inheritedChannel() [" +channel+ "]. Trying a new ServerSocketChannel at " + getHost() + ":" + getPort());
52
53 if ( _acceptChannel != null )
54 _acceptChannel.configureBlocking(false);
55 }
56 catch(NoSuchMethodError e)
57 {
58 Log.warn("Need at least Java 5 to use socket inherited from xinetd/inetd.");
59 }
60
61 if (_acceptChannel == null)
62 super.open();
63 }
64 }
65
66 }