001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import java.util.Map;
018    import java.util.concurrent.ConcurrentHashMap;
019    import java.util.concurrent.ConcurrentMap;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class WebAppPool {
025    
026            public static void clear() {
027                    _instance._webAppPool.clear();
028            }
029    
030            public static Object get(Long webAppId, String key) {
031                    return _instance._get(webAppId, key);
032            }
033    
034            public static void put(Long webAppId, String key, Object obj) {
035                    _instance._put(webAppId, key, obj);
036            }
037    
038            public static Object remove(Long webAppId, String key) {
039                    return _instance._remove(webAppId, key);
040            }
041    
042            private WebAppPool() {
043                    _webAppPool = new ConcurrentHashMap<Long, Map<String, Object>>();
044            }
045    
046            private Object _get(Long webAppId, String key) {
047                    Map<String, Object> map = _webAppPool.get(webAppId);
048    
049                    if (map == null) {
050                            return null;
051                    }
052                    else {
053                            return map.get(key);
054                    }
055            }
056    
057            private void _put(Long webAppId, String key, Object obj) {
058                    Map<String, Object> map = _webAppPool.get(webAppId);
059    
060                    if (map == null) {
061                            map = new ConcurrentHashMap<String, Object>();
062    
063                            Map<String, Object> previousMap = _webAppPool.putIfAbsent(
064                                    webAppId, map);
065    
066                            if (previousMap != null) {
067                                    map = previousMap;
068                            }
069                    }
070    
071                    map.put(key, obj);
072            }
073    
074            private Object _remove(Long webAppId, String key) {
075                    Map<String, Object> map = _webAppPool.get(webAppId);
076    
077                    if (map == null) {
078                            return null;
079                    }
080                    else {
081                            return map.remove(key);
082                    }
083            }
084    
085            private static WebAppPool _instance = new WebAppPool();
086    
087            private ConcurrentMap<Long, Map<String, Object>> _webAppPool;
088    
089    }