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  
23  package net.sourceforge.servletspy.handler.servlet;
24  
25  import java.util.Enumeration;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import javax.servlet.ServletContext;
30  
31  import net.sourceforge.servletspy.IContext;
32  import net.sourceforge.servletspy.IContextHandler;
33  import net.sourceforge.servletspy.config.Param;
34  
35  /***
36   * @author arno schumacher
37   */
38  public final class ServletContextHandler implements IContextHandler {
39  
40      /*** The name of the session attribute map used for the sub context. */
41      private static final String BASE_KEY_NAME = "~";
42  
43      /*** The name of the sub context for the context attributes. */
44      private static final String ATTRIBUTES_KEY_NAME = BASE_KEY_NAME
45              + "attributes";
46  
47      /*** The name of the sub context for the init parameters. */
48      private static final String INITPARAMETERS_KEY_NAME = BASE_KEY_NAME
49              + "initParameters";
50  
51      /* (non-Javadoc)
52       * @see net.sourceforge.servletspy.IContextHandler#handle(net.sourceforge.servletspy.IContext)
53       */
54      public void handle(final IContext contex) throws Exception {
55          if (contex.getSubject() instanceof ServletContext) {
56              final ServletContext servletContext = (ServletContext) contex
57                      .getSubject();
58              contex.proceed();
59              contex.add(ATTRIBUTES_KEY_NAME,
60                      generateAttributeMap(servletContext));
61              contex.add(INITPARAMETERS_KEY_NAME,
62                      generateInitParameterMap(servletContext));
63          } else {
64              contex.proceed();
65          }
66      }
67      
68      /***
69       * Returns a map holding all attributes of the provided context object.
70       *
71       * @param servletCtx
72       *            The servlet context.
73       * @return The attribute map generated using the provided context object.
74       */
75      private static Map generateAttributeMap(final ServletContext servletCtx) {
76          final Map map = new HashMap();
77          final Enumeration enumeration = servletCtx.getAttributeNames();
78          while (enumeration.hasMoreElements()) {
79              final String key = String.valueOf(enumeration.nextElement());
80              final Object value = servletCtx.getAttribute(key);
81              map.put(key, value);
82          }
83          return map;
84      }
85  
86      /***
87       * Returns a map holding all configuration parameters of the provided 
88       * context object.
89       *
90       * @param servletContext
91       *            The servlet context.
92       * @return The attribute map generated using the provided context object.
93       */
94      private static Map generateInitParameterMap(
95              final ServletContext servletContext) {
96          final Map map = new HashMap();
97          final Enumeration enumeration = servletContext.getInitParameterNames();
98          while (enumeration.hasMoreElements()) {
99              final String key = String.valueOf(enumeration.nextElement());
100             final Object value = servletContext.getInitParameter(key);
101             map.put(key, value);
102         }
103         return map;
104     }
105 
106     /* (non-Javadoc)
107      * @see net.sourceforge.servletspy.ILifecycle#init(net.sourceforge.servletspy.config.Param[])
108      */
109     public void init(final Param[] params) {
110     }
111 
112     /* (non-Javadoc)
113      * @see net.sourceforge.servletspy.ILifecycle#destroy()
114      */
115     public void destroy() {
116     }
117 }