Search in sources :

Example 1 with SocketImpl

use of java.net.SocketImpl in project robovm by robovm.

the class OldSocketImplFactoryTest method test_createSocketImpl.

public void test_createSocketImpl() throws IOException {
    MockSocketImplFactory factory = new MockSocketImplFactory();
    if (isTestable) {
        assertFalse(isCreateSocketImpl);
        Socket.setSocketImplFactory(factory);
        try {
            new Socket();
            assertTrue(isCreateSocketImpl);
            assertTrue(iSocketImplCalled);
        } catch (Exception e) {
            fail("Exception during test : " + e.getMessage());
        }
        try {
            Socket.setSocketImplFactory(factory);
            fail("SocketException was not thrown.");
        } catch (SocketException se) {
        //expected
        }
        try {
            Socket.setSocketImplFactory(null);
            fail("SocketException was not thrown.");
        } catch (SocketException se) {
        //expected
        }
    } else {
        SocketImpl si = factory.createSocketImpl();
        try {
            assertNull(si.getOption(0));
        } catch (SocketException e) {
            fail("SocketException was thrown.");
        }
    }
}
Also used : SocketException(java.net.SocketException) SocketImpl(java.net.SocketImpl) Socket(java.net.Socket) SocketException(java.net.SocketException) IOException(java.io.IOException)

Example 2 with SocketImpl

use of java.net.SocketImpl in project j2objc by google.

the class SocketTest method test_SocketOptions_setOption.

// SocketOptions.setOption has weird behavior for setSoLinger/SO_LINGER.
// This test ensures we do what the RI does.
public void test_SocketOptions_setOption() throws Exception {
    class MySocketImpl extends SocketImpl {

        public int option;

        public Object value;

        public boolean createCalled;

        public boolean createStream;

        public MySocketImpl() {
            super();
        }

        @Override
        protected void accept(SocketImpl arg0) throws IOException {
        }

        @Override
        protected int available() throws IOException {
            return 0;
        }

        @Override
        protected void bind(InetAddress arg0, int arg1) throws IOException {
        }

        @Override
        protected void close() throws IOException {
        }

        @Override
        protected void connect(String arg0, int arg1) throws IOException {
        }

        @Override
        protected void connect(InetAddress arg0, int arg1) throws IOException {
        }

        @Override
        protected void connect(SocketAddress arg0, int arg1) throws IOException {
        }

        @Override
        protected InputStream getInputStream() throws IOException {
            return null;
        }

        @Override
        protected OutputStream getOutputStream() throws IOException {
            return null;
        }

        @Override
        protected void listen(int arg0) throws IOException {
        }

        @Override
        protected void sendUrgentData(int arg0) throws IOException {
        }

        public Object getOption(int arg0) throws SocketException {
            return null;
        }

        @Override
        protected void create(boolean isStream) throws IOException {
            this.createCalled = true;
            this.createStream = isStream;
        }

        public void setOption(int option, Object value) throws SocketException {
            this.option = option;
            this.value = value;
        }
    }
    class MySocket extends Socket {

        public MySocket(SocketImpl impl) throws SocketException {
            super(impl);
        }
    }
    MySocketImpl impl = new MySocketImpl();
    Socket s = new MySocket(impl);
    // Check that, as per the SocketOptions.setOption documentation, we pass false rather
    // than -1 to the SocketImpl when setSoLinger is called with the first argument false.
    s.setSoLinger(false, -1);
    assertEquals(Boolean.FALSE, (Boolean) impl.value);
    // We also check that SocketImpl.create was called. SocketChannelImpl.SocketAdapter
    // subclasses Socket, and whether or not to call SocketImpl.create is the main behavioral
    // difference.
    assertEquals(true, impl.createCalled);
    s.setSoLinger(false, 0);
    assertEquals(Boolean.FALSE, (Boolean) impl.value);
    s.setSoLinger(false, 1);
    assertEquals(Boolean.FALSE, (Boolean) impl.value);
    // Check that otherwise, we pass down an Integer.
    s.setSoLinger(true, 0);
    assertEquals(Integer.valueOf(0), (Integer) impl.value);
    s.setSoLinger(true, 1);
    assertEquals(Integer.valueOf(1), (Integer) impl.value);
}
Also used : SocketImpl(java.net.SocketImpl) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 3 with SocketImpl

use of java.net.SocketImpl in project robovm by robovm.

the class SocketTest method test_SocketOptions_setOption.

// SocketOptions.setOption has weird behavior for setSoLinger/SO_LINGER.
// This test ensures we do what the RI does.
public void test_SocketOptions_setOption() throws Exception {
    class MySocketImpl extends SocketImpl {

        public int option;

        public Object value;

        public boolean createCalled;

        public boolean createStream;

        public MySocketImpl() {
            super();
        }

        @Override
        protected void accept(SocketImpl arg0) throws IOException {
        }

        @Override
        protected int available() throws IOException {
            return 0;
        }

        @Override
        protected void bind(InetAddress arg0, int arg1) throws IOException {
        }

        @Override
        protected void close() throws IOException {
        }

        @Override
        protected void connect(String arg0, int arg1) throws IOException {
        }

        @Override
        protected void connect(InetAddress arg0, int arg1) throws IOException {
        }

        @Override
        protected void connect(SocketAddress arg0, int arg1) throws IOException {
        }

        @Override
        protected InputStream getInputStream() throws IOException {
            return null;
        }

        @Override
        protected OutputStream getOutputStream() throws IOException {
            return null;
        }

        @Override
        protected void listen(int arg0) throws IOException {
        }

        @Override
        protected void sendUrgentData(int arg0) throws IOException {
        }

        public Object getOption(int arg0) throws SocketException {
            return null;
        }

        @Override
        protected void create(boolean isStream) throws IOException {
            this.createCalled = true;
            this.createStream = isStream;
        }

        public void setOption(int option, Object value) throws SocketException {
            this.option = option;
            this.value = value;
        }
    }
    class MySocket extends Socket {

        public MySocket(SocketImpl impl) throws SocketException {
            super(impl);
        }
    }
    MySocketImpl impl = new MySocketImpl();
    Socket s = new MySocket(impl);
    // Check that, as per the SocketOptions.setOption documentation, we pass false rather
    // than -1 to the SocketImpl when setSoLinger is called with the first argument false.
    s.setSoLinger(false, -1);
    assertEquals(Boolean.FALSE, (Boolean) impl.value);
    // We also check that SocketImpl.create was called. SocketChannelImpl.SocketAdapter
    // subclasses Socket, and whether or not to call SocketImpl.create is the main behavioral
    // difference.
    assertEquals(true, impl.createCalled);
    s.setSoLinger(false, 0);
    assertEquals(Boolean.FALSE, (Boolean) impl.value);
    s.setSoLinger(false, 1);
    assertEquals(Boolean.FALSE, (Boolean) impl.value);
    // Check that otherwise, we pass down an Integer.
    s.setSoLinger(true, 0);
    assertEquals(Integer.valueOf(0), (Integer) impl.value);
    s.setSoLinger(true, 1);
    assertEquals(Integer.valueOf(1), (Integer) impl.value);
}
Also used : SocketImpl(java.net.SocketImpl) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 4 with SocketImpl

use of java.net.SocketImpl in project robovm by robovm.

the class OldSocketTest method test_setKeepAliveZ.

public void test_setKeepAliveZ() throws Exception {
    // crashed machines. Just make sure we can set it
    try {
        int sport = startServer("SServer setKeepAlive");
        Socket theSocket = new Socket(InetAddress.getLocalHost(), sport, null, 0);
        theSocket.setKeepAlive(true);
        theSocket.setKeepAlive(false);
        ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
    } catch (Exception e) {
        handleException(e, SO_KEEPALIVE);
    }
    // regression test for HARMONY-1136
    new TestSocket((SocketImpl) null).setKeepAlive(true);
    try {
        Socket theSocket = new Socket();
        theSocket.close();
        theSocket.setKeepAlive(true);
        fail("SocketException was not thrown.");
    } catch (SocketException ioe) {
    //expected
    }
}
Also used : SocketException(java.net.SocketException) SocketImpl(java.net.SocketImpl) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) BindException(java.net.BindException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException)

Example 5 with SocketImpl

use of java.net.SocketImpl in project geode by apache.

the class NativeCalls method getSocketKernelDescriptor.

/**
   * Get the native kernel descriptor given the java Socket. This is a horribly implementation
   * dependent code checking various cases to get to the underlying kernel socket descriptor but
   * works for the JDK's we support or intend to support directly or indirectly (e.g. GCJ for ODBC
   * clients).
   * 
   * @param sock the java socket
   * @param sockStream the {@link InputStream} of the java socket, if available
   * 
   * @throws UnsupportedOperationException if the kernel descriptor could not be extracted
   */
protected int getSocketKernelDescriptor(Socket sock, InputStream sockStream) throws UnsupportedOperationException {
    Method m;
    Field f;
    Object obj;
    FileDescriptor fd = null;
    // in some cases (for SSL) the Socket can be a wrapper one
    try {
        f = getAnyField(sock.getClass(), "self");
        if (f != null) {
            f.setAccessible(true);
            final Object self = f.get(sock);
            if (self instanceof Socket) {
                sock = (Socket) self;
                sockStream = sock.getInputStream();
            }
        }
    } catch (NoSuchFieldException fe) {
    // ignore if there is no such field
    } catch (RuntimeException re) {
        throw re;
    } catch (Exception ex) {
        throw new UnsupportedOperationException(ex);
    }
    // first try using FileInputStream
    if (sockStream instanceof FileInputStream) {
        try {
            fd = ((FileInputStream) sockStream).getFD();
        } catch (Exception e) {
        // go the fallback route
        }
    }
    // else fallback to SocketImpl route
    try {
        if (fd == null) {
            try {
                // package private Socket.getImpl() to get SocketImpl
                m = getAnyMethod(sock.getClass(), "getImpl");
            } catch (Exception ex) {
                try {
                    m = getAnyMethod(sock.getClass(), "getPlainSocketImpl");
                } catch (Exception e) {
                    // try forcing the InputStream route
                    m = null;
                    if (sockStream == null) {
                        sockStream = sock.getInputStream();
                        if (sockStream instanceof FileInputStream) {
                            fd = ((FileInputStream) sockStream).getFD();
                        }
                    } else {
                        throw e;
                    }
                }
            }
            if (m != null) {
                m.setAccessible(true);
                final SocketImpl sockImpl = (SocketImpl) m.invoke(sock);
                if (sockImpl != null) {
                    try {
                        m = getAnyMethod(sockImpl.getClass(), "getFileDescriptor");
                        if (m != null) {
                            m.setAccessible(true);
                            fd = (FileDescriptor) m.invoke(sockImpl);
                        }
                    } catch (NoSuchMethodException nme) {
                    // go to field reflection route
                    }
                }
            }
        }
        if (fd != null) {
            // get the kernel descriptor using reflection
            f = getAnyField(fd.getClass(), "fd");
            if (f != null) {
                f.setAccessible(true);
                obj = f.get(fd);
                if (obj instanceof Integer) {
                    return ((Integer) obj).intValue();
                }
            }
        }
        throw new UnsupportedOperationException();
    } catch (SecurityException se) {
        throw new UnsupportedOperationException(se);
    } catch (RuntimeException re) {
        throw re;
    } catch (Exception ex) {
        throw new UnsupportedOperationException(ex);
    }
}
Also used : Method(java.lang.reflect.Method) FileDescriptor(java.io.FileDescriptor) IOException(java.io.IOException) SocketException(java.net.SocketException) FileInputStream(java.io.FileInputStream) Field(java.lang.reflect.Field) SocketImpl(java.net.SocketImpl) Socket(java.net.Socket)

Aggregations

Socket (java.net.Socket)5 SocketImpl (java.net.SocketImpl)5 IOException (java.io.IOException)3 ServerSocket (java.net.ServerSocket)3 SocketException (java.net.SocketException)3 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 FileDescriptor (java.io.FileDescriptor)1 FileInputStream (java.io.FileInputStream)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 BindException (java.net.BindException)1 ConnectException (java.net.ConnectException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)1