📄 environmentnode.java
字号:
/*
* Copyright (c) 2000-2005, University of Salford
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the University of Salford nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package issrg.pba.rbac.xmlpolicy.ifstatement;
/**
* This is the class that represents an Environment node and evaluates the
* environmental variables
* at run-time.
*
* <p>EnvironmentNode expects the environment to contain either
* issrg.pba.rbac.Argument objects, in which case the string value will be
* converted using Types.construct, or other objects, which will be passed
* as is to the interpreters.
*
* <p>If the environment contains the variables of types for which there is no
* constructor provided
* by the AEF (not registered with the Types), or the AEF failed to provide the
* variable with the name
* stated in the policy, an EvaluationException will occur at run-time.
*
* <p>Note that if the evaluation does not reach the point where the variable
* is needed, no
* exception will be thrown.
*
* @see Types#construct
*
* @author A.Otenko
*/
public class EnvironmentNode extends TermNode {
/**
* This is the name of the node that represents the Environment - a reference to an environmental
* parameter.
*/
public final static String ENVIRONMENT_NODE = "Environment";
/**
* This is the name of the attribute telling the name of the environmental parameter.
*/
public final static String PARAMETER_ATTRIBUTE = "Parameter";
/**
* This method should be called to register the node with the XML Parser
*/
public static void register(){
try{
issrg.pba.rbac.xmlpolicy.XMLPolicyParser.registerXMLNode(ENVIRONMENT_NODE, EnvironmentNode.class);
}catch (NoSuchMethodException nsme){
nsme.printStackTrace();
}
}
protected String type;
protected String parameter_name;
protected EnvironmentNode() {}
/**
* This constructor builds an EnvironmentNode given the XMLPolicyParser and
* the set of attributes of the XML element. It expects that a
* issrg.pba.rbac.xmlpolicy.XMLTags.TYPE_ATTRIBUTE and
* PARAMETER_ATTRIBUTE ("Type" and "Parameter") are
* present in this XML element. The value is converted from string into a
* Java object using the Types.construct() method at runtime, so the type of
* the value
* must be registered there.
*
* @param p - the XMLPolicyParser that builds this EnvironmentNode
* @param attr - the attributes of this XML element
* @see Types#construct
*/
public EnvironmentNode(issrg.pba.rbac.xmlpolicy.XMLPolicyParser p, org.xml.sax.Attributes attr) throws issrg.pba.rbac.PolicyParsingException{
super(ENVIRONMENT_NODE, attr, -1, 0);
type=((String)getAttributes().get(issrg.pba.rbac.xmlpolicy.XMLTags.TYPE_ATTRIBUTE)).intern();
if (type==null) throw new issrg.pba.rbac.PolicyParsingException(issrg.pba.rbac.xmlpolicy.XMLTags.TYPE_ATTRIBUTE+" attribute was expected in "+ENVIRONMENT_NODE);
parameter_name=(String)getAttributes().get(PARAMETER_ATTRIBUTE);
if (parameter_name==null) throw new issrg.pba.rbac.PolicyParsingException(PARAMETER_ATTRIBUTE+" attribute was expected in "+ENVIRONMENT_NODE);
}
/**
* This method returns the type of the environmental parameter as determined
* at construction type.
*/
public String getType(){
return type;
}
/**
* This method evaluates the environmental parameter, as it is at run-time.
* It looks for the environmental parameter in the Environment by its name,
* as determined at construction time. If the value is of type other than
* issrg.pba.rbac.Argument, then it is the result of this method; otherwise
* the String value of the issrg.pba.rbac.Argument is extracted and decoded
* into a Java object using Types.construct method.
*
* @param env - the Environment with the variables in it indexed by their
* name
*
* @return Object of type guaranteed by getType() method
*
* @throws EvaluationException, if there is no variable in the Environment
* with the expected name, or if its type is not the expected type, or if
* Types.construct failed to decode the value from String (no decoder
* registered or other decoding problem)
*
* @see Types#construct
*/
public Object evaluate(Environment env) throws EvaluationException{
try{
Object o = env.getEnv().get(parameter_name);
if (o instanceof issrg.pba.rbac.Argument){
issrg.pba.rbac.Argument arg = (issrg.pba.rbac.Argument)o;
if (arg==null || arg.getType().intern()!=type){
throw new EvaluationException("No such variable in the environment: "+parameter_name+" of type "+type);
}
return Types.construct(type, arg.getValue());
}else{
return o;
}
}catch (Throwable th){
throw new EvaluationException("Evaluation run-time error", th);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -