001    /**
002     * Copyright (c) 2000-2011 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.CharPool;
020    import com.liferay.portal.kernel.util.InstanceFactory;
021    import com.liferay.portal.kernel.util.PropertiesUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.util.HtmlImpl;
025    import com.liferay.portal.util.PropsUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.io.IOException;
029    
030    import java.util.Properties;
031    
032    import net.sf.ehcache.CacheManager;
033    import net.sf.ehcache.distribution.CacheManagerPeerProvider;
034    import net.sf.ehcache.distribution.CacheManagerPeerProviderFactory;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class LiferayCacheManagerPeerProviderFactory
040            extends CacheManagerPeerProviderFactory {
041    
042            public LiferayCacheManagerPeerProviderFactory() {
043                    String className =
044                            PropsValues.EHCACHE_CACHE_MANAGER_PEER_PROVIDER_FACTORY;
045    
046                    if (_log.isDebugEnabled()) {
047                            _log.debug("Instantiating " + className + " " + this.hashCode());
048                    }
049    
050                    try {
051                            _cacheManagerPeerProviderFactory =
052                                    (CacheManagerPeerProviderFactory)InstanceFactory.newInstance(
053                                            className);
054                    }
055                    catch (Exception e) {
056                            throw new RuntimeException(e);
057                    }
058            }
059    
060            @Override
061            public CacheManagerPeerProvider createCachePeerProvider(
062                    CacheManager cacheManager, Properties properties) {
063    
064                    String portalPropertyKey = properties.getProperty("portalPropertyKey");
065    
066                    if (Validator.isNull(portalPropertyKey)) {
067                            throw new RuntimeException("portalPropertyKey is null");
068                    }
069    
070                    Properties propsUtilProperties = PropsUtil.getProperties();
071    
072                    String portalPropertiesString = propsUtilProperties.getProperty(
073                            portalPropertyKey);
074    
075                    if (_log.isInfoEnabled()) {
076                            _log.info(
077                                    "portalPropertyKey " + portalPropertyKey + " has value " +
078                                            portalPropertiesString);
079                    }
080    
081                    portalPropertiesString = StringUtil.replace(
082                            portalPropertiesString, CharPool.COMMA, CharPool.NEW_LINE);
083    
084                    Properties portalProperties = null;
085    
086                    try {
087                            portalProperties = PropertiesUtil.load(
088                                    portalPropertiesString);
089                    }
090                    catch (IOException ioe) {
091                            _log.error(ioe, ioe);
092    
093                            throw new RuntimeException(ioe.getMessage());
094                    }
095    
096                    Object[] keys = portalProperties.keySet().toArray();
097    
098                    for (Object key : keys) {
099                            String value = (String)portalProperties.remove(key);
100    
101                            value = _htmlUtil.unescape(value);
102    
103                            portalProperties.put(key, value);
104                    }
105    
106                    if (_log.isDebugEnabled()) {
107                            _log.debug(PropertiesUtil.list(portalProperties));
108                    }
109    
110                    return _cacheManagerPeerProviderFactory.createCachePeerProvider(
111                            cacheManager, portalProperties);
112            }
113    
114            private static Log _log = LogFactoryUtil.getLog(
115                    LiferayCacheManagerPeerProviderFactory.class);
116    
117            private static HtmlImpl _htmlUtil = new HtmlImpl();
118    
119            private CacheManagerPeerProviderFactory _cacheManagerPeerProviderFactory;
120    
121    }