DefaultSocketFactory.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2000, 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 */
25package com.sun.corba.se.impl.legacy.connection;
26
27import java.io.IOException;
28import java.net.InetSocketAddress;
29import java.net.ServerSocket;
30import java.net.Socket;
31import java.net.UnknownHostException;
32import java.nio.channels.ServerSocketChannel;
33import java.nio.channels.SocketChannel;
34
35import org.omg.CORBA.ORB;
36import org.omg.CORBA.COMM_FAILURE;
37import org.omg.CORBA.CompletionStatus;
38
39import com.sun.corba.se.spi.ior.IOR;
40import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate ;
41import com.sun.corba.se.spi.ior.iiop.IIOPAddress ;
42import com.sun.corba.se.spi.legacy.connection.GetEndPointInfoAgainException;
43import com.sun.corba.se.spi.legacy.connection.ORBSocketFactory;
44import com.sun.corba.se.spi.logging.CORBALogDomains;
45import com.sun.corba.se.spi.transport.SocketInfo;
46
47import com.sun.corba.se.impl.legacy.connection.EndPointInfoImpl;
48import com.sun.corba.se.impl.logging.ORBUtilSystemException;
49import com.sun.corba.se.impl.orbutil.ORBConstants;
50
51public class DefaultSocketFactory
52    implements
53        ORBSocketFactory
54{
55    private com.sun.corba.se.spi.orb.ORB orb;
56    private static ORBUtilSystemException wrapper = ORBUtilSystemException.get(
57        CORBALogDomains.RPC_TRANSPORT ) ;
58
59    public DefaultSocketFactory()
60    {
61    }
62
63    public void setORB(com.sun.corba.se.spi.orb.ORB orb)
64    {
65        this.orb = orb;
66    }
67
68    public ServerSocket createServerSocket(String type, int port)
69        throws
70            IOException
71    {
72        if (! type.equals(ORBSocketFactory.IIOP_CLEAR_TEXT)) {
73            throw wrapper.defaultCreateServerSocketGivenNonIiopClearText( type ) ;
74        }
75
76        ServerSocket serverSocket;
77
78        if (orb.getORBData().acceptorSocketType().equals(ORBConstants.SOCKETCHANNEL)) {
79            ServerSocketChannel serverSocketChannel =
80                ServerSocketChannel.open();
81            serverSocket = serverSocketChannel.socket();
82        } else {
83            serverSocket = new ServerSocket();
84        }
85        serverSocket.bind(new InetSocketAddress(port));
86        return serverSocket;
87    }
88
89    public SocketInfo getEndPointInfo(ORB orb,
90                                        IOR ior,
91                                        SocketInfo socketInfo)
92    {
93        IIOPProfileTemplate temp =
94            (IIOPProfileTemplate)ior.getProfile().getTaggedProfileTemplate() ;
95        IIOPAddress primary = temp.getPrimaryAddress() ;
96
97        return new EndPointInfoImpl(ORBSocketFactory.IIOP_CLEAR_TEXT,
98                                    primary.getPort(),
99                                    primary.getHost().toLowerCase());
100    }
101
102    public Socket createSocket(SocketInfo socketInfo)
103        throws
104            IOException,
105            GetEndPointInfoAgainException
106    {
107        Socket socket;
108
109        if (orb.getORBData().acceptorSocketType().equals(ORBConstants.SOCKETCHANNEL)) {
110            InetSocketAddress address =
111                new InetSocketAddress(socketInfo.getHost(),
112                                      socketInfo.getPort());
113            SocketChannel socketChannel = SocketChannel.open(address);
114            socket = socketChannel.socket();
115        } else {
116            socket = new Socket(socketInfo.getHost(),
117                                socketInfo.getPort());
118        }
119
120        // REVISIT - this is done in SocketOrChannelConnectionImpl
121        try {
122            socket.setTcpNoDelay(true);
123        } catch (Exception e) {
124            ;
125        }
126        return socket;
127    }
128}
129
130// End of file.
131