DefaultSocketFactoryImpl.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2004, 2013, 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
26package com.sun.corba.se.impl.transport;
27
28import java.io.IOException;
29import java.net.InetSocketAddress;
30import java.net.Socket;
31import java.net.SocketException;
32import java.net.ServerSocket;
33import java.nio.channels.SocketChannel;
34import java.nio.channels.ServerSocketChannel;
35import java.security.PrivilegedAction;
36
37import com.sun.corba.se.pept.transport.Acceptor;
38
39import com.sun.corba.se.spi.orb.ORB;
40import com.sun.corba.se.spi.transport.ORBSocketFactory;
41
42import com.sun.corba.se.impl.orbutil.ORBConstants;
43
44public class DefaultSocketFactoryImpl
45    implements ORBSocketFactory
46{
47    private ORB orb;
48    private static final boolean keepAlive;
49
50    static {
51        keepAlive = java.security.AccessController.doPrivileged(
52            new PrivilegedAction<Boolean>() {
53                @Override
54                public Boolean run () {
55                    String value =
56                        System.getProperty("com.sun.CORBA.transport.enableTcpKeepAlive");
57                    if (value != null)
58                        return new Boolean(!"false".equalsIgnoreCase(value));
59
60                    return Boolean.FALSE;
61                }
62            });
63    }
64
65    public void setORB(ORB orb)
66    {
67        this.orb = orb;
68    }
69
70    public ServerSocket createServerSocket(String type,
71                                           InetSocketAddress inetSocketAddress)
72        throws IOException
73    {
74        ServerSocketChannel serverSocketChannel = null;
75        ServerSocket serverSocket = null;
76
77        if (orb.getORBData().acceptorSocketType().equals(ORBConstants.SOCKETCHANNEL)) {
78            serverSocketChannel = ServerSocketChannel.open();
79            serverSocket = serverSocketChannel.socket();
80        } else {
81            serverSocket = new ServerSocket();
82        }
83        serverSocket.bind(inetSocketAddress);
84        return serverSocket;
85    }
86
87    public Socket createSocket(String type,
88                               InetSocketAddress inetSocketAddress)
89        throws IOException
90    {
91        SocketChannel socketChannel = null;
92        Socket socket = null;
93
94        if (orb.getORBData().connectionSocketType().equals(ORBConstants.SOCKETCHANNEL)) {
95            socketChannel = SocketChannel.open(inetSocketAddress);
96            socket = socketChannel.socket();
97        } else {
98            socket = new Socket(inetSocketAddress.getHostName(),
99                                inetSocketAddress.getPort());
100        }
101
102        // Disable Nagle's algorithm (i.e., always send immediately).
103        socket.setTcpNoDelay(true);
104
105        if (keepAlive)
106            socket.setKeepAlive(true);
107
108        return socket;
109    }
110
111    public void setAcceptedSocketOptions(Acceptor acceptor,
112                                         ServerSocket serverSocket,
113                                         Socket socket)
114        throws SocketException
115    {
116        // Disable Nagle's algorithm (i.e., always send immediately).
117        socket.setTcpNoDelay(true);
118        if (keepAlive)
119            socket.setKeepAlive(true);
120    }
121}
122
123// End of file.
124