1. /*
  2. * @(#)Policies.java 1.21 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.POA;
  8. import java.util.HashMap ;
  9. import java.util.Iterator ;
  10. import com.sun.corba.se.internal.orbutil.ORBConstants ;
  11. import org.omg.CORBA.*;
  12. import org.omg.PortableServer.*;
  13. import org.omg.PortableServer.POAPackage.*;
  14. public final class Policies {
  15. private boolean cachingServantAllowed = false ;
  16. private int threadModel;
  17. private int lifespan;
  18. private int idUniqueness;
  19. private int idAssignment;
  20. private int retention;
  21. private int requestProcessing;
  22. private int implicitActivation;
  23. private HashMap policies = new HashMap() ; // Maps Integer(policy type) to Policy
  24. public static final Policies defaultPolicies
  25. = new Policies(ThreadPolicyValue._ORB_CTRL_MODEL,
  26. LifespanPolicyValue._TRANSIENT,
  27. IdUniquenessPolicyValue._UNIQUE_ID,
  28. IdAssignmentPolicyValue._SYSTEM_ID,
  29. ServantRetentionPolicyValue._RETAIN,
  30. RequestProcessingPolicyValue._USE_ACTIVE_OBJECT_MAP_ONLY,
  31. ImplicitActivationPolicyValue._NO_IMPLICIT_ACTIVATION);
  32. public static final Policies rootPOAPolicies
  33. = new Policies(ThreadPolicyValue._ORB_CTRL_MODEL,
  34. LifespanPolicyValue._TRANSIENT,
  35. IdUniquenessPolicyValue._UNIQUE_ID,
  36. IdAssignmentPolicyValue._SYSTEM_ID,
  37. ServantRetentionPolicyValue._RETAIN,
  38. RequestProcessingPolicyValue._USE_ACTIVE_OBJECT_MAP_ONLY,
  39. ImplicitActivationPolicyValue._IMPLICIT_ACTIVATION);
  40. private Policies(int threadModel, int lifespan, int idUniqueness, int idAssignment,
  41. int retention, int requestProcessing, int implicitActivation) {
  42. this.threadModel = threadModel;
  43. this.lifespan = lifespan;
  44. this.idUniqueness = idUniqueness;
  45. this.idAssignment = idAssignment;
  46. this.retention = retention;
  47. this.requestProcessing = requestProcessing;
  48. this.implicitActivation = implicitActivation;
  49. }
  50. Policies() {
  51. this.threadModel = defaultPolicies.threadModel;
  52. this.lifespan = defaultPolicies.lifespan;
  53. this.idUniqueness = defaultPolicies.idUniqueness;
  54. this.idAssignment = defaultPolicies.idAssignment;
  55. this.retention = defaultPolicies.retention;
  56. this.requestProcessing = defaultPolicies.requestProcessing;
  57. this.implicitActivation = defaultPolicies.implicitActivation;
  58. }
  59. Policies(int bitmap) {
  60. this.threadModel = (bitmap & 0x0080) >> 7;
  61. this.lifespan = (bitmap & 0x0040) >> 6;
  62. this.idUniqueness = (bitmap & 0x0020) >> 5;
  63. this.idAssignment = (bitmap & 0x0010) >> 4;
  64. this.implicitActivation = (bitmap & 0x0008) >> 3;
  65. this.retention = (bitmap & 0x0004) >> 2;
  66. this.requestProcessing = bitmap & 0x0003;
  67. }
  68. public String toString() {
  69. StringBuffer buffer = new StringBuffer();
  70. buffer.append( "Policies[" ) ;
  71. boolean first = true ;
  72. Iterator iter = policies.values().iterator() ;
  73. while (iter.hasNext()) {
  74. if (first)
  75. first = false ;
  76. else
  77. buffer.append( "," ) ;
  78. buffer.append( iter.next().toString() ) ;
  79. }
  80. buffer.append( "]" ) ;
  81. return buffer.toString() ;
  82. }
  83. Policies(Policy[] policies) throws InvalidPolicy {
  84. // Make sure the defaults are set according to the POA spec
  85. this();
  86. if ( policies == null )
  87. return;
  88. // XXX: Check for duplicates ?
  89. for(short i = 0; i < policies.length; i++) {
  90. Policy policy = policies[i];
  91. // Save the policy in the HashMap to support POA.get_effective_policy
  92. Integer key = new Integer( policy.policy_type() ) ;
  93. this.policies.put( key, policy ) ;
  94. // Set up the local copy of standard POA policy information
  95. if (policy instanceof ThreadPolicy) {
  96. threadModel = ((ThreadPolicy) policy).value().value();
  97. } else if (policy instanceof LifespanPolicy) {
  98. lifespan = ((LifespanPolicy) policy).value().value();
  99. } else if (policy instanceof IdUniquenessPolicy) {
  100. idUniqueness = ((IdUniquenessPolicy) policy).value().value();
  101. } else if (policy instanceof IdAssignmentPolicy) {
  102. idAssignment = ((IdAssignmentPolicy) policy).value().value();
  103. } else if (policy instanceof ServantRetentionPolicy) {
  104. retention = ((ServantRetentionPolicy) policy).value().value();
  105. } else if (policy instanceof RequestProcessingPolicy) {
  106. requestProcessing = ((RequestProcessingPolicy) policy).value().value();
  107. } else if (policy instanceof ImplicitActivationPolicy) {
  108. implicitActivation = ((ImplicitActivationPolicy) policy).value().value();
  109. } else if (policy.policy_type() == ORBConstants.SERVANT_CACHING_POLICY)
  110. cachingServantAllowed = true ;
  111. }
  112. short firstIndexOfBadCombination =
  113. POAPolicyCombinationValidator.checkForInvalidPolicyCombinations(policies);
  114. if ( !( firstIndexOfBadCombination <= -1 ) )
  115. invalidPolicy(firstIndexOfBadCombination);
  116. }
  117. public Policy get_effective_policy( int type )
  118. {
  119. Integer key = new Integer( type ) ;
  120. Policy result = (Policy)(policies.get(key)) ;
  121. return result ;
  122. }
  123. public final int getBitMap() {
  124. return (threadModel << 7 | lifespan << 6 | idUniqueness << 5
  125. | idAssignment << 4 | implicitActivation << 3
  126. | retention << 2 | requestProcessing);
  127. }
  128. public final boolean servantCachingAllowed()
  129. {
  130. return cachingServantAllowed ;
  131. }
  132. /* Thread Policies */
  133. public final boolean isOrbControlledThreads() {
  134. return threadModel == ThreadPolicyValue._ORB_CTRL_MODEL;
  135. }
  136. public final boolean isSingleThreaded() {
  137. return threadModel == ThreadPolicyValue._SINGLE_THREAD_MODEL;
  138. }
  139. /* Lifespan */
  140. public final boolean isTransient() {
  141. return lifespan == LifespanPolicyValue._TRANSIENT;
  142. }
  143. public final boolean isPersistent() {
  144. return lifespan == LifespanPolicyValue._PERSISTENT;
  145. }
  146. /* ID Uniqueness */
  147. public final boolean isUniqueIds() {
  148. return idUniqueness == IdUniquenessPolicyValue._UNIQUE_ID;
  149. }
  150. public final boolean isMultipleIds() {
  151. return idUniqueness == IdUniquenessPolicyValue._MULTIPLE_ID;
  152. }
  153. /* ID Assignment */
  154. public final boolean isUserAssignedIds() {
  155. return idAssignment == IdAssignmentPolicyValue._USER_ID;
  156. }
  157. public final boolean isSystemAssignedIds() {
  158. return idAssignment == IdAssignmentPolicyValue._SYSTEM_ID;
  159. }
  160. /* Servant Rentention */
  161. public final boolean retainServants() {
  162. return retention == ServantRetentionPolicyValue._RETAIN;
  163. }
  164. /* Request Processing */
  165. public final boolean useActiveMapOnly() {
  166. return
  167. requestProcessing == RequestProcessingPolicyValue._USE_ACTIVE_OBJECT_MAP_ONLY;
  168. }
  169. public final boolean useDefaultServant() {
  170. return
  171. requestProcessing == RequestProcessingPolicyValue._USE_DEFAULT_SERVANT;
  172. }
  173. public final boolean useServantManager() {
  174. return
  175. requestProcessing == RequestProcessingPolicyValue._USE_SERVANT_MANAGER;
  176. }
  177. /* Implicit Activation */
  178. public final boolean isImplicitlyActivated() {
  179. return implicitActivation == ImplicitActivationPolicyValue._IMPLICIT_ACTIVATION;
  180. }
  181. private void invalidPolicy(short index) throws InvalidPolicy {
  182. throw new InvalidPolicy(index);
  183. }
  184. }