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
10
11
12
13 public class AbstractParametricVelocityTool<E extends AbstractParametricVelocityTool> {
14
15
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
25
26
27
28
29
30
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
43
44
45
46
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
56
57
58
59
60 @SuppressWarnings("unchecked")
61 public E remove(String key) {
62 parameters.get().remove(key);
63 return (E) this;
64 }
65
66
67
68
69
70
71 @SuppressWarnings("unchecked")
72 public E clear() {
73 parameters.get().clear();
74 return (E) this;
75 }
76
77
78
79
80
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 }