1 /*
   2  * Copyright (c) 2004, 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 com.sun.corba.se.impl.transport;
  27 
  28 import java.io.IOException;
  29 import java.net.InetSocketAddress;
  30 import java.net.Socket;
  31 import java.net.SocketException;
  32 import java.net.ServerSocket;
  33 import java.nio.channels.SocketChannel;
  34 import java.nio.channels.ServerSocketChannel;
  35 
  36 import com.sun.corba.se.pept.transport.Acceptor;
  37 
  38 import com.sun.corba.se.spi.orb.ORB;
  39 import com.sun.corba.se.spi.transport.ORBSocketFactory;
  40 
  41 import com.sun.corba.se.impl.orbutil.ORBConstants;
  42 
  43 public class DefaultSocketFactoryImpl
  44     implements ORBSocketFactory
  45 {
  46     private ORB orb;
  47 
  48     public void setORB(ORB orb)
  49     {
  50         this.orb = orb;
  51     }
  52 
  53     public ServerSocket createServerSocket(String type,
  54                                            InetSocketAddress inetSocketAddress)
  55         throws IOException
  56     {
  57         ServerSocketChannel serverSocketChannel = null;
  58         ServerSocket serverSocket = null;
  59 
  60         if (orb.getORBData().acceptorSocketType().equals(ORBConstants.SOCKETCHANNEL)) {
  61             serverSocketChannel = ServerSocketChannel.open();
  62             serverSocket = serverSocketChannel.socket();
  63         } else {
  64             serverSocket = new ServerSocket();
  65         }
  66         serverSocket.bind(inetSocketAddress);
  67         return serverSocket;
  68     }
  69 
  70     public Socket createSocket(String type,
  71                                InetSocketAddress inetSocketAddress)
  72         throws IOException
  73     {
  74         SocketChannel socketChannel = null;
  75         Socket socket = null;
  76 
  77         if (orb.getORBData().connectionSocketType().equals(ORBConstants.SOCKETCHANNEL)) {
  78             socketChannel = SocketChannel.open(inetSocketAddress);
  79             socket = socketChannel.socket();
  80         } else {
  81             socket = new Socket(inetSocketAddress.getHostName(),
  82                                 inetSocketAddress.getPort());
  83         }
  84 
  85         // Disable Nagle's algorithm (i.e., always send immediately).
  86         socket.setTcpNoDelay(true);
  87 
  88         return socket;
  89     }
  90 
  91     public void setAcceptedSocketOptions(Acceptor acceptor,
  92                                          ServerSocket serverSocket,
  93                                          Socket socket)
  94         throws SocketException
  95     {
  96         // Disable Nagle's algorithm (i.e., always send immediately).
  97         socket.setTcpNoDelay(true);
  98     }
  99 }
 100 
 101 // End of file.