1 /*
   2  * Copyright (c) 1998, 2003, 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.ServerSocket;
  29 import java.util.ArrayList;
  30 import java.util.Collection;
  31 import java.util.Iterator;
  32 
  33 import org.omg.CORBA.INITIALIZE;
  34 import org.omg.CORBA.INTERNAL;
  35 import org.omg.CORBA.CompletionStatus;
  36 
  37 import com.sun.corba.se.pept.transport.Acceptor;
  38 import com.sun.corba.se.pept.transport.ByteBufferPool;
  39 import com.sun.corba.se.pept.transport.ContactInfo;
  40 import com.sun.corba.se.pept.transport.Selector;
  41 
  42 import com.sun.corba.se.spi.ior.IOR;
  43 import com.sun.corba.se.spi.ior.iiop.IIOPProfile ;
  44 import com.sun.corba.se.spi.ior.ObjectKeyTemplate;
  45 import com.sun.corba.se.spi.ior.ObjectId ;
  46 import com.sun.corba.se.spi.orb.ORB;
  47 import com.sun.corba.se.spi.transport.CorbaTransportManager;
  48 import com.sun.corba.se.spi.legacy.connection.LegacyServerSocketEndPointInfo;
  49 import com.sun.corba.se.spi.legacy.connection.LegacyServerSocketManager;
  50 import com.sun.corba.se.spi.transport.SocketOrChannelAcceptor;
  51 import com.sun.corba.se.spi.logging.CORBALogDomains;
  52 
  53 import com.sun.corba.se.impl.encoding.EncapsOutputStream;
  54 import com.sun.corba.se.impl.legacy.connection.SocketFactoryAcceptorImpl;
  55 import com.sun.corba.se.impl.legacy.connection.USLPort;
  56 import com.sun.corba.se.impl.orbutil.ORBUtility;
  57 import com.sun.corba.se.impl.transport.SocketOrChannelAcceptorImpl;
  58 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
  59 
  60 public class LegacyServerSocketManagerImpl
  61     implements
  62         LegacyServerSocketManager
  63 {
  64     protected ORB orb;
  65     private ORBUtilSystemException wrapper ;
  66 
  67     public LegacyServerSocketManagerImpl(ORB orb)
  68     {
  69         this.orb = orb;
  70         wrapper = ORBUtilSystemException.get( orb,
  71             CORBALogDomains.RPC_TRANSPORT ) ;
  72     }
  73 
  74     ////////////////////////////////////////////////////
  75     //
  76     // LegacyServerSocketManager
  77     //
  78 
  79     // Only used in ServerManagerImpl.
  80     public int legacyGetTransientServerPort(String type)
  81     {
  82         return legacyGetServerPort(type, false);
  83     }
  84 
  85     // Only used by POAPolicyMediatorBase.
  86     public synchronized int legacyGetPersistentServerPort(String socketType)
  87     {
  88         if (orb.getORBData().getServerIsORBActivated()) {
  89             // this server is activated by orbd
  90             return legacyGetServerPort(socketType, true);
  91         } else if (orb.getORBData().getPersistentPortInitialized()) {
  92             // this is a user-activated server
  93             return orb.getORBData().getPersistentServerPort();
  94         } else {
  95             throw wrapper.persistentServerportNotSet(
  96                 CompletionStatus.COMPLETED_MAYBE);
  97         }
  98     }
  99 
 100     // Only used by PI IORInfoImpl.
 101     public synchronized int legacyGetTransientOrPersistentServerPort(
 102         String socketType)
 103     {
 104             return legacyGetServerPort(socketType,
 105                                        orb.getORBData()
 106                                        .getServerIsORBActivated());
 107     }
 108 
 109     // Used in RepositoryImpl, ServerManagerImpl, POAImpl,
 110     // POAPolicyMediatorBase, TOAImpl.
 111     // To get either default or bootnaming endpoint.
 112     public synchronized LegacyServerSocketEndPointInfo legacyGetEndpoint(
 113         String name)
 114     {
 115         Iterator iterator = getAcceptorIterator();
 116         while (iterator.hasNext()) {
 117             LegacyServerSocketEndPointInfo endPoint = cast(iterator.next());
 118             if (endPoint != null && name.equals(endPoint.getName())) {
 119                 return endPoint;
 120             }
 121         }
 122         throw new INTERNAL("No acceptor for: " + name);
 123     }
 124 
 125     // Check to see if the given port is equal to any of the ORB Server Ports.
 126     // XXX Does this need to change for the multi-homed case?
 127     // Used in IIOPProfileImpl, ORBImpl.
 128     public boolean legacyIsLocalServerPort(int port)
 129     {
 130         Iterator iterator = getAcceptorIterator();
 131         while (iterator.hasNext()) {
 132             LegacyServerSocketEndPointInfo endPoint = cast(iterator.next());
 133             if (endPoint != null && endPoint.getPort() == port) {
 134                 return true;
 135             }
 136         }
 137         return false;
 138     }
 139 
 140     ////////////////////////////////////////////////////
 141     //
 142     // Implementation.
 143     //
 144 
 145     private int legacyGetServerPort (String socketType, boolean isPersistent)
 146     {
 147         Iterator endpoints = getAcceptorIterator();
 148         while (endpoints.hasNext()) {
 149             LegacyServerSocketEndPointInfo ep = cast(endpoints.next());
 150             if (ep != null && ep.getType().equals(socketType)) {
 151                 if (isPersistent) {
 152                     return ep.getLocatorPort();
 153                 } else {
 154                     return ep.getPort();
 155                 }
 156             }
 157         }
 158         return -1;
 159     }
 160 
 161     private Iterator getAcceptorIterator()
 162     {
 163         Collection acceptors =
 164             orb.getCorbaTransportManager().getAcceptors(null, null);
 165         if (acceptors != null) {
 166             return acceptors.iterator();
 167         }
 168 
 169         throw wrapper.getServerPortCalledBeforeEndpointsInitialized() ;
 170     }
 171 
 172     private LegacyServerSocketEndPointInfo cast(Object o)
 173     {
 174         if (o instanceof LegacyServerSocketEndPointInfo) {
 175             return (LegacyServerSocketEndPointInfo) o;
 176         }
 177         return null;
 178     }
 179 
 180     protected void dprint(String msg)
 181     {
 182         ORBUtility.dprint("LegacyServerSocketManagerImpl", msg);
 183     }
 184 }
 185 
 186 // End of file.