1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.components;
23
24 import com.opensymphony.xwork2.inject.Inject;
25 import com.opensymphony.xwork2.util.ValueStack;
26 import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
27
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpServletRequest;
30 import java.io.Writer;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import org.apache.struts2.views.annotations.StrutsTag;
37 import org.apache.struts2.StrutsException;
38
39 @StrutsTag(name="debug", tldTagClass="org.apache.struts2.views.jsp.ui.DebugTag",
40 description="Prints debugging information")
41 public class Debug extends UIBean {
42 public static final String TEMPLATE = "debug";
43
44 protected ReflectionProvider reflectionProvider;
45
46
47
48 public Debug(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
49 super(stack, request, response);
50 }
51
52 @Inject
53 public void setReflectionProvider(ReflectionProvider prov) {
54 this.reflectionProvider = prov;
55 }
56
57 protected String getDefaultTemplate() {
58 return TEMPLATE;
59 }
60
61 public boolean start(Writer writer) {
62 boolean result = super.start(writer);
63
64 ValueStack stack = getStack();
65 Iterator iter = stack.getRoot().iterator();
66 List stackValues = new ArrayList(stack.getRoot().size());
67 while (iter.hasNext()) {
68 Object o = iter.next();
69 Map values;
70 try {
71 values = reflectionProvider.getBeanMap(o);
72 } catch (Exception e) {
73 throw new StrutsException("Caught an exception while getting the property values of " + o, e);
74 }
75 stackValues.add(new DebugMapEntry(o.getClass().getName(), values));
76 }
77
78 addParameter("stackValues", stackValues);
79
80 return result;
81 }
82
83 private static class DebugMapEntry implements Map.Entry {
84 private Object key;
85 private Object value;
86
87 DebugMapEntry(Object key, Object value) {
88 this.key = key;
89 this.value = value;
90 }
91
92 public Object getKey() {
93 return key;
94 }
95
96 public Object getValue() {
97 return value;
98 }
99
100 public Object setValue(Object newVal) {
101 Object oldVal = value;
102 value = newVal;
103 return oldVal;
104 }
105 }
106
107 }