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.CharPool;
20  import com.liferay.portal.kernel.util.InstanceFactory;
21  import com.liferay.portal.kernel.util.PropertiesUtil;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.HtmlImpl;
25  import com.liferay.portal.util.PropsUtil;
26  import com.liferay.portal.util.PropsValues;
27  
28  import java.io.IOException;
29  
30  import java.util.Properties;
31  
32  import net.sf.ehcache.CacheManager;
33  import net.sf.ehcache.distribution.CacheManagerPeerProvider;
34  import net.sf.ehcache.distribution.CacheManagerPeerProviderFactory;
35  
36  /**
37   * <a href="LiferayCacheManagerPeerProviderFactory.java.html"><b><i>View Source
38   * </i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class LiferayCacheManagerPeerProviderFactory
43      extends CacheManagerPeerProviderFactory {
44  
45      public LiferayCacheManagerPeerProviderFactory() {
46          String className =
47              PropsValues.EHCACHE_CACHE_MANAGER_PEER_PROVIDER_FACTORY;
48  
49          if (_log.isDebugEnabled()) {
50              _log.debug("Instantiating " + className + " " + this.hashCode());
51          }
52  
53          try {
54              _cacheManagerPeerProviderFactory =
55                  (CacheManagerPeerProviderFactory)InstanceFactory.newInstance(
56                      className);
57          }
58          catch (Exception e) {
59              throw new RuntimeException(e);
60          }
61      }
62  
63      public CacheManagerPeerProvider createCachePeerProvider(
64          CacheManager cacheManager, Properties properties) {
65  
66          String portalPropertyKey = properties.getProperty("portalPropertyKey");
67  
68          if (Validator.isNull(portalPropertyKey)) {
69              throw new RuntimeException("portalPropertyKey is null");
70          }
71  
72          Properties propsUtilProperties = PropsUtil.getProperties();
73  
74          String portalPropertiesString = propsUtilProperties.getProperty(
75              portalPropertyKey);
76  
77          if (_log.isInfoEnabled()) {
78              _log.info(
79                  "portalPropertyKey " + portalPropertyKey + " has value " +
80                      portalPropertiesString);
81          }
82  
83          portalPropertiesString = StringUtil.replace(
84              portalPropertiesString, CharPool.COMMA, CharPool.NEW_LINE);
85  
86          Properties portalProperties = null;
87  
88          try {
89              portalProperties = PropertiesUtil.load(
90                  portalPropertiesString);
91          }
92          catch (IOException ioe) {
93              _log.error(ioe, ioe);
94  
95              throw new RuntimeException(ioe.getMessage());
96          }
97  
98          Object[] keys = portalProperties.keySet().toArray();
99  
100         for (Object key : keys) {
101             String value = (String)portalProperties.remove(key);
102 
103             value = _htmlUtil.unescape(value);
104 
105             portalProperties.put(key, value);
106         }
107 
108         if (_log.isDebugEnabled()) {
109             _log.debug(PropertiesUtil.list(portalProperties));
110         }
111 
112         return _cacheManagerPeerProviderFactory.createCachePeerProvider(
113             cacheManager, portalProperties);
114     }
115 
116     private static Log _log = LogFactoryUtil.getLog(
117         LiferayCacheManagerPeerProviderFactory.class);
118 
119     private static HtmlImpl _htmlUtil = new HtmlImpl();
120 
121     private CacheManagerPeerProviderFactory _cacheManagerPeerProviderFactory;
122 
123 }