1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.cache.ehcache;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.IPDetector;
20  import com.liferay.portal.kernel.util.OSDetector;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  
25  import java.util.Properties;
26  
27  import net.sf.ehcache.CacheManager;
28  import net.sf.ehcache.distribution.CacheManagerPeerProvider;
29  import net.sf.ehcache.distribution.CacheManagerPeerProviderFactory;
30  
31  /**
32   * <a href="JGroupsCacheManagerPeerProviderFactory.java.html"><b><i>View Source
33   * </i></b></a>
34   *
35   * <p>
36   * See http://issues.liferay.com/browse/LPS-11061.
37   * </p>
38   *
39   * @author Tina Tian
40   */
41  public class JGroupsCacheManagerPeerProviderFactory
42      extends CacheManagerPeerProviderFactory {
43  
44      public CacheManagerPeerProvider createCachePeerProvider(
45          CacheManager cacheManager, Properties properties) {
46  
47          if (_log.isDebugEnabled()) {
48              _log.debug("Creating JGroups peer provider");
49          }
50  
51          String clusterName = properties.getProperty("clusterName");
52  
53          if (clusterName == null) {
54              clusterName = _DEFAULT_CLUSTER_NAME;
55          }
56  
57          if (_log.isDebugEnabled()) {
58              _log.debug("Cluster name " + clusterName);
59          }
60  
61          String channelProperties = properties.getProperty("channelProperties");
62  
63          if (channelProperties != null) {
64              channelProperties = channelProperties.replaceAll(
65                  StringPool.SPACE, StringPool.BLANK);
66  
67              if (Validator.isNull(channelProperties)) {
68                  channelProperties = null;
69              }
70          }
71  
72          if (_log.isDebugEnabled()) {
73              _log.debug("Channel properties " + channelProperties);
74          }
75  
76          if (!_initialized) {
77              if (OSDetector.isUnix() && IPDetector.isSupportsV6() &&
78                  !IPDetector.isPrefersV4() && _log.isWarnEnabled()) {
79  
80                  StringBundler sb = new StringBundler(4);
81  
82                  sb.append(
83                      "You are on an Unix server with IPv6 enabled. JGroups ");
84                  sb.append("may not work with IPv6. If you see a multicast ");
85                  sb.append("error, try adding java.net.preferIPv4Stack=true ");
86                  sb.append("as a JVM startup parameter.");
87  
88                  _log.warn(sb.toString());
89              }
90  
91              _initialized = true;
92          }
93  
94          return new JGroupsManager(cacheManager, clusterName, channelProperties);
95      }
96  
97      private static final String _DEFAULT_CLUSTER_NAME = "Ehcache";
98  
99      private static Log _log = LogFactoryUtil.getLog(
100         JGroupsCacheManagerPeerProviderFactory.class);
101 
102     private static boolean _initialized;
103 
104 }