Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sun/xacml/cond/NOfFunction.java


1   
2   /*
3    * @(#)NOfFunction.java
4    *
5    * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions are met:
9    *
10   *   1. Redistribution of source code must retain the above copyright notice,
11   *      this list of conditions and the following disclaimer.
12   * 
13   *   2. Redistribution in binary form must reproduce the above copyright
14   *      notice, this list of conditions and the following disclaimer in the
15   *      documentation and/or other materials provided with the distribution.
16   *
17   * Neither the name of Sun Microsystems, Inc. or the names of contributors may
18   * be used to endorse or promote products derived from this software without
19   * specific prior written permission.
20   * 
21   * This software is provided "AS IS," without a warranty of any kind. ALL
22   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
24   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
25   * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
26   * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
27   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
28   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
29   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
30   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
31   * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32   *
33   * You acknowledge that this software is not designed or intended for use in
34   * the design, construction, operation or maintenance of any nuclear facility.
35   */
36  
37  package com.sun.xacml.cond;
38  
39  import com.sun.xacml.EvaluationCtx;
40  
41  import com.sun.xacml.attr.AttributeValue;
42  import com.sun.xacml.attr.BooleanAttribute;
43  import com.sun.xacml.attr.IntegerAttribute;
44  
45  import java.util.HashSet;
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Set;
49  
50  
51  /**
52   * A class that implements the n-of function. It requires
53   * at least one argument. The first argument must be an integer
54   * and the rest of the arguments must be booleans. If the number of
55   * boolean arguments that evaluate to true is at least the value of the
56   * first argument, the function returns true. Otherwise, it returns false
57   * (or indeterminate, as described in the next paragraph.
58   * <p>
59   * This function evaluates the arguments one at a time, starting with
60   * the first one. As soon as the result of the function can be determined,
61   * evaluation stops and that result is returned. During this process, if
62   * any argument evaluates to indeterminate, an indeterminate result is
63   * returned.
64   *
65   * @since 1.0
66   * @author Steve Hanne
67   * @author Seth Proctor
68   */
69  public class NOfFunction extends FunctionBase
70  {
71  
72      /**
73       * Standard identifier for the n-of function.
74       */
75      public static final String NAME_N_OF = FUNCTION_NS + "n-of";
76  
77      /**
78       * Creates a new <code>NOfFunction</code> object.
79       *
80       * @param functionName the standard XACML name of the function to be
81       *                     handled by this object, including the full namespace
82       *
83       * @throws IllegalArgumentException if the function is unknown
84       */
85      public NOfFunction(String functionName) {
86          super(NAME_N_OF, 0, BooleanAttribute.identifier, false);
87  
88          if (! functionName.equals(NAME_N_OF))
89              throw new IllegalArgumentException("unknown nOf function: "
90                                                 + functionName);
91      }
92  
93      /**
94       * Returns a <code>Set</code> containing all the function identifiers
95       * supported by this class.
96       *
97       * @return a <code>Set</code> of <code>String</code>s
98       */
99      public static Set getSupportedIdentifiers() {
100         Set set = new HashSet();
101 
102         set.add(NAME_N_OF);
103 
104         return set;
105     }
106 
107     /**
108      * Evaluate the function, using the specified parameters.
109      *
110      * @param inputs a <code>List</code> of <code>Evaluatable</code>
111      *               objects representing the arguments passed to the function
112      * @param context an <code>EvaluationCtx</code> so that the
113      *                <code>Evaluatable</code> objects can be evaluated
114      * @return an <code>EvaluationResult</code> representing the
115      *         function's result
116      */
117     public EvaluationResult evaluate(List inputs, EvaluationCtx context) {
118 
119         // Evaluate the arguments one by one. As soon as we can return
120         // a result, do so. Return  Indeterminate if any argument
121         // evaluated is indeterminate.
122         Iterator it = inputs.iterator();
123         Evaluatable eval = (Evaluatable)(it.next());
124 
125         // Evaluate the first argument
126         EvaluationResult result = eval.evaluate(context);
127         if (result.indeterminate())
128             return result;
129 
130         // if there were no problems, we know 'n'
131         long n = ((IntegerAttribute)(result.getAttributeValue())).getValue();
132 
133         // If the number of trues needed is less than zero, report an error.
134         if (n < 0)
135             return makeProcessingError("First argument to " + getFunctionName()
136                                        + " cannot be negative.");
137 
138         // If the number of trues needed is zero, return true.
139         if (n == 0)
140             return EvaluationResult.getTrueInstance();
141 
142         // make sure it's possible to find n true values
143         long remainingArgs = inputs.size() - 1;
144         if (n > remainingArgs)
145             return makeProcessingError("not enough arguments to n-of to " +
146                                        "find " + n + " true values");
147 
148         // loop through the inputs, trying to find at least n trues
149         while (remainingArgs >= n) {
150             eval = (Evaluatable)(it.next());
151             
152             // evaluate the next argument
153             result = eval.evaluate(context);
154             if (result.indeterminate())
155                 return result;
156             
157             // get the next value, and see if it's true
158             if (((BooleanAttribute)(result.getAttributeValue())).getValue()) {
159                 // we're one closer to our goal...see if we met it
160                 if (--n == 0)
161                     return EvaluationResult.getTrueInstance();
162             }
163 
164             // we're still looking, but we've got one fewer arguments
165             remainingArgs--;
166         }
167 
168         // if we got here then we didn't meet our quota
169         return EvaluationResult.getFalseInstance();
170     }
171 
172     /**
173      *
174      */
175     public void checkInputs(List inputs) throws IllegalArgumentException {
176         // check that none of the inputs is a bag
177         Object [] list = inputs.toArray();
178         for (int i = 0; i < list.length; i++)
179             if (((Evaluatable)(list[i])).evaluatesToBag())
180                 throw new IllegalArgumentException("n-of can't use bags");
181 
182         // if we got here then there were no bags, so ask the other check
183         // method to finish the checking
184         checkInputsNoBag(inputs);
185     }
186 
187     /**
188      *
189      */
190     public void checkInputsNoBag(List inputs) throws IllegalArgumentException {
191         Object [] list = inputs.toArray();
192         
193         // check that there is at least one arg
194         if (list.length == 0)
195             throw new IllegalArgumentException("n-of requires an argument");
196 
197         // check that the first element is an Integer
198         Evaluatable eval = (Evaluatable)(list[0]);
199         if (! eval.getType().toString().equals(IntegerAttribute.identifier))
200             throw new IllegalArgumentException("first argument to n-of must" +
201                                                " be an integer");
202         
203         // now check that the rest of the args are booleans
204         for (int i = 1; i < list.length; i++) {
205             if (! ((Evaluatable)(list[i])).getType().toString().
206                 equals(BooleanAttribute.identifier))
207                 throw new IllegalArgumentException("invalid parameter in n-of"
208                                                    + ": expected boolean");
209         }
210     }
211 
212 }