jdk/src/java.base/share/classes/sun/net/ext/ExtendedSocketOptions.java
author chegar
Wed, 27 Apr 2016 20:36:02 +0100
changeset 37676 24ef455da1b0
permissions -rw-r--r--
8044773: Refactor jdk.net API so that it can be moved out of the base module Reviewed-by: alanb, erikj, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
     1
/*
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
     4
 *
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    10
 *
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    15
 * accompanied this code).
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    16
 *
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    20
 *
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    23
 * questions.
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    24
 */
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    25
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    26
package sun.net.ext;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    27
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    28
import java.io.FileDescriptor;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    29
import java.net.SocketException;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    30
import java.net.SocketOption;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    31
import java.util.Collections;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    32
import java.util.Set;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    33
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    34
/**
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    35
 * Defines the infrastructure to support extended socket options, beyond those
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    36
 * defined in {@link java.net.StandardSocketOptions}.
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    37
 *
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    38
 * Extended socket options are accessed through the jdk.net API, which is in
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    39
 * the jdk.net module.
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    40
 */
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    41
public abstract class ExtendedSocketOptions {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    42
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    43
    private final Set<SocketOption<?>> options;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    44
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    45
    /** Tells whether or not the option is supported. */
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    46
    public final boolean isOptionSupported(SocketOption<?> option) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    47
        return options().contains(option);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    48
    }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    49
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    50
    /** Return the, possibly empty, set of extended socket options available. */
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    51
    public final Set<SocketOption<?>> options() { return options; }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    52
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    53
    /** Sets the value of a socket option, for the given socket. */
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    54
    public abstract void setOption(FileDescriptor fd, SocketOption<?> option, Object value)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    55
            throws SocketException;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    56
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    57
    /** Returns the value of a socket option, for the given socket. */
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    58
    public abstract Object getOption(FileDescriptor fd, SocketOption<?> option)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    59
            throws SocketException;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    60
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    61
    protected ExtendedSocketOptions(Set<SocketOption<?>> options) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    62
        this.options = options;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    63
    }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    64
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    65
    private static volatile ExtendedSocketOptions instance;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    66
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    67
    public static final ExtendedSocketOptions getInstance() { return instance; }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    68
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    69
    /** Registers support for extended socket options. Invoked by the jdk.net module. */
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    70
    public static final void register(ExtendedSocketOptions extOptions) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    71
        if (instance != null)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    72
            throw new InternalError("Attempting to reregister extended options");
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    73
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    74
        instance = extOptions;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    75
    }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    76
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    77
    static {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    78
        try {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    79
            // If the class is present, it will be initialized which
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    80
            // triggers registration of the extended socket options.
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    81
            Class<?> c = Class.forName("jdk.net.ExtendedSocketOptions");
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    82
        } catch (ClassNotFoundException e) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    83
            // the jdk.net module is not present => no extended socket options
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    84
            instance = new NoExtendedSocketOptions();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    85
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    86
    }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    87
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    88
    static final class NoExtendedSocketOptions extends ExtendedSocketOptions {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    89
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    90
        NoExtendedSocketOptions() {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    91
            super(Collections.<SocketOption<?>>emptySet());
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    92
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    93
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    94
        @Override
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    95
        public void setOption(FileDescriptor fd, SocketOption<?> option, Object value)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    96
            throws SocketException
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    97
        {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    98
            throw new UnsupportedOperationException(
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
    99
                    "no extended options: " + option.name());
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   100
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   101
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   102
        @Override
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   103
        public Object getOption(FileDescriptor fd, SocketOption<?> option)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   104
            throws SocketException
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   105
        {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   106
            throw new UnsupportedOperationException(
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   107
                    "no extended options: " + option.name());
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   108
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   109
    }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
diff changeset
   110
}