View Javadoc

1   /*
2    * $Id: Debug.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 }