001    /**
002     * Copyright (c) 2000-present 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.bootstrap;
016    
017    import com.liferay.portal.kernel.cache.BootstrapLoader;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    
022    import java.util.Properties;
023    
024    /**
025     * @author Tina Tian
026     */
027    public class ClusterLinkBootstrapLoader implements BootstrapLoader {
028    
029            public ClusterLinkBootstrapLoader(Properties properties) {
030                    if (properties != null) {
031                            _bootstrapAsynchronously = GetterUtil.getBoolean(
032                                    properties.getProperty("bootstrapAsynchronously"));
033                    }
034                    else {
035                            _bootstrapAsynchronously = true;
036                    }
037            }
038    
039            @Override
040            public boolean isAsynchronous() {
041                    return _bootstrapAsynchronously;
042            }
043    
044            @Override
045            public void load(String portalCacheManagerName, String portalCacheName) {
046                    if (ClusterLinkBootstrapLoaderHelperUtil.isSkipped()) {
047                            return;
048                    }
049    
050                    if (_bootstrapAsynchronously) {
051                            BootstrapLoaderClientThread bootstrapLoaderClientThread =
052                                    new BootstrapLoaderClientThread(
053                                            portalCacheManagerName, portalCacheName);
054    
055                            bootstrapLoaderClientThread.start();
056                    }
057                    else {
058                            doLoad(portalCacheManagerName, portalCacheName);
059                    }
060            }
061    
062            protected void doLoad(
063                    String portalCacheManagerName, String portalCacheName) {
064    
065                    if (_log.isDebugEnabled()) {
066                            _log.debug("Bootstraping " + portalCacheName);
067                    }
068    
069                    try {
070                            ClusterLinkBootstrapLoaderHelperUtil.loadCachesFromCluster(
071                                    portalCacheManagerName, portalCacheName);
072                    }
073                    catch (Exception e) {
074                            if (_log.isWarnEnabled()) {
075                                    _log.warn("Unable to load cache data from the cluster", e);
076                            }
077                    }
078            }
079    
080            private static final Log _log = LogFactoryUtil.getLog(
081                    ClusterLinkBootstrapLoader.class);
082    
083            private final boolean _bootstrapAsynchronously;
084    
085            private class BootstrapLoaderClientThread extends Thread {
086    
087                    public BootstrapLoaderClientThread(
088                            String portalCacheManagerName, String portalCacheName) {
089    
090                            if (_log.isDebugEnabled()) {
091                                    _log.debug(
092                                            "Bootstrap loader client thread for cache " +
093                                                    portalCacheName + " from cache manager " +
094                                                            portalCacheManagerName);
095                            }
096    
097                            _portalCacheManagerName = portalCacheManagerName;
098                            _portalCacheName = portalCacheName;
099    
100                            setDaemon(true);
101                            setName(
102                                    BootstrapLoaderClientThread.class.getName() + " - " +
103                                            portalCacheManagerName + " - " + portalCacheName);
104                            setPriority(Thread.NORM_PRIORITY);
105                    }
106    
107                    @Override
108                    public void run() {
109                            try {
110                                    doLoad(_portalCacheManagerName, _portalCacheName);
111                            }
112                            catch (Exception e) {
113                                    if (_log.isWarnEnabled()) {
114                                            _log.warn("Unable to asynchronously stream bootstrap", e);
115                                    }
116                            }
117                    }
118    
119                    private final String _portalCacheManagerName;
120                    private final String _portalCacheName;
121    
122            }
123    
124    }