jdk/src/share/classes/sun/nio/ch/ServerSocketAdaptor.java
author xdono
Thu, 02 Oct 2008 19:58:32 -0700
changeset 1247 b4c26443dee5
parent 1152 29d6145d1097
child 5506 202f599c92aa
permissions -rw-r--r--
6754988: Update copyright year Summary: Update for files that have been modified starting July 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1247
b4c26443dee5 6754988: Update copyright year
xdono
parents: 1152
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
// Make a server-socket channel look like a server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
// The methods in this class are defined in exactly the same order as in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
// java.net.ServerSocket so as to simplify tracking future changes to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
// class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class ServerSocketAdaptor                        // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    extends ServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // The channel being adapted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final ServerSocketChannelImpl ssc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // Timeout "option" value for accepts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private volatile int timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static ServerSocket create(ServerSocketChannelImpl ssc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            return new ServerSocketAdaptor(ssc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // ## super will create a useless impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private ServerSocketAdaptor(ServerSocketChannelImpl ssc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.ssc = ssc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public void bind(SocketAddress local) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        bind(local, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public void bind(SocketAddress local, int backlog) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (local == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            local = new InetSocketAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            ssc.bind(local, backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (!ssc.isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return Net.asInetSocketAddress(ssc.localAddress()).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (!ssc.isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return Net.asInetSocketAddress(ssc.localAddress()).getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public Socket accept() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        synchronized (ssc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (!ssc.isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (timeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    SocketChannel sc = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    if (sc == null && !ssc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    return sc.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                // Implement timeout with a selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                SelectionKey sk = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                Selector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                ssc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    SocketChannel sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    if ((sc = ssc.accept()) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        return sc.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    sel = Util.getTemporarySelector(ssc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    sk = ssc.register(sel, SelectionKey.OP_ACCEPT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    long to = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        if (!ssc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        long st = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        int ns = sel.select(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        if (ns > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            sk.isAcceptable() && ((sc = ssc.accept()) != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                            return sc.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        if (to <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                            throw new SocketTimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    if (sk != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        sk.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    if (ssc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        ssc.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    if (sel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        Util.releaseTemporarySelector(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                return null;            // Never happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public ServerSocketChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return ssc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return ssc.isBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return !ssc.isOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        this.timeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int getSoTimeout() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void setReuseAddress(boolean on) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   175
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   176
            ssc.setOption(StandardSocketOption.SO_REUSEADDR, on);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   177
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   178
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   179
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public boolean getReuseAddress() throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   183
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   184
            return ssc.getOption(StandardSocketOption.SO_REUSEADDR).booleanValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   185
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   186
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   187
            return false;       // Never happens
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   188
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return "ServerSocket[unbound]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return "ServerSocket[addr=" + getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            //          ",port=" + getPort() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                ",localport=" + getLocalPort()  + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public void setReceiveBufferSize(int size) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   200
        // size 0 valid for ServerSocketChannel, invalid for ServerSocket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   201
        if (size <= 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   202
            throw new IllegalArgumentException("size cannot be 0 or negative");
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   203
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   204
            ssc.setOption(StandardSocketOption.SO_RCVBUF, size);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   205
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   206
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   207
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public int getReceiveBufferSize() throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   211
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   212
            return ssc.getOption(StandardSocketOption.SO_RCVBUF).intValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   213
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   214
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   215
            return -1;          // Never happens
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   216
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}