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