View Javadoc

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  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      /* (non-Javadoc)
74       * @see net.sourceforge.servletspy.ILifecycle#destroy()
75       */
76      public void destroy() {
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see net.sourceforge.servletspy.IContextHandler#handle(net.sourceforge.servletspy.IContext)
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     /* (non-Javadoc)
110      * @see net.sourceforge.servletspy.ILifecycle#init(net.sourceforge.servletspy.config.Param[])
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 }