View Javadoc

1   /*
2    Copyright 2006 Ernest Micklei @ PhilemonWorks.com
3   
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7   
8    http://www.apache.org/licenses/LICENSE-2.0
9   
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15   
16   */
17  package com.philemonworks.selfdiagnose;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import ognl.Ognl;
23  import ognl.OgnlException;
24  
25  public class ExecutionContext {
26  	private Map values = new HashMap();
27  
28  	/**
29  	 * Store the value for a variable unless no variable was specified (== null)
30  	 * @param variableName : String || null
31  	 * @param newValue : Object
32  	 * @throws DiagnoseException
33  	 */
34  	public void setValue(String variableName, Object newValue) throws DiagnoseException {
35  		// only store the value if a variable name was specified
36  		if (variableName != null)
37  			values.put(variableName, newValue);
38  	}
39  	/**
40  	 * Return the stored value for a variable. Null is allowed for the value.
41  	 * Throws an exception if this value is not available
42  	 * @param expression : String
43  	 * @return Object
44  	 * @throws DiagnoseException
45  	 */
46  	public Object getValue(String expression) throws DiagnoseException {
47          try {
48              return Ognl.getValue(expression, values);
49          } catch (OgnlException e) {
50              throw new DiagnoseException("Unable to evaluate expression ["+expression+"]",e);
51          }
52  	}
53  	/**
54  	 * The argument is either a value or an expression.
55  	 * Expressions are specified using the syntax ${myexpression}
56  	 * Return the stored value for a variable.
57  	 * @param valueOrExpression
58  	 * @return Object
59  	 * @throws DiagnoseException
60  	 */
61  	public Object resolveValue(String valueOrExpression) throws DiagnoseException {
62  		if (valueOrExpression == null) return null; // should not happen?
63  		if (valueOrExpression.startsWith("${")) {
64  			String var = valueOrExpression.substring(2,valueOrExpression.length()-1);
65  			return this.getValue(var);
66  		}
67  		return valueOrExpression;
68  	}
69  	/**
70  	 * Checks and makes sure the return value is a String
71  	 * @param valueOrExpression : String
72  	 * @return String
73  	 * @throws DiagnoseException
74  	 */
75  	public String resolveString(String valueOrExpression) throws DiagnoseException {
76  		Object value = this.resolveValue(valueOrExpression);
77  		if (!(value instanceof String))
78  			throw new DiagnoseException("String value expected for ["+valueOrExpression+"] but value is ["+value+"]");
79  		return (String)value;
80  	}	
81  }