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.IPDetector;
020    import com.liferay.portal.kernel.util.OSDetector;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.util.Properties;
026    
027    import net.sf.ehcache.CacheManager;
028    import net.sf.ehcache.distribution.CacheManagerPeerProvider;
029    import net.sf.ehcache.distribution.CacheManagerPeerProviderFactory;
030    
031    /**
032     * <p>
033     * See http://issues.liferay.com/browse/LPS-11061.
034     * </p>
035     *
036     * @author Tina Tian
037     */
038    public class JGroupsCacheManagerPeerProviderFactory
039            extends CacheManagerPeerProviderFactory {
040    
041            @Override
042            public CacheManagerPeerProvider createCachePeerProvider(
043                    CacheManager cacheManager, Properties properties) {
044    
045                    if (_log.isDebugEnabled()) {
046                            _log.debug("Creating JGroups peer provider");
047                    }
048    
049                    String clusterName = properties.getProperty("clusterName");
050    
051                    if (clusterName == null) {
052                            clusterName = _DEFAULT_CLUSTER_NAME;
053                    }
054    
055                    if (_log.isDebugEnabled()) {
056                            _log.debug("Cluster name " + clusterName);
057                    }
058    
059                    String channelProperties = properties.getProperty("channelProperties");
060    
061                    if (channelProperties != null) {
062                            channelProperties = channelProperties.replaceAll(
063                                    StringPool.SPACE, StringPool.BLANK);
064    
065                            if (Validator.isNull(channelProperties)) {
066                                    channelProperties = null;
067                            }
068                    }
069    
070                    if (_log.isDebugEnabled()) {
071                            _log.debug("Channel properties " + channelProperties);
072                    }
073    
074                    if (!_initialized) {
075                            if (OSDetector.isUnix() && IPDetector.isSupportsV6() &&
076                                    !IPDetector.isPrefersV4() && _log.isWarnEnabled()) {
077    
078                                    StringBundler sb = new StringBundler(4);
079    
080                                    sb.append(
081                                            "You are on an Unix server with IPv6 enabled. JGroups ");
082                                    sb.append("may not work with IPv6. If you see a multicast ");
083                                    sb.append("error, try adding java.net.preferIPv4Stack=true ");
084                                    sb.append("as a JVM startup parameter.");
085    
086                                    _log.warn(sb.toString());
087                            }
088    
089                            _initialized = true;
090                    }
091    
092                    return new JGroupsManager(cacheManager, clusterName, channelProperties);
093            }
094    
095            private static final String _DEFAULT_CLUSTER_NAME = "Ehcache";
096    
097            private static Log _log = LogFactoryUtil.getLog(
098                    JGroupsCacheManagerPeerProviderFactory.class);
099    
100            private static boolean _initialized;
101    
102    }