< prev index next >

src/java.base/share/classes/java/net/ServerSocket.java

Print this page




  59 class ServerSocket implements java.io.Closeable {
  60     /**
  61      * Various states of this socket.
  62      */
  63     private boolean created = false;
  64     private boolean bound = false;
  65     private boolean closed = false;
  66     private Object closeLock = new Object();
  67 
  68     /**
  69      * The implementation of this Socket.
  70      */
  71     private SocketImpl impl;
  72 
  73     /**
  74      * Are we using an older SocketImpl?
  75      */
  76     private boolean oldImpl = false;
  77 
  78     /**
  79      * Package-private constructor to create a ServerSocket associated with
  80      * the given SocketImpl.

  81      */
  82     ServerSocket(SocketImpl impl) {
  83         this.impl = impl;
  84         impl.setServerSocket(this);
  85     }
  86 
  87     /**
  88      * Creates an unbound server socket.
  89      *
  90      * @exception IOException IO error when opening the socket.
  91      * @revised 1.4
  92      */
  93     public ServerSocket() throws IOException {
  94         setImpl();
  95     }
  96 
  97     /**
  98      * Creates a server socket, bound to the specified port. A port number
  99      * of {@code 0} means that the port number is automatically
 100      * allocated, typically from an ephemeral port range. This port
 101      * number can then be retrieved by calling {@link #getLocalPort getLocalPort}.
 102      * <p>


 973      *         support the option.
 974      *
 975      * @throws IOException if an I/O error occurs, or if the socket is closed.
 976      *
 977      * @throws NullPointerException if name is {@code null}
 978      *
 979      * @throws SecurityException if a security manager is set and if the socket
 980      *         option requires a security permission and if the caller does
 981      *         not have the required permission.
 982      *         {@link java.net.StandardSocketOptions StandardSocketOptions}
 983      *         do not require any security permission.
 984      *
 985      * @since 9
 986      */
 987     public <T> T getOption(SocketOption<T> name) throws IOException {
 988         return getImpl().getOption(name);
 989     }
 990 
 991     private static Set<SocketOption<?>> options;
 992     private static boolean optionsSet = false;

 993 
 994     /**
 995      * Returns a set of the socket options supported by this server socket.
 996      *
 997      * This method will continue to return the set of options even after
 998      * the socket has been closed.
 999      *
1000      * @return A set of the socket options supported by this socket. This set
1001      *         may be empty if the socket's SocketImpl cannot be created.
1002      *
1003      * @since 9
1004      */
1005     public Set<SocketOption<?>> supportedOptions() {
1006         synchronized (ServerSocket.class) {
1007             if (optionsSet) {


1008                 return options;
1009             }
1010             try {
1011                 SocketImpl impl = getImpl();
1012                 options = Collections.unmodifiableSet(impl.supportedOptions());
1013             } catch (IOException e) {
1014                 options = Collections.emptySet();
1015             }





1016             optionsSet = true;

1017             return options;
1018         }
1019     }
1020 
1021     static {
1022         SharedSecrets.setJavaNetSocketAccess(
1023             new JavaNetSocketAccess() {
1024                 @Override
1025                 public ServerSocket newServerSocket(SocketImpl impl) {
1026                     return new ServerSocket(impl);
1027                 }
1028 
1029                 @Override
1030                 public SocketImpl newSocketImpl(Class<? extends SocketImpl> implClass) {
1031                     try {
1032                         Constructor<? extends SocketImpl> ctor =
1033                             implClass.getDeclaredConstructor();
1034                         return ctor.newInstance();
1035                     } catch (NoSuchMethodException | InstantiationException |
1036                              IllegalAccessException | InvocationTargetException e) {


  59 class ServerSocket implements java.io.Closeable {
  60     /**
  61      * Various states of this socket.
  62      */
  63     private boolean created = false;
  64     private boolean bound = false;
  65     private boolean closed = false;
  66     private Object closeLock = new Object();
  67 
  68     /**
  69      * The implementation of this Socket.
  70      */
  71     private SocketImpl impl;
  72 
  73     /**
  74      * Are we using an older SocketImpl?
  75      */
  76     private boolean oldImpl = false;
  77 
  78     /**
  79      * Creates a server socket with a user-specified SocketImpl.
  80      *
  81      * @param      impl the user-specified SocketImpl
  82      */
  83     protected ServerSocket(SocketImpl impl) {
  84         this.impl = impl;
  85         impl.setServerSocket(this);
  86     }
  87 
  88     /**
  89      * Creates an unbound server socket.
  90      *
  91      * @exception IOException IO error when opening the socket.
  92      * @revised 1.4
  93      */
  94     public ServerSocket() throws IOException {
  95         setImpl();
  96     }
  97 
  98     /**
  99      * Creates a server socket, bound to the specified port. A port number
 100      * of {@code 0} means that the port number is automatically
 101      * allocated, typically from an ephemeral port range. This port
 102      * number can then be retrieved by calling {@link #getLocalPort getLocalPort}.
 103      * <p>


 974      *         support the option.
 975      *
 976      * @throws IOException if an I/O error occurs, or if the socket is closed.
 977      *
 978      * @throws NullPointerException if name is {@code null}
 979      *
 980      * @throws SecurityException if a security manager is set and if the socket
 981      *         option requires a security permission and if the caller does
 982      *         not have the required permission.
 983      *         {@link java.net.StandardSocketOptions StandardSocketOptions}
 984      *         do not require any security permission.
 985      *
 986      * @since 9
 987      */
 988     public <T> T getOption(SocketOption<T> name) throws IOException {
 989         return getImpl().getOption(name);
 990     }
 991 
 992     private static Set<SocketOption<?>> options;
 993     private static boolean optionsSet = false;
 994     private static boolean rdmaOptionsSet = false;
 995 
 996     /**
 997      * Returns a set of the socket options supported by this server socket.
 998      *
 999      * This method will continue to return the set of options even after
1000      * the socket has been closed.
1001      *
1002      * @return A set of the socket options supported by this socket. This set
1003      *         may be empty if the socket's SocketImpl cannot be created.
1004      *
1005      * @since 9
1006      */
1007     public Set<SocketOption<?>> supportedOptions() {
1008         synchronized (ServerSocket.class) {
1009             String currentImpl = impl.getClass().getName();
1010             boolean rdma = currentImpl.equals("rdma.ch.RdmaSocketImpl");
1011             if ((rdma && rdmaOptionsSet) || (!rdma && optionsSet)) {
1012                 return options;
1013             }
1014             try {
1015                 SocketImpl impl = getImpl();
1016                 options = Collections.unmodifiableSet(impl.supportedOptions());
1017             } catch (IOException e) {
1018                 options = Collections.emptySet();
1019             }
1020             if (rdma) {
1021                 rdmaOptionsSet = true;
1022                 optionsSet = false;
1023             } else {
1024                 rdmaOptionsSet = false;
1025                 optionsSet = true;
1026             }
1027             return options;
1028         }
1029     }
1030 
1031     static {
1032         SharedSecrets.setJavaNetSocketAccess(
1033             new JavaNetSocketAccess() {
1034                 @Override
1035                 public ServerSocket newServerSocket(SocketImpl impl) {
1036                     return new ServerSocket(impl);
1037                 }
1038 
1039                 @Override
1040                 public SocketImpl newSocketImpl(Class<? extends SocketImpl> implClass) {
1041                     try {
1042                         Constructor<? extends SocketImpl> ctor =
1043                             implClass.getDeclaredConstructor();
1044                         return ctor.newInstance();
1045                     } catch (NoSuchMethodException | InstantiationException |
1046                              IllegalAccessException | InvocationTargetException e) {
< prev index next >