Skip to content

Commit 72d16da

Browse files
authored
Merge pull request apache#674 from atlassian/WW-5298-strutsvelocitycontext
WW-5298 Clean up StrutsVelocityContext
2 parents 7d1c821 + d31c8c4 commit 72d16da

File tree

1 file changed

+18
-58
lines changed

1 file changed

+18
-58
lines changed

plugins/velocity/src/main/java/org/apache/struts2/views/velocity/StrutsVelocityContext.java

+18-58
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818
*/
1919
package org.apache.struts2.views.velocity;
2020

21-
import org.apache.velocity.VelocityContext;
22-
2321
import com.opensymphony.xwork2.util.ValueStack;
22+
import org.apache.velocity.VelocityContext;
2423

2524
import java.util.ArrayList;
2625
import java.util.Arrays;
2726
import java.util.List;
2827

2928
public class StrutsVelocityContext extends VelocityContext {
3029

31-
private ValueStack stack;
32-
private List<VelocityContext> chainedContexts;
30+
private final ValueStack stack;
31+
private final List<VelocityContext> chainedContexts;
3332

3433
/**
3534
* Creates a content with link to the ValueStack and any other Velocity contexts
@@ -44,7 +43,7 @@ public StrutsVelocityContext(List<VelocityContext> chainedContexts, ValueStack s
4443
}
4544

4645
/**
47-
* @deprecated please use newly added {@link StrutsVelocityContext(List<VelocityContext)} constructor instead
46+
* @deprecated please use {@link #StrutsVelocityContext(List, ValueStack)}
4847
* and pass {null} or empty list if no chained contexts were defined
4948
*/
5049
@Deprecated
@@ -53,79 +52,40 @@ public StrutsVelocityContext(ValueStack stack) {
5352
}
5453

5554
/**
56-
* @deprecated please use newly added {@link StrutsVelocityContext(List<VelocityContext)} constructor instead
55+
* @deprecated please use {@link #StrutsVelocityContext(List, ValueStack)}
5756
*/
5857
@Deprecated()
5958
public StrutsVelocityContext(VelocityContext[] chainedContexts, ValueStack stack) {
6059
this(new ArrayList<>(Arrays.asList(chainedContexts)), stack);
6160
}
6261

6362
public boolean internalContainsKey(String key) {
64-
boolean contains = super.internalContainsKey(key);
65-
66-
// first let's check to see if we contain the requested key
67-
if (contains) {
68-
return true;
69-
}
70-
71-
// if not, let's search for the key in the ognl value stack
72-
if (stack != null) {
73-
Object o = stack.findValue(key);
74-
75-
if (o != null) {
76-
return true;
77-
}
78-
79-
o = stack.getContext().get(key);
80-
if (o != null) {
81-
return true;
82-
}
83-
}
84-
85-
// if we still haven't found it, le's search through our chained contexts
86-
if (chainedContexts != null) {
87-
for (VelocityContext chainedContext : chainedContexts) {
88-
if (chainedContext.containsKey(key)) {
89-
return true;
90-
}
91-
}
92-
}
93-
94-
// nope, i guess it's really not here
95-
return false;
63+
return internalGet(key) != null;
9664
}
9765

9866
public Object internalGet(String key) {
99-
// first, let's check to see if have the requested value
100-
if (super.internalContainsKey(key)) {
101-
return super.internalGet(key);
67+
Object val = super.internalGet(key);
68+
if (val != null) {
69+
return val;
10270
}
103-
104-
// still no luck? let's look against the value stack
10571
if (stack != null) {
106-
Object object = stack.findValue(key);
107-
108-
if (object != null) {
109-
return object;
72+
val = stack.findValue(key);
73+
if (val != null) {
74+
return val;
11075
}
111-
112-
object = stack.getContext().get(key);
113-
if (object != null) {
114-
return object;
76+
val = stack.getContext().get(key);
77+
if (val != null) {
78+
return val;
11579
}
116-
11780
}
118-
119-
// finally, if we're chained to other contexts, let's look in them
12081
if (chainedContexts != null) {
12182
for (VelocityContext chainedContext : chainedContexts) {
122-
if (chainedContext.containsKey(key)) {
123-
return chainedContext.internalGet(key);
83+
val = chainedContext.internalGet(key);
84+
if (val != null) {
85+
return val;
12486
}
12587
}
12688
}
127-
128-
// nope, i guess it's really not here
12989
return null;
13090
}
13191
}

0 commit comments

Comments
 (0)