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