ServantCachingPolicy.java revision 608:7e06bf1dcb09
138889Sjdp/*
2218822Sdim * Copyright (c) 2001, 2003, Oracle and/or its affiliates. All rights reserved.
3130561Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
438889Sjdp *
538889Sjdp * This code is free software; you can redistribute it and/or modify it
685815Sobrien * under the terms of the GNU General Public License version 2 only, as
738889Sjdp * published by the Free Software Foundation.  Oracle designates this
885815Sobrien * particular file as subject to the "Classpath" exception as provided
985815Sobrien * by Oracle in the LICENSE file that accompanied this code.
1085815Sobrien *
1185815Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1238889Sjdp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1385815Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1485815Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1585815Sobrien * accompanied this code).
1685815Sobrien *
1738889Sjdp * You should have received a copy of the GNU General Public License version
1885815Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1985815Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20218822Sdim *
21218822Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2238889Sjdp * or visit www.oracle.com if you need additional information or have any
23218822Sdim * questions.
2438889Sjdp */
2538889Sjdp
2638889Sjdppackage com.sun.corba.se.spi.extension ;
2738889Sjdp
2889857Sobrienimport org.omg.CORBA.Policy ;
2938889Sjdpimport org.omg.CORBA.LocalObject ;
3038889Sjdpimport com.sun.corba.se.impl.orbutil.ORBConstants ;
3138889Sjdp
3285815Sobrien/** Policy used to implement servant caching optimization in the POA.
33130561Sobrien* Creating a POA with an instance pol of this policy where
3438889Sjdp* pol.getType() > NO_SERVANT_CACHING will cause the servant to be
35218822Sdim* looked up in the POA and cached in the LocalClientRequestDispatcher when
36218822Sdim* the ClientRequestDispatcher is colocated with the implementation of the
37218822Sdim* objref.  This greatly speeds up invocations at the cost of violating the
38218822Sdim* POA semantics.  In particular, every request to a particular objref
39218822Sdim* must be handled by the same servant.  Note that this is typically the
40218822Sdim* case for EJB implementations.
41218822Sdim* <p>
42218822Sdim* If servant caching is used, there are two different additional
43218822Sdim* features of the POA that are expensive:
44218822Sdim* <ol>
45218822Sdim* <li>POA current semantics
46218822Sdim* <li>Proper handling of POA destroy.
47218822Sdim* <ol>
48218822Sdim* POA current semantics requires maintaining a ThreadLocal stack of
49218822Sdim* invocation information that is always available for POACurrent operations.
50218822Sdim* Maintaining this stack is expensive on the timescale of optimized co-located
51218822Sdim* calls, so the option is provided to turn it off.  Similarly, causing
52218822Sdim* POA.destroy() calls to wait for all active calls in the POA to complete
53218822Sdim* requires careful tracking of the entry and exit of invocations in the POA.
54218822Sdim* Again, tracking this is somewhat expensive.
55218822Sdim*/
56218822Sdimpublic class ServantCachingPolicy extends LocalObject implements Policy
5738889Sjdp{
5838889Sjdp    /** Do not cache servants in the ClientRequestDispatcher.  This will
5938889Sjdp     * always support the full POA semantics, including changing the
60218822Sdim     * servant that handles requests on a particular objref.
61218822Sdim     */
62218822Sdim    public static final int NO_SERVANT_CACHING = 0 ;
63218822Sdim
64218822Sdim    /** Perform servant caching, preserving POA current and POA destroy semantics.
65218822Sdim    * We will use this as the new default, as the app server is making heavier use
66218822Sdim    * now of POA facilities.
67218822Sdim    */
68218822Sdim    public static final int FULL_SEMANTICS = 1 ;
69218822Sdim
70218822Sdim    /** Perform servant caching, preservent only POA current semantics.
71218822Sdim    * At least this level is required in order to support selection of ObjectCopiers
72218822Sdim    * for co-located RMI-IIOP calls, as the current copier is stored in
7338889Sjdp    * OAInvocationInfo, which must be present on the stack inside the call.
7438889Sjdp    */
75218822Sdim    public static final int INFO_ONLY_SEMANTICS =  2 ;
76218822Sdim
77218822Sdim    /** Perform servant caching, not preserving POA current or POA destroy semantics.
78218822Sdim    */
79218822Sdim    public static final int MINIMAL_SEMANTICS = 3 ;
80218822Sdim
81218822Sdim    private static ServantCachingPolicy policy = null ;
82218822Sdim    private static ServantCachingPolicy infoOnlyPolicy = null ;
83218822Sdim    private static ServantCachingPolicy minimalPolicy = null ;
84218822Sdim
85218822Sdim    private int type ;
86218822Sdim
87218822Sdim    public String typeToName()
8838889Sjdp    {
8938889Sjdp        switch (type) {
90218822Sdim            case FULL_SEMANTICS:
91218822Sdim                return "FULL" ;
92218822Sdim            case INFO_ONLY_SEMANTICS:
93218822Sdim                return "INFO_ONLY" ;
94218822Sdim            case MINIMAL_SEMANTICS:
95218822Sdim                return "MINIMAL" ;
96218822Sdim            default:
97218822Sdim                return "UNKNOWN(" + type + ")" ;
98218822Sdim        }
99218822Sdim    }
100218822Sdim
101218822Sdim    public String toString()
102218822Sdim    {
10338889Sjdp        return "ServantCachingPolicy[" + typeToName() + "]" ;
10438889Sjdp    }
105218822Sdim
106218822Sdim    private ServantCachingPolicy( int type )
107218822Sdim    {
108218822Sdim        this.type = type ;
109218822Sdim    }
110218822Sdim
111218822Sdim    public int getType()
112218822Sdim    {
113218822Sdim        return type ;
114218822Sdim    }
115218822Sdim
116218822Sdim    /** Return the default servant caching policy.
117218822Sdim    */
11838889Sjdp    public synchronized static ServantCachingPolicy getPolicy()
11938889Sjdp    {
12038889Sjdp        return getFullPolicy() ;
12138889Sjdp    }
12238889Sjdp
12338889Sjdp    public synchronized static ServantCachingPolicy getFullPolicy()
12460484Sobrien    {
12538889Sjdp        if (policy == null)
12638889Sjdp            policy = new ServantCachingPolicy( FULL_SEMANTICS ) ;
12738889Sjdp
12838889Sjdp        return policy ;
12938889Sjdp    }
13038889Sjdp
13138889Sjdp    public synchronized static ServantCachingPolicy getInfoOnlyPolicy()
13238889Sjdp    {
13338889Sjdp        if (infoOnlyPolicy == null)
13438889Sjdp            infoOnlyPolicy = new ServantCachingPolicy( INFO_ONLY_SEMANTICS ) ;
13538889Sjdp
13638889Sjdp        return infoOnlyPolicy ;
13738889Sjdp    }
138218822Sdim
139218822Sdim    public synchronized static ServantCachingPolicy getMinimalPolicy()
14038889Sjdp    {
14138889Sjdp        if (minimalPolicy == null)
14238889Sjdp            minimalPolicy = new ServantCachingPolicy( MINIMAL_SEMANTICS ) ;
14389857Sobrien
14489857Sobrien        return minimalPolicy ;
14589857Sobrien    }
14689857Sobrien
14738889Sjdp    public int policy_type ()
14838889Sjdp    {
14938889Sjdp        return ORBConstants.SERVANT_CACHING_POLICY ;
150218822Sdim    }
151218822Sdim
152218822Sdim    public org.omg.CORBA.Policy copy ()
153218822Sdim    {
154218822Sdim        return this ;
155218822Sdim    }
156218822Sdim
157218822Sdim    public void destroy ()
158218822Sdim    {
159218822Sdim        // NO-OP
160218822Sdim    }
161218822Sdim}
162218822Sdim