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.base;
23  
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.StringTokenizer;
28  
29  import net.sourceforge.servletspy.IContext;
30  import net.sourceforge.servletspy.IContextHandler;
31  import net.sourceforge.servletspy.config.Param;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  /***
37   * <code>AbstractClassFilter</code> prevents passing of subjects to the next
38   * <code>IContextHandler</code> in case they are assignable to an element of a
39   * set of classes and interfaces. In this case a string representation of the
40   * subject is created.
41   *
42   * @author arno schumacher
43   */
44  public abstract class AbstractClassFilter implements IContextHandler {
45  
46      /*** The logger. */
47      private static final Log LOGGER = LogFactory
48              .getLog(AbstractClassFilter.class);
49  
50      /***
51       * Configuration parameter name used to pass a list of space separated
52       * class names to exclude.
53       */
54      public static final String PARAM_CLASSES = "CLASSES";
55  
56      /***
57       * Generates a list of <code>java.lang.Class</code> objects
58       * using the a space seaparated list of class names.
59       *
60       * @param clazzes The classes as string.
61       * @return A list of <code>java.lang.Class</code> objects.
62       */
63      protected static List fromString(final String clazzes) {
64          final List classList = new ArrayList();
65          final StringTokenizer st = new StringTokenizer(clazzes);
66          while (st.hasMoreTokens()) {
67              final Class clazz = loadClass(st.nextToken().trim());
68              if (clazz != null) {
69                  classList.add(clazz);
70              }
71          }
72          return classList;
73      }
74  
75      /***
76       * Loads a class.
77       *
78       * @param s The class name.
79       * @return The class object or <code>null</code> in case of a problem.
80       */
81      private static Class loadClass(final String s) {
82          Class clazz = null;
83          try {
84              clazz = Thread.currentThread().getContextClassLoader().loadClass(s);
85          } catch (Exception e) {
86              LOGGER.warn("Could not load class '" + s + "', ignoring.");
87          }
88          return clazz;
89      }
90  
91      /*** The list of classes to exclude. */
92      protected final List clazzes = new ArrayList();
93  
94      /***
95       * Constructor.
96       *
97       * @param classesString
98       *            a string holding a space separated list of qualified class or
99       *            interface names to filter out.
100      */
101     protected AbstractClassFilter(final String classesString) {
102         clazzes.addAll(fromString(classesString));
103     }
104 
105     /***
106      * Constructor.
107      *
108      * @param clazzArray
109      *            a string array holding qualified class or interface names to
110      *            filter out.
111      */
112     protected AbstractClassFilter(final String[] clazzArray) {
113         if (clazzArray != null) {
114             for (int i = 0; i < clazzArray.length; i++) {
115                 if (clazzArray[i] != null) {
116                     Class clazz = loadClass(clazzArray[i]);
117                     if (clazz != null) {
118                         clazzes.add(clazz);
119                     }
120                 }
121             }
122         }
123     }
124 
125     /* (non-Javadoc)
126      * @see net.sourceforge.servletspy.ILifecycle#destroy()
127      */
128     public void destroy() {
129     }
130 
131     /* (non-Javadoc)
132      * @see net.sourceforge.servletspy.IContextHandler#handle(net.sourceforge.servletspy.IContext)
133      */
134     public final void handle(final IContext context) throws Exception {
135         final Iterator iterator = clazzes.iterator();
136         while (iterator.hasNext()) {
137             final Class clazz = (Class) iterator.next();
138             if (clazz.isAssignableFrom(context.getSubject().getClass())) {
139                 context.setValue(String.valueOf(context.getSubject()));
140                 return;
141             }
142         }
143         context.proceed();
144     }
145 
146     /***
147      * @param params
148      *            may hold an entries with the key <code>CLASSES</code>
149      *            holding a space separated list of qualified class or interface
150      *            names to filter out.
151      */
152     public final void init(final Param[] params) {
153         for (int i = 0; i < params.length; i++) {
154             if (PARAM_CLASSES.equals(params[i].getName())) {
155                 clazzes.addAll(fromString(params[i].getValue()));
156             }
157         }
158         LOGGER.info("Excluding '" + clazzes + "'");
159     }
160 }