001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.loader;
030:
031: import java.util.Collection;
032: import java.util.HashMap;
033: import java.util.Map;
034: import java.util.Set;
035:
036: /**
037: * Creates a ClassLoader-dependent hashmap.
038: */
039: public class EnvironmentMap extends HashMap {
040: private EnvironmentLocal<HashMap> _envMap = new EnvironmentLocal<HashMap>();
041:
042: private HashMap _global;
043:
044: /**
045: * Creates a new environment map.
046: */
047: public EnvironmentMap(HashMap global) {
048: _global = global;
049: }
050:
051: public EnvironmentMap() {
052: this (new HashMap());
053: }
054:
055: public HashMap getGlobal() {
056: return _global;
057: }
058:
059: public HashMap setGlobal(HashMap global) {
060: HashMap old = _global;
061:
062: _global = global;
063:
064: return old;
065: }
066:
067: public int size() {
068: return getEnvironmentMap().size();
069: }
070:
071: public boolean isEmpty() {
072: return getEnvironmentMap().isEmpty();
073: }
074:
075: public boolean containsValue(Object value) {
076: return getEnvironmentMap().containsValue(value);
077: }
078:
079: public boolean containsKey(Object value) {
080: return getEnvironmentMap().containsKey(value);
081: }
082:
083: public Object get(Object key) {
084: Map map = getEnvironmentMap();
085:
086: Object value = map.get(key);
087:
088: if (value != null)
089: return value;
090: else
091: return _global.get(key);
092: }
093:
094: public Object put(Object key, Object value) {
095: return getPutEnvironmentMap().put(key, value);
096: }
097:
098: public Object remove(Object key) {
099: return getPutEnvironmentMap().remove(key);
100: }
101:
102: public void putAll(Map map) {
103: getPutEnvironmentMap().putAll(map);
104: }
105:
106: public void clear() {
107: getPutEnvironmentMap().clear();
108: }
109:
110: public Object clone() {
111: return getEnvironmentMap().clone();
112: }
113:
114: public String toString() {
115: return getEnvironmentMap().toString();
116: }
117:
118: public Set keySet() {
119: return getPutEnvironmentMap().keySet();
120: }
121:
122: public Set entrySet() {
123: return getPutEnvironmentMap().entrySet();
124: }
125:
126: public Collection values() {
127: return getPutEnvironmentMap().values();
128: }
129:
130: public boolean equals(Object o) {
131: return getEnvironmentMap().equals(o);
132: }
133:
134: public int hashCode() {
135: return getEnvironmentMap().hashCode();
136: }
137:
138: private HashMap getEnvironmentMap() {
139: HashMap map = _envMap.get();
140:
141: if (map != null)
142: return map;
143: else
144: return _global;
145: }
146:
147: private synchronized HashMap getPutEnvironmentMap() {
148: HashMap map = _envMap.getLevel();
149:
150: if (map == null) {
151: map = new HashMap();
152: HashMap parentMap = _envMap.get();
153: if (parentMap == null)
154: parentMap = _global;
155:
156: map.putAll(parentMap);
157:
158: _envMap.set(map);
159:
160: return map;
161: } else
162: return map;
163: }
164: }
|