1 /*
   2  * Copyright (c) 2002, 2013, 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.spi.orb;
  27 
  28 import java.util.Map ;
  29 import java.util.HashMap ;
  30 import java.util.Properties ;
  31 import java.util.concurrent.ConcurrentHashMap;
  32 import java.util.logging.Logger ;
  33 
  34 import java.security.AccessController ;
  35 import java.security.PrivilegedAction ;
  36 
  37 import org.omg.CORBA.TCKind ;
  38 
  39 import com.sun.corba.se.pept.broker.Broker ;
  40 import com.sun.corba.se.pept.transport.ByteBufferPool;
  41 
  42 import com.sun.corba.se.spi.protocol.RequestDispatcherRegistry ;
  43 import com.sun.corba.se.spi.protocol.ClientDelegateFactory ;
  44 import com.sun.corba.se.spi.protocol.CorbaServerRequestDispatcher ;
  45 import com.sun.corba.se.spi.protocol.CorbaMessageMediator ;
  46 import com.sun.corba.se.spi.protocol.PIHandler ;
  47 import com.sun.corba.se.spi.resolver.LocalResolver ;
  48 import com.sun.corba.se.spi.resolver.Resolver ;
  49 import com.sun.corba.se.spi.transport.CorbaContactInfoListFactory ;
  50 import com.sun.corba.se.spi.legacy.connection.LegacyServerSocketEndPointInfo;
  51 import com.sun.corba.se.spi.legacy.connection.LegacyServerSocketManager;
  52 import com.sun.corba.se.spi.monitoring.MonitoringConstants;
  53 import com.sun.corba.se.spi.monitoring.MonitoringManager;
  54 import com.sun.corba.se.spi.monitoring.MonitoringManagerFactory;
  55 import com.sun.corba.se.spi.monitoring.MonitoringFactories;
  56 
  57 import com.sun.corba.se.spi.ior.IdentifiableFactoryFinder ;
  58 import com.sun.corba.se.spi.ior.TaggedComponentFactoryFinder ;
  59 import com.sun.corba.se.spi.ior.ObjectKey ;
  60 import com.sun.corba.se.spi.ior.ObjectKeyFactory ;
  61 import com.sun.corba.se.spi.ior.IOR ;
  62 
  63 import com.sun.corba.se.spi.orbutil.closure.Closure ;
  64 
  65 import com.sun.corba.se.spi.orb.Operation ;
  66 import com.sun.corba.se.spi.orb.ORBData ;
  67 import com.sun.corba.se.spi.orb.ORBVersion ;
  68 import com.sun.corba.se.spi.orbutil.threadpool.ThreadPoolManager;
  69 
  70 import com.sun.corba.se.spi.oa.OAInvocationInfo ;
  71 import com.sun.corba.se.spi.transport.CorbaTransportManager;
  72 
  73 import com.sun.corba.se.spi.logging.LogWrapperFactory ;
  74 import com.sun.corba.se.spi.logging.LogWrapperBase ;
  75 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  76 
  77 import com.sun.corba.se.spi.copyobject.CopierManager ;
  78 
  79 import com.sun.corba.se.spi.presentation.rmi.PresentationManager ;
  80 import com.sun.corba.se.spi.presentation.rmi.PresentationDefaults ;
  81 
  82 import com.sun.corba.se.spi.servicecontext.ServiceContextRegistry ;
  83 
  84 // XXX needs an SPI or else it does not belong here
  85 import com.sun.corba.se.impl.corba.TypeCodeImpl ;
  86 import com.sun.corba.se.impl.corba.TypeCodeFactory ;
  87 
  88 // XXX Should there be a SPI level constants ?
  89 import com.sun.corba.se.impl.orbutil.ORBConstants ;
  90 
  91 import com.sun.corba.se.impl.oa.poa.BadServerIdHandler ;
  92 
  93 import com.sun.corba.se.impl.transport.ByteBufferPoolImpl;
  94 
  95 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  96 import com.sun.corba.se.impl.logging.OMGSystemException ;
  97 
  98 import com.sun.corba.se.impl.presentation.rmi.PresentationManagerImpl ;
  99 
 100 import sun.awt.AppContext;
 101 import sun.corba.SharedSecrets;
 102 
 103 public abstract class ORB extends com.sun.corba.se.org.omg.CORBA.ORB
 104     implements Broker, TypeCodeFactory
 105 {
 106     // As much as possible, this class should be stateless.  However,
 107     // there are a few reasons why it is not:
 108     //
 109     // 1. The ORB debug flags are defined here because they are accessed
 110     //    frequently, and we do not want a cast to the impl just for that.
 111     // 2. typeCodeMap and primitiveTypeCodeConstants are here because they
 112     //    are needed in both ORBImpl and ORBSingleton.
 113     // 3. Logging support is here so that we can avoid problems with
 114     //    incompletely initialized ORBs that need to perform logging.
 115 
 116     // Flag set at compile time to debug flag processing: this can't
 117     // be one of the xxxDebugFlags because it is used to debug the mechanism
 118     // that sets the xxxDebugFlags!
 119     public static boolean ORBInitDebug = false;
 120 
 121     // Currently defined debug flags.  Any additions must be called xxxDebugFlag.
 122     // All debug flags must be public boolean types.
 123     // These are set by passing the flag -ORBDebug x,y,z in the ORB init args.
 124     // Note that x,y,z must not contain spaces.
 125     public boolean transportDebugFlag = false ;
 126     public boolean subcontractDebugFlag = false ;
 127     public boolean poaDebugFlag = false ;
 128     public boolean poaConcurrencyDebugFlag = false ;
 129     public boolean poaFSMDebugFlag = false ;
 130     public boolean orbdDebugFlag = false ;
 131     public boolean namingDebugFlag = false ;
 132     public boolean serviceContextDebugFlag = false ;
 133     public boolean transientObjectManagerDebugFlag = false ;
 134     public boolean giopVersionDebugFlag = false;
 135     public boolean shutdownDebugFlag = false;
 136     public boolean giopDebugFlag = false;
 137     public boolean invocationTimingDebugFlag = false ;
 138 
 139     // SystemException log wrappers.  Protected so that they can be used in
 140     // subclasses.
 141     protected static ORBUtilSystemException staticWrapper ;
 142     protected ORBUtilSystemException wrapper ;
 143     protected OMGSystemException omgWrapper ;
 144 
 145     // This map is needed for resolving recursive type code placeholders
 146     // based on the unique repository id.
 147     // XXX Should this be a WeakHashMap for GC?
 148     private Map typeCodeMap ;
 149 
 150     private TypeCodeImpl[] primitiveTypeCodeConstants ;
 151 
 152     // ByteBufferPool - needed by both ORBImpl and ORBSingleton
 153     ByteBufferPool byteBufferPool;
 154 
 155     // Local testing
 156     // XXX clean this up, probably remove these
 157     public abstract boolean isLocalHost( String hostName ) ;
 158     public abstract boolean isLocalServerId( int subcontractId, int serverId ) ;
 159 
 160     // Invocation stack manipulation
 161     public abstract OAInvocationInfo peekInvocationInfo() ;
 162     public abstract void pushInvocationInfo( OAInvocationInfo info ) ;
 163     public abstract OAInvocationInfo popInvocationInfo() ;
 164 
 165     public abstract CorbaTransportManager getCorbaTransportManager();
 166     public abstract LegacyServerSocketManager getLegacyServerSocketManager();
 167 
 168     // wrapperMap maintains a table of LogWrapper instances used by
 169     // different classes to log exceptions.  The key is a StringPair
 170     // representing LogDomain and ExceptionGroup.
 171     private Map wrapperMap ;
 172 
 173     private static Map staticWrapperMap = new ConcurrentHashMap();
 174 
 175     protected MonitoringManager monitoringManager;
 176 
 177     private static PresentationManager setupPresentationManager() {
 178         staticWrapper = ORBUtilSystemException.get(
 179             CORBALogDomains.RPC_PRESENTATION ) ;
 180 
 181         boolean useDynamicStub =
 182             ((Boolean)AccessController.doPrivileged(
 183                 new PrivilegedAction() {
 184                     public java.lang.Object run() {
 185                         return Boolean.valueOf( Boolean.getBoolean (
 186                             ORBConstants.USE_DYNAMIC_STUB_PROPERTY ) ) ;
 187                     }
 188                 }
 189             )).booleanValue() ;
 190 
 191         PresentationManager.StubFactoryFactory dynamicStubFactoryFactory =
 192             (PresentationManager.StubFactoryFactory)AccessController.doPrivileged(
 193                 new PrivilegedAction() {
 194                     public java.lang.Object run() {
 195                         PresentationManager.StubFactoryFactory sff =
 196                             PresentationDefaults.getProxyStubFactoryFactory() ;
 197 
 198                         String className = System.getProperty(
 199                             ORBConstants.DYNAMIC_STUB_FACTORY_FACTORY_CLASS,
 200                             "com.sun.corba.se.impl.presentation.rmi.bcel.StubFactoryFactoryBCELImpl" ) ;
 201 
 202                         try {
 203                             // First try the configured class name, if any
 204                             Class<?> cls = SharedSecrets.getJavaCorbaAccess().loadClass( className ) ;
 205                             sff = (PresentationManager.StubFactoryFactory)cls.newInstance() ;
 206                         } catch (Exception exc) {
 207                             // Use the default. Log the error as a warning.
 208                             staticWrapper.errorInSettingDynamicStubFactoryFactory(
 209                                 exc, className ) ;
 210                         }
 211 
 212                         return sff ;
 213                     }
 214                 }
 215             ) ;
 216 
 217         PresentationManager pm = new PresentationManagerImpl( useDynamicStub ) ;
 218         pm.setStubFactoryFactory( false,
 219             PresentationDefaults.getStaticStubFactoryFactory() ) ;
 220         pm.setStubFactoryFactory( true, dynamicStubFactoryFactory ) ;
 221         return pm;
 222     }
 223 
 224     public void destroy() {
 225         wrapper = null;
 226         omgWrapper = null;
 227         typeCodeMap = null;
 228         primitiveTypeCodeConstants = null;
 229         byteBufferPool = null;
 230     }
 231 
 232     /**
 233      * Returns the Presentation Manager for the current thread group, using the ThreadGroup-specific
 234      * AppContext to hold it. Creates and records one if needed.
 235      */
 236     public static PresentationManager getPresentationManager()
 237     {
 238         AppContext ac = AppContext.getAppContext();
 239         PresentationManager pm = (PresentationManager) ac.get(PresentationManager.class);
 240         if (pm == null) {
 241             pm = setupPresentationManager();
 242             ac.put(PresentationManager.class, pm);
 243         }
 244         return pm;
 245     }
 246 
 247     /** Get the appropriate StubFactoryFactory.  This
 248      * will be dynamic or static depending on whether
 249      * com.sun.CORBA.ORBUseDynamicStub is true or false.
 250      */
 251     public static PresentationManager.StubFactoryFactory
 252         getStubFactoryFactory()
 253     {
 254         PresentationManager gPM = getPresentationManager();
 255         boolean useDynamicStubs = gPM.useDynamicStubs() ;
 256         return gPM.getStubFactoryFactory( useDynamicStubs ) ;
 257     }
 258 
 259     protected ORB()
 260     {
 261         // Initialize logging first, since it is needed nearly
 262         // everywhere (for example, in TypeCodeImpl).
 263         wrapperMap = new ConcurrentHashMap();
 264         wrapper = ORBUtilSystemException.get( this,
 265             CORBALogDomains.RPC_PRESENTATION ) ;
 266         omgWrapper = OMGSystemException.get( this,
 267             CORBALogDomains.RPC_PRESENTATION ) ;
 268 
 269         typeCodeMap = new HashMap();
 270 
 271         primitiveTypeCodeConstants = new TypeCodeImpl[] {
 272             new TypeCodeImpl(this, TCKind._tk_null),
 273             new TypeCodeImpl(this, TCKind._tk_void),
 274             new TypeCodeImpl(this, TCKind._tk_short),
 275             new TypeCodeImpl(this, TCKind._tk_long),
 276             new TypeCodeImpl(this, TCKind._tk_ushort),
 277             new TypeCodeImpl(this, TCKind._tk_ulong),
 278             new TypeCodeImpl(this, TCKind._tk_float),
 279             new TypeCodeImpl(this, TCKind._tk_double),
 280             new TypeCodeImpl(this, TCKind._tk_boolean),
 281             new TypeCodeImpl(this, TCKind._tk_char),
 282             new TypeCodeImpl(this, TCKind._tk_octet),
 283             new TypeCodeImpl(this, TCKind._tk_any),
 284             new TypeCodeImpl(this, TCKind._tk_TypeCode),
 285             new TypeCodeImpl(this, TCKind._tk_Principal),
 286             new TypeCodeImpl(this, TCKind._tk_objref),
 287             null,       // tk_struct
 288             null,       // tk_union
 289             null,       // tk_enum
 290             new TypeCodeImpl(this, TCKind._tk_string),
 291             null,       // tk_sequence
 292             null,       // tk_array
 293             null,       // tk_alias
 294             null,       // tk_except
 295             new TypeCodeImpl(this, TCKind._tk_longlong),
 296             new TypeCodeImpl(this, TCKind._tk_ulonglong),
 297             new TypeCodeImpl(this, TCKind._tk_longdouble),
 298             new TypeCodeImpl(this, TCKind._tk_wchar),
 299             new TypeCodeImpl(this, TCKind._tk_wstring),
 300             new TypeCodeImpl(this, TCKind._tk_fixed),
 301             new TypeCodeImpl(this, TCKind._tk_value),
 302             new TypeCodeImpl(this, TCKind._tk_value_box),
 303             new TypeCodeImpl(this, TCKind._tk_native),
 304             new TypeCodeImpl(this, TCKind._tk_abstract_interface)
 305         } ;
 306 
 307         monitoringManager =
 308             MonitoringFactories.getMonitoringManagerFactory( ).
 309                 createMonitoringManager(
 310                 MonitoringConstants.DEFAULT_MONITORING_ROOT,
 311                 MonitoringConstants.DEFAULT_MONITORING_ROOT_DESCRIPTION);
 312     }
 313 
 314     // Typecode support: needed in both ORBImpl and ORBSingleton
 315     public TypeCodeImpl get_primitive_tc(int kind)
 316     {
 317         synchronized (this) {
 318             checkShutdownState();
 319         }
 320         try {
 321             return primitiveTypeCodeConstants[kind] ;
 322         } catch (Throwable t) {
 323             throw wrapper.invalidTypecodeKind( t, new Integer(kind) ) ;
 324         }
 325     }
 326 
 327     public synchronized void setTypeCode(String id, TypeCodeImpl code)
 328     {
 329         checkShutdownState();
 330         typeCodeMap.put(id, code);
 331     }
 332 
 333     public synchronized TypeCodeImpl getTypeCode(String id)
 334     {
 335         checkShutdownState();
 336         return (TypeCodeImpl)typeCodeMap.get(id);
 337     }
 338 
 339     public MonitoringManager getMonitoringManager( ) {
 340         synchronized (this) {
 341             checkShutdownState();
 342         }
 343         return monitoringManager;
 344     }
 345 
 346     // Special non-standard set_parameters method for
 347     // creating a precisely controlled ORB instance.
 348     // An ORB created by this call is affected only by
 349     // those properties passes explicitly in props, not by
 350     // the system properties and orb.properties files as
 351     // with the standard ORB.init methods.
 352     public abstract void set_parameters( Properties props ) ;
 353 
 354     // ORB versioning
 355     public abstract ORBVersion getORBVersion() ;
 356     public abstract void setORBVersion( ORBVersion version ) ;
 357 
 358     // XXX This needs a better name
 359     public abstract IOR getFVDCodeBaseIOR() ;
 360 
 361     /**
 362      * Handle a bad server id for the given object key.  This should
 363      * always through an exception: either a ForwardException to
 364      * allow another server to handle the request, or else an error
 365      * indication.  XXX Remove after ORT for ORBD work is integrated.
 366      */
 367     public abstract void handleBadServerId( ObjectKey okey ) ;
 368     public abstract void setBadServerIdHandler( BadServerIdHandler handler ) ;
 369     public abstract void initBadServerIdHandler() ;
 370 
 371     public abstract void notifyORB() ;
 372 
 373     public abstract PIHandler getPIHandler() ;
 374 
 375     public abstract void checkShutdownState();
 376 
 377     // Dispatch support: in the ORB because it is needed for shutdown.
 378     // This is used by the first level server side subcontract.
 379     public abstract boolean isDuringDispatch() ;
 380     public abstract void startingDispatch();
 381     public abstract void finishedDispatch();
 382 
 383     /** Return this ORB's transient server ID.  This is needed for
 384      * initializing object adapters.
 385      */
 386     public abstract int getTransientServerId();
 387 
 388     public abstract ServiceContextRegistry getServiceContextRegistry() ;
 389 
 390     public abstract RequestDispatcherRegistry getRequestDispatcherRegistry();
 391 
 392     public abstract ORBData getORBData() ;
 393 
 394     public abstract void setClientDelegateFactory( ClientDelegateFactory factory ) ;
 395 
 396     public abstract ClientDelegateFactory getClientDelegateFactory() ;
 397 
 398     public abstract void setCorbaContactInfoListFactory( CorbaContactInfoListFactory factory ) ;
 399 
 400     public abstract CorbaContactInfoListFactory getCorbaContactInfoListFactory() ;
 401 
 402     // XXX These next 7 methods should be moved to a ResolverManager.
 403 
 404     /** Set the resolver used in this ORB.  This resolver will be used for list_initial_services
 405      * and resolve_initial_references.
 406      */
 407     public abstract void setResolver( Resolver resolver ) ;
 408 
 409     /** Get the resolver used in this ORB.  This resolver will be used for list_initial_services
 410      * and resolve_initial_references.
 411      */
 412     public abstract Resolver getResolver() ;
 413 
 414     /** Set the LocalResolver used in this ORB.  This LocalResolver is used for
 415      * register_initial_reference only.
 416      */
 417     public abstract void setLocalResolver( LocalResolver resolver ) ;
 418 
 419     /** Get the LocalResolver used in this ORB.  This LocalResolver is used for
 420      * register_initial_reference only.
 421      */
 422     public abstract LocalResolver getLocalResolver() ;
 423 
 424     /** Set the operation used in string_to_object calls.  The Operation must expect a
 425      * String and return an org.omg.CORBA.Object.
 426      */
 427     public abstract void setURLOperation( Operation stringToObject ) ;
 428 
 429     /** Get the operation used in string_to_object calls.  The Operation must expect a
 430      * String and return an org.omg.CORBA.Object.
 431      */
 432     public abstract Operation getURLOperation() ;
 433 
 434     /** Set the ServerRequestDispatcher that should be used for handling INS requests.
 435      */
 436     public abstract void setINSDelegate( CorbaServerRequestDispatcher insDelegate ) ;
 437 
 438     // XXX The next 5 operations should be moved to an IORManager.
 439 
 440     /** Factory finders for the various parts of the IOR: tagged components, tagged
 441      * profiles, and tagged profile templates.
 442      */
 443     public abstract TaggedComponentFactoryFinder getTaggedComponentFactoryFinder() ;
 444     public abstract IdentifiableFactoryFinder getTaggedProfileFactoryFinder() ;
 445     public abstract IdentifiableFactoryFinder getTaggedProfileTemplateFactoryFinder() ;
 446 
 447     public abstract ObjectKeyFactory getObjectKeyFactory() ;
 448     public abstract void setObjectKeyFactory( ObjectKeyFactory factory ) ;
 449 
 450     // Logging SPI
 451 
 452     /**
 453      * Returns the logger based on the category.
 454      */
 455     public Logger getLogger( String domain )
 456     {
 457         synchronized (this) {
 458             checkShutdownState();
 459         }
 460         ORBData odata = getORBData() ;
 461 
 462         // Determine the correct ORBId.  There are 3 cases:
 463         // 1. odata is null, which happens if we are getting a logger before
 464         //    ORB initialization is complete.  In this case we cannot determine
 465         //    the ORB ID (it's not known yet), so we set the ORBId to
 466         //    _INITIALIZING_.
 467         // 2. odata is not null, so initialization is complete, but ORBId is set to
 468         //    the default "".  To avoid a ".." in
 469         //    the log domain, we simply use _DEFAULT_ in this case.
 470         // 3. odata is not null, ORBId is not "": just use the ORBId.
 471         String ORBId ;
 472         if (odata == null)
 473             ORBId = "_INITIALIZING_" ;
 474         else {
 475             ORBId = odata.getORBId() ;
 476             if (ORBId.equals(""))
 477                 ORBId = "_DEFAULT_" ;
 478         }
 479 
 480         return getCORBALogger( ORBId, domain ) ;
 481     }
 482 
 483     public static Logger staticGetLogger( String domain )
 484     {
 485         return getCORBALogger( "_CORBA_", domain ) ;
 486     }
 487 
 488     private static Logger getCORBALogger( String ORBId, String domain )
 489     {
 490         String fqLogDomain = CORBALogDomains.TOP_LEVEL_DOMAIN + "." +
 491             ORBId + "." + domain;
 492 
 493         return Logger.getLogger( fqLogDomain, ORBConstants.LOG_RESOURCE_FILE );
 494     }
 495 
 496     /** get the log wrapper class (its type is dependent on the exceptionGroup) for the
 497      * given log domain and exception group in this ORB instance.
 498      */
 499     public LogWrapperBase getLogWrapper( String logDomain,
 500         String exceptionGroup, LogWrapperFactory factory )
 501     {
 502         StringPair key = new StringPair( logDomain, exceptionGroup ) ;
 503 
 504         LogWrapperBase logWrapper = (LogWrapperBase)wrapperMap.get( key );
 505         if (logWrapper == null) {
 506             logWrapper = factory.create( getLogger( logDomain ) );
 507             wrapperMap.put( key, logWrapper );
 508         }
 509 
 510         return logWrapper;
 511     }
 512 
 513     /** get the log wrapper class (its type is dependent on the exceptionGroup) for the
 514      * given log domain and exception group in this ORB instance.
 515      */
 516     public static LogWrapperBase staticGetLogWrapper( String logDomain,
 517         String exceptionGroup, LogWrapperFactory factory )
 518     {
 519         StringPair key = new StringPair( logDomain, exceptionGroup ) ;
 520 
 521         LogWrapperBase logWrapper = (LogWrapperBase)staticWrapperMap.get( key );
 522         if (logWrapper == null) {
 523             logWrapper = factory.create( staticGetLogger( logDomain ) );
 524             staticWrapperMap.put( key, logWrapper );
 525         }
 526 
 527         return logWrapper;
 528     }
 529 
 530     // get a reference to a ByteBufferPool, a pool of NIO ByteBuffers
 531     // NOTE: ByteBuffer pool must be unique per ORB, not per process.
 532     //       There can be more than one ORB per process.
 533     //       This method must also be inherited by both ORB and ORBSingleton.
 534     public ByteBufferPool getByteBufferPool()
 535     {
 536         synchronized (this) {
 537             checkShutdownState();
 538         }
 539         if (byteBufferPool == null)
 540             byteBufferPool = new ByteBufferPoolImpl(this);
 541 
 542         return byteBufferPool;
 543     }
 544 
 545     public abstract void setThreadPoolManager(ThreadPoolManager mgr);
 546 
 547     public abstract ThreadPoolManager getThreadPoolManager();
 548 
 549     public abstract CopierManager getCopierManager() ;
 550 }
 551 
 552 // End of file.