1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sourceforge.servletspy.handler.filter;
23
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.StringTokenizer;
30
31 import net.sourceforge.servletspy.IContext;
32 import net.sourceforge.servletspy.IContextHandler;
33 import net.sourceforge.servletspy.config.Param;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /***
39 * @author arno schumacher
40 */
41 public final class PackageFilter implements IContextHandler {
42
43 /*** The logger. */
44 private static final Log LOGGER = LogFactory.getLog(PackageFilter.class);
45
46 /***
47 * Configuration parameter name used to pass a list of space separated
48 * package names to exclude.
49 */
50 public static final String PARAM_EXCLUDES = "PACKAGES";
51
52 /***
53 * The set of packages not to consider within the serialization process.
54 */
55 protected Set pkgsNames = new HashSet();
56
57 /***
58 * Constructor.
59 */
60 public PackageFilter() {
61 }
62
63 protected void addFromString(final String clazzes) {
64 List list = new ArrayList();
65 StringTokenizer st = new StringTokenizer(clazzes);
66 while (st.hasMoreTokens()) {
67 list.add(st.nextToken().trim());
68 }
69 pkgsNames.addAll(list);
70 LOGGER.info("Excluding '" + pkgsNames + "'");
71 }
72
73
74
75
76 public void destroy() {
77 }
78
79
80
81
82
83
84 public final void handle(final IContext context) throws Exception {
85 final Object subject = context.getSubject();
86 if (subject == null) {
87 context.proceed();
88 return;
89 }
90
91 final Package subjectPackage = subject.getClass().getPackage();
92 if (subjectPackage == null) {
93 context.proceed();
94 return;
95 }
96
97 final String pkg = subjectPackage.getName();
98 for (Iterator iterator = pkgsNames.iterator(); iterator.hasNext();) {
99 final String toExclude = String.valueOf(iterator.next());
100 if (pkg.startsWith(toExclude)) {
101 LOGGER.debug("Matched " + pkg + "/" + toExclude);
102 context.setValue(String.valueOf(context.getSubject()));
103 return;
104 }
105 }
106 context.proceed();
107 }
108
109
110
111
112 public void init(final Param[] params) {
113 for (int i = 0; i < params.length; i++) {
114 if (PARAM_EXCLUDES.equals(params[i].getName())) {
115 final String value = params[i].getValue();
116 addFromString(value);
117 }
118 }
119 }
120 }