001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 + " " + 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(portalPropertiesString);
088                    }
089                    catch (IOException ioe) {
090                            _log.error(ioe, ioe);
091    
092                            throw new RuntimeException(ioe.getMessage());
093                    }
094    
095                    Object[] keys = portalProperties.keySet().toArray();
096    
097                    for (Object key : keys) {
098                            String value = (String)portalProperties.remove(key);
099    
100                            value = _htmlUtil.unescape(value);
101    
102                            portalProperties.put(key, value);
103                    }
104    
105                    if (_log.isDebugEnabled()) {
106                            _log.debug(PropertiesUtil.list(portalProperties));
107                    }
108    
109                    return _cacheManagerPeerProviderFactory.createCachePeerProvider(
110                            cacheManager, portalProperties);
111            }
112    
113            private static Log _log = LogFactoryUtil.getLog(
114                    LiferayCacheManagerPeerProviderFactory.class);
115    
116            private static HtmlImpl _htmlUtil = new HtmlImpl();
117    
118            private CacheManagerPeerProviderFactory _cacheManagerPeerProviderFactory;
119    
120    }