< prev index next >

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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 



  28 import java.io.FileDescriptor;
  29 import java.io.IOException;


  30 import java.nio.channels.ServerSocketChannel;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedExceptionAction;
  33 import java.util.Set;
  34 import java.util.Collections;
  35 
  36 /**
  37  * This class implements server sockets. A server socket waits for
  38  * requests to come in over the network. It performs some operation
  39  * based on that request, and then possibly returns a result to the requester.
  40  * <p>
  41  * The actual work of the server socket is performed by an instance
  42  * of the {@code SocketImpl} class. An application can
  43  * change the socket factory that creates the socket
  44  * implementation to configure itself to create sockets
  45  * appropriate to the local firewall.
  46  *
  47  * @author  unascribed
  48  * @see     java.net.SocketImpl
  49  * @see     java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)


 994      * @return A set of the socket options supported by this socket. This set
 995      *         may be empty if the socket's SocketImpl cannot be created.
 996      *
 997      * @since 9
 998      */
 999     public Set<SocketOption<?>> supportedOptions() {
1000         synchronized (ServerSocket.class) {
1001             if (optionsSet) {
1002                 return options;
1003             }
1004             try {
1005                 SocketImpl impl = getImpl();
1006                 options = Collections.unmodifiableSet(impl.supportedOptions());
1007             } catch (IOException e) {
1008                 options = Collections.emptySet();
1009             }
1010             optionsSet = true;
1011             return options;
1012         }
1013     }























1014 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import jdk.internal.misc.JavaNetSocketAccess;
  29 import jdk.internal.misc.SharedSecrets;
  30 
  31 import java.io.FileDescriptor;
  32 import java.io.IOException;
  33 import java.lang.reflect.Constructor;
  34 import java.lang.reflect.InvocationTargetException;
  35 import java.nio.channels.ServerSocketChannel;
  36 import java.security.AccessController;
  37 import java.security.PrivilegedExceptionAction;
  38 import java.util.Set;
  39 import java.util.Collections;
  40 
  41 /**
  42  * This class implements server sockets. A server socket waits for
  43  * requests to come in over the network. It performs some operation
  44  * based on that request, and then possibly returns a result to the requester.
  45  * <p>
  46  * The actual work of the server socket is performed by an instance
  47  * of the {@code SocketImpl} class. An application can
  48  * change the socket factory that creates the socket
  49  * implementation to configure itself to create sockets
  50  * appropriate to the local firewall.
  51  *
  52  * @author  unascribed
  53  * @see     java.net.SocketImpl
  54  * @see     java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)


 999      * @return A set of the socket options supported by this socket. This set
1000      *         may be empty if the socket's SocketImpl cannot be created.
1001      *
1002      * @since 9
1003      */
1004     public Set<SocketOption<?>> supportedOptions() {
1005         synchronized (ServerSocket.class) {
1006             if (optionsSet) {
1007                 return options;
1008             }
1009             try {
1010                 SocketImpl impl = getImpl();
1011                 options = Collections.unmodifiableSet(impl.supportedOptions());
1012             } catch (IOException e) {
1013                 options = Collections.emptySet();
1014             }
1015             optionsSet = true;
1016             return options;
1017         }
1018     }
1019 
1020     static {
1021         SharedSecrets.setJavaNetSocketAccess(
1022             new JavaNetSocketAccess() {
1023                 @Override
1024                 public ServerSocket newServerSocket(SocketImpl impl) {
1025                     return new ServerSocket(impl);
1026                 }
1027 
1028                 @Override
1029                 public SocketImpl newSocketImpl(Class<? extends SocketImpl> implClass) {
1030                     try {
1031                         Constructor<? extends SocketImpl> ctor =
1032                             implClass.getDeclaredConstructor();
1033                         return ctor.newInstance();
1034                     } catch (NoSuchMethodException | InstantiationException |
1035                              IllegalAccessException | InvocationTargetException e) {
1036                         throw new AssertionError(e);
1037                     }
1038                 }
1039             }
1040         );
1041     }
1042 }
< prev index next >