1
2 /*
3 * Copyright (C) 2005 by Arno Schumacher
4 *
5 * This file is part of net.sourceforge.servletspy
6 *
7 * net.sourceforge.servletspy is free software; you can redistribute
8 * it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2, or (at your option) any later version.
11 *
12 * net.sourceforge.servletspy is distributed in the hope that it will
13 * be useful, but WITHOUT ANY WARRANTY; without even the implied
14 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA
20 */
21
22
23 package net.sourceforge.servletspy.handler.container;
24
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import net.sourceforge.servletspy.IContext;
30 import net.sourceforge.servletspy.IContextHandler;
31 import net.sourceforge.servletspy.config.Param;
32
33 /***
34 * @author arno schumacher
35 */
36 public final class MapTransformer implements IContextHandler {
37 private static final int MAX_MAP_SIZE = 64;
38
39 public static final String PARAM_MAP_SIZE = "MAX_MAP_SIZE";
40
41 private int maxMapSize = MAX_MAP_SIZE;
42
43 /*** for debugging purpose only ... */
44 private static final String[] KEYPREFIXES = new String[] {}; // "weblogic.",
45
46 // "com.ibm.websphere.",
47 // "org.apache.tomcat"
48 // };
49
50 /*
51 * (non-Javadoc)
52 *
53 * @see net.sourceforge.servletspy.IContextHandler#handle(net.sourceforge.servletspy.IContext)
54 */
55 public void handle(final IContext context) throws Exception {
56 if (context.getSubject() instanceof Map) {
57 final Map current = (Map) context.getSubject();
58
59 // TODO just to avoid concurrent modification exceptions... is this the right way? Is the cycle detection not working correctly?
60 final HashMap map = new HashMap( current );
61 final Iterator iterator = map.entrySet().iterator();
62
63 final int size = map.size();
64
65 if (size > maxMapSize) {
66 context.add(" Map[].size", String.valueOf(size)
67 + " / ouput truncated");
68 } else {
69 context.add(" Map[].size", String.valueOf(size));
70 }
71
72 for (int k = 0; iterator.hasNext() && k < maxMapSize; k++) {
73 Map.Entry entry = (Map.Entry) iterator.next();
74 Object key = entry.getKey();
75 Object value = entry.getValue();
76
77 if (key == null) {
78 key = "<null>";
79 }
80 for (int i = 0; i < KEYPREFIXES.length; i++) {
81 if (String.valueOf(key).startsWith(KEYPREFIXES[i])) {
82 value = String.valueOf(value);
83 }
84 }
85 context.add(" Map[" + key + "]", value);
86 }
87 } else {
88 context.proceed();
89 }
90 }
91
92 /*
93 * (non-Javadoc)
94 *
95 * @see net.sourceforge.servletspy.ILifecycle#destroy()
96 */
97 public void destroy() {
98 }
99
100 /*
101 * (non-Javadoc)
102 *
103 * @see net.sourceforge.servletspy.ILifecycle#init(net.sourceforge.servletspy.config.Param[])
104 */
105 public void init(final Param[] params) {
106 for (int i = 0; i < params.length; i++) {
107 if (PARAM_MAP_SIZE.equals(params[i].getName())) {
108 int mapSize = Integer.MIN_VALUE;
109 try {
110 mapSize = Integer.parseInt(params[i].getValue());
111 } catch (Exception e) {
112 }
113 if (mapSize >= 0) {
114 maxMapSize = mapSize;
115 }
116 }
117 }
118 }
119 }