1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
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
107
108
109 public void init(final Param[] params) {
110 }
111
112
113
114
115 public void destroy() {
116 }
117 }