jdk/src/java.base/unix/classes/java/net/PlainSocketImpl.java
changeset 37676 24ef455da1b0
parent 36115 0676e37a0b9c
equal deleted inserted replaced
37675:a9be5f4baa63 37676:24ef455da1b0
    26 
    26 
    27 import java.io.IOException;
    27 import java.io.IOException;
    28 import java.io.FileDescriptor;
    28 import java.io.FileDescriptor;
    29 import java.util.Set;
    29 import java.util.Set;
    30 import java.util.HashSet;
    30 import java.util.HashSet;
    31 import java.util.Collections;
    31 import sun.net.ext.ExtendedSocketOptions;
    32 import jdk.net.*;
       
    33 
       
    34 import static sun.net.ExtendedOptionsImpl.*;
       
    35 
    32 
    36 /*
    33 /*
    37  * On Unix systems we simply delegate to native methods.
    34  * On Unix systems we simply delegate to native methods.
    38  *
    35  *
    39  * @author Chris Hegarty
    36  * @author Chris Hegarty
    55      */
    52      */
    56     PlainSocketImpl(FileDescriptor fd) {
    53     PlainSocketImpl(FileDescriptor fd) {
    57         this.fd = fd;
    54         this.fd = fd;
    58     }
    55     }
    59 
    56 
       
    57     static final ExtendedSocketOptions extendedOptions =
       
    58             ExtendedSocketOptions.getInstance();
       
    59 
    60     protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
    60     protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
    61         if (!name.equals(ExtendedSocketOptions.SO_FLOW_SLA)) {
    61         if (!extendedOptions.isOptionSupported(name)) {
    62             if (!name.equals(StandardSocketOptions.SO_REUSEPORT)) {
    62             if (!name.equals(StandardSocketOptions.SO_REUSEPORT)) {
    63                 super.setOption(name, value);
    63                 super.setOption(name, value);
    64             } else {
    64             } else {
    65                 if (supportedOptions().contains(name)) {
    65                 if (supportedOptions().contains(name)) {
    66                     super.setOption(name, value);
    66                     super.setOption(name, value);
    67                 } else {
    67                 } else {
    68                     throw new UnsupportedOperationException("unsupported option");
    68                     throw new UnsupportedOperationException("unsupported option");
    69                 }
    69                 }
    70             }
    70             }
    71         } else {
    71         } else {
    72             if (getSocket() == null || !flowSupported()) {
    72             if (getSocket() == null) {
    73                 throw new UnsupportedOperationException("unsupported option");
    73                 throw new UnsupportedOperationException("unsupported option");
    74             }
    74             }
    75             if (isClosedOrPending()) {
    75             if (isClosedOrPending()) {
    76                 throw new SocketException("Socket closed");
    76                 throw new SocketException("Socket closed");
    77             }
    77             }
    78             checkSetOptionPermission(name);
    78             extendedOptions.setOption(fd, name, value);
    79             checkValueType(value, SocketFlow.class);
       
    80             setFlowOption(getFileDescriptor(), (SocketFlow)value);
       
    81         }
    79         }
    82     }
    80     }
    83 
    81 
    84     @SuppressWarnings("unchecked")
    82     @SuppressWarnings("unchecked")
    85     protected <T> T getOption(SocketOption<T> name) throws IOException {
    83     protected <T> T getOption(SocketOption<T> name) throws IOException {
    86         if (!name.equals(ExtendedSocketOptions.SO_FLOW_SLA)) {
    84         if (!extendedOptions.isOptionSupported(name)) {
    87             if (!name.equals(StandardSocketOptions.SO_REUSEPORT)) {
    85             if (!name.equals(StandardSocketOptions.SO_REUSEPORT)) {
    88                 return super.getOption(name);
    86                 return super.getOption(name);
    89             } else {
    87             } else {
    90                 if (supportedOptions().contains(name)) {
    88                 if (supportedOptions().contains(name)) {
    91                     return super.getOption(name);
    89                     return super.getOption(name);
    92                 } else {
    90                 } else {
    93                     throw new UnsupportedOperationException("unsupported option");
    91                     throw new UnsupportedOperationException("unsupported option");
    94                 }
    92                 }
    95             }
    93             }
       
    94         } else {
       
    95             if (getSocket() == null) {
       
    96                 throw new UnsupportedOperationException("unsupported option");
       
    97             }
       
    98             if (isClosedOrPending()) {
       
    99                 throw new SocketException("Socket closed");
       
   100             }
       
   101             return (T) extendedOptions.getOption(fd, name);
    96         }
   102         }
    97         if (getSocket() == null || !flowSupported()) {
       
    98             throw new UnsupportedOperationException("unsupported option");
       
    99         }
       
   100         if (isClosedOrPending()) {
       
   101             throw new SocketException("Socket closed");
       
   102         }
       
   103         checkGetOptionPermission(name);
       
   104         SocketFlow flow = SocketFlow.create();
       
   105         getFlowOption(getFileDescriptor(), flow);
       
   106         return (T)flow;
       
   107     }
   103     }
   108 
   104 
   109     protected Set<SocketOption<?>> supportedOptions() {
   105     protected Set<SocketOption<?>> supportedOptions() {
   110         HashSet<SocketOption<?>> options = new HashSet<>(
   106         HashSet<SocketOption<?>> options = new HashSet<>(super.supportedOptions());
   111             super.supportedOptions());
   107         if (getSocket() != null) {
   112 
   108             options.addAll(extendedOptions.options());
   113         if (getSocket() != null && flowSupported()) {
       
   114             options.add(ExtendedSocketOptions.SO_FLOW_SLA);
       
   115         }
   109         }
   116         return options;
   110         return options;
   117     }
   111     }
   118 
   112 
   119     protected void socketSetOption(int opt, boolean b, Object val) throws SocketException {
   113     protected void socketSetOption(int opt, boolean b, Object val) throws SocketException {
   120         if (opt == SocketOptions.SO_REUSEPORT && !supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
   114         if (opt == SocketOptions.SO_REUSEPORT &&
       
   115             !supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
   121             throw new UnsupportedOperationException("unsupported option");
   116             throw new UnsupportedOperationException("unsupported option");
   122         }
   117         }
   123         try {
   118         try {
   124             socketSetOption0(opt, b, val);
   119             socketSetOption0(opt, b, val);
   125         } catch (SocketException se) {
   120         } catch (SocketException se) {