EnvironmentNode | Line # 59 | 20 | 8 | 57.6% |
0.57575756
|
No Tests | |||
1 | /* | |
2 | * Copyright (c) 2000-2005, University of Salford | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * Redistributions of source code must retain the above copyright notice, this | |
9 | * list of conditions and the following disclaimer. | |
10 | * | |
11 | * Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
14 | * | |
15 | * Neither the name of the University of Salford nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from this | |
17 | * software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | package issrg.pba.rbac.xmlpolicy.ifstatement; | |
33 | ||
34 | /** | |
35 | * This is the class that represents an Environment node and evaluates the | |
36 | * environmental variables | |
37 | * at run-time. | |
38 | * | |
39 | * <p>EnvironmentNode expects the environment to contain either | |
40 | * issrg.pba.rbac.Argument objects, in which case the string value will be | |
41 | * converted using Types.construct, or other objects, which will be passed | |
42 | * as is to the interpreters. | |
43 | * | |
44 | * <p>If the environment contains the variables of types for which there is no | |
45 | * constructor provided | |
46 | * by the AEF (not registered with the Types), or the AEF failed to provide the | |
47 | * variable with the name | |
48 | * stated in the policy, an EvaluationException will occur at run-time. | |
49 | * | |
50 | * <p>Note that if the evaluation does not reach the point where the variable | |
51 | * is needed, no | |
52 | * exception will be thrown. | |
53 | * | |
54 | * @see Types#construct | |
55 | * | |
56 | * @author A.Otenko | |
57 | */ | |
58 | ||
59 | public class EnvironmentNode extends TermNode { | |
60 | /** | |
61 | * This is the name of the node that represents the Environment - a reference to an environmental | |
62 | * parameter. | |
63 | */ | |
64 | public final static String ENVIRONMENT_NODE = "Environment"; | |
65 | ||
66 | /** | |
67 | * This is the name of the attribute telling the name of the environmental parameter. | |
68 | */ | |
69 | public final static String PARAMETER_ATTRIBUTE = "Parameter"; | |
70 | ||
71 | /** | |
72 | * This method should be called to register the node with the XML Parser | |
73 | */ | |
74 | 21 | public static void register(){ |
75 | 21 | try{ |
76 | 21 | issrg.pba.rbac.xmlpolicy.XMLPolicyParser.registerXMLNode(ENVIRONMENT_NODE, EnvironmentNode.class); |
77 | }catch (NoSuchMethodException nsme){ | |
78 | 0 | nsme.printStackTrace(); |
79 | } | |
80 | } | |
81 | ||
82 | protected String type; | |
83 | protected String parameter_name; | |
84 | ||
85 | 0 | protected EnvironmentNode() {} |
86 | ||
87 | /** | |
88 | * This constructor builds an EnvironmentNode given the XMLPolicyParser and | |
89 | * the set of attributes of the XML element. It expects that a | |
90 | * issrg.pba.rbac.xmlpolicy.XMLTags.TYPE_ATTRIBUTE and | |
91 | * PARAMETER_ATTRIBUTE ("Type" and "Parameter") are | |
92 | * present in this XML element. The value is converted from string into a | |
93 | * Java object using the Types.construct() method at runtime, so the type of | |
94 | * the value | |
95 | * must be registered there. | |
96 | * | |
97 | * @param p - the XMLPolicyParser that builds this EnvironmentNode | |
98 | * @param attr - the attributes of this XML element | |
99 | * @see Types#construct | |
100 | */ | |
101 | 22 | public EnvironmentNode(issrg.pba.rbac.xmlpolicy.XMLPolicyParser p, org.xml.sax.Attributes attr) throws issrg.pba.rbac.PolicyParsingException{ |
102 | 22 | super(ENVIRONMENT_NODE, attr, -1, 0); |
103 | ||
104 | 22 | type=((String)getAttributes().get(issrg.pba.rbac.xmlpolicy.XMLTags.TYPE_ATTRIBUTE)).intern(); |
105 | 0 | if (type==null) throw new issrg.pba.rbac.PolicyParsingException(issrg.pba.rbac.xmlpolicy.XMLTags.TYPE_ATTRIBUTE+" attribute was expected in "+ENVIRONMENT_NODE); |
106 | 22 | parameter_name=(String)getAttributes().get(PARAMETER_ATTRIBUTE); |
107 | 0 | if (parameter_name==null) throw new issrg.pba.rbac.PolicyParsingException(PARAMETER_ATTRIBUTE+" attribute was expected in "+ENVIRONMENT_NODE); |
108 | } | |
109 | ||
110 | /** | |
111 | * This method returns the type of the environmental parameter as determined | |
112 | * at construction type. | |
113 | */ | |
114 | 330 | public String getType(){ |
115 | 330 | return type; |
116 | } | |
117 | ||
118 | /** | |
119 | * This method evaluates the environmental parameter, as it is at run-time. | |
120 | * It looks for the environmental parameter in the Environment by its name, | |
121 | * as determined at construction time. If the value is of type other than | |
122 | * issrg.pba.rbac.Argument, then it is the result of this method; otherwise | |
123 | * the String value of the issrg.pba.rbac.Argument is extracted and decoded | |
124 | * into a Java object using Types.construct method. | |
125 | * | |
126 | * @param env - the Environment with the variables in it indexed by their | |
127 | * name | |
128 | * | |
129 | * @return Object of type guaranteed by getType() method | |
130 | * | |
131 | * @throws EvaluationException, if there is no variable in the Environment | |
132 | * with the expected name, or if its type is not the expected type, or if | |
133 | * Types.construct failed to decode the value from String (no decoder | |
134 | * registered or other decoding problem) | |
135 | * | |
136 | * @see Types#construct | |
137 | */ | |
138 | 122 | public Object evaluate(Environment env) throws EvaluationException{ |
139 | 122 | try{ |
140 | 122 | Object o = env.getEnv().get(parameter_name); |
141 | 122 | if (o instanceof issrg.pba.rbac.Argument){ |
142 | 0 | issrg.pba.rbac.Argument arg = (issrg.pba.rbac.Argument)o; |
143 | ||
144 | 0 | if (arg==null || arg.getType().intern()!=type){ |
145 | 0 | throw new EvaluationException("No such variable in the environment: "+parameter_name+" of type "+type); |
146 | } | |
147 | ||
148 | 0 | return Types.construct(type, arg.getValue()); |
149 | }else{ | |
150 | 122 | return o; |
151 | } | |
152 | }catch (Throwable th){ | |
153 | 0 | throw new EvaluationException("Evaluation run-time error", th); |
154 | } | |
155 | } | |
156 | } |
|