001    /**
002     * Copyright (c) 2000-2010 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.InstanceFactory;
020    import com.liferay.portal.kernel.util.PropertiesUtil;
021    import com.liferay.portal.kernel.util.StringPool;
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            public CacheManagerPeerProvider createCachePeerProvider(
061                    CacheManager cacheManager, Properties properties) {
062    
063                    String portalPropertyKey = properties.getProperty("portalPropertyKey");
064    
065                    if (Validator.isNull(portalPropertyKey)) {
066                            throw new RuntimeException("portalPropertyKey is null");
067                    }
068    
069                    Properties propsUtilProperties = PropsUtil.getProperties();
070    
071                    String portalPropertiesString = propsUtilProperties.getProperty(
072                            portalPropertyKey);
073    
074                    if (_log.isInfoEnabled()) {
075                            _log.info(
076                                    "portalPropertyKey " + portalPropertyKey + " has value " +
077                                            portalPropertiesString);
078                    }
079    
080                    portalPropertiesString = StringUtil.replace(
081                            portalPropertiesString, StringPool.COMMA, StringPool.NEW_LINE);
082    
083                    Properties portalProperties = null;
084    
085                    try {
086                            portalProperties = PropertiesUtil.load(
087                                    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    }