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 icc : inboundConnectionCaches.values()) {
 192                 ((ConnectionCache)icc).close() ;
 193                 unregisterAcceptor(((InboundConnectionCache)icc).getAcceptor());
 194             }
 195             getSelector(0).close();
 196         } finally {
 197             if (orb.transportDebugFlag) {
 198                 dprint(".close<-");
 199             }
 200         }
 201     }
 202 
 203     ////////////////////////////////////////////////////
 204     //
 205     // CorbaTransportManager
 206     //
 207 
 208     public Collection getAcceptors(String objectAdapterManagerId,
 209                                    ObjectAdapterId objectAdapterId)
 210     {
 211         // REVISIT - need to filter based on arguments.
 212 
 213         // REVISIT - initialization will be moved to OA.
 214         // Lazy initialization of acceptors.
 215         Iterator iterator = acceptors.iterator();
 216         while (iterator.hasNext()) {
 217             Acceptor acceptor = (Acceptor) iterator.next();
 218             if (acceptor.initialize()) {
 219                 if (acceptor.shouldRegisterAcceptEvent()) {
 220                     orb.getTransportManager().getSelector(0)
 221                         .registerForEvent(acceptor.getEventHandler());
 222                 }
 223             }
 224         }
 225         return acceptors;
 226     }
 227 
 228     // REVISIT - POA specific policies
 229     public void addToIORTemplate(IORTemplate iorTemplate,
 230                                  Policies policies,
 231                                  String codebase,
 232                                  String objectAdapterManagerId,
 233                                  ObjectAdapterId objectAdapterId)
 234     {
 235         Iterator iterator =
 236             getAcceptors(objectAdapterManagerId, objectAdapterId).iterator();
 237         while (iterator.hasNext()) {
 238             CorbaAcceptor acceptor = (CorbaAcceptor) iterator.next();
 239             acceptor.addToIORTemplate(iorTemplate, policies, codebase);
 240         }
 241     }
 242 
 243 
 244     ////////////////////////////////////////////////////
 245     //
 246     // implemenation
 247     //
 248 
 249     protected void dprint(String msg)
 250     {
 251         ORBUtility.dprint("CorbaTransportManagerImpl", msg);
 252     }
 253 }
 254 
 255 // End of file.