001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.impl;
017:
018: import java.util.AbstractMap;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.directwebremoting.Container;
025:
026: /**
027: * A {@link Map} that uses a {@link Container} as it's source of data.
028: * @author Joe Walker [joe at getahead dot ltd dot uk]
029: */
030: public class ContainerMap extends AbstractMap<String, Object> implements
031: Map<String, Object> {
032: /**
033: * Create a ContainerMap with a Container to proxy requests to
034: * @param container The Container that we proxy to
035: * @param filterNonStringValues Does the map include created beans?
036: */
037: public ContainerMap(Container container,
038: boolean filterNonStringValues) {
039: this .container = container;
040: this .filterNonStringValues = filterNonStringValues;
041: }
042:
043: /**
044: * Read the {@link Container} and cache the values
045: */
046: private void init() {
047: if (proxy == null) {
048: proxy = new HashMap<String, Object>();
049: for (String name : container.getBeanNames()) {
050: Object value = container.getBean(name);
051: if (!filterNonStringValues || value instanceof String) {
052: proxy.put(name, value);
053: }
054: }
055: }
056: }
057:
058: /* (non-Javadoc)
059: * @see java.util.AbstractMap#get(java.lang.Object)
060: */
061: @Override
062: public Object get(Object key) {
063: init();
064: return proxy.get(key);
065: }
066:
067: /* (non-Javadoc)
068: * @see java.util.AbstractMap#entrySet()
069: */
070: @Override
071: public Set<Map.Entry<String, Object>> entrySet() {
072: init();
073: return proxy.entrySet();
074: }
075:
076: /* (non-Javadoc)
077: * @see java.util.AbstractMap#containsKey(java.lang.Object)
078: */
079: @Override
080: public boolean containsKey(Object key) {
081: init();
082: return proxy.containsKey(key);
083: }
084:
085: /* (non-Javadoc)
086: * @see java.util.AbstractMap#containsValue(java.lang.Object)
087: */
088: @Override
089: public boolean containsValue(Object value) {
090: init();
091: return proxy.containsValue(value);
092: }
093:
094: /* (non-Javadoc)
095: * @see java.util.AbstractMap#equals(java.lang.Object)
096: */
097: @Override
098: public boolean equals(Object o) {
099: if (this == o) {
100: return true;
101: }
102:
103: init();
104:
105: if (o.getClass() != this .getClass()) {
106: return false;
107: }
108:
109: return proxy.equals(o);
110: }
111:
112: /* (non-Javadoc)
113: * @see java.util.AbstractMap#hashCode()
114: */
115: @Override
116: public int hashCode() {
117: init();
118: return proxy.hashCode();
119: }
120:
121: /* (non-Javadoc)
122: * @see java.util.AbstractMap#isEmpty()
123: */
124: @Override
125: public boolean isEmpty() {
126: init();
127: return proxy.isEmpty();
128: }
129:
130: /* (non-Javadoc)
131: * @see java.util.AbstractMap#keySet()
132: */
133: @Override
134: public Set<String> keySet() {
135: init();
136: return proxy.keySet();
137: }
138:
139: /* (non-Javadoc)
140: * @see java.util.AbstractMap#size()
141: */
142: @Override
143: public int size() {
144: init();
145: return proxy.size();
146: }
147:
148: /* (non-Javadoc)
149: * @see java.util.AbstractMap#values()
150: */
151: @Override
152: public Collection<Object> values() {
153: init();
154: return proxy.values();
155: }
156:
157: /* (non-Javadoc)
158: * @see java.util.AbstractMap#toString()
159: */
160: @Override
161: public String toString() {
162: init();
163: return proxy.toString();
164: }
165:
166: /* (non-Javadoc)
167: * @see java.util.AbstractMap#put(java.lang.Object, java.lang.Object)
168: */
169: @Override
170: public Object put(String key, Object value) {
171: throw new UnsupportedOperationException(
172: "ContainerMaps are read only");
173: }
174:
175: /* (non-Javadoc)
176: * @see java.util.AbstractMap#putAll(java.util.Map)
177: */
178: @Override
179: public void putAll(Map<? extends String, ?> t) {
180: throw new UnsupportedOperationException(
181: "ContainerMaps are read only");
182: }
183:
184: /* (non-Javadoc)
185: * @see java.util.AbstractMap#remove(java.lang.Object)
186: */
187: @Override
188: public Object remove(Object key) {
189: throw new UnsupportedOperationException(
190: "ContainerMaps are read only");
191: }
192:
193: /* (non-Javadoc)
194: * @see java.util.AbstractMap#clear()
195: */
196: @Override
197: public void clear() {
198: throw new UnsupportedOperationException(
199: "ContainerMaps are read only");
200: }
201:
202: /**
203: * Are we filtering non-string values from the container
204: */
205: private boolean filterNonStringValues;
206:
207: /**
208: * The Container that we proxy to.
209: */
210: private Container container;
211:
212: /**
213: * The cache of filtered values
214: */
215: protected Map<String, Object> proxy;
216: }
|