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

Quick Search    Search Deep

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


1   
2   /*
3    * @(#)LogicalFunction.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  
44  import java.util.HashSet;
45  import java.util.Iterator;
46  import java.util.List;
47  import java.util.Set;
48  
49  
50  /**
51   * A class that implements the logical functions "or" and "and".
52   * These functions take any number of boolean arguments and evaluate
53   * them one at a time, starting with the first argument. As soon as
54   * the result of the function can be determined, evaluation stops and
55   * that result is returned. During this process, if any argument
56   * evaluates to indeterminate, an indeterminate result is returned.
57   *
58   * @since 1.0
59   * @author Steve Hanna
60   * @author Seth Proctor
61   */
62  public class LogicalFunction extends FunctionBase
63  {
64  
65      /**
66       * Standard identifier for the or function.
67       */
68      public static final String NAME_OR = FUNCTION_NS + "or";
69  
70      /**
71       * Standard identifier for the and function.
72       */
73      public static final String NAME_AND = FUNCTION_NS + "and";
74  
75      // internal identifiers for each of the supported functions
76      private static final int ID_OR = 0;
77      private static final int ID_AND = 1;
78  
79      /**
80       * Creates a new <code>LogicalFunction</code> object.
81       *
82       * @param functionName the standard XACML name of the function to be
83       *                     handled by this object, including the full namespace
84       *
85       * @throws IllegalArgumentException if the functionName is unknown
86       */
87      public LogicalFunction(String functionName) {
88          super(functionName, getId(functionName), BooleanAttribute.identifier,
89                false, -1, BooleanAttribute.identifier, false);
90      }
91  
92      /**
93       * Private helper that looks up the private id based on the function name.
94       */
95      private static int getId(String functionName) {
96          if (functionName.equals(NAME_OR))
97              return ID_OR;
98          else if (functionName.equals(NAME_AND))
99              return ID_AND;
100         else
101             throw new IllegalArgumentException("unknown logical function: " +
102                                                functionName);
103     }
104 
105     /**
106      * Returns a <code>Set</code> containing all the function identifiers
107      * supported by this class.
108      *
109      * @return a <code>Set</code> of <code>String</code>s
110      */
111     public static Set getSupportedIdentifiers() {
112         Set set = new HashSet();
113 
114         set.add(NAME_OR);
115         set.add(NAME_AND);
116 
117         return set;
118     }
119 
120     /**
121      * Evaluate the function, using the specified parameters.
122      *
123      * @param inputs a <code>List</code> of <code>Evaluatable</code>
124      *               objects representing the arguments passed to the function
125      * @param context an <code>EvaluationCtx</code> so that the
126      *                <code>Evaluatable</code> objects can be evaluated
127      * @return an <code>EvaluationResult</code> representing the
128      *         function's result
129      */
130     public EvaluationResult evaluate(List inputs, EvaluationCtx context) {
131 
132         // Evaluate the arguments one by one. As soon as we can
133         // return a result, do so. Return Indeterminate if any argument
134         // evaluated is indeterminate.
135         Iterator it = inputs.iterator();
136         while (it.hasNext()) {
137             Evaluatable eval = (Evaluatable)(it.next());
138 
139             // Evaluate the argument
140             EvaluationResult result = eval.evaluate(context);
141             if (result.indeterminate())
142                 return result;
143 
144             AttributeValue value = result.getAttributeValue();
145             boolean argBooleanValue = ((BooleanAttribute)value).getValue();
146 
147             switch (getFunctionId()) {
148             case ID_OR:
149                 if (argBooleanValue)
150                     return EvaluationResult.getTrueInstance();
151                 break;
152             case ID_AND:
153                 if (!argBooleanValue)
154                     return EvaluationResult.getFalseInstance();
155                 break;
156             }
157         }
158 
159         if (getFunctionId() == ID_OR)
160             return EvaluationResult.getFalseInstance();
161         else
162             return EvaluationResult.getTrueInstance();
163     }
164 }