1. /*
  2. * @(#)POAPolicyCombinationValidator.java 1.6 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 org.omg.CORBA.*;
  9. import org.omg.CORBA.portable.*;
  10. import org.omg.PortableServer.*;
  11. import org.omg.PortableServer.POAPackage.*;
  12. import org.omg.PortableServer.ServantLocatorPackage.*;
  13. import com.sun.corba.se.internal.core.*;
  14. import com.sun.corba.se.internal.corba.*;
  15. import com.sun.corba.se.internal.util.*;
  16. import java.util.*;
  17. // NOTE: Current implementation does not check for duplicates and
  18. // repeated policy objects of the same type.
  19. public final class POAPolicyCombinationValidator{
  20. // policy processing.
  21. //Always return _index <= -1 if there are no invalid policies in
  22. //the scope of constraint resolution.
  23. public static short checkForInvalidPolicyCombinations(Policy[] policies) {
  24. short returnIndex = -1;
  25. short currentIndex = -1;
  26. //Validity for this orb implementation. See the POA spec on policies.
  27. returnIndex = checkValidityForThisORBImplementation(policies);
  28. //Consistency of the combination of policies. See the POA spec on policies.
  29. currentIndex = checkConsistency(policies);
  30. returnIndex = findNonNegativeMinimumIfItExists(currentIndex,returnIndex);
  31. //Missing administrative actions. See the POA spec on policies.
  32. currentIndex = checkMissingAdministrativeActions(policies);
  33. returnIndex = findNonNegativeMinimumIfItExists(currentIndex,returnIndex);
  34. //Return the minimal non-negative index of the offending policy or -1;
  35. //-1 indicates no policy violations detected by current algorithm.
  36. return returnIndex;
  37. }
  38. private static short checkValidityForThisORBImplementation(Policy[] policies){
  39. return -1;
  40. }
  41. private static short checkConsistency(Policy[] policies){
  42. short returnIndex = -1;
  43. short currentIndex = -1;
  44. //uaomo_req_r == USE_ACTIVE_OBJECT_MAP_ONLY requires RETAIN
  45. returnIndex = uaomo_req_r(policies);
  46. //nR_req_uds_o_usm == NON_RETAIN requires USE_DEFAULT_SERVANT OR USE_SERVANT_MANAGER
  47. currentIndex = nR_req_uds_o_usm(policies);
  48. returnIndex = findNonNegativeMinimumIfItExists(currentIndex,returnIndex);
  49. //ia_req_si_a_r == IMPLICIT_ACTIVATION requires SYSTEM_ID and RETAIN
  50. currentIndex = ia_req_si_a_r(policies);
  51. returnIndex = findNonNegativeMinimumIfItExists(currentIndex,returnIndex);
  52. //life_span_excluded_middle == ! (PERSISTENT && TRANSIENT)
  53. currentIndex = life_span_excluded_middle(policies);
  54. returnIndex = findNonNegativeMinimumIfItExists(currentIndex,returnIndex);
  55. //servant_retention_excluded_middle == ! (RETAIN && NON_RETAIN)
  56. currentIndex = servant_retention_excluded_middle(policies);
  57. returnIndex = findNonNegativeMinimumIfItExists(currentIndex,returnIndex);
  58. //id_uniqueness_excluded_middle == ! (UNIQUE_ID && MULTIPLE_ID)
  59. currentIndex = id_uniqueness_excluded_middle(policies);
  60. returnIndex = findNonNegativeMinimumIfItExists(currentIndex,returnIndex);
  61. // can add other consistency checks with the same procedure for index management.
  62. return returnIndex;
  63. }
  64. // Returns -1 if non-negative minimum does not exist.
  65. private static short findNonNegativeMinimumIfItExists(short currentIndex, short returnIndex){
  66. //If any of them is -1, set it to the other.
  67. if ( returnIndex == -1 ) returnIndex = currentIndex;
  68. if ( currentIndex == -1 ) currentIndex = returnIndex ;
  69. //By now, if any of them is not -1 and the other is, the other is set to it.
  70. //If both are -1, -1 will be returned in the last statement.
  71. if ( currentIndex != -1 || returnIndex != -1 ) returnIndex = (short)Math.min(currentIndex,returnIndex);
  72. return returnIndex;
  73. }
  74. private static short checkMissingAdministrativeActions(Policy[] policies){
  75. return -1;
  76. }
  77. //uaomo_req_r == USE_ACTIVE_OBJECT_MAP_ONLY requires RETAIN
  78. private static short uaomo_req_r(Policy[] policies){
  79. RequestProcessingPolicy rpp = null; short rpp_index = -1;
  80. ServantRetentionPolicy srp = null; short srp_index = -1;
  81. for(short i=0; i<policies.length; i++){
  82. if ( policies[i] instanceof RequestProcessingPolicy ){
  83. rpp=(RequestProcessingPolicy)policies[i];
  84. rpp_index=i;
  85. }
  86. if ( policies[i] instanceof ServantRetentionPolicy ) {
  87. srp=(ServantRetentionPolicy)policies[i];
  88. srp_index=i;
  89. }
  90. }
  91. if (rpp!=null && rpp.value()==
  92. org.omg.PortableServer.RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY){
  93. if (srp_index==-1) /* Default is Retain so we're OK*/
  94. return -1;
  95. else {
  96. if(srp!=null && srp.value()
  97. ==org.omg.PortableServer.ServantRetentionPolicyValue.NON_RETAIN)
  98. return (short)Math.min(rpp_index,srp_index);
  99. else return -1;
  100. }
  101. } else return -1;
  102. }
  103. //nR_req_uds_o_usm == NON_RETAIN requires USE_DEFAULT_SERVANT OR USE_SERVANT_MANAGER
  104. private static short nR_req_uds_o_usm(Policy[] policies){
  105. return -1;
  106. }
  107. //ia_req_si_a_r == IMPLICIT_ACTIVATION requires SYSTEM_ID and RETAIN
  108. //When combination is violated, returned index is the minimum index
  109. //of *the* violating combination.
  110. private static short ia_req_si_a_r(Policy[] policies){
  111. short returnIndex = -1;
  112. ImplicitActivationPolicy iap = null; short iap_index = -1;
  113. IdAssignmentPolicy idp = null; short idp_index = -1;
  114. ServantRetentionPolicy srp = null; short srp_index = -1;
  115. //Collect policies and indeces.
  116. for(short i=0; i<policies.length; i++){
  117. if ( policies[i] instanceof ImplicitActivationPolicy ){
  118. iap = (ImplicitActivationPolicy)policies[i];
  119. iap_index = i;
  120. }
  121. if ( policies[i] instanceof IdAssignmentPolicy ) {
  122. idp = (IdAssignmentPolicy)policies[i];
  123. idp_index = i;
  124. }
  125. if ( policies[i] instanceof ServantRetentionPolicy ) {
  126. srp = (ServantRetentionPolicy)policies[i];
  127. srp_index = i;
  128. }
  129. }
  130. if (iap == null) return -1; //The rule is for IMPLICIT_ACTIVATION which is not the default.
  131. else { //ImplicitActivationPolicy is set.
  132. if (iap.value()!=org.omg.PortableServer.ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION) {
  133. return -1; // Default is NO_IMPLICIT_ACTIVATION, so we're OK.
  134. }
  135. else { // IMPLICIT_ACTIVATION has been set.
  136. if( srp == null ) { //Default: RETAIN
  137. srp_index = -1;
  138. if ( idp == null ) return -1; // Defaults are RETAIN and SYSTEM_ID. So we're OK.
  139. else { //IdAssignmentPolicy is set by user.
  140. //if IdAssignmentPolicy violates the rule, return minimal index of the set.
  141. if ( idp.value()!=org.omg.PortableServer.IdAssignmentPolicyValue.SYSTEM_ID)
  142. return findNonNegativeMinimumIfItExists(idp_index,iap_index);
  143. else //IdAssignmentPolicy does not violate the rule;
  144. return -1;
  145. }
  146. }
  147. else { // ServantRetentionPolicy is set by user.
  148. //If ServantRetentionPolicy violates the rule, return minimal index of the set.
  149. if(srp.value()!=org.omg.PortableServer.ServantRetentionPolicyValue.RETAIN){
  150. if ( idp == null) //Default: SYSTEM_ID
  151. return findNonNegativeMinimumIfItExists(srp_index , iap_index);
  152. else { //IdAssignmentPolicy is set by the user.
  153. if (idp.value()!=org.omg.PortableServer.IdAssignmentPolicyValue.SYSTEM_ID)
  154. //All policies are set wrong !!!
  155. return findNonNegativeMinimumIfItExists
  156. (srp_index , findNonNegativeMinimumIfItExists(iap_index,idp_index));
  157. else //IdAssignmentPolicy does not violate the rule.
  158. return findNonNegativeMinimumIfItExists(srp_index , iap_index);
  159. }
  160. }
  161. else{ //The user's setting of ServantRetentionPolicy does not violate the rule
  162. if ( idp == null) //Default: SYSTEM_ID
  163. return -1 ;
  164. else { //IdAssignmentPolicy is set by the user.
  165. //If IdAssignmentPolicy violates the rule return the appropriate index.
  166. if (idp.value()!=org.omg.PortableServer.IdAssignmentPolicyValue.SYSTEM_ID)
  167. return findNonNegativeMinimumIfItExists(iap_index,idp_index);
  168. else //IdAssignmentPolicy does not violate the rule.
  169. return -1;
  170. }
  171. }
  172. }
  173. }
  174. //Some index will be returned by one of the clauses above.
  175. } //ImplicitAssignmentPolicy set to IMPLICIT_ACTIVATION
  176. }
  177. //life_span_excluded_middle == ! (PERSISTENT && TRANSIENT)
  178. private static short life_span_excluded_middle(Policy[] policies){
  179. LifespanPolicy lp1 = null; short lp1_index = -1;
  180. LifespanPolicy lp2 = null; /* short lp2_index = -1; */
  181. for(short i=0; i<policies.length; i++){
  182. if ( policies[i] instanceof LifespanPolicy ){
  183. if(lp1 == null){
  184. lp1=(LifespanPolicy)policies[i];
  185. lp1_index=i;
  186. } else {
  187. lp2=(LifespanPolicy)policies[i];
  188. /* lp2_index=i; */
  189. // LifespanPolicy can be either but not both
  190. // of the two possible values
  191. if ( lp2.value() != lp1.value() ) return lp1_index;
  192. else return -1;
  193. }
  194. }
  195. }
  196. return -1;
  197. }
  198. //servant_retention_excluded_middle == ! (RETAIN && NON_RETAIN)
  199. private static short servant_retention_excluded_middle(Policy[] policies){
  200. ServantRetentionPolicy rp1 = null; short rp1_index = -1;
  201. ServantRetentionPolicy rp2 = null; /* short rp2_index = -1; */
  202. for(short i=0; i<policies.length; i++){
  203. if ( policies[i] instanceof ServantRetentionPolicy ){
  204. if(rp1 == null){
  205. rp1=(ServantRetentionPolicy)policies[i];
  206. rp1_index=i;
  207. } else {
  208. rp2=(ServantRetentionPolicy)policies[i];
  209. /* rp2_index=i; */
  210. // ServantRetentionPolicy can be either but not both
  211. // of the two possible values
  212. if ( rp2.value() != rp1.value() ) return rp1_index;
  213. else return -1;
  214. }
  215. }
  216. }
  217. return -1;
  218. }
  219. //id_uniqueness_excluded_middle == ! (UNIQUE_ID && MULTIPLE_ID)
  220. private static short id_uniqueness_excluded_middle(Policy[] policies){
  221. IdUniquenessPolicy id1 = null; short id1_index = -1;
  222. IdUniquenessPolicy id2 = null; /* short id2_index = -1; */
  223. for(short i=0; i<policies.length; i++){
  224. if ( policies[i] instanceof IdUniquenessPolicy ){
  225. if(id1 == null){
  226. id1=(IdUniquenessPolicy)policies[i];
  227. id1_index=i;
  228. } else {
  229. id2=(IdUniquenessPolicy)policies[i];
  230. /* id2_index=i; */
  231. // IdUniquenessPolicy can be either but not both
  232. // of the two possible values
  233. if ( id2.value() != id1.value() ) return id1_index;
  234. else return -1;
  235. }
  236. }
  237. }
  238. return -1;
  239. }
  240. }