1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 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 sun.net.ext; 27 28 import java.io.FileDescriptor; 29 import java.net.SocketException; 30 import java.net.SocketOption; 31 import java.util.Collections; 32 import java.util.Set; 33 34 /** 35 * Defines the infrastructure to support extended socket options, beyond those 36 * defined in {@link java.net.StandardSocketOptions}. 37 * 38 * Extended socket options are accessed through the jdk.net API, which is in 39 * the jdk.net module. 40 */ 41 public abstract class ExtendedSocketOptions { 42 43 private final Set<SocketOption<?>> options; 44 45 /** Tells whether or not the option is supported. */ 46 public final boolean isOptionSupported(SocketOption<?> option) { 47 return options().contains(option); 48 } 49 50 /** Return the, possibly empty, set of extended socket options available. */ 51 public final Set<SocketOption<?>> options() { return options; } 52 53 /** Sets the value of a socket option, for the given socket. */ 54 public abstract void setOption(FileDescriptor fd, SocketOption<?> option, Object value) 55 throws SocketException; 56 57 /** Returns the value of a socket option, for the given socket. */ 58 public abstract Object getOption(FileDescriptor fd, SocketOption<?> option) 59 throws SocketException; 60 61 protected ExtendedSocketOptions(Set<SocketOption<?>> options) { 62 this.options = options; 63 } 64 65 private static volatile ExtendedSocketOptions instance; 66 67 public static final ExtendedSocketOptions getInstance() { return instance; } 68 69 /** Registers support for extended socket options. Invoked by the jdk.net module. */ 70 public static final void register(ExtendedSocketOptions extOptions) { 71 if (instance != null) 72 throw new InternalError("Attempting to reregister extended options"); 73 74 instance = extOptions; 75 } 76 77 static { 78 try { 79 // If the class is present, it will be initialized which 80 // triggers registration of the extended socket options. 81 Class<?> c = Class.forName("jdk.net.ExtendedSocketOptions"); 82 } catch (ClassNotFoundException e) { 83 // the jdk.net module is not present => no extended socket options 84 instance = new NoExtendedSocketOptions(); 85 } 86 } 87 88 static final class NoExtendedSocketOptions extends ExtendedSocketOptions { 89 90 NoExtendedSocketOptions() { 91 super(Collections.unmodifiableSet(Collections.<SocketOption<?>>emptySet())); 92 } 93 94 @Override 95 public void setOption(FileDescriptor fd, SocketOption<?> option, Object value) 96 throws SocketException 97 { 98 throw new UnsupportedOperationException("no extended options"); 99 } 100 101 @Override 102 public Object getOption(FileDescriptor fd, SocketOption<?> option) 103 throws SocketException 104 { 105 throw new UnsupportedOperationException("no extended options"); 106 } 107 } 108 }