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