1 /*
   2  * Copyright (c) 2003, 2010, 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.net.ServerSocket;
  29 import java.util.ArrayList;
  30 import java.util.Collection;
  31 import java.util.HashMap;
  32 import java.util.Iterator;
  33 import java.util.List;
  34 import java.util.Map;
  35 
  36 import org.omg.CORBA.INITIALIZE;
  37 import org.omg.CORBA.INTERNAL;
  38 import org.omg.CORBA.CompletionStatus;
  39 
  40 import com.sun.corba.se.pept.transport.Acceptor;
  41 import com.sun.corba.se.pept.transport.ConnectionCache;
  42 import com.sun.corba.se.pept.transport.ByteBufferPool;
  43 import com.sun.corba.se.pept.transport.ContactInfo;
  44 import com.sun.corba.se.pept.transport.InboundConnectionCache;
  45 import com.sun.corba.se.pept.transport.OutboundConnectionCache;
  46 import com.sun.corba.se.pept.transport.Selector;
  47 
  48 import com.sun.corba.se.spi.ior.IORTemplate;
  49 import com.sun.corba.se.spi.ior.ObjectAdapterId;
  50 import com.sun.corba.se.spi.orb.ORB;
  51 import com.sun.corba.se.spi.transport.CorbaAcceptor;
  52 import com.sun.corba.se.spi.transport.CorbaTransportManager;
  53 import com.sun.corba.se.pept.transport.Connection;
  54 import com.sun.corba.se.pept.transport.ConnectionCache;
  55 
  56 // REVISIT - impl/poa specific:
  57 import com.sun.corba.se.impl.oa.poa.Policies;
  58 import com.sun.corba.se.impl.orbutil.ORBUtility;
  59 
  60 /**
  61  * @author Harold Carr
  62  */
  63 public class CorbaTransportManagerImpl
  64     implements
  65         CorbaTransportManager
  66 {
  67     protected ORB orb;
  68     protected List acceptors;
  69     protected Map outboundConnectionCaches;
  70     protected Map inboundConnectionCaches;
  71     protected Selector selector;
  72 
  73     public CorbaTransportManagerImpl(ORB orb)
  74     {
  75         this.orb = orb;
  76         acceptors = new ArrayList();
  77         outboundConnectionCaches = new HashMap();
  78         inboundConnectionCaches = new HashMap();
  79         selector = new SelectorImpl(orb);
  80     }
  81 
  82     ////////////////////////////////////////////////////
  83     //
  84     // pept TransportManager
  85     //
  86 
  87     public ByteBufferPool getByteBufferPool(int id)
  88     {
  89         throw new RuntimeException();
  90     }
  91 
  92     public OutboundConnectionCache getOutboundConnectionCache(
  93         ContactInfo contactInfo)
  94     {
  95         synchronized (contactInfo) {
  96             if (contactInfo.getConnectionCache() == null) {
  97                 OutboundConnectionCache connectionCache = null;
  98                 synchronized (outboundConnectionCaches) {
  99                     connectionCache = (OutboundConnectionCache)
 100                         outboundConnectionCaches.get(
 101                             contactInfo.getConnectionCacheType());
 102                     if (connectionCache == null) {
 103                         // REVISIT: Would like to be able to configure
 104                         // the connection cache type used.
 105                         connectionCache =
 106                             new CorbaOutboundConnectionCacheImpl(orb,
 107                                                                  contactInfo);
 108                         outboundConnectionCaches.put(
 109                             contactInfo.getConnectionCacheType(),
 110                             connectionCache);
 111                     }
 112                 }
 113                 contactInfo.setConnectionCache(connectionCache);
 114             }
 115             return contactInfo.getConnectionCache();
 116         }
 117     }
 118 
 119     public Collection getOutboundConnectionCaches()
 120     {
 121         return outboundConnectionCaches.values();
 122     }
 123 
 124     public InboundConnectionCache getInboundConnectionCache(
 125         Acceptor acceptor)
 126     {
 127         synchronized (acceptor) {
 128             if (acceptor.getConnectionCache() == null) {
 129                 InboundConnectionCache connectionCache = null;
 130                 synchronized (inboundConnectionCaches) {
 131                     connectionCache = (InboundConnectionCache)
 132                         inboundConnectionCaches.get(
 133                             acceptor.getConnectionCacheType());
 134                     if (connectionCache == null) {
 135                         // REVISIT: Would like to be able to configure
 136                         // the connection cache type used.
 137                         connectionCache =
 138                             new CorbaInboundConnectionCacheImpl(orb,
 139                                                                 acceptor);
 140                         inboundConnectionCaches.put(
 141                             acceptor.getConnectionCacheType(),
 142                             connectionCache);
 143                     }
 144                 }
 145                 acceptor.setConnectionCache(connectionCache);
 146             }
 147             return acceptor.getConnectionCache();
 148         }
 149     }
 150 
 151     public Collection getInboundConnectionCaches()
 152     {
 153         return inboundConnectionCaches.values();
 154     }
 155 
 156     public Selector getSelector(int id)
 157     {
 158         return selector;
 159     }
 160 
 161     public synchronized void registerAcceptor(Acceptor acceptor)
 162     {
 163         if (orb.transportDebugFlag) {
 164             dprint(".registerAcceptor->: " + acceptor);
 165         }
 166         acceptors.add(acceptor);
 167         if (orb.transportDebugFlag) {
 168             dprint(".registerAcceptor<-: " + acceptor);
 169         }
 170     }
 171 
 172     public Collection getAcceptors()
 173     {
 174         return getAcceptors(null, null);
 175     }
 176 
 177     public synchronized void unregisterAcceptor(Acceptor acceptor)
 178     {
 179         acceptors.remove(acceptor);
 180     }
 181 
 182     public void close()
 183     {
 184         try {
 185             if (orb.transportDebugFlag) {
 186                 dprint(".close->");
 187             }
 188             for (Object cc : outboundConnectionCaches.values()) {
 189                 ((ConnectionCache)cc).close() ;
 190             }
 191             for (Object cc : inboundConnectionCaches.values()) {
 192                 ((ConnectionCache)cc).close() ;
 193             }
 194             getSelector(0).close();
 195         } finally {
 196             if (orb.transportDebugFlag) {
 197                 dprint(".close<-");
 198             }
 199         }
 200     }
 201 
 202     ////////////////////////////////////////////////////
 203     //
 204     // CorbaTransportManager
 205     //
 206 
 207     public Collection getAcceptors(String objectAdapterManagerId,
 208                                    ObjectAdapterId objectAdapterId)
 209     {
 210         // REVISIT - need to filter based on arguments.
 211 
 212         // REVISIT - initialization will be moved to OA.
 213         // Lazy initialization of acceptors.
 214         Iterator iterator = acceptors.iterator();
 215         while (iterator.hasNext()) {
 216             Acceptor acceptor = (Acceptor) iterator.next();
 217             if (acceptor.initialize()) {
 218                 if (acceptor.shouldRegisterAcceptEvent()) {
 219                     orb.getTransportManager().getSelector(0)
 220                         .registerForEvent(acceptor.getEventHandler());
 221                 }
 222             }
 223         }
 224         return acceptors;
 225     }
 226 
 227     // REVISIT - POA specific policies
 228     public void addToIORTemplate(IORTemplate iorTemplate,
 229                                  Policies policies,
 230                                  String codebase,
 231                                  String objectAdapterManagerId,
 232                                  ObjectAdapterId objectAdapterId)
 233     {
 234         Iterator iterator =
 235             getAcceptors(objectAdapterManagerId, objectAdapterId).iterator();
 236         while (iterator.hasNext()) {
 237             CorbaAcceptor acceptor = (CorbaAcceptor) iterator.next();
 238             acceptor.addToIORTemplate(iorTemplate, policies, codebase);
 239         }
 240     }
 241 
 242 
 243     ////////////////////////////////////////////////////
 244     //
 245     // implemenation
 246     //
 247 
 248     protected void dprint(String msg)
 249     {
 250         ORBUtility.dprint("CorbaTransportManagerImpl", msg);
 251     }
 252 }
 253 
 254 // End of file.