Index: VERSION.txt
===================================================================
--- VERSION.txt	(revision 3534)
+++ VERSION.txt	(working copy)
@@ -1,4 +1,5 @@
 jetty-7.0-SNAPSHOT
+ + JETTY-496 Support inetd/xinetd through use of System.inheritedChannel()
  + JETTY-591 No server classes for jetty-web.xml
  + JETTY-676 ResourceHandler doesn't support HTTP HEAD requests
  + JETTY-677 GWT serialization issue
Index: modules/server/jetty/src/main/java/org/mortbay/jetty/nio/AbstractNIOConnector.java
===================================================================
--- modules/server/jetty/src/main/java/org/mortbay/jetty/nio/AbstractNIOConnector.java	(revision 3534)
+++ modules/server/jetty/src/main/java/org/mortbay/jetty/nio/AbstractNIOConnector.java	(working copy)
@@ -29,6 +29,7 @@
 public abstract class AbstractNIOConnector extends AbstractConnector implements NIOConnector
 {
     private boolean _useDirectBuffers=true;
+    protected boolean _fromService;
  
     /* ------------------------------------------------------------------------------- */
     public boolean getUseDirectBuffers()
@@ -70,5 +71,19 @@
         return buf;
     }
     
-
+    /* ------------------------------------------------------------ */
+    public boolean isFromService()
+    {
+        return _fromService;
+    }
+         
+    /* ------------------------------------------------------------ */
+    /**
+     * @param fromService If true, try to inherit from the channel provided by the system. 
+     * Will have no effect Jetty was not started from xinetd/inetd 
+     */
+    public void setFromService(boolean fromService)
+    {
+        _fromService = fromService;
+    }    
 }
Index: modules/server/jetty/src/main/java/org/mortbay/jetty/nio/SelectChannelConnector.java
===================================================================
--- modules/server/jetty/src/main/java/org/mortbay/jetty/nio/SelectChannelConnector.java	(revision 3534)
+++ modules/server/jetty/src/main/java/org/mortbay/jetty/nio/SelectChannelConnector.java	(working copy)
@@ -17,6 +17,7 @@
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.Socket;
+import java.nio.channels.Channel;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
@@ -185,6 +186,20 @@
     {
         synchronized(this)
         {
+            // Use existing socket passed by xinetd or inetd
+            if ( _fromService )
+            {
+                Channel channel = System.inheritedChannel();
+                if ( channel instanceof ServerSocketChannel )
+                    _acceptChannel = (ServerSocketChannel)channel;
+                else
+                    Log.warn("Unable to use System.inheritedChannel() [" +channel+ "]. Trying a new ServerSocketChannel at " + getHost() + ":" + getPort());
+                
+                if ( _acceptChannel != null )
+                    _acceptChannel.configureBlocking(false);
+            }
+
+
             if (_acceptChannel == null)
             {
                 // Create a new server socket
@@ -267,7 +282,6 @@
         _lowResourcesMaxIdleTime=lowResourcesMaxIdleTime;
         super.setLowResourceMaxIdleTime(lowResourcesMaxIdleTime); 
     }
-
     
     /* ------------------------------------------------------------ */
     /*
Index: modules/server/jetty/src/main/java/org/mortbay/jetty/nio/BlockingChannelConnector.java
===================================================================
--- modules/server/jetty/src/main/java/org/mortbay/jetty/nio/BlockingChannelConnector.java	(revision 3534)
+++ modules/server/jetty/src/main/java/org/mortbay/jetty/nio/BlockingChannelConnector.java	(working copy)
@@ -18,6 +18,7 @@
 import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.nio.channels.ByteChannel;
+import java.nio.channels.Channel;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
 
@@ -63,15 +64,28 @@
     /* ------------------------------------------------------------ */
     public void open() throws IOException
     {
+        // Use existing socket passed by xinetd or inetd 
+        if ( _fromService )
+        {
+            Channel channel = System.inheritedChannel();
+            if ( channel instanceof ServerSocketChannel )
+                _acceptChannel = (ServerSocketChannel)channel;
+            else
+                Log.warn("Unable to use System.inheritedChannel() [" +channel+ "]. Trying a new ServerSocketChannel at " + getHost() + ":" + getPort());
+        }
+
         // Create a new server socket and set to non blocking mode
-        _acceptChannel= ServerSocketChannel.open();
-        _acceptChannel.configureBlocking(true);
-
-        // Bind the server socket to the local host and port
-        InetSocketAddress addr = getHost()==null?new InetSocketAddress(getPort()):new InetSocketAddress(getHost(),getPort());
-        _acceptChannel.socket().bind(addr,getAcceptQueueSize());
+        if ( _acceptChannel == null )
+        {
+            _acceptChannel = ServerSocketChannel.open();
+            // Bind the server socket to the local host and port
+            _acceptChannel.socket().setReuseAddress(getReuseAddress());
+            InetSocketAddress addr = getHost()==null?new InetSocketAddress(getPort()):new InetSocketAddress(getHost(),getPort());
+            _acceptChannel.socket().bind(addr,getAcceptQueueSize());
+            _acceptChannel.configureBlocking(true);
+        }
     }
-
+    
     /* ------------------------------------------------------------ */
     public void close() throws IOException
     {
@@ -84,7 +98,10 @@
     public void accept(int acceptorID)
     	throws IOException, InterruptedException
     {   
+        
         SocketChannel channel = _acceptChannel.accept();
+        
+        if(channel == null) return;
         channel.configureBlocking(true);
         Socket socket=channel.socket();
         configure(socket);
@@ -116,7 +133,7 @@
             return -1;
         return _acceptChannel.socket().getLocalPort();
     }
-    
+
     /* ------------------------------------------------------------------------------- */
     /* ------------------------------------------------------------------------------- */
     /* ------------------------------------------------------------------------------- */
@@ -188,3 +205,4 @@
         }
     }
 }
+    
\ No newline at end of file
Index: etc/jetty.xml
===================================================================
--- etc/jetty.xml	(revision 3534)
+++ etc/jetty.xml	(working copy)
@@ -44,12 +44,13 @@
           <New class="org.mortbay.jetty.nio.SelectChannelConnector">
             <Set name="host"><SystemProperty name="jetty.host" /></Set>
             <Set name="port"><SystemProperty name="jetty.port" default="8080"/></Set>
+            <Set name="fromService">false</Set>
             <Set name="maxIdleTime">300000</Set>
             <Set name="Acceptors">2</Set>
             <Set name="statsOn">false</Set>
             <Set name="confidentialPort">8443</Set>
-	    <Set name="lowResourcesConnections">20000</Set>
-	    <Set name="lowResourcesMaxIdleTime">5000</Set>
+	        <Set name="lowResourcesConnections">20000</Set>
+	        <Set name="lowResourcesMaxIdleTime">5000</Set>
           </New>
       </Arg>
     </Call>
@@ -66,6 +67,13 @@
     <!--   java -jar start.jar etc/jetty.xml etc/jetty-bio.xml           -->
     <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
     
+    <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
+    <!-- To allow Jetty to be started as a service                       -->
+    <!-- mixin jetty-xinetd.xml:                                        -->
+    <!--   java -jar start.jar etc/jetty.xml etc/jetty-xinetd.xml       -->
+    <!--                                                                 -->
+    <!-- See jetty-xinetd.xml for further instructions.                 -->
+    <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
 
     <!-- =========================================================== -->
     <!-- Set handler Collection Structure                            --> 
Index: etc/jetty-xinetd.xml
===================================================================
--- etc/jetty-xinetd.xml	(revision 0)
+++ etc/jetty-xinetd.xml	(revision 0)
@@ -0,0 +1,57 @@
+<?xml version="1.0"?>
+<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
+
+<!-- =============================================================== -->
+<!-- Configuration for starting up Jetty using inetd/xinetd          -->
+<!-- This feature requires at least Java 5                           -->
+<!--                                                                 -->
+<!-- Making it a mixin for convenience, but note that if used        -->
+<!-- with jetty.xml, Jetty will use multiple connectors              -->
+<!-- =============================================================== -->
+
+<!-- Sample xinetd configuration (restart xinetd after adding the configuration file)
+
+service jetty
+{
+    disable     = no
+
+    id          = jetty
+    type        = UNLISTED     
+    wait        = yes          
+    socket_type = stream
+
+    # change this
+    user        = username
+    group       = groupname
+    port        = 2001
+
+    # sample script for running jetty as a service
+    # replace $JETTY_HOME with /path/to/jetty_home/
+    server      = $JETTY_HOME/bin/jetty-xinetd.sh
+}
+
+-->
+
+<Configure id="Server" class="org.mortbay.jetty.Server">
+    <Call name="addConnector">
+      <Arg>
+          <!-- Inherited channel (from inetd/xinetd) -->
+          <New class="org.mortbay.jetty.nio.SelectChannelConnector">
+
+            <!-- use channel from service instead of creating a new one -->
+            <Set name="fromService">true</Set>
+
+            <!-- Optional. Fallback in case System.inheritedChannel() does not give a ServerSocketChannel 
+            <Set name="port"><SystemProperty name="jetty.service.port" default="8082"/></Set>
+            -->
+
+            <!-- sane defaults -->
+            <Set name="maxIdleTime">300000</Set>
+            <Set name="Acceptors">2</Set>
+            <Set name="statsOn">false</Set>
+	    <Set name="lowResourcesConnections">20000</Set>
+	    <Set name="lowResourcesMaxIdleTime">5000</Set>
+          </New>
+      </Arg>
+    </Call>
+</Configure>
Index: bin/jetty-xinetd.sh
===================================================================
--- bin/jetty-xinetd.sh	(revision 0)
+++ bin/jetty-xinetd.sh	(revision 0)
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+
+# look for JETTY_HOME
+if [ -z "$JETTY_HOME" ] 
+then
+  JETTY_HOME_1=`dirname "$0"`
+  JETTY_HOME_1=`dirname "$JETTY_HOME_1"`
+  JETTY_HOME=${JETTY_HOME_1} 
+fi
+
+cd $JETTY_HOME
+exec /usr/bin/java -jar start.jar etc/jetty.xml etc/jetty-xinetd.xml
+
