001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public CacheManagerPeerProvider createCachePeerProvider(
042                    CacheManager cacheManager, Properties properties) {
043    
044                    if (_log.isDebugEnabled()) {
045                            _log.debug("Creating JGroups peer provider");
046                    }
047    
048                    String clusterName = properties.getProperty("clusterName");
049    
050                    if (clusterName == null) {
051                            clusterName = _DEFAULT_CLUSTER_NAME;
052                    }
053    
054                    if (_log.isDebugEnabled()) {
055                            _log.debug("Cluster name " + clusterName);
056                    }
057    
058                    String channelProperties = properties.getProperty("channelProperties");
059    
060                    if (channelProperties != null) {
061                            channelProperties = channelProperties.replaceAll(
062                                    StringPool.SPACE, StringPool.BLANK);
063    
064                            if (Validator.isNull(channelProperties)) {
065                                    channelProperties = null;
066                            }
067                    }
068    
069                    if (_log.isDebugEnabled()) {
070                            _log.debug("Channel properties " + channelProperties);
071                    }
072    
073                    if (!_initialized) {
074                            if (OSDetector.isUnix() && IPDetector.isSupportsV6() &&
075                                    !IPDetector.isPrefersV4() && _log.isWarnEnabled()) {
076    
077                                    StringBundler sb = new StringBundler(4);
078    
079                                    sb.append(
080                                            "You are on an Unix server with IPv6 enabled. JGroups ");
081                                    sb.append("may not work with IPv6. If you see a multicast ");
082                                    sb.append("error, try adding java.net.preferIPv4Stack=true ");
083                                    sb.append("as a JVM startup parameter.");
084    
085                                    _log.warn(sb.toString());
086                            }
087    
088                            _initialized = true;
089                    }
090    
091                    return new JGroupsManager(cacheManager, clusterName, channelProperties);
092            }
093    
094            private static final String _DEFAULT_CLUSTER_NAME = "Ehcache";
095    
096            private static Log _log = LogFactoryUtil.getLog(
097                    JGroupsCacheManagerPeerProviderFactory.class);
098    
099            private static boolean _initialized;
100    
101    }