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 jdk.internal.net.rdma;
  27 
  28 import java.io.IOException;
  29 import java.net.ProtocolFamily;
  30 import java.nio.channels.ServerSocketChannel;
  31 import java.nio.channels.SocketChannel;
  32 import java.nio.channels.spi.AbstractSelector;
  33 import java.nio.channels.spi.SelectorProvider;
  34 import sun.nio.ch.SelectorProviderImpl;
  35 import java.security.AccessController;
  36 import java.security.PrivilegedAction;
  37 
  38 public class RdmaPollSelectorProvider
  39     extends SelectorProviderImpl
  40 {
  41     private static final Object lock = new Object();
  42     private static SelectorProvider provider = null;
  43 
  44     private static PlatformRdmaPollSelectorProvider pPollSelectorProvider =
  45             PlatformRdmaPollSelectorProvider.get();
  46 
  47     public static SelectorProvider provider() {
  48         synchronized (lock) {
  49             if (provider != null)
  50                 return provider;
  51             return AccessController.doPrivileged(
  52                 new PrivilegedAction<>() {
  53                     public SelectorProvider run() {
  54                             provider = new RdmaPollSelectorProvider();
  55                             return provider;
  56                         }
  57                     });
  58         }
  59     }
  60 
  61     public AbstractSelector openSelector() throws IOException {
  62         return pPollSelectorProvider.openSelector(this);
  63     }
  64 
  65     public SocketChannel openSocketChannel(ProtocolFamily family)
  66             throws IOException {
  67         return pPollSelectorProvider.openSocketChannel(this, family);
  68     }
  69 
  70     public ServerSocketChannel openServerSocketChannel(ProtocolFamily family)
  71             throws IOException {
  72         return pPollSelectorProvider.openServerSocketChannel(this, family);
  73     }
  74 
  75     static class PlatformRdmaPollSelectorProvider {
  76 
  77         @SuppressWarnings("unchecked")
  78         private static PlatformRdmaPollSelectorProvider newInstance(String cn) {
  79             Class<PlatformRdmaPollSelectorProvider> c;
  80             try {
  81                 c = (Class<PlatformRdmaPollSelectorProvider>)Class.forName(cn);
  82                 return c.getConstructor(new Class<?>[] {}).newInstance();
  83             } catch (ReflectiveOperationException x) {
  84                 throw new AssertionError(x);
  85             }
  86         }
  87 
  88         private static PlatformRdmaPollSelectorProvider create() {
  89             String osname = AccessController.doPrivileged(
  90                     new PrivilegedAction<String>() {
  91                         public String run() {
  92                             return System.getProperty("os.name");
  93                         }
  94                     });
  95             if ("Linux".equals(osname))
  96                 return newInstance(
  97                         "jdk.internal.net.rdma.LinuxRdmaPollSelectorProvider");
  98             return new PlatformRdmaPollSelectorProvider();
  99         }
 100 
 101         private static final PlatformRdmaPollSelectorProvider instance = create();
 102 
 103         static PlatformRdmaPollSelectorProvider get() {
 104             return instance;
 105         }
 106    
 107         public AbstractSelector openSelector(RdmaPollSelectorProvider sp) 
 108                 throws IOException {
 109             throw new UnsupportedOperationException(
 110                     "unsupported socket operation");
 111         }
 112 
 113         public SocketChannel openSocketChannel(RdmaPollSelectorProvider sp,
 114                                                ProtocolFamily family)
 115                 throws IOException {
 116             throw new UnsupportedOperationException(
 117                     "unsupported socket operation");
 118         }
 119 
 120         public ServerSocketChannel openServerSocketChannel(RdmaPollSelectorProvider sp,
 121                                                            ProtocolFamily family)
 122                 throws IOException {
 123             throw new UnsupportedOperationException(
 124                     "unsupported socket operation");
 125         }
 126     }
 127 }