001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella.driver.script;
017:
018: import scriptella.spi.ParametersCallback;
019: import scriptella.spi.QueryCallback;
020:
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Map;
024: import java.util.Set;
025:
026: /**
027: * {@link java.util.Map} implementation of {@link ParametersCallback} for
028: * integration into Scriptella execution environment.
029: * <p/>
030: * This class allows local variables to be set via {@link #put(String,Object)} method.
031: * <br>{@link #getParameter(String)} allows reading variables.
032: * <p>In query mode, a virtual variable <code>query</code> is available and exposes a method
033: * {@link #next()} to populate result set.
034: * </p>
035: * <p><em>Note:</em> current implementation does not distinguish if a vairable is absent
036: * or has a value of null.
037: *
038: * @author Fyodor Kupolov
039: * @version 1.0
040: */
041: public class ParametersCallbackMap implements ParametersCallback,
042: Map<String, Object> {
043: private Map<String, Object> localVariables;
044: private ParametersCallback parentParameters;
045: private QueryCallback queryCallback;
046:
047: /**
048: * Initializes instance and set parent parameters to use in {@link #getParameter(String)}.
049: *
050: * @param parentParameters parent parameters.
051: */
052: public ParametersCallbackMap(ParametersCallback parentParameters) {
053: this .parentParameters = parentParameters;
054: }
055:
056: /**
057: * Initializes parameters callback for query element.
058: * @param parentParameters parent parameters.
059: * @param queryCallback callback to notify on row iteration.
060: */
061: public ParametersCallbackMap(ParametersCallback parentParameters,
062: QueryCallback queryCallback) {
063: this .parentParameters = parentParameters;
064: setQueryCallback(queryCallback);
065: }
066:
067: /**
068: * Returns specified variable value.
069: * <p>The local variables set by {@link #put(String,Object)} method
070: * take priority of variables in parentParameters object.
071: *
072: * @param name variable name
073: * @return value of variable or null if variable not found.
074: */
075: public Object getParameter(final String name) {
076: Object v = localVariables == null ? null : localVariables
077: .get(name);
078: if (v != null) {
079: return v;
080: }
081: return parentParameters.getParameter(name);
082: }
083:
084: /**
085: * Use {@link #getParameter(String)}.
086: *
087: * @param key variable name.
088: * @return value of variable.
089: */
090: public Object get(Object key) {
091: return (key instanceof String) ? getParameter((String) key)
092: : null;
093: }
094:
095: public boolean containsKey(Object key) {
096: return (localVariables != null && localVariables
097: .containsKey(key))
098: || (parentParameters.getParameter((String) key) != null);
099: }
100:
101: /**
102: * Sets local variable.
103: *
104: * @param key variable name.
105: * @param value variable value.
106: * @return previous variable value.
107: */
108: public Object put(String key, Object value) {
109: if (localVariables == null) {
110: localVariables = new HashMap<String, Object>();
111: }
112: return localVariables.put(key, value);
113: }
114:
115: /**
116: * Removes local variable.
117: *
118: * @param key variable name.
119: * @return previous value.
120: */
121: public Object remove(Object key) {
122: return localVariables == null ? null : localVariables
123: .remove(key);
124: }
125:
126: /**
127: * Registers local variables.
128: *
129: * @param t local variables map.
130: */
131: public void putAll(Map<? extends String, ?> t) {
132: if (localVariables == null) {
133: localVariables = new HashMap<String, Object>();
134: }
135: localVariables.putAll(t);
136: }
137:
138: /**
139: * Clears local variables.
140: */
141: public void clear() {
142: if (localVariables != null) {
143: localVariables.clear();
144: }
145: }
146:
147: /**
148: * Sets query callback and enables the query mode, i.e. query variable is exposed.
149: * @param queryCallback query callback.
150: */
151: public void setQueryCallback(QueryCallback queryCallback) {
152: this .queryCallback = queryCallback;
153: put("query", this );
154: }
155:
156: /**
157: * Executes nested elements and exposes local variables set by the current query.
158: */
159: public void next() {
160: queryCallback.processRow(this );
161: }
162:
163: //Unsupported operations
164:
165: public int size() {
166: throw new UnsupportedOperationException("size");
167: }
168:
169: public boolean isEmpty() {
170: throw new UnsupportedOperationException("isEmpty");
171: }
172:
173: public boolean containsValue(Object value) {
174: throw new UnsupportedOperationException("containsValue");
175: }
176:
177: public Set<String> keySet() {
178: throw new UnsupportedOperationException("keySet");
179: }
180:
181: public Collection<Object> values() {
182: throw new UnsupportedOperationException("values");
183: }
184:
185: public Set<Map.Entry<String, Object>> entrySet() {
186: throw new UnsupportedOperationException("entrySet");
187: }
188: }
|