2736N/A/*
2736N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2736N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2736N/A *
2736N/A * This code is free software; you can redistribute it and/or modify it
2736N/A * under the terms of the GNU General Public License version 2 only, as
2736N/A * published by the Free Software Foundation. Oracle designates this
2736N/A * particular file as subject to the "Classpath" exception as provided
2736N/A * by Oracle in the LICENSE file that accompanied this code.
2736N/A *
2736N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2736N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2736N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2736N/A * version 2 for more details (a copy is included in the LICENSE file that
2736N/A * accompanied this code).
2736N/A *
2736N/A * You should have received a copy of the GNU General Public License version
2736N/A * 2 along with this work; if not, write to the Free Software Foundation,
2736N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2736N/A *
2736N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2736N/A * or visit www.oracle.com if you need additional information or have any
2736N/A * questions.
2736N/A */
2736N/A
2736N/Apackage com.oracle.net;
2736N/A
2736N/Aimport java.net.Socket;
2736N/Aimport java.net.ServerSocket;
2736N/Aimport java.net.SocketImpl;
2736N/Aimport java.net.SocketImplFactory;
2736N/Aimport java.net.SocketException;
2736N/Aimport java.nio.channels.SocketChannel;
2736N/Aimport java.nio.channels.ServerSocketChannel;
2736N/Aimport java.io.IOException;
2736N/Aimport java.io.FileDescriptor;
2736N/Aimport java.security.AccessController;
2736N/Aimport java.security.PrivilegedAction;
2736N/Aimport java.lang.reflect.Constructor;
2736N/Aimport java.lang.reflect.AccessibleObject;
2736N/Aimport java.lang.reflect.InvocationTargetException;
2736N/A
2736N/Aimport sun.net.sdp.SdpSupport;
2736N/A
2736N/A/**
2736N/A * This class consists exclusively of static methods that Sockets or Channels to
2736N/A * sockets that support the InfiniBand Sockets Direct Protocol (SDP).
2736N/A */
2736N/A
2736N/Apublic final class Sdp {
2736N/A private Sdp() { }
2736N/A
2736N/A /**
2736N/A * The package-privage ServerSocket(SocketImpl) constructor
2736N/A */
2736N/A private static final Constructor<ServerSocket> serverSocketCtor;
2736N/A static {
2736N/A try {
2736N/A serverSocketCtor = (Constructor<ServerSocket>)
2736N/A ServerSocket.class.getDeclaredConstructor(SocketImpl.class);
2736N/A setAccessible(serverSocketCtor);
2736N/A } catch (NoSuchMethodException e) {
2736N/A throw new AssertionError(e);
2736N/A }
2736N/A }
2736N/A
2736N/A /**
2736N/A * The package-private SdpSocketImpl() constructor
2736N/A */
2736N/A private static final Constructor<SocketImpl> socketImplCtor;
2736N/A static {
2736N/A try {
2736N/A Class<?> cl = Class.forName("java.net.SdpSocketImpl", true, null);
2736N/A socketImplCtor = (Constructor<SocketImpl>)cl.getDeclaredConstructor();
2736N/A setAccessible(socketImplCtor);
2736N/A } catch (ClassNotFoundException e) {
2736N/A throw new AssertionError(e);
2736N/A } catch (NoSuchMethodException e) {
2736N/A throw new AssertionError(e);
2736N/A }
2736N/A }
2736N/A
2736N/A private static void setAccessible(final AccessibleObject o) {
2736N/A AccessController.doPrivileged(new PrivilegedAction<Void>() {
2736N/A public Void run() {
2736N/A o.setAccessible(true);
2736N/A return null;
2736N/A }
2736N/A });
2736N/A }
2736N/A
2736N/A /**
2736N/A * SDP enabled Socket.
2736N/A */
2736N/A private static class SdpSocket extends Socket {
2736N/A SdpSocket(SocketImpl impl) throws SocketException {
2736N/A super(impl);
2736N/A }
2736N/A }
2736N/A
2736N/A /**
2736N/A * Creates a SDP enabled SocketImpl
2736N/A */
2736N/A private static SocketImpl createSocketImpl() {
2736N/A try {
2736N/A return socketImplCtor.newInstance();
2736N/A } catch (InstantiationException x) {
2736N/A throw new AssertionError(x);
2736N/A } catch (IllegalAccessException x) {
2736N/A throw new AssertionError(x);
2736N/A } catch (InvocationTargetException x) {
2736N/A throw new AssertionError(x);
2736N/A }
2736N/A }
2736N/A
2736N/A /**
2736N/A * Creates an unconnected and unbound SDP socket. The {@code Socket} is
2736N/A * associated with a {@link java.net.SocketImpl} of the system-default type.
2736N/A *
2736N/A * @return a new Socket
2736N/A *
2736N/A * @throws UnsupportedOperationException
2736N/A * If SDP is not supported
2736N/A * @throws IOException
2736N/A * If an I/O error occurs
2736N/A */
2736N/A public static Socket openSocket() throws IOException {
2736N/A SocketImpl impl = createSocketImpl();
2736N/A return new SdpSocket(impl);
2736N/A }
2736N/A
2736N/A /**
2736N/A * Creates an unbound SDP server socket. The {@code ServerSocket} is
2736N/A * associated with a {@link java.net.SocketImpl} of the system-default type.
2736N/A *
2736N/A * @return a new ServerSocket
2736N/A *
2736N/A * @throws UnsupportedOperationException
2736N/A * If SDP is not supported
2736N/A * @throws IOException
2736N/A * If an I/O error occurs
2736N/A */
2736N/A public static ServerSocket openServerSocket() throws IOException {
2736N/A // create ServerSocket via package-private constructor
2736N/A SocketImpl impl = createSocketImpl();
2736N/A try {
2736N/A return serverSocketCtor.newInstance(impl);
2736N/A } catch (IllegalAccessException x) {
2736N/A throw new AssertionError(x);
2736N/A } catch (InstantiationException x) {
2736N/A throw new AssertionError(x);
2736N/A } catch (InvocationTargetException x) {
2736N/A Throwable cause = x.getCause();
2736N/A if (cause instanceof IOException)
2736N/A throw (IOException)cause;
2736N/A if (cause instanceof RuntimeException)
2736N/A throw (RuntimeException)cause;
2736N/A throw new RuntimeException(x);
2736N/A }
2736N/A }
2736N/A
2736N/A /**
2736N/A * Opens a socket channel to a SDP socket.
2736N/A *
2736N/A * <p> The channel will be associated with the system-wide default
2736N/A * {@link java.nio.channels.spi.SelectorProvider SelectorProvider}.
2736N/A *
2736N/A * @return a new SocketChannel
2736N/A *
2736N/A * @throws UnsupportedOperationException
2736N/A * If SDP is not supported or not supported by the default selector
2736N/A * provider
2736N/A * @throws IOException
2736N/A * If an I/O error occurs.
2736N/A */
2736N/A public static SocketChannel openSocketChannel() throws IOException {
2736N/A FileDescriptor fd = SdpSupport.createSocket();
2736N/A return sun.nio.ch.Secrets.newSocketChannel(fd);
2736N/A }
2736N/A
2736N/A /**
2736N/A * Opens a socket channel to a SDP socket.
2736N/A *
2736N/A * <p> The channel will be associated with the system-wide default
2736N/A * {@link java.nio.channels.spi.SelectorProvider SelectorProvider}.
2736N/A *
2736N/A * @return a new ServerSocketChannel
2736N/A *
2736N/A * @throws UnsupportedOperationException
2736N/A * If SDP is not supported or not supported by the default selector
2736N/A * provider
2736N/A * @throws IOException
2736N/A * If an I/O error occurs
2736N/A */
2736N/A public static ServerSocketChannel openServerSocketChannel()
2736N/A throws IOException
2736N/A {
2736N/A FileDescriptor fd = SdpSupport.createSocket();
2736N/A return sun.nio.ch.Secrets.newServerSocketChannel(fd);
2736N/A }
2736N/A}