KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > jexl > JexlContextMap


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 package scriptella.driver.jexl;
17
18 import org.apache.commons.jexl.JexlContext;
19 import scriptella.spi.ParametersCallback;
20
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * Mutable {@link org.apache.commons.jexl.JexlContext} implementation for
28  * integration into Scriptella execution environment.
29  * This class allows local variables to be set via {@link #put(String,Object)} method.
30  * <br>{@link #getParameter(String)} allows reading variables.
31  *
32  * @author Fyodor Kupolov
33  * @version 1.0
34  */

35 public final class JexlContextMap implements ParametersCallback, JexlContext, Map JavaDoc<String JavaDoc, Object JavaDoc> {
36     private Map JavaDoc<String JavaDoc, Object JavaDoc> localVariables = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
37     private ParametersCallback parentParameters;
38
39
40     /**
41      * Initializes instance and set parent parameters to use in {@link #getParameter(String)}.
42      *
43      * @param parentParameters parent parameters.
44      */

45     public JexlContextMap(ParametersCallback parentParameters) {
46         this.parentParameters = parentParameters;
47     }
48
49     /**
50      * Returns specified variable value.
51      * <p>The local variables set by {@link #put(String,Object)} method
52      * take priority of variables in parentParameters object.
53      *
54      * @param name variable name
55      * @return value of variable or null if variable not found.
56      */

57     public Object JavaDoc getParameter(final String JavaDoc name) {
58         Object JavaDoc v = localVariables.get(name);
59         if (v != null) {
60             return v;
61         }
62         return parentParameters.getParameter(name);
63     }
64
65     @SuppressWarnings JavaDoc("unchecked")
66     public void setVars(Map JavaDoc vars) {
67         localVariables.clear();
68         localVariables.putAll(vars);
69     }
70
71     public Map JavaDoc<String JavaDoc, Object JavaDoc> getVars() {
72         return this;
73     }
74
75     /**
76      * Use {@link #getParameter(String)}.
77      * @param key variable name.
78      * @return value of variable.
79      */

80     public Object JavaDoc get(Object JavaDoc key) {
81         return (key instanceof String JavaDoc) ? getParameter((String JavaDoc) key) : null;
82     }
83
84     /**
85      * Sets local variable.
86      * @param key variable name.
87      * @param value variable value.
88      * @return previous variable value.
89      */

90     public Object JavaDoc put(String JavaDoc key, Object JavaDoc value) {
91         return localVariables.put(key, value);
92     }
93
94     /**
95      * Removes local variable.
96      * @param key variable name.
97      * @return previous value.
98      */

99     public Object JavaDoc remove(Object JavaDoc key) {
100         return localVariables.remove(key);
101     }
102
103     /**
104      * Registers local variables.
105      * @param t local variables map.
106      */

107     public void putAll(Map JavaDoc<? extends String JavaDoc, ?> t) {
108         localVariables.putAll(t);
109     }
110
111     /**
112      * Clears local variables.
113      */

114     public void clear() {
115         localVariables.clear();
116     }
117
118     //Unsupported operations
119

120     public int size() {
121         throw new UnsupportedOperationException JavaDoc();
122     }
123
124     public boolean isEmpty() {
125         throw new UnsupportedOperationException JavaDoc();
126     }
127
128     public boolean containsKey(Object JavaDoc key) {
129         throw new UnsupportedOperationException JavaDoc();
130     }
131
132     public boolean containsValue(Object JavaDoc value) {
133         throw new UnsupportedOperationException JavaDoc();
134     }
135
136
137     public Set JavaDoc<String JavaDoc> keySet() {
138         throw new UnsupportedOperationException JavaDoc();
139     }
140
141     public Collection JavaDoc<Object JavaDoc> values() {
142         throw new UnsupportedOperationException JavaDoc();
143     }
144
145     public Set JavaDoc<Map.Entry JavaDoc<String JavaDoc, Object JavaDoc>> entrySet() {
146         throw new UnsupportedOperationException JavaDoc();
147     }
148 }
149
Popular Tags