1 /*
   2  * Copyright (c) 2003, 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.legacy.connection;
  27 
  28 import java.net.Socket;
  29 import java.nio.channels.SocketChannel;
  30 import java.util.Collections;
  31 
  32 import org.omg.CORBA.COMM_FAILURE;
  33 import org.omg.CORBA.CompletionStatus;
  34 
  35 import com.sun.corba.se.pept.transport.Acceptor;
  36 import com.sun.corba.se.pept.transport.ContactInfo;
  37 
  38 import com.sun.corba.se.spi.ior.IOR;
  39 import com.sun.corba.se.spi.legacy.connection.GetEndPointInfoAgainException;
  40 import com.sun.corba.se.spi.orb.ORB;
  41 import com.sun.corba.se.spi.transport.CorbaConnection;
  42 import com.sun.corba.se.spi.transport.CorbaContactInfo;
  43 import com.sun.corba.se.spi.transport.SocketInfo;
  44 
  45 import com.sun.corba.se.impl.orbutil.ORBUtility;
  46 import com.sun.corba.se.impl.transport.SocketOrChannelContactInfoImpl;
  47 import com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl;
  48 
  49 /**
  50  * @author Harold Carr
  51  */
  52 public class SocketFactoryConnectionImpl
  53     extends
  54         SocketOrChannelConnectionImpl
  55 {
  56     // Socket-factory client constructor.
  57     public SocketFactoryConnectionImpl(ORB orb,
  58                                        CorbaContactInfo contactInfo,
  59                                        boolean useSelectThreadToWait,
  60                                        boolean useWorkerThread)
  61     {
  62         super(orb, useSelectThreadToWait, useWorkerThread);
  63 
  64         // REVISIT - probably need a contact info for both
  65         // client and server for removing connections from cache?
  66         this.contactInfo = contactInfo;
  67 
  68         boolean isBlocking = !useSelectThreadToWait;
  69         SocketInfo socketInfo =
  70             // REVISIT - case - needs interface method
  71             ((SocketFactoryContactInfoImpl)contactInfo).socketInfo;
  72         try {
  73             socket =
  74                 orb.getORBData().getLegacySocketFactory().createSocket(socketInfo);
  75             socketChannel = socket.getChannel();
  76             if (socketChannel != null) {
  77                 socketChannel.configureBlocking(isBlocking);
  78             } else {
  79                 // IMPORTANT: non-channel-backed sockets must use
  80                 // dedicated reader threads.
  81                 setUseSelectThreadToWait(false);
  82             }
  83             if (orb.transportDebugFlag) {
  84                 dprint(".initialize: connection created: " + socket);
  85             }
  86         } catch (GetEndPointInfoAgainException ex) {
  87             throw wrapper.connectFailure(
  88                 ex, socketInfo.getType(), socketInfo.getHost(),
  89                 Integer.toString(socketInfo.getPort())) ;
  90         } catch (Exception ex) {
  91             throw wrapper.connectFailure(
  92                 ex, socketInfo.getType(), socketInfo.getHost(),
  93                 Integer.toString(socketInfo.getPort())) ;
  94         }
  95         state = OPENING;
  96     }
  97 
  98     public String toString()
  99     {
 100         synchronized ( stateEvent ){
 101             return
 102                 "SocketFactoryConnectionImpl[" + " "
 103                 + (socketChannel == null ?
 104                    socket.toString() : socketChannel.toString()) + " "
 105                 + getStateString( state ) + " "
 106                 + shouldUseSelectThreadToWait() + " "
 107                 + shouldUseWorkerThreadForEvent()
 108                 + "]" ;
 109         }
 110     }
 111 
 112     // Note: public to override parent.
 113     public void dprint(String msg)
 114     {
 115         ORBUtility.dprint("SocketFactoryConnectionImpl", msg);
 116     }
 117 
 118 }
 119 
 120 // End of file.