1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.PropertiesUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.util.PropsUtil;
24  
25  import java.io.IOException;
26  
27  import java.util.Properties;
28  
29  import net.sf.ehcache.CacheManager;
30  import net.sf.ehcache.distribution.CacheManagerPeerProvider;
31  
32  /**
33   * <a href="RMICacheManagerPeerProviderFactory.java.html"><b><i>View Source</i>
34   * </b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class RMICacheManagerPeerProviderFactory
39      extends net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory {
40  
41      public RMICacheManagerPeerProviderFactory() {
42          if (_log.isDebugEnabled()) {
43              _log.debug("Instantiating " + this.hashCode());
44          }
45      }
46  
47      public CacheManagerPeerProvider createCachePeerProvider(
48          CacheManager cacheManager, Properties properties) {
49  
50          String portalPropertyKey = properties.getProperty("portalPropertyKey");
51  
52          if (Validator.isNull(portalPropertyKey)) {
53              throw new RuntimeException("portalPropertyKey is null");
54          }
55  
56          String portalPropertiesString = PropsUtil.getProperties().getProperty(
57              portalPropertyKey);
58  
59          if (_log.isInfoEnabled()) {
60              _log.info(
61                  "portalPropertyKey " + portalPropertyKey + " has value " +
62                      portalPropertiesString);
63          }
64  
65          portalPropertiesString = StringUtil.replace(
66              portalPropertiesString, StringPool.COMMA, StringPool.NEW_LINE);
67  
68          Properties portalProperties = null;
69  
70          try {
71              portalProperties = PropertiesUtil.load(
72                  portalPropertiesString);
73          }
74          catch (IOException ioe) {
75              _log.error(ioe, ioe);
76  
77              throw new RuntimeException(ioe.getMessage());
78          }
79  
80          if (_log.isDebugEnabled()) {
81              _log.debug(PropertiesUtil.list(portalProperties));
82          }
83  
84          return super.createCachePeerProvider(cacheManager, portalProperties);
85      }
86  
87      private static Log _log = LogFactoryUtil.getLog(
88          RMICacheManagerPeerProviderFactory.class);
89  
90  }