001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: ScopedMap.java 20283 2007-06-11 09:45:21Z dmihalache $
020 */
021
022package org.nuxeo.common.collections;
023
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032
033/**
034 * Scoped map holding data for a given scope.
035 * <p>
036 * Used to store context data and invalidate some data given its scope. Implements Map for easier use from interface.
037 *
038 * @deprecated since 9.1, use a regular {@link HashMap} instead
039 * @see ScopeType
040 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
041 */
042@Deprecated
043public class ScopedMap extends HashMap<String, Serializable> {
044
045    private static final Log log = LogFactory.getLog(ScopedMap.class);
046
047    private static final long serialVersionUID = 1L;
048
049    private static final String DEFAULT_PREFIX = ScopeType.DEFAULT.getScopePrefix();
050
051    private static final String REQUEST_PREFIX = ScopeType.REQUEST.getScopePrefix();
052
053    public ScopedMap() {
054        super();
055    }
056
057    public ScopedMap(Map<String, Serializable> map) {
058        super(map);
059    }
060
061    /**
062     * Gets value for given scope and given key.
063     */
064    public Serializable getScopedValue(ScopeType scope, String key) {
065        Serializable res = null;
066        if (scope != null && key != null) {
067            res = super.get(scope.getScopedKey(key));
068        }
069        return res;
070    }
071
072    /**
073     * Gets value for given key using default scope.
074     */
075    public Serializable getScopedValue(String key) {
076        return getScopedValue(ScopeType.DEFAULT, key);
077    }
078
079    /**
080     * Creates a Map with entries from DEFAULT scope.
081     */
082    public Map<String, Serializable> getDefaultScopeValues() {
083        return getScopeValues(ScopeType.DEFAULT);
084    }
085
086    /**
087     * Creates a Map with entries from specified scope.
088     */
089    public Map<String, Serializable> getScopeValues(ScopeType scopeType) {
090        Map<String, Serializable> defMap = new HashMap<String, Serializable>();
091
092        final String defaultScopePrefix = scopeType.getScopePrefix();
093        final int prefixLen = defaultScopePrefix.length();
094        for (Map.Entry<String, Serializable> entry : entrySet()) {
095            String key = entry.getKey();
096            if (key.startsWith(defaultScopePrefix)) {
097                defMap.put(key.substring(prefixLen), entry.getValue());
098            }
099        }
100
101        return defMap;
102    }
103
104    /**
105     * Sets value for given scope and given key.
106     */
107    public void putScopedValue(ScopeType scope, String key, Serializable value) {
108        if (scope == null || key == null) {
109            log.error(String.format("Cannot set scope value using scopeType=%s and key=%s", scope, key));
110        } else {
111            super.put(scope.getScopedKey(key), value);
112        }
113    }
114
115    /**
116     * Sets key using default scope.
117     */
118    public void putScopedValue(String key, Serializable value) {
119        putScopedValue(ScopeType.DEFAULT, key, value);
120    }
121
122    /**
123     * Removes all mappings for given scope.
124     */
125    public void clearScope(ScopeType scopeType) {
126        if (scopeType == null) {
127            log.error("Cannot clear map, specified scope is null");
128        } else {
129            String prefix = scopeType.getScopePrefix();
130            List<String> toRemove = new ArrayList<String>();
131            for (Map.Entry<String, Serializable> entry : entrySet()) {
132                String key = entry.getKey();
133                if (key.startsWith(prefix)) {
134                    toRemove.add(key);
135                }
136            }
137            for (String key : toRemove) {
138                remove(key);
139            }
140        }
141    }
142
143    // used in unit tests
144    protected Serializable superGet(Object key) {
145        return super.get(key);
146    }
147
148    // used in unit tests
149    protected Serializable superPut(String key, Serializable value) {
150        return super.put(key, value);
151    }
152
153    /* Compatibility with code using old keys */
154    @Override
155    public Serializable get(Object key) {
156        if (key instanceof String) {
157            key = compatKey((String) key);
158        }
159        return super.get(key);
160    }
161
162    /* Compatibility with code using old keys */
163    @Override
164    public Serializable put(String key, Serializable value) {
165        return super.put(compatKey(key), value);
166    }
167
168    /**
169     * Removes the prefix default/ or request/ from the key.
170     */
171    protected String compatKey(String key) {
172        if (key.startsWith(DEFAULT_PREFIX)) {
173            return key.substring(DEFAULT_PREFIX.length());
174        } else if (key.startsWith(REQUEST_PREFIX)) {
175            return key.substring(REQUEST_PREFIX.length());
176        } else {
177            return key;
178        }
179    }
180
181}