001    /**
002     * Copyright (c) 2000-2010 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.kernel.cache;
016    
017    import com.liferay.portal.kernel.concurrent.CompeteLatch;
018    
019    import java.io.Serializable;
020    
021    import java.util.Collection;
022    import java.util.concurrent.ConcurrentHashMap;
023    import java.util.concurrent.ConcurrentMap;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class BlockingPortalCache implements PortalCache {
029    
030            public BlockingPortalCache(PortalCache portalCache) {
031                    _portalCache = portalCache;
032            }
033    
034            public void destroy() {
035            }
036    
037            public Collection<Object> get(Collection<String> keys) {
038                    return _portalCache.get(keys);
039            }
040    
041            public Object get(String key) {
042                    Object obj = _portalCache.get(key);
043    
044                    if (obj != null) {
045                            return obj;
046                    }
047    
048                    CompeteLatch lastCompeteLatch = _competeLatch.get();
049    
050                    if (lastCompeteLatch != null) {
051                            lastCompeteLatch.done();
052    
053                            _competeLatch.set(null);
054                    }
055    
056                    CompeteLatch currentCompeteLatch = _competeLatchMap.get(key);
057    
058                    if (currentCompeteLatch == null) {
059                            CompeteLatch newCompeteLatch = new CompeteLatch();
060    
061                            currentCompeteLatch = _competeLatchMap.putIfAbsent(
062                                    key, newCompeteLatch);
063    
064                            if (currentCompeteLatch == null) {
065                                    currentCompeteLatch = newCompeteLatch;
066                            }
067                    }
068    
069                    _competeLatch.set(currentCompeteLatch);
070    
071                    if (!currentCompeteLatch.compete()) {
072                            currentCompeteLatch.await();
073    
074                            _competeLatch.set(null);
075    
076                            obj = _portalCache.get(key);
077                    }
078    
079                    return obj;
080            }
081    
082            public void put(String key, Object obj) {
083                    if (key == null) {
084                            throw new IllegalArgumentException("Key is null");
085                    }
086    
087                    if (obj == null) {
088                            throw new IllegalArgumentException("Object is null");
089                    }
090    
091                    _portalCache.put(key, obj);
092    
093                    CompeteLatch competeLatch = _competeLatch.get();
094    
095                    if (competeLatch != null) {
096                            competeLatch.done();
097    
098                            _competeLatch.set(null);
099                    }
100    
101                    _competeLatchMap.remove(key);
102            }
103    
104            public void put(String key, Object obj, int timeToLive) {
105                    if (key == null) {
106                            throw new IllegalArgumentException("Key is null");
107                    }
108    
109                    if (obj == null) {
110                            throw new IllegalArgumentException("Object is null");
111                    }
112    
113                    _portalCache.put(key, obj, timeToLive);
114    
115                    CompeteLatch competeLatch = _competeLatch.get();
116    
117                    if (competeLatch != null) {
118                            competeLatch.done();
119    
120                            _competeLatch.set(null);
121                    }
122    
123                    _competeLatchMap.remove(key);
124            }
125    
126            public void put(String key, Serializable obj) {
127                    if (key == null) {
128                            throw new IllegalArgumentException("Key is null");
129                    }
130    
131                    if (obj == null) {
132                            throw new IllegalArgumentException("Object is null");
133                    }
134    
135                    _portalCache.put(key, obj);
136    
137                    CompeteLatch competeLatch = _competeLatch.get();
138    
139                    if (competeLatch != null) {
140                            competeLatch.done();
141    
142                            _competeLatch.set(null);
143                    }
144    
145                    _competeLatchMap.remove(key);
146            }
147    
148            public void put(String key, Serializable obj, int timeToLive) {
149                    if (key == null) {
150                            throw new IllegalArgumentException("Key is null");
151                    }
152    
153                    if (obj == null) {
154                            throw new IllegalArgumentException("Object is null");
155                    }
156    
157                    _portalCache.put(key, obj, timeToLive);
158    
159                    CompeteLatch competeLatch = _competeLatch.get();
160    
161                    if (competeLatch != null) {
162                            competeLatch.done();
163    
164                            _competeLatch.set(null);
165                    }
166    
167                    _competeLatchMap.remove(key);
168            }
169    
170            public void remove(String key) {
171                    _portalCache.remove(key);
172                    _competeLatchMap.remove(key);
173            }
174    
175            public void removeAll() {
176                    _portalCache.removeAll();
177                    _competeLatchMap.clear();
178            }
179    
180            public void setDebug(boolean debug) {
181                    _portalCache.setDebug(debug);
182            }
183    
184            private static ThreadLocal<CompeteLatch> _competeLatch =
185                    new ThreadLocal<CompeteLatch>();
186            private final ConcurrentMap<String, CompeteLatch> _competeLatchMap =
187                    new ConcurrentHashMap<String, CompeteLatch>();
188            private final PortalCache _portalCache;
189    
190    }