View Javadoc

1   package org.tinyjee.maven.dim.sources;
2   
3   import org.tinyjee.maven.dim.spi.Globals;
4   
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   /**
9    * Is an abstract base for tools that
10   *
11   * @author Juergen_Kellerer, 2011-10-17
12   */
13  public class AbstractParametricVelocityTool<E extends AbstractParametricVelocityTool> {
14  
15  	// Thread-safe parameter store
16  	private final ThreadLocal<Map<String, String>> parameters = new ThreadLocal<Map<String, String>>() {
17  		@Override
18  		protected Map<String, String> initialValue() {
19  			return new HashMap<String, String>();
20  		}
21  	};
22  
23  	/**
24  	 * Returns the current value of the internally stored parameter for the given key.
25  	 * <p/>
26  	 * If a parameter is not internally stored, the method retrieves the default value for the given key from the request parameters.
27  	 * If there's no default value, 'null' will be returned.
28  	 *
29  	 * @param key the name of the parameter to get.
30  	 * @return the value of the parameter, may be 'null' if the parameter does not exist.
31  	 */
32  	public String get(String key) {
33  		String value = parameters.get().get(key);
34  		if (value == null) {
35  			Object rawValue = Globals.getInstance().getRequest().getParameter(key);
36  			if (rawValue != null) value = String.valueOf(rawValue);
37  		}
38  		return value;
39  	}
40  
41  	/**
42  	 * Sets a parameter that will be used with the next invocation.
43  	 *
44  	 * @param key   the name of the parameter to set.
45  	 * @param value the value to set.
46  	 * @return an instance of this tool to allow invocation chaining.
47  	 */
48  	@SuppressWarnings("unchecked")
49  	public E put(String key, String value) {
50  		parameters.get().put(key, value);
51  		return (E) this;
52  	}
53  
54  	/**
55  	 * Removes a parameter form the internally maintained parameter list.
56  	 *
57  	 * @param key the name of the parameter to unset.
58  	 * @return an instance of this tool to allow invocation chaining.
59  	 */
60  	@SuppressWarnings("unchecked")
61  	public E remove(String key) {
62  		parameters.get().remove(key);
63  		return (E) this;
64  	}
65  
66  	/**
67  	 * Clears all parameters form the internally maintained parameter list.
68  	 *
69  	 * @return an instance of this tool to allow invocation chaining.
70  	 */
71  	@SuppressWarnings("unchecked")
72  	public E clear() {
73  		parameters.get().clear();
74  		return (E) this;
75  	}
76  
77  	/**
78  	 * Creates the next parameters to be used to invoke a command inside this tool.
79  	 *
80  	 * @return the next parameters to be used to invoke a command inside this tool.
81  	 */
82  	protected Map<String, String> nextInvocationParameters() {
83  		@SuppressWarnings("unchecked")
84  		Map<String, String> params = new HashMap<String, String>(Globals.getInstance().getRequest().getParameters());
85  		params.putAll(parameters.get());
86  
87  		return params;
88  	}
89  
90  	@Override
91  	public String toString() {
92  		return getClass().getSimpleName() + '{' +
93  				"parameters=" + parameters +
94  				'}';
95  	}
96  }