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