Policies.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1997, 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
26package com.sun.corba.se.impl.oa.poa;
27
28import java.util.HashMap ;
29import java.util.BitSet ;
30import java.util.Iterator ;
31
32import com.sun.corba.se.impl.orbutil.ORBConstants ;
33import com.sun.corba.se.spi.extension.ServantCachingPolicy ;
34import com.sun.corba.se.spi.extension.ZeroPortPolicy ;
35import com.sun.corba.se.spi.extension.CopyObjectPolicy ;
36
37import org.omg.CORBA.*;
38import org.omg.PortableServer.*;
39import org.omg.PortableServer.POAPackage.*;
40
41public final class Policies {
42/* Order of *POLICY_ID :
43   THREAD_
44   LIFESPAN_
45   ID_UNIQUENESS_
46   ID_ASSIGNMENT_
47   IMPLICIT_ACTIVATION_
48   SERvANT_RETENTION_
49   REQUEST_PROCESSING_
50   The code in this class depends on this order!
51*/
52    private static final int MIN_POA_POLICY_ID = THREAD_POLICY_ID.value ;
53    private static final int MAX_POA_POLICY_ID = REQUEST_PROCESSING_POLICY_ID.value ;
54    private static final int POLICY_TABLE_SIZE = MAX_POA_POLICY_ID -
55        MIN_POA_POLICY_ID + 1 ;
56
57    int defaultObjectCopierFactoryId ;
58
59    private HashMap policyMap = new HashMap() ; // Maps Integer(policy type) to Policy
60
61    public static final Policies defaultPolicies
62        = new Policies() ;
63
64    public static final Policies rootPOAPolicies
65        = new Policies(
66            ThreadPolicyValue._ORB_CTRL_MODEL,
67            LifespanPolicyValue._TRANSIENT,
68            IdUniquenessPolicyValue._UNIQUE_ID,
69            IdAssignmentPolicyValue._SYSTEM_ID,
70            ImplicitActivationPolicyValue._IMPLICIT_ACTIVATION,
71            ServantRetentionPolicyValue._RETAIN,
72            RequestProcessingPolicyValue._USE_ACTIVE_OBJECT_MAP_ONLY ) ;
73
74    private int[] poaPolicyValues ;
75
76    private int getPolicyValue( int id )
77    {
78        return poaPolicyValues[ id - MIN_POA_POLICY_ID ] ;
79    }
80
81    private void setPolicyValue( int id, int value )
82    {
83        poaPolicyValues[ id - MIN_POA_POLICY_ID ] = value ;
84    }
85
86    private Policies(
87        int threadModel,
88        int lifespan,
89        int idUniqueness,
90        int idAssignment,
91        int implicitActivation,
92        int retention,
93        int requestProcessing )
94    {
95        poaPolicyValues = new int[] {
96            threadModel,
97            lifespan,
98            idUniqueness,
99            idAssignment,
100            implicitActivation,
101            retention,
102            requestProcessing };
103    }
104
105    private Policies() {
106        this( ThreadPolicyValue._ORB_CTRL_MODEL,
107            LifespanPolicyValue._TRANSIENT,
108            IdUniquenessPolicyValue._UNIQUE_ID,
109            IdAssignmentPolicyValue._SYSTEM_ID,
110            ImplicitActivationPolicyValue._NO_IMPLICIT_ACTIVATION,
111            ServantRetentionPolicyValue._RETAIN,
112            RequestProcessingPolicyValue._USE_ACTIVE_OBJECT_MAP_ONLY ) ;
113    }
114
115    public String toString() {
116        StringBuffer buffer = new StringBuffer();
117        buffer.append( "Policies[" ) ;
118        boolean first = true ;
119        Iterator iter = policyMap.values().iterator() ;
120        while (iter.hasNext()) {
121            if (first)
122                first = false ;
123            else
124                buffer.append( "," ) ;
125
126            buffer.append( iter.next().toString() ) ;
127        }
128        buffer.append( "]" ) ;
129        return buffer.toString() ;
130    }
131
132    /* Returns the integer value of the POA policy, if this is a
133     * POA policy, otherwise returns -1.
134     */
135    private int getPOAPolicyValue( Policy policy)
136    {
137        if (policy instanceof ThreadPolicy) {
138            return ((ThreadPolicy) policy).value().value();
139        } else if (policy instanceof LifespanPolicy) {
140            return ((LifespanPolicy) policy).value().value();
141        } else if (policy instanceof IdUniquenessPolicy) {
142            return ((IdUniquenessPolicy) policy).value().value();
143        } else if (policy instanceof IdAssignmentPolicy) {
144            return ((IdAssignmentPolicy) policy).value().value();
145        } else if (policy instanceof ServantRetentionPolicy) {
146            return ((ServantRetentionPolicy) policy).value().value();
147        } else if (policy instanceof RequestProcessingPolicy) {
148            return  ((RequestProcessingPolicy) policy).value().value();
149        } else if (policy instanceof ImplicitActivationPolicy) {
150            return ((ImplicitActivationPolicy) policy).value().value();
151        }  else
152            return -1 ;
153    }
154
155    /** If any errors were found, throw INVALID_POLICY with the smallest
156     * index of any offending policy.
157     */
158    private void checkForPolicyError( BitSet errorSet ) throws InvalidPolicy
159    {
160        for (short ctr=0; ctr<errorSet.length(); ctr++ )
161            if (errorSet.get(ctr))
162                throw new InvalidPolicy(ctr);
163    }
164
165    /** Add the first index in policies at which the policy is of type
166    * policyId to errorSet, if the polictId is in policies (it may not be).
167    */
168    private void addToErrorSet( Policy[] policies, int policyId,
169        BitSet errorSet )
170    {
171        for (int ctr=0; ctr<policies.length; ctr++ )
172            if (policies[ctr].policy_type() == policyId) {
173                errorSet.set( ctr ) ;
174                return ;
175            }
176    }
177
178    /** Main constructor used from POA::create_POA.  This need only be visible
179    * within the POA package.
180    */
181    Policies(Policy[] policies, int id ) throws InvalidPolicy
182    {
183        // Make sure the defaults are set according to the POA spec
184        this();
185
186        defaultObjectCopierFactoryId = id ;
187
188        if ( policies == null )
189            return;
190
191        // Set to record all indices in policies for which errors
192        // were observed.
193        BitSet errorSet = new BitSet( policies.length ) ;
194
195        for(short i = 0; i < policies.length; i++) {
196            Policy policy = policies[i];
197            int POAPolicyValue = getPOAPolicyValue( policy ) ;
198
199            // Save the policy in policyMap to support
200            // POA.get_effective_policy, if it was not already saved
201            // in policyMap.
202            Integer key = new Integer( policy.policy_type() ) ;
203            Policy prev = (Policy)(policyMap.get( key )) ;
204            if (prev == null)
205                policyMap.put( key, policy ) ;
206
207            if (POAPolicyValue >= 0) {
208                setPolicyValue( key.intValue(), POAPolicyValue  ) ;
209
210                // if the value of this POA policy was previously set to a
211                // different value than the current value given in
212                // POAPolicyValue, record an error.
213                if ((prev != null) &&
214                    (getPOAPolicyValue( prev ) != POAPolicyValue))
215                    errorSet.set( i ) ;
216            }
217        }
218
219        // Check for bad policy combinations
220
221        // NON_RETAIN requires USE_DEFAULT_SERVANT or USE_SERVANT_MANAGER
222        if (!retainServants() && useActiveMapOnly() ) {
223            addToErrorSet( policies, SERVANT_RETENTION_POLICY_ID.value,
224                errorSet ) ;
225            addToErrorSet( policies, REQUEST_PROCESSING_POLICY_ID.value,
226                errorSet ) ;
227        }
228
229        // IMPLICIT_ACTIVATION requires SYSTEM_ID and RETAIN
230        if (isImplicitlyActivated()) {
231            if (!retainServants()) {
232                addToErrorSet( policies, IMPLICIT_ACTIVATION_POLICY_ID.value,
233                    errorSet ) ;
234                addToErrorSet( policies, SERVANT_RETENTION_POLICY_ID.value,
235                    errorSet ) ;
236            }
237
238            if (!isSystemAssignedIds()) {
239                addToErrorSet( policies, IMPLICIT_ACTIVATION_POLICY_ID.value,
240                    errorSet ) ;
241                addToErrorSet( policies, ID_ASSIGNMENT_POLICY_ID.value,
242                    errorSet ) ;
243            }
244        }
245
246        checkForPolicyError( errorSet ) ;
247    }
248
249    public Policy get_effective_policy( int type )
250    {
251        Integer key = new Integer( type ) ;
252        Policy result = (Policy)(policyMap.get(key)) ;
253        return result ;
254    }
255
256    /* Thread Policies */
257    public final boolean isOrbControlledThreads() {
258        return getPolicyValue( THREAD_POLICY_ID.value ) ==
259            ThreadPolicyValue._ORB_CTRL_MODEL;
260    }
261    public final boolean isSingleThreaded() {
262        return getPolicyValue( THREAD_POLICY_ID.value ) ==
263            ThreadPolicyValue._SINGLE_THREAD_MODEL;
264    }
265
266    /* Lifespan */
267    public final boolean isTransient() {
268        return getPolicyValue( LIFESPAN_POLICY_ID.value ) ==
269            LifespanPolicyValue._TRANSIENT;
270    }
271    public final boolean isPersistent() {
272        return getPolicyValue( LIFESPAN_POLICY_ID.value ) ==
273            LifespanPolicyValue._PERSISTENT;
274    }
275
276    /* ID Uniqueness */
277    public final boolean isUniqueIds() {
278        return getPolicyValue( ID_UNIQUENESS_POLICY_ID.value ) ==
279            IdUniquenessPolicyValue._UNIQUE_ID;
280    }
281    public final boolean isMultipleIds() {
282        return getPolicyValue( ID_UNIQUENESS_POLICY_ID.value ) ==
283            IdUniquenessPolicyValue._MULTIPLE_ID;
284    }
285
286    /* ID Assignment */
287    public final boolean isUserAssignedIds() {
288        return getPolicyValue( ID_ASSIGNMENT_POLICY_ID.value ) ==
289            IdAssignmentPolicyValue._USER_ID;
290    }
291    public final boolean isSystemAssignedIds() {
292        return getPolicyValue( ID_ASSIGNMENT_POLICY_ID.value ) ==
293            IdAssignmentPolicyValue._SYSTEM_ID;
294    }
295
296    /* Servant Rentention */
297    public final boolean retainServants() {
298        return getPolicyValue( SERVANT_RETENTION_POLICY_ID.value ) ==
299            ServantRetentionPolicyValue._RETAIN;
300    }
301
302    /* Request Processing */
303    public final boolean useActiveMapOnly() {
304        return getPolicyValue( REQUEST_PROCESSING_POLICY_ID.value ) ==
305            RequestProcessingPolicyValue._USE_ACTIVE_OBJECT_MAP_ONLY;
306    }
307    public final boolean useDefaultServant() {
308        return getPolicyValue( REQUEST_PROCESSING_POLICY_ID.value ) ==
309            RequestProcessingPolicyValue._USE_DEFAULT_SERVANT;
310    }
311    public final boolean useServantManager() {
312        return getPolicyValue( REQUEST_PROCESSING_POLICY_ID.value ) ==
313            RequestProcessingPolicyValue._USE_SERVANT_MANAGER;
314    }
315
316    /* Implicit Activation */
317    public final boolean isImplicitlyActivated() {
318        return getPolicyValue( IMPLICIT_ACTIVATION_POLICY_ID.value ) ==
319        ImplicitActivationPolicyValue._IMPLICIT_ACTIVATION;
320    }
321
322    /* proprietary servant caching policy */
323    public final int servantCachingLevel()
324    {
325        Integer key = new Integer( ORBConstants.SERVANT_CACHING_POLICY ) ;
326        ServantCachingPolicy policy = (ServantCachingPolicy)policyMap.get( key ) ;
327        if (policy == null)
328            return ServantCachingPolicy.NO_SERVANT_CACHING ;
329        else
330            return policy.getType() ;
331    }
332
333    public final boolean forceZeroPort()
334    {
335        Integer key = new Integer( ORBConstants.ZERO_PORT_POLICY ) ;
336        ZeroPortPolicy policy = (ZeroPortPolicy)policyMap.get( key ) ;
337        if (policy == null)
338            return false ;
339        else
340            return policy.forceZeroPort() ;
341    }
342
343    public final int getCopierId()
344    {
345        Integer key = new Integer( ORBConstants.COPY_OBJECT_POLICY ) ;
346        CopyObjectPolicy policy = (CopyObjectPolicy)policyMap.get( key ) ;
347        if (policy != null)
348            return policy.getValue() ;
349        else
350            return defaultObjectCopierFactoryId ;
351    }
352}
353