KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > oa > poa > Policies


1 /*
2  * @(#)Policies.java 1.23 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.oa.poa;
9
10 import java.util.HashMap JavaDoc ;
11 import java.util.BitSet JavaDoc ;
12 import java.util.Iterator JavaDoc ;
13
14 import com.sun.corba.se.impl.orbutil.ORBConstants ;
15 import com.sun.corba.se.spi.extension.ServantCachingPolicy ;
16 import com.sun.corba.se.spi.extension.ZeroPortPolicy ;
17 import com.sun.corba.se.spi.extension.CopyObjectPolicy ;
18
19 import org.omg.CORBA.*;
20 import org.omg.PortableServer.*;
21 import org.omg.PortableServer.POAPackage.*;
22
23 public final class Policies {
24 /* Order of *POLICY_ID :
25    THREAD_
26    LIFESPAN_
27    ID_UNIQUENESS_
28    ID_ASSIGNMENT_
29    IMPLICIT_ACTIVATION_
30    SERvANT_RETENTION_
31    REQUEST_PROCESSING_
32    The code in this class depends on this order!
33 */

34     private static final int MIN_POA_POLICY_ID = THREAD_POLICY_ID.value ;
35     private static final int MAX_POA_POLICY_ID = REQUEST_PROCESSING_POLICY_ID.value ;
36     private static final int POLICY_TABLE_SIZE = MAX_POA_POLICY_ID -
37         MIN_POA_POLICY_ID + 1 ;
38
39     int defaultObjectCopierFactoryId ;
40
41     private HashMap JavaDoc policyMap = new HashMap JavaDoc() ; // Maps Integer(policy type) to Policy
42

43     public static final Policies defaultPolicies
44     = new Policies() ;
45
46     public static final Policies rootPOAPolicies
47         = new Policies(
48         ThreadPolicyValue._ORB_CTRL_MODEL,
49             LifespanPolicyValue._TRANSIENT,
50             IdUniquenessPolicyValue._UNIQUE_ID,
51             IdAssignmentPolicyValue._SYSTEM_ID,
52             ImplicitActivationPolicyValue._IMPLICIT_ACTIVATION,
53             ServantRetentionPolicyValue._RETAIN,
54             RequestProcessingPolicyValue._USE_ACTIVE_OBJECT_MAP_ONLY ) ;
55               
56     private int[] poaPolicyValues ;
57
58     private int getPolicyValue( int id )
59     {
60         return poaPolicyValues[ id - MIN_POA_POLICY_ID ] ;
61     }
62
63     private void setPolicyValue( int id, int value )
64     {
65         poaPolicyValues[ id - MIN_POA_POLICY_ID ] = value ;
66     }
67     
68     private Policies(
69         int threadModel,
70     int lifespan,
71     int idUniqueness,
72     int idAssignment,
73     int implicitActivation,
74         int retention,
75     int requestProcessing )
76     {
77     poaPolicyValues = new int[] {
78         threadModel,
79         lifespan,
80         idUniqueness,
81         idAssignment,
82         implicitActivation,
83         retention,
84         requestProcessing };
85     }
86
87     private Policies() {
88         this( ThreadPolicyValue._ORB_CTRL_MODEL,
89         LifespanPolicyValue._TRANSIENT,
90         IdUniquenessPolicyValue._UNIQUE_ID,
91         IdAssignmentPolicyValue._SYSTEM_ID,
92         ImplicitActivationPolicyValue._NO_IMPLICIT_ACTIVATION,
93         ServantRetentionPolicyValue._RETAIN,
94         RequestProcessingPolicyValue._USE_ACTIVE_OBJECT_MAP_ONLY ) ;
95     }
96
97     public String JavaDoc toString() {
98     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
99     buffer.append( "Policies[" ) ;
100     boolean first = true ;
101     Iterator JavaDoc iter = policyMap.values().iterator() ;
102     while (iter.hasNext()) {
103         if (first)
104         first = false ;
105         else
106         buffer.append( "," ) ;
107
108         buffer.append( iter.next().toString() ) ;
109     }
110     buffer.append( "]" ) ;
111     return buffer.toString() ;
112     }
113
114     /* Returns the integer value of the POA policy, if this is a
115      * POA policy, otherwise returns -1.
116      */

117     private int getPOAPolicyValue( Policy policy)
118     {
119         if (policy instanceof ThreadPolicy) {
120         return ((ThreadPolicy) policy).value().value();
121         } else if (policy instanceof LifespanPolicy) {
122         return ((LifespanPolicy) policy).value().value();
123         } else if (policy instanceof IdUniquenessPolicy) {
124         return ((IdUniquenessPolicy) policy).value().value();
125         } else if (policy instanceof IdAssignmentPolicy) {
126         return ((IdAssignmentPolicy) policy).value().value();
127         } else if (policy instanceof ServantRetentionPolicy) {
128         return ((ServantRetentionPolicy) policy).value().value();
129         } else if (policy instanceof RequestProcessingPolicy) {
130         return ((RequestProcessingPolicy) policy).value().value();
131         } else if (policy instanceof ImplicitActivationPolicy) {
132         return ((ImplicitActivationPolicy) policy).value().value();
133         } else
134         return -1 ;
135     }
136
137     /** If any errors were found, throw INVALID_POLICY with the smallest
138      * index of any offending policy.
139      */

140     private void checkForPolicyError( BitSet JavaDoc errorSet ) throws InvalidPolicy
141     {
142         for (short ctr=0; ctr<errorSet.length(); ctr++ )
143         if (errorSet.get(ctr))
144         throw new InvalidPolicy(ctr);
145     }
146
147     /** Add the first index in policies at which the policy is of type
148     * policyId to errorSet, if the polictId is in policies (it may not be).
149     */

150     private void addToErrorSet( Policy[] policies, int policyId,
151     BitSet JavaDoc errorSet )
152     {
153         for (int ctr=0; ctr<policies.length; ctr++ )
154         if (policies[ctr].policy_type() == policyId) {
155         errorSet.set( ctr ) ;
156         return ;
157         }
158     }
159
160     /** Main constructor used from POA::create_POA. This need only be visible
161     * within the POA package.
162     */

163     Policies(Policy[] policies, int id ) throws InvalidPolicy
164     {
165     // Make sure the defaults are set according to the POA spec
166
this();
167
168     defaultObjectCopierFactoryId = id ;
169
170     if ( policies == null )
171         return;
172
173     // Set to record all indices in policies for which errors
174
// were observed.
175
BitSet JavaDoc errorSet = new BitSet JavaDoc( policies.length ) ;
176
177     for(short i = 0; i < policies.length; i++) {
178         Policy policy = policies[i];
179         int POAPolicyValue = getPOAPolicyValue( policy ) ;
180
181         // Save the policy in policyMap to support
182
// POA.get_effective_policy, if it was not already saved
183
// in policyMap.
184
Integer JavaDoc key = new Integer JavaDoc( policy.policy_type() ) ;
185         Policy prev = (Policy)(policyMap.get( key )) ;
186         if (prev == null)
187             policyMap.put( key, policy ) ;
188
189         if (POAPolicyValue >= 0) {
190             setPolicyValue( key.intValue(), POAPolicyValue ) ;
191
192         // if the value of this POA policy was previously set to a
193
// different value than the current value given in
194
// POAPolicyValue, record an error.
195
if ((prev != null) &&
196             (getPOAPolicyValue( prev ) != POAPolicyValue))
197             errorSet.set( i ) ;
198         }
199     }
200
201     // Check for bad policy combinations
202

203     // NON_RETAIN requires USE_DEFAULT_SERVANT or USE_SERVANT_MANAGER
204
if (!retainServants() && useActiveMapOnly() ) {
205         addToErrorSet( policies, SERVANT_RETENTION_POLICY_ID.value,
206         errorSet ) ;
207         addToErrorSet( policies, REQUEST_PROCESSING_POLICY_ID.value,
208         errorSet ) ;
209     }
210
211     // IMPLICIT_ACTIVATION requires SYSTEM_ID and RETAIN
212
if (isImplicitlyActivated()) {
213         if (!retainServants()) {
214         addToErrorSet( policies, IMPLICIT_ACTIVATION_POLICY_ID.value,
215             errorSet ) ;
216         addToErrorSet( policies, SERVANT_RETENTION_POLICY_ID.value,
217             errorSet ) ;
218         }
219
220         if (!isSystemAssignedIds()) {
221         addToErrorSet( policies, IMPLICIT_ACTIVATION_POLICY_ID.value,
222             errorSet ) ;
223         addToErrorSet( policies, ID_ASSIGNMENT_POLICY_ID.value,
224             errorSet ) ;
225         }
226     }
227
228     checkForPolicyError( errorSet ) ;
229     }
230     
231     public Policy get_effective_policy( int type )
232     {
233     Integer JavaDoc key = new Integer JavaDoc( type ) ;
234     Policy result = (Policy)(policyMap.get(key)) ;
235     return result ;
236     }
237
238     /* Thread Policies */
239     public final boolean isOrbControlledThreads() {
240     return getPolicyValue( THREAD_POLICY_ID.value ) ==
241         ThreadPolicyValue._ORB_CTRL_MODEL;
242     }
243     public final boolean isSingleThreaded() {
244     return getPolicyValue( THREAD_POLICY_ID.value ) ==
245         ThreadPolicyValue._SINGLE_THREAD_MODEL;
246     }
247
248     /* Lifespan */
249     public final boolean isTransient() {
250     return getPolicyValue( LIFESPAN_POLICY_ID.value ) ==
251         LifespanPolicyValue._TRANSIENT;
252     }
253     public final boolean isPersistent() {
254     return getPolicyValue( LIFESPAN_POLICY_ID.value ) ==
255         LifespanPolicyValue._PERSISTENT;
256     }
257
258     /* ID Uniqueness */
259     public final boolean isUniqueIds() {
260     return getPolicyValue( ID_UNIQUENESS_POLICY_ID.value ) ==
261         IdUniquenessPolicyValue._UNIQUE_ID;
262     }
263     public final boolean isMultipleIds() {
264     return getPolicyValue( ID_UNIQUENESS_POLICY_ID.value ) ==
265         IdUniquenessPolicyValue._MULTIPLE_ID;
266     }
267
268     /* ID Assignment */
269     public final boolean isUserAssignedIds() {
270     return getPolicyValue( ID_ASSIGNMENT_POLICY_ID.value ) ==
271         IdAssignmentPolicyValue._USER_ID;
272     }
273     public final boolean isSystemAssignedIds() {
274     return getPolicyValue( ID_ASSIGNMENT_POLICY_ID.value ) ==
275         IdAssignmentPolicyValue._SYSTEM_ID;
276     }
277
278     /* Servant Rentention */
279     public final boolean retainServants() {
280     return getPolicyValue( SERVANT_RETENTION_POLICY_ID.value ) ==
281         ServantRetentionPolicyValue._RETAIN;
282     }
283
284     /* Request Processing */
285     public final boolean useActiveMapOnly() {
286     return getPolicyValue( REQUEST_PROCESSING_POLICY_ID.value ) ==
287         RequestProcessingPolicyValue._USE_ACTIVE_OBJECT_MAP_ONLY;
288     }
289     public final boolean useDefaultServant() {
290     return getPolicyValue( REQUEST_PROCESSING_POLICY_ID.value ) ==
291         RequestProcessingPolicyValue._USE_DEFAULT_SERVANT;
292     }
293     public final boolean useServantManager() {
294     return getPolicyValue( REQUEST_PROCESSING_POLICY_ID.value ) ==
295         RequestProcessingPolicyValue._USE_SERVANT_MANAGER;
296     }
297
298     /* Implicit Activation */
299     public final boolean isImplicitlyActivated() {
300     return getPolicyValue( IMPLICIT_ACTIVATION_POLICY_ID.value ) ==
301     ImplicitActivationPolicyValue._IMPLICIT_ACTIVATION;
302     }
303
304     /* proprietary servant caching policy */
305     public final int servantCachingLevel()
306     {
307     Integer JavaDoc key = new Integer JavaDoc( ORBConstants.SERVANT_CACHING_POLICY ) ;
308     ServantCachingPolicy policy = (ServantCachingPolicy)policyMap.get( key ) ;
309     if (policy == null)
310         return ServantCachingPolicy.NO_SERVANT_CACHING ;
311     else
312         return policy.getType() ;
313     }
314
315     public final boolean forceZeroPort()
316     {
317     Integer JavaDoc key = new Integer JavaDoc( ORBConstants.ZERO_PORT_POLICY ) ;
318     ZeroPortPolicy policy = (ZeroPortPolicy)policyMap.get( key ) ;
319     if (policy == null)
320         return false ;
321     else
322         return policy.forceZeroPort() ;
323     }
324
325     public final int getCopierId()
326     {
327     Integer JavaDoc key = new Integer JavaDoc( ORBConstants.COPY_OBJECT_POLICY ) ;
328     CopyObjectPolicy policy = (CopyObjectPolicy)policyMap.get( key ) ;
329     if (policy != null)
330         return policy.getValue() ;
331     else
332         return defaultObjectCopierFactoryId ;
333     }
334 }
335
Popular Tags