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.cache.ehcache;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.InitialThreadLocal;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    import java.util.Properties;
025    
026    import net.sf.ehcache.CacheException;
027    import net.sf.ehcache.Ehcache;
028    import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
029    
030    /**
031     * @author Shuyang Zhou
032     * @author Sherry Yang
033     */
034    public class EhcacheStreamBootstrapCacheLoader implements BootstrapCacheLoader {
035    
036            public static void resetSkip() {
037                    _skipBootstrapThreadLocal.remove();
038            }
039    
040            public static void setSkip() {
041                    _skipBootstrapThreadLocal.set(Boolean.TRUE);
042            }
043    
044            public static synchronized void start() {
045                    if (!_started) {
046                            _started = true;
047                    }
048    
049                    for (Ehcache ehcache : _deferredEhcaches) {
050                            if (_log.isDebugEnabled()) {
051                                    _log.debug("Loading deferred cache " + ehcache.getName());
052                            }
053    
054                            try {
055                                    EhcacheStreamBootstrapHelpUtil.acquireCachePeers(ehcache);
056                            }
057                            catch (Exception e) {
058                                    throw new CacheException(e);
059                            }
060                    }
061            }
062    
063            public EhcacheStreamBootstrapCacheLoader(Properties properties) {
064                    if (properties != null) {
065                            _bootstrapAsynchronously = GetterUtil.getBoolean(
066                                    properties.getProperty("bootstrapAsynchronously"));
067                    }
068            }
069    
070            @Override
071            public Object clone() {
072                    return this;
073            }
074    
075            public void doLoad(Ehcache ehcache) {
076                    synchronized (EhcacheStreamBootstrapCacheLoader.class) {
077                            if (!_started) {
078                                    _deferredEhcaches.add(ehcache);
079    
080                                    return;
081                            }
082                    }
083    
084                    if (_skipBootstrapThreadLocal.get()) {
085                            return;
086                    }
087    
088                    if (_log.isDebugEnabled()) {
089                            _log.debug("Bootstraping " + ehcache.getName());
090                    }
091    
092                    try {
093                            EhcacheStreamBootstrapHelpUtil.acquireCachePeers(ehcache);
094                    }
095                    catch (Exception e) {
096                            throw new CacheException(e);
097                    }
098            }
099    
100            @Override
101            public boolean isAsynchronous() {
102                    return _bootstrapAsynchronously;
103            }
104    
105            @Override
106            public void load(Ehcache ehcache) {
107                    if (_bootstrapAsynchronously) {
108                            EhcacheStreamClientThread streamBootstrapThread =
109                                    new EhcacheStreamClientThread(ehcache);
110    
111                            streamBootstrapThread.start();
112                    }
113                    else {
114                            doLoad(ehcache);
115                    }
116            }
117    
118            private static Log _log = LogFactoryUtil.getLog(
119                    EhcacheStreamBootstrapCacheLoader.class);
120    
121            private static List<Ehcache> _deferredEhcaches = new ArrayList<Ehcache>();
122            private static ThreadLocal<Boolean> _skipBootstrapThreadLocal =
123                    new InitialThreadLocal<Boolean>(
124                            EhcacheStreamBootstrapCacheLoader.class +
125                                    "._skipBootstrapThreadLocal",
126                            false);
127            private static boolean _started;
128    
129            private boolean _bootstrapAsynchronously = true;
130    
131            private class EhcacheStreamClientThread extends Thread {
132    
133                    public EhcacheStreamClientThread(Ehcache ehcache) {
134                            if (_log.isDebugEnabled()) {
135                                    _log.debug(
136                                            "Ehcache stream client thread for cache " +
137                                                    ehcache.getName());
138                            }
139    
140                            _ehcache = ehcache;
141    
142                            setDaemon(true);
143                            setName(
144                                    EhcacheStreamClientThread.class.getName() + " - " +
145                                            ehcache.getName());
146                            setPriority(Thread.NORM_PRIORITY);
147                    }
148    
149                    @Override
150                    public void run() {
151                            try {
152                                    doLoad(_ehcache);
153                            }
154                            catch (Exception e) {
155                                    if (_log.isWarnEnabled()) {
156                                            _log.warn("Unable to asynchronously stream bootstrap", e);
157                                    }
158                            }
159                    }
160    
161                    private Ehcache _ehcache;
162    
163            }
164    
165    }