1 /*
   2  * Copyright (c) 2018, 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 RDMA socket options.
  36  *
  37  * Extended socket options are accessed through the jdk.net API, which is in
  38  * the jdk.net module.
  39  */
  40 public abstract class RdmaSocketOptions {
  41 
  42     private final Set<SocketOption<?>> options;
  43 
  44     /** Tells whether or not the option is supported. */
  45     public final boolean isOptionSupported(SocketOption<?> option) {
  46         return options().contains(option);
  47     }
  48 
  49     /** Return the, possibly empty, set of extended socket options available. */
  50     public final Set<SocketOption<?>> options() { return options; }
  51 
  52     /** Sets the value of a socket option, for the given socket. */
  53     public abstract void setOption(FileDescriptor fd, SocketOption<?> option, Object value)
  54             throws SocketException;
  55 
  56     /** Returns the value of a socket option, for the given socket. */
  57     public abstract Object getOption(FileDescriptor fd, SocketOption<?> option)
  58             throws SocketException;
  59 
  60     protected RdmaSocketOptions(Set<SocketOption<?>> options) {
  61         this.options = options;
  62     }
  63 
  64     private static volatile RdmaSocketOptions instance;
  65 
  66     public static final RdmaSocketOptions getInstance() { return instance; }
  67 
  68     /** Registers support for extended socket options. Invoked by the jdk.net module. */
  69     public static final void register(RdmaSocketOptions rdmaOptions) {
  70         if (instance != null)
  71             throw new InternalError("Attempting to reregister RDMA options");
  72 
  73         instance = rdmaOptions;
  74     }
  75 
  76     static {
  77         try {
  78             // If the class is present, it will be initialized which
  79             // triggers registration of the extended socket options.
  80             Class<?> c = Class.forName("jdk.net.RdmaSocketOptions");
  81         } catch (ClassNotFoundException e) {
  82             // the jdk.net module is not present => no extended socket options
  83             instance = new NoRdmaSocketOptions();
  84         }
  85     }
  86 
  87     static final class NoRdmaSocketOptions extends RdmaSocketOptions {
  88 
  89         NoRdmaSocketOptions() {
  90             super(Collections.<SocketOption<?>>emptySet());
  91         }
  92 
  93         @Override
  94         public void setOption(FileDescriptor fd, SocketOption<?> option, Object value)
  95             throws SocketException
  96         {
  97             throw new UnsupportedOperationException(
  98                     "no extended options: " + option.name());
  99         }
 100 
 101         @Override
 102         public Object getOption(FileDescriptor fd, SocketOption<?> option)
 103             throws SocketException
 104         {
 105             throw new UnsupportedOperationException(
 106                     "no extended options: " + option.name());
 107         }
 108     }
 109 }