View Javadoc

1   /*
2    * $Id: Debug.java 497654 2007-01-19 00:21:57Z rgielen $
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  package org.apache.struts2.components;
22  
23  import com.opensymphony.xwork2.util.ValueStack;
24  import com.opensymphony.xwork2.util.OgnlUtil;
25  
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.http.HttpServletRequest;
28  import java.io.Writer;
29  import java.util.Iterator;
30  import java.util.Map;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import org.apache.struts2.views.annotations.StrutsTag;
35  import org.apache.struts2.StrutsException;
36  
37  @StrutsTag(name="debug", tldTagClass="org.apache.struts2.views.jsp.ui.DebugTag",
38          description="Prints debugging information")
39  public class Debug extends UIBean {
40      public static final String TEMPLATE = "debug";
41  
42      public Debug(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
43          super(stack, request, response);
44      }
45  
46      protected String getDefaultTemplate() {
47          return TEMPLATE;
48      }
49  
50      public boolean start(Writer writer) {
51          boolean result = super.start(writer);
52  
53          ValueStack stack = getStack();
54          Iterator iter = stack.getRoot().iterator();
55          List stackValues = new ArrayList(stack.getRoot().size());
56          while (iter.hasNext()) {
57              Object o = iter.next();
58              Map values;
59              try {
60                  values = OgnlUtil.getBeanMap(o);
61              } catch (Exception e) {
62                  throw new StrutsException("Caught an exception while getting the property values of " + o, e);
63              }
64              stackValues.add(new DebugMapEntry(o.getClass().getName(), values));
65          }
66  
67          addParameter("stackValues", stackValues);
68  
69          return result;
70      }
71  
72      private class DebugMapEntry implements Map.Entry {
73          private Object key;
74          private Object value;
75  
76          DebugMapEntry(Object key, Object value) {
77              this.key = key;
78              this.value = value;
79          }
80  
81          public Object getKey() {
82              return key;
83          }
84  
85          public Object getValue() {
86              return value;
87          }
88  
89          public Object setValue(Object newVal) {
90              Object oldVal = value;
91              value = newVal;
92              return oldVal;
93          }
94      }
95  
96  }