Source code: com/sun/xacml/cond/ConditionBagFunction.java
1
2 /*
3 * @(#)ConditionBagFunction.java
4 *
5 * Copyright 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.BagAttribute;
43 import com.sun.xacml.attr.BooleanAttribute;
44
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.HashSet;
48 import java.util.List;
49 import java.util.Set;
50
51
52 /**
53 * Specific <code>BagFunction</code> class that supports the single
54 * condition bag function: type-is-in.
55 *
56 * @since 1.2
57 * @author Seth Proctor
58 */
59 public class ConditionBagFunction extends BagFunction
60 {
61
62 // mapping of function name to its associated argument type
63 private static HashMap argMap;
64
65 /**
66 * Static initializer that sets up the argument info for all the
67 * supported functions.
68 */
69 static {
70 argMap = new HashMap();
71
72 for (int i = 0; i < baseTypes.length; i++) {
73 String [] args = { baseTypes[i], baseTypes[i] };
74
75 argMap.put(FUNCTION_NS + simpleTypes[i] + NAME_BASE_IS_IN, args);
76 }
77 }
78
79 /**
80 * Constructor that is used to create one of the condition standard bag
81 * functions. The name supplied must be one of the standard XACML
82 * functions supported by this class, including the full namespace,
83 * otherwise an exception is thrown. Look in <code>BagFunction</code>
84 * for details about the supported names.
85 *
86 * @param functionName the name of the function to create
87 *
88 * @throws IllegalArgumentException if the function is unknown
89 */
90 public ConditionBagFunction(String functionName) {
91 super(functionName, 0, getArguments(functionName));
92 }
93
94 /**
95 * Constructor that is used to create instances of condition bag
96 * functions for new (non-standard) datatypes. This is equivalent to
97 * using the <code>getInstance</code> methods in <code>BagFunction</code>
98 * and is generally only used by the run-time configuration code.
99 *
100 * @param functionName the name of the new function
101 * @param datatype the full identifier for the supported datatype
102 */
103 public ConditionBagFunction(String functionName, String datatype) {
104 super(functionName, 0, new String [] {datatype, datatype});
105 }
106
107 /**
108 * Private helper that returns the argument types for the given standard
109 * function.
110 */
111 private static String [] getArguments(String functionName) {
112 String [] args = (String [])(argMap.get(functionName));
113
114 if (args == null)
115 throw new IllegalArgumentException("unknown bag function: " +
116 functionName);
117
118 return args;
119 }
120
121 /**
122 * Returns a <code>Set</code> containing all the function identifiers
123 * supported by this class.
124 *
125 * @return a <code>Set</code> of <code>String</code>s
126 */
127 public static Set getSupportedIdentifiers() {
128 return Collections.unmodifiableSet(argMap.keySet());
129 }
130
131 /**
132 * Evaluate the function, using the specified parameters.
133 *
134 * @param inputs a <code>List</code> of <code>Evaluatable</code>
135 * objects representing the arguments passed to the function
136 * @param context an <code>EvaluationCtx</code> so that the
137 * <code>Evaluatable</code> objects can be evaluated
138 * @return an <code>EvaluationResult</code> representing the
139 * function's result
140 */
141 public EvaluationResult evaluate(List inputs, EvaluationCtx context) {
142
143 // Evaluate the arguments
144 AttributeValue [] argValues = new AttributeValue[inputs.size()];
145 EvaluationResult result = evalArgs(inputs, context, argValues);
146 if (result != null)
147 return result;
148
149 // *-is-in takes a bag and an element of baseType and
150 // returns a single boolean value
151 AttributeValue item = (AttributeValue)(argValues[0]);
152 BagAttribute bag = (BagAttribute)(argValues[1]);
153
154 return new EvaluationResult(BooleanAttribute.
155 getInstance(bag.contains(item)));
156 }
157
158 }