KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > ApplicationMap


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher;
6
7 import javax.servlet.ServletContext JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.util.*;
10
11
12 /**
13  * A simple implementation of the {@link java.util.Map} interface to handle a collection of attributes and
14  * init parameters in a {@link javax.servlet.ServletContext} object. The {@link #entrySet()} method
15  * enumerates over all servlet context attributes and init parameters and returns a collection of both.
16  * Note, this will occur lazily - only when the entry set is asked for.
17  *
18  * @author <a HREF="mailto:rickard@middleware-company.com">Rickard Öberg</a>
19  * @author Bill Lynch (docs)
20  */

21 public class ApplicationMap extends AbstractMap implements Serializable JavaDoc {
22     //~ Instance fields ////////////////////////////////////////////////////////
23

24     ServletContext JavaDoc context;
25     Set entries;
26
27     //~ Constructors ///////////////////////////////////////////////////////////
28

29     /**
30      * Creates a new map object given the servlet context.
31      *
32      * @param ctx the servlet context
33      */

34     public ApplicationMap(ServletContext JavaDoc ctx) {
35         this.context = ctx;
36     }
37
38     //~ Methods ////////////////////////////////////////////////////////////////
39

40     /**
41      * Removes all entries from the Map and removes all attributes from the servlet context.
42      */

43     public void clear() {
44         entries = null;
45
46         Enumeration e = context.getAttributeNames();
47
48         while (e.hasMoreElements()) {
49             context.removeAttribute(e.nextElement().toString());
50         }
51     }
52
53     /**
54      * Creates a Set of all servlet context attributes as well as context init parameters.
55      *
56      * @return a Set of all servlet context attributes as well as context init parameters.
57      */

58     public Set entrySet() {
59         if (entries == null) {
60             entries = new HashSet();
61
62             // Add servlet context attributes
63
Enumeration enumeration = context.getAttributeNames();
64
65             while (enumeration.hasMoreElements()) {
66                 final String JavaDoc key = enumeration.nextElement().toString();
67                 final Object JavaDoc value = context.getAttribute(key);
68                 entries.add(new Map.Entry() {
69                     public boolean equals(Object JavaDoc obj) {
70                         Map.Entry entry = (Map.Entry) obj;
71
72                         return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
73                     }
74
75                     public int hashCode() {
76                         return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
77                     }
78
79                     public Object JavaDoc getKey() {
80                         return key;
81                     }
82
83                     public Object JavaDoc getValue() {
84                         return value;
85                     }
86
87                     public Object JavaDoc setValue(Object JavaDoc obj) {
88                         context.setAttribute(key.toString(), obj);
89
90                         return value;
91                     }
92                 });
93             }
94
95             // Add servlet context init params
96
enumeration = context.getInitParameterNames();
97
98             while (enumeration.hasMoreElements()) {
99                 final String JavaDoc key = enumeration.nextElement().toString();
100                 final Object JavaDoc value = context.getInitParameter(key);
101                 entries.add(new Map.Entry() {
102                     public boolean equals(Object JavaDoc obj) {
103                         Map.Entry entry = (Map.Entry) obj;
104
105                         return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
106                     }
107
108                     public int hashCode() {
109                         return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
110                     }
111
112                     public Object JavaDoc getKey() {
113                         return key;
114                     }
115
116                     public Object JavaDoc getValue() {
117                         return value;
118                     }
119
120                     public Object JavaDoc setValue(Object JavaDoc obj) {
121                         context.setAttribute(key.toString(), obj);
122
123                         return value;
124                     }
125                 });
126             }
127         }
128
129         return entries;
130     }
131
132     /**
133      * Returns the servlet context attribute or init parameter based on the given key. If the
134      * entry is not found, <tt>null</tt> is returned.
135      *
136      * @param key the entry key.
137      * @return the servlet context attribute or init parameter or <tt>null</tt> if the entry is not found.
138      */

139     public Object JavaDoc get(Object JavaDoc key) {
140         // Try context attributes first, then init params
141
// This gives the proper shadowing effects
142
String JavaDoc keyString = key.toString();
143         Object JavaDoc value = context.getAttribute(keyString);
144
145         return (value == null) ? context.getInitParameter(keyString) : value;
146     }
147
148     /**
149      * Sets a servlet context attribute given a attribute name and value.
150      *
151      * @param key the name of the attribute.
152      * @param value the value to set.
153      * @return the attribute that was just set.
154      */

155     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
156         entries = null;
157         context.setAttribute(key.toString(), value);
158
159         return get(key);
160     }
161
162     /**
163      * Removes the specified servlet context attribute.
164      *
165      * @param key the attribute to remove.
166      * @return the entry that was just removed.
167      */

168     public Object JavaDoc remove(Object JavaDoc key) {
169         entries = null;
170
171         Object JavaDoc value = get(key);
172         context.removeAttribute(key.toString());
173
174         return value;
175     }
176 }
177
Popular Tags