You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/05/30 06:43:54 UTC

svn commit: r1128991 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/ java/org/apache/commons/runtime/net/ java/org/apache/commons/runtime/platform/unix/ native/ native/include/acr/ native/os/unix/ test/org/apache/commons/...

Author: mturk
Date: Mon May 30 04:43:53 2011
New Revision: 1128991

URL: http://svn.apache.org/viewvc?rev=1128991&view=rev
Log:
Axe SelectEvent and add more api

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/OverflowException.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointType.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyFactory.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ShutdownHow.java   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c   (with props)
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java   (with props)
Removed:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionEvent.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelector.java
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketSelectorFactory.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SelectionKeyImpl.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java
    commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr/iodefs.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/OverflowException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/OverflowException.java?rev=1128991&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/OverflowException.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/OverflowException.java Mon May 30 04:43:53 2011
@@ -0,0 +1,38 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+import java.io.IOException;
+
+/**
+ * OverflowException thrown when the operation would overlow.
+ * This class mimics the os {@code EOVERFLOW} error.
+ *
+ * @since Runtime 1.0
+ */
+public class OverflowException extends RuntimeException
+{
+
+    public OverflowException()
+    {
+        super();
+    }
+
+    public OverflowException(String msg)
+    {
+        super(msg);
+    }
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/OverflowException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Endpoint.java Mon May 30 04:43:53 2011
@@ -32,17 +32,9 @@ import org.apache.commons.runtime.io.Des
 public abstract class Endpoint implements Closeable
 {
     /**
-     * Further reception will be disallowed.
+     * Selection key.
      */
-    public static final int     SHUT_RD     = 0;
-    /**
-     * Further transimission will be disallowed.
-     */
-    public static final int     SHUT_WR     = 1;
-    /**
-     * Further receptions and transmissions will be disallowed.
-     */
-    public static final int     SHUT_RDWR   = 2;
+    protected SelectionKey      key;
 
     /**
      * Creates a new Endpoint object.
@@ -52,6 +44,11 @@ public abstract class Endpoint implement
     }
 
     /**
+     * Gets the endpoint's type.
+     */
+    public abstract EndpointType type();
+
+    /**
      * Gets the endpoint's  descriptor object.
      */
     public abstract Descriptor descriptor();
@@ -68,5 +65,29 @@ public abstract class Endpoint implement
      */
     public abstract void close()
         throws IOException;
-    
+
+    /**
+     * Returns the key representing this endpoint's registration with
+     * the given selector.
+     *
+     * @param selector the selector.
+     *
+     * @return A key representing the registration of this endpoint
+     *         with the given selector. 
+     * @throws NullPonterException if the selector is {@code null}
+     * @throws IllegalSelectorException if the endpoint is already
+     *          registered with a different selector.
+     */
+    public synchronized SelectionKey key(Selector selector)
+        throws NullPointerException, IllegalSelectorException
+    {
+        if (selector == null)
+            throw new NullPointerException(Local.sm.get("selector.NULL"));
+        if (key == null)
+            key = SelectionKeyFactory.newInstance(selector, this);
+        if (key.selector() != selector)
+            throw new IllegalSelectorException();
+        return key;
+    }
+
 }

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointType.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointType.java?rev=1128991&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointType.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointType.java Mon May 30 04:43:53 2011
@@ -0,0 +1,55 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.net;
+
+/** Represents the type of the communication endpoint.
+ */
+public enum EndpointType
+{
+    /** Unspecified type */
+    UNSPEC(     0),
+    /** Socket */
+    SOCKET(     1),
+    /** Unix domain socket or Windows pipe */
+    LOCAL(      2);
+
+    private int value;
+    private EndpointType(int v)
+    {
+        value = v;
+    }
+
+    public int valueOf()
+    {
+        return value;
+    }
+
+    public boolean isLocal()
+    {
+        return value > 1;
+    }
+
+    public static EndpointType valueOf(int value)
+    {
+        for (EndpointType e : values()) {
+            if (e.value == value)
+                return e;
+        }
+        return UNSPEC;
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/EndpointType.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java?rev=1128991&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java Mon May 30 04:43:53 2011
@@ -0,0 +1,37 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.net;
+
+/**
+ * An {@code IllegalSelectorException} is thrown when an operation that
+ * requires a specific selector is invoked on ainvalid selector.
+ *
+ * @since Runtime 1.0
+ */
+public class IllegalSelectorException extends IllegalStateException
+{
+
+    public IllegalSelectorException()
+    {
+        super();
+    }
+
+    public IllegalSelectorException(String msg)
+    {
+        super(msg);
+    }
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IllegalSelectorException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketEndpoint.java Mon May 30 04:43:53 2011
@@ -43,6 +43,20 @@ public class LocalSocketEndpoint extends
     {
     }
 
+    /**
+     * Creates a new socket object from socket descriptor.
+     */
+    public LocalSocketEndpoint(LocalSocketDescriptor sd)
+    {
+        this.sd = sd;
+    }
+
+    @Override
+    public EndpointType type()
+    {
+        return EndpointType.LOCAL;
+    }
+
     @Override
     public Descriptor descriptor()
     {

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketSelectorFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketSelectorFactory.java?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketSelectorFactory.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketSelectorFactory.java Mon May 30 04:43:53 2011
@@ -18,6 +18,8 @@
 
 package org.apache.commons.runtime.net;
 
+import org.apache.commons.runtime.InvalidArgumentException;
+
 /**
  * Creates a new local socket selector instance.
  */
@@ -28,23 +30,50 @@ public final class LocalSocketSelectorFa
         // No instance
     }
 
-    private static final int             maxSize;
-    private static native int            nmax0();
-    private static native SocketSelector new0(int size)
+    /**
+     * Maximum size of the selector limited by the operating system
+     */
+    public  static final int            MAX_CAPACITY;
+
+    private static native int           init0();
+    private static native Selector      new0(int size)
         throws OutOfMemoryError;
     static {
-        maxSize = nmax0();
+        MAX_CAPACITY = init0();
     }
 
     /**
-     * Creates a new selector instance.
+     * Creates a new {@code Selector} instance with the given size.
+     *
+     * @return the new socket selector.
+     *
+     * @throws InvalidArgumentException if {@code size} is outside the
+     *          valid range.
+     * @throws OutOfMemoryError if the memory allocation fails.
      */
-    public static SocketSelector createSelector(int size)
-        throws OutOfMemoryError
+    public static Selector newInstance(int size)
+        throws InvalidArgumentException, OutOfMemoryError
     {
-        if (size == 0)
-            size = maxSize;
+        if (size < 1 || size > MAX_CAPACITY)
+            throw new InvalidArgumentException();
         return new0(size);
     }
 
+    /**
+     * Creates a new {@code Selector} instance.
+     *
+     * @return the new local socket selector.
+     *
+     * @throws InvalidArgumentException if the selector capacity cannot be
+     *          determined.
+     * @throws OutOfMemoryError if the memory allocation fails.
+     */
+    public static Selector newInstance()
+        throws InvalidArgumentException, OutOfMemoryError
+    {
+        if (MAX_CAPACITY < 1)
+            throw new InvalidArgumentException();
+        return newInstance(MAX_CAPACITY);
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties Mon May 30 04:43:53 2011
@@ -15,3 +15,4 @@
 addr.EHOSTNAME=Invalid hostname
 port.ERANGE=Port is outside allowed range
 socketd.CLOSED=Socket is already closed
+selector.NULL=Sellector can't be null
\ No newline at end of file

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Poll.java Mon May 30 04:43:53 2011
@@ -35,37 +35,37 @@ public final class Poll
     /**
      * There is data to read
      */
-    public static final short     POLLIN        = 0x0001;
+    public static final short     POLLIN        = SelectionKey.OP_READ;
     /**
      * There is urgent data to read.
      * e.g., out-of-band data on TCP socket; pseudo-terminal master in  packet  mode
      * has seen state change in slave.
      */
-    public static final short     POLLPRI       = 0x0002;
+    public static final short     POLLPRI       = SelectionKey.OP_PRI;
     /**
      * Writing now will not block.
      */
-    public static final short     POLLOUT       = 0x0004;
+    public static final short     POLLOUT       = SelectionKey.OP_WRITE;
     /**
-     * An error has occurred.
+     * A Stream socket peer closed  connection, or shut down writing half of connection.
      */
-    public static final short     POLLERR       = 0x0008;
+    public static final short     POLLRDHUP     = SelectionKey.OP_RDHUP;
     /**
      * A stream-oriented connection was either diconnected or aborted.
      */
-    public static final short     POLLHUP       = 0x0010;
+    public static final short     POLLHUP       = SelectionKey.OP_HANGUP;
     /**
-     * A Stream socket peer closed  connection, or shut down writing half of connection.
+     * An invalid descriptor was used.
      */
-    public static final short     POLLRDHUP     = 0x0020;
+    public static final short     POLLNVAL      = SelectionKey.OP_ERROR;
     /**
-     * An invalid descriptor was used.
+     * An error has occurred.
      */
-    public static final short     POLLNVAL      = 0x0040;
+    public static final short     POLLERR       = SelectionKey.OP_ERROR;
     /**
      * Time-To-Live event occurred.
      */
-    public static final short     POLLTTL       = 0x0080;
+    public static final short     POLLTTL       = SelectionKey.OP_TIMEOUT;
 
 
     private static native int wait0(int[] fds, short[] events, short[] revents,
@@ -74,7 +74,7 @@ public final class Poll
                InvalidDescriptorException;
     private static native short wait1(int fd, short events, int timeout);
 
-    public static short wait(int fd, short events, int timeout)
+    private static short wait(int fd, short events, int timeout)
         throws OutOfMemoryError,
                InvalidArgumentException,
                InvalidDescriptorException
@@ -89,11 +89,19 @@ public final class Poll
         return se;
     }
 
-    public static short wait(int fd, short events)
+    public static short wait(SocketEndpoint endpoint, short events, int timeout)
+        throws OutOfMemoryError,
+               InvalidArgumentException,
+               InvalidDescriptorException
+    {
+        return wait(endpoint.descriptor().fd(), events, timeout);
+    }
+
+    public static short wait(SocketEndpoint endpoint, short events)
         throws OutOfMemoryError,
                InvalidArgumentException,
                InvalidDescriptorException
     {
-        return wait(fd, events, -1);
+        return wait(endpoint, events, -1);
     }
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKey.java Mon May 30 04:43:53 2011
@@ -16,7 +16,6 @@
 
 package org.apache.commons.runtime.net;
 
-import java.util.EnumSet;
 import org.apache.commons.runtime.io.Descriptor;
 import org.apache.commons.runtime.net.Endpoint;
 
@@ -27,11 +26,72 @@ import org.apache.commons.runtime.net.En
 public abstract class SelectionKey
 {
 
+    private Object          attachment;
+    private Selector        selector;
+    private Endpoint        endpoint;
+    private int             timeout;
+
+    /** Accept operation.
+     * <p>
+     * </p>
+     */
+    static final int OP_ACCEPT          = 0x0001;
+    /** Connect operation.
+     * <p>
+     * </p>
+     */
+    static final int OP_CONNECT         = 0x0002;
+    /** Read operation.
+     * <p>
+     * </p>
+     */
+    static final int OP_READ            = 0x0004;
+    /** Write operation.
+     * <p>
+     * </p>
+     */
+    static final int OP_PRI             = 0x0008;
+    /** Write operation.
+     * <p>
+     * </p>
+     */
+    static final int OP_WRITE           = 0x0010;
+    /** Read hang up operation.
+     * <p>
+     * If present in output it signals that
+     * stream socket peer closed connection,
+     * or shut down writing half of connection.
+     * </p>
+     */
+    static final int OP_RDHUP           = 0x0020;
+    /** Hang up happened on the associated descriptor.
+     * <p>
+     * This is output only flag.
+     * </p>
+     */
+    static final int OP_HANGUP          = 0x0040;
+    /** Error condition happened on the associated descriptor.
+     * <p>
+     * This is output only flag.
+     * </p>
+     */
+    static final int OP_ERROR           = 0x0100;
+    /** Timeout condition happened on the associated descriptor.
+     * <p>
+     * This is output only flag and indicates that Time-To-Live
+     * event occurred.
+     * </p>
+     */
+    static final int OP_TIMEOUT         = 0x0200;
+
     /**
-     * Constructs a new {@code SelectionKey}.
+     * Constructs a new {@code SelectionKey} for given selector and enpoint
      */
-    protected SelectionKey()
+    protected SelectionKey(Selector selector, Endpoint endpoint)
     {
+        this.selector = selector;
+        this.endpoint = endpoint;
+        this.timeout  = -1;
     }
 
     /**
@@ -41,34 +101,90 @@ public abstract class SelectionKey
      *
      * @return the event set of this key.
      */
-    public abstract EnumSet<SelectionEvent> events();
+    public abstract int events();
+
+    /**
+     * Sets this key's event set.
+     *
+     * @param set The new event set.
+     * @return this selection key.
+     */
+    public abstract SelectionKey events(int set);
+
+    /**
+     * Returns the endpoint for which this key was created.
+     *
+     * @return the related endpoint.
+     */
+    public Endpoint endpoint()
+    {
+        return endpoint;
+    }
 
     /**
      * Returns the selector for which this key was created.
      *
      * @return the related selector.
      */
-    public abstract Selector selector();
+    public Selector selector()
+    {
+        return selector;
+    }
 
     /**
      * Attaches an object to this key.
      * It is acceptable to attach {@code null}, this discards the
      * old attachment.
      *
-     * @param ep
-     *          the endpoint to attach, or {@code null} to discard the current
+     * @param anObj
+     *          the object to attach, or {@code null} to discard the current
      *          attachment.
      * @return the last attached object or {@code null} if no object has been
      *         attached.
      */
-    public abstract Endpoint attach(Endpoint ep);
-
+    public Object attach(Object obj)
+    {
+        Object prev = attachment;
+        attachment  = obj;
+        return prev;
+    }
+    
     /**
-     * Gets the attached endpoint.
+     * Gets the attached object.
      *
-     * @return the attached endpoint or {@code null} if no endpoint has been
+     * @return the attached object or {@code null} if no object has been
      *         attached.
      */
-    public abstract Endpoint attachment();
-    
+    public Object attachment()
+    {
+        return attachment;
+    }
+
+    /**
+     * Gets the timeout.
+     *
+     * @return the key's timeout in milliseconds.
+     */
+    public int timeout()
+    {
+        return timeout;
+    }
+
+    /**
+     * Sets the time-to-live in milliseconds.
+     * <p>
+     * This value is the registration timeout upon which the endpoint
+     * will be signaled in case no other event was signaled within the
+     * timeout value.
+     * </p>
+     *
+     * @return the key's previous timeout.
+     */
+    public int timeout(int timeout)
+    {
+        int prevtime = this.timeout;
+        this.timeout = timeout;
+        return prevtime;
+    }
+
 }

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyFactory.java?rev=1128991&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyFactory.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyFactory.java Mon May 30 04:43:53 2011
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.commons.runtime.net;
+
+/**
+ * Creates a new selection key instance.
+ */
+final class SelectionKeyFactory
+{
+    private SelectionKeyFactory()
+    {
+        // No instance
+    }
+
+    private static native boolean        init0();
+    private static native SelectionKey   new0(Selector s, Endpoint e)
+        throws OutOfMemoryError;
+    static {
+        init0();
+    }
+
+    /**
+     * Creates a new {@code SelectionKey} instance.
+     *
+     * @return the new selector.
+     */
+    public static SelectionKey newInstance(Selector selector, Endpoint endpoint)
+        throws OutOfMemoryError
+    {
+        return new0(selector, endpoint);
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SelectionKeyFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Selector.java Mon May 30 04:43:53 2011
@@ -18,24 +18,85 @@
 
 package org.apache.commons.runtime.net;
 
-import org.apache.commons.runtime.Status;
-import org.apache.commons.runtime.InvalidArgumentException;
+import java.io.Closeable;
+import java.io.IOException;
+import org.apache.commons.runtime.io.ClosedDescriptorException;
 import org.apache.commons.runtime.io.InvalidDescriptorException;
+import org.apache.commons.runtime.OverflowException;
 
 /**
  * Wait for some event on set of descriptors.
  */
-public abstract class Selector
+public abstract class Selector implements Closeable
 {
-    protected Selector()
+    private int capacity;
+
+    private Selector()
     {
         // No instance
     }
 
     /**
+     * Creates a new object instance.
+     */
+    protected Selector(int capacity)
+    {
+        this.capacity = capacity;
+    }
+
+    /**
+     * Returns the maximum number of endpoints this selector can handle.
+     */
+    public int capacity()
+    {
+        return capacity;
+    }
+
+    /**
+     * Returns the current number of endpoints registered to this selector.
+     *
+     * @return the current number of registered endpoints.
+     *
+     * @throws IllegalSelectorException if this selector is closed.
+     */
+    public abstract int size()
+        throws IllegalSelectorException;
+
+    /**
      * Interrupt this pollset.
+     *
+     * @throws IllegalSelectorException if this selector is closed.
      */
-    public abstract void interrupt();
+    public abstract void interrupt()
+        throws IllegalSelectorException;
 
+    /**
+     * Registers the selection key with this selector.
+     *
+     * @return {@code true} if the key was registered, {@code false} if
+     *          it was already registered.
+     *
+     * @throws ClosedDescriptorException if the key's endpoint is closed.
+     * @throws IllegalSelectorException if this selector is closed.
+     * @throws OverflowException if the selector reached it's capacity.
+     */
+    protected abstract boolean register(SelectionKey key, int ops)
+        throws ClosedDescriptorException,
+               IllegalSelectorException,
+               OverflowException,
+               IOException;
+        
+    /**
+     * Free the allocated resource by the Operating system.
+     * <p>
+     * Note that {@code Object.finalize()} method will call
+     * this function. However if the native code can block for
+     * long time explicit {@code close()} should be called.
+     * </p>
+     * @see java.io.Closeable#close()
+     * @throws IOException if an I/O error occurs.
+     */
+    public abstract void close()
+        throws IOException;
 
 }

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ShutdownHow.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ShutdownHow.java?rev=1128991&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ShutdownHow.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ShutdownHow.java Mon May 30 04:43:53 2011
@@ -0,0 +1,62 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.net;
+
+/**
+ * Represents the endpoint's shutdown mode.
+ */
+public enum ShutdownHow
+{
+    /**
+     * Further reception will be disallowed.
+     */
+    READ(       0),
+    /**
+     * Further transimission will be disallowed.
+     */
+    WRITE(      1),
+    /**
+     * Further receptions and transmissions will be disallowed.
+     */
+    RDWR(       2);
+
+    private int value;
+    private ShutdownHow(int v)
+    {
+        value = v;
+    }
+
+    public int valueOf()
+    {
+        return value;
+    }
+
+    public boolean isLocal()
+    {
+        return value > 1;
+    }
+
+    public static ShutdownHow valueOf(int value)
+    {
+        for (ShutdownHow e : values()) {
+            if (e.value == value)
+                return e;
+        }
+        return RDWR;
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/ShutdownHow.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketEndpoint.java Mon May 30 04:43:53 2011
@@ -49,6 +49,12 @@ public class SocketEndpoint extends Endp
     }
 
     @Override
+    public EndpointType type()
+    {
+        return EndpointType.SOCKET;
+    }
+
+    @Override
     public Descriptor descriptor()
     {
         return sd;
@@ -59,6 +65,7 @@ public class SocketEndpoint extends Endp
         throws IOException
     {
         sd.close();
+        key = null;
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketSelectorFactory.java Mon May 30 04:43:53 2011
@@ -18,6 +18,8 @@
 
 package org.apache.commons.runtime.net;
 
+import org.apache.commons.runtime.InvalidArgumentException;
+
 /**
  * Creates a new socket selector instance.
  */
@@ -28,25 +30,50 @@ public final class SocketSelectorFactory
         // No instance
     }
 
-    private static final int             maxSize;
-    private static native int            nmax0();
-    private static native SocketSelector new0(int size)
+    /**
+     * Maximum size of the selector limited by the operating system
+     */
+    public  static final int            MAX_CAPACITY;
+
+    private static native int           init0();
+    private static native Selector      new0(int size)
         throws OutOfMemoryError;
     static {
-        maxSize = nmax0();
+        MAX_CAPACITY = init0();
     }
 
     /**
-     * Creates a new {@code SocketSelector} instance.
+     * Creates a new {@code Selector} instance with the given size.
+     *
+     * @return the new socket selector.
      *
-     * @return the new selector.
+     * @throws InvalidArgumentException if {@code size} is outside the
+     *          valid range.
+     * @throws OutOfMemoryError if the memory allocation fails.
      */
-    public static SocketSelector createSelector(int size)
-        throws OutOfMemoryError
+    public static Selector newInstance(int size)
+        throws InvalidArgumentException, OutOfMemoryError
     {
-        if (size == 0)
-            size = maxSize;
+        if (size < 1 || size > MAX_CAPACITY)
+            throw new InvalidArgumentException();        
         return new0(size);
     }
 
+    /**
+     * Creates a new {@code Selector} instance.
+     *
+     * @return the new socket selector.
+     *
+     * @throws InvalidArgumentException if the selector capacity cannot be
+     *          determined.
+     * @throws OutOfMemoryError if the memory allocation fails.
+     */
+    public static Selector newInstance()
+        throws InvalidArgumentException, OutOfMemoryError
+    {
+        if (MAX_CAPACITY < 1)
+            throw new InvalidArgumentException();
+        return newInstance(MAX_CAPACITY);
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SelectionKeyImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SelectionKeyImpl.java?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SelectionKeyImpl.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SelectionKeyImpl.java Mon May 30 04:43:53 2011
@@ -16,7 +16,6 @@
 package org.apache.commons.runtime.platform.unix;
 
 import org.apache.commons.runtime.net.Endpoint;
-import org.apache.commons.runtime.net.SelectionEvent;
 import org.apache.commons.runtime.net.SelectionKey;
 import org.apache.commons.runtime.net.Selector;
 import org.apache.commons.runtime.net.SocketEndpoint;
@@ -27,7 +26,6 @@ import org.apache.commons.runtime.NoSuch
 import org.apache.commons.runtime.OperationNotImplementedException;
 import org.apache.commons.runtime.SystemException;
 import org.apache.commons.runtime.Errno;
-import java.util.EnumSet;
 
 /**
  * Selector implementation class.
@@ -39,44 +37,33 @@ import java.util.EnumSet;
 final class SelectionKeyImpl extends SelectionKey
 {
 
-    public  int                 ievents;
-    public  int                 revents;
-    private SocketSelectorImpl  selector;
-    private Endpoint            endpoint;
+    public  int             ievents;
+    public  int             revents;
 
-    public SelectionKeyImpl(SocketSelectorImpl selector, int ievents)
+    public SelectionKeyImpl(Selector selector, Endpoint endpoint,
+                            int ievents)
     {
-        this.selector = selector;
-        this.ievents  = ievents;
-        this.revents  = 0;
-        this.endpoint = null;
+        super(selector, endpoint);
+        this.ievents    = ievents;
+        this.revents    = 0;
     }
 
-    @Override
-    public EnumSet<SelectionEvent> events()
+    public SelectionKeyImpl(Selector selector, Endpoint endpoint)
     {
-        return SelectionEvent.valueOf(revents);
+        this(selector, endpoint, 0);
     }
 
     @Override
-    public Selector selector()
+    public int events()
     {
-        return selector;
+        return revents;
     }
 
     @Override
-    public Endpoint attach(Endpoint ep)
-    {
-        Endpoint prev = endpoint;
-        endpoint = ep;
-        return prev;
-    }
-    
-    @Override
-    public Endpoint attachment()
+    public SelectionKey events(int set)
     {
-        return endpoint;
+        ievents = set;
+        return this;
     }
-    
-}
 
+}

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/SocketSelectorImpl.java Mon May 30 04:43:53 2011
@@ -15,18 +15,24 @@
  */
 package org.apache.commons.runtime.platform.unix;
 
+import java.io.IOException;
+import java.net.SocketException;
 import java.util.ArrayList;
 import java.util.HashSet;
-import org.apache.commons.runtime.net.SelectionEvent;
+import org.apache.commons.runtime.io.ClosedDescriptorException;
+import org.apache.commons.runtime.net.Endpoint;
+import org.apache.commons.runtime.net.IllegalSelectorException;
+import org.apache.commons.runtime.net.Poll;
 import org.apache.commons.runtime.net.SelectionKey;
 import org.apache.commons.runtime.net.Selector;
-import org.apache.commons.runtime.net.SocketSelector;
 import org.apache.commons.runtime.AlreadyExistsException;
 import org.apache.commons.runtime.InvalidArgumentException;
 import org.apache.commons.runtime.NoSuchObjectException;
 import org.apache.commons.runtime.OperationNotImplementedException;
+import org.apache.commons.runtime.OverflowException;
 import org.apache.commons.runtime.SystemException;
 import org.apache.commons.runtime.Errno;
+import org.apache.commons.runtime.Status;
 
 /**
  * Socket Selector implementation class.
@@ -35,14 +41,9 @@ import org.apache.commons.runtime.Errno;
  *
  * @since Runtime 1.0
  */
-final class SocketSelectorImpl extends SocketSelector
+final class SocketSelectorImpl extends Selector
 {
 
-    private SocketSelectorImpl()
-    {
-        // No Instance
-    }
-
     private short[]             revents;
     private SelectionKeyImpl[]  keyset;
     private long                pollset;
@@ -52,23 +53,72 @@ final class SocketSelectorImpl extends S
                SystemException;
     private static native void  destroy0(long pollset);
     private static native void  wakeup0(long pollset);
+    private static native int   size0(long pollset);
     private static native int   add0(long pollset, SelectionKeyImpl key, int fd, int events, int ttl);
     private static native int   del0(long pollset, SelectionKeyImpl key, int fd);
     private static native int   clear0(long pollset, SelectionKeyImpl[] set);
     private static native int   wait0(long pollset, SelectionKeyImpl[] set, short[] events, int timeout, boolean remove);
 
+    /*
+     * Created from native
+     */
     private SocketSelectorImpl(int size)
     {
+        super(size);
         pollset = create0(size);
         revents = new short[size];
         keyset  = new SelectionKeyImpl[size];
     }
 
     @Override
+    public int size()
+        throws IllegalSelectorException
+    {
+        if (pollset == 0L)
+            throw new IllegalSelectorException();
+        return size0(pollset);
+    }
+
+    @Override
     public void interrupt()
+        throws IllegalSelectorException
     {
+        if (pollset == 0L)
+            throw new IllegalSelectorException();
         wakeup0(pollset);
     }
 
+    @Override
+    public boolean register(SelectionKey key, int ops)
+        throws IllegalSelectorException, ClosedDescriptorException, OverflowException, IOException
+    {
+        SelectionKeyImpl skey = (SelectionKeyImpl)key;
+        int fd   = skey.endpoint().descriptor().fd();
+        if (fd == -1)
+            throw new ClosedDescriptorException();
+        if (pollset == 0L)
+            throw new IllegalSelectorException();
+        int rc = add0(pollset, skey, fd, ops, skey.timeout());
+        if (rc != 0) {
+            if (rc == Errno.EALREADY)
+                return false;
+            if (rc == Errno.EOVERFLOW)
+                throw new OverflowException();
+            else
+                throw new IOException(Status.describe(rc));
+        }
+        return true;
+    }
+
+    @Override
+    public void close()
+        throws IOException
+    {
+        if (pollset == 0L)
+            throw new ClosedDescriptorException();
+        destroy0(pollset);
+        pollset = 0L;
+    }
+
 }
 

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Mon May 30 04:43:53 2011
@@ -70,6 +70,7 @@ UNIX_SOURCES=\
 	$(TOPDIR)/os/unix/posixapi.c \
 	$(TOPDIR)/os/unix/procmutex.c \
 	$(TOPDIR)/os/unix/shmem.c \
+	$(TOPDIR)/os/unix/selectkey.c \
 	$(TOPDIR)/os/unix/semaphore.c \
 	$(TOPDIR)/os/unix/time.c \
 	$(TOPDIR)/os/unix/usock.c \

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/iodefs.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/iodefs.h?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/iodefs.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/iodefs.h Mon May 30 04:43:53 2011
@@ -44,4 +44,15 @@
 #define ACR_POLLNVAL         0x40
 #define ACR_POLLTTL          0x80
 
+#define ACR_OP_ACCEPT        0x0001
+#define ACR_OP_CONNECT       0x0002
+#define ACR_OP_READ          0x0004
+#define ACR_OP_INP           0x0007
+#define ACR_OP_PRI           0x0008
+#define ACR_OP_WRITE         0x0010
+#define ACR_OP_RDHUP         0x0020
+#define ACR_OP_HANGUP        0x0040
+#define ACR_OP_ERROR         0x0100
+#define ACR_OP_TIMEOUT       0x0200
+
 #endif /* _ACR_IODEFS_H */

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c?rev=1128991&r1=1128990&r2=1128991&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c Mon May 30 04:43:53 2011
@@ -71,11 +71,11 @@ static short ieventt(int event)
 {
     short rv = 0;
 
-    if (event & ACR_POLLIN)
+    if (event & ACR_OP_INP)
         rv |= POLLIN;
-    if (event & ACR_POLLPRI)
+    if (event & ACR_OP_PRI)
         rv |= POLLPRI;
-    if (event & ACR_POLLOUT)
+    if (event & ACR_OP_WRITE)
         rv |= POLLOUT;
     /* POLLERR, POLLHUP, and POLLNVAL aren't valid as requested events
      */
@@ -87,21 +87,21 @@ static short reventt(short event)
     short rv = 0;
 
     if (event & POLLIN)
-        rv |= ACR_POLLIN;
+        rv |= ACR_OP_INP;
     if (event & POLLPRI)
-        rv |= ACR_POLLPRI;
+        rv |= ACR_OP_PRI;
     if (event & POLLOUT)
-        rv |= ACR_POLLOUT;
+        rv |= ACR_OP_WRITE;
     if (event & POLLERR)
-        rv |= ACR_POLLERR;
+        rv |= ACR_OP_ERROR;
     if (event & POLLHUP)
-        rv |= ACR_POLLHUP;
+        rv |= ACR_OP_HANGUP;
 #if defined(POLLRDHUP)
     if (event & POLLRDHUP)
-        rv |= ACR_POLLRDHUP;
+        rv |= ACR_OP_RDHUP;
 #endif
     if (event & POLLNVAL)
-        rv |= ACR_POLLNVAL;
+        rv |= ACR_OP_ERROR;
     return rv;
 }
 
@@ -111,22 +111,16 @@ ACR_NET_EXPORT(jobject, SocketSelectorFa
 {
     if (_clazzn.u == 1)
         return (*env)->NewObject(env, _clazzn.i, J4MID(0000), size);
-    if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+    else
         return 0;
-    R_LOAD_METHOD(0000, 0);
-    _clazzn.u = 1;
-    return (*env)->NewObject(env, _clazzn.i, J4MID(0000), size);
 }
 
 ACR_NET_EXPORT(jobject, LocalSocketSelectorFactory, new0)(JNI_STDARGS, jint size)
 {
     if (_clazzn.u == 1)
         return (*env)->NewObject(env, _clazzn.i, J4MID(0000), size);
-    if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+    else
         return 0;
-    R_LOAD_METHOD(0000, 0);
-    _clazzn.u = 1;
-    return (*env)->NewObject(env, _clazzn.i, J4MID(0000), size);
 }
 
 static int maxopendesc(void)
@@ -147,13 +141,25 @@ static int maxopendesc(void)
     return nm;
 }
 
-ACR_NET_EXPORT(jint, SocketSelectorFactory, nmax0)(JNI_STDARGS)
+ACR_NET_EXPORT(jint, SocketSelectorFactory, init0)(JNI_STDARGS)
 {
+    if (_clazzn.u == 0) {
+        if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+            return 0;
+        R_LOAD_METHOD(0000, 0);
+        _clazzn.u = 1;
+    }
     return maxopendesc();
 }
 
-ACR_NET_EXPORT(jint, LocalSocketSelectorFactory, nmax0)(JNI_STDARGS)
+ACR_NET_EXPORT(jint, LocalSocketSelectorFactory, init0)(JNI_STDARGS)
 {
+    if (_clazzn.u == 0) {
+        if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+            return 0;
+        R_LOAD_METHOD(0000, 0);
+        _clazzn.u = 1;
+    }
     return maxopendesc();
 }
 
@@ -300,6 +306,17 @@ ACR_UNX_EXPORT(void, SocketSelectorImpl,
     pthread_mutex_unlock(&ps->mutex);
 }
 
+ACR_UNX_EXPORT(jint, SocketSelectorImpl, size0)(JNI_STDARGS, jlong pollset)
+{
+    int rv;
+    acr_pollset_t *ps = J2P(pollset, acr_pollset_t *);
+
+    pthread_mutex_lock(&ps->mutex);
+    rv = ps->used - 1;
+    pthread_mutex_unlock(&ps->mutex);
+    return rv;
+}
+
 ACR_UNX_EXPORT(jint, SocketSelectorImpl, wait0)(JNI_STDARGS, jlong pollset,
                                                 jobjectArray rs, jshortArray revents,
                                                 jint timeout, jboolean rmsignaled)

Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c?rev=1128991&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c Mon May 30 04:43:53 2011
@@ -0,0 +1,59 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/clazz.h"
+#include "acr/jniapi.h"
+#include "acr/misc.h"
+#include "arch_opts.h"
+
+#if POLLSET_USE_POLL
+#define SELKEY_CP      ACR_UNX_CP
+#endif
+
+J_DECLARE_CLAZZ = {
+    INVALID_FIELD_OFFSET,
+    0,
+    0,
+    0,
+    SELKEY_CP "SelectionKeyImpl"
+};
+
+J_DECLARE_M_ID(0000) = {
+    0,
+    "<init>",
+    "(L" ACR_NET_CP  "Selector;L" ACR_NET_CP "Endpoint;)V"
+};
+
+ACR_NET_EXPORT(jboolean, SelectionKeyFactory, init0)(JNI_STDARGS)
+{
+    if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+        return JNI_FALSE;
+    J_LOAD_METHOD(0000);
+    _clazzn.u = 1;
+    return JNI_TRUE;    
+}
+
+ACR_NET_EXPORT(jobject, SelectionKeyFactory, new0)(JNI_STDARGS,
+                                                   jobject selector,
+                                                   jobject endpoint)
+{
+    if (_clazzn.u == 1)
+        return (*env)->NewObject(env, _clazzn.i, J4MID(0000), selector, endpoint);
+    else
+        return 0;
+}
+
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/selectkey.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java?rev=1128991&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java (added)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java Mon May 30 04:43:53 2011
@@ -0,0 +1,42 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.net;
+
+import java.io.IOException;
+import java.net.SocketException;
+import org.testng.annotations.*;
+import org.testng.Assert;
+
+public class TestSelectionKey extends Assert
+{
+
+    @Test(groups = { "core" })
+    public void createSelectionKey()
+        throws IOException
+    {
+        // Create socket bound to the first free port
+        //
+        SocketEndpoint ep = new SocketEndpoint();
+        Selector ss = SocketSelectorFactory.newInstance();
+        assertNotNull(ss);
+        System.out.println("SocketSelector capacity=" + ss.capacity());
+        SelectionKey skey = ep.key(ss);
+        assertNotNull(skey);
+        ss.close();
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSelectionKey.java
------------------------------------------------------------------------------
    svn:eol-style = native