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.util.Arrays;
029    import java.util.Properties;
030    
031    import net.sf.ehcache.CacheManager;
032    import net.sf.ehcache.distribution.CacheManagerPeerProvider;
033    import net.sf.ehcache.distribution.CacheManagerPeerProviderFactory;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class LiferayCacheManagerPeerProviderFactory
039            extends CacheManagerPeerProviderFactory {
040    
041            public LiferayCacheManagerPeerProviderFactory() {
042                    String className =
043                            PropsValues.EHCACHE_CACHE_MANAGER_PEER_PROVIDER_FACTORY;
044    
045                    if (_log.isDebugEnabled()) {
046                            _log.debug("Instantiating " + className + " " + hashCode());
047                    }
048    
049                    try {
050                            _cacheManagerPeerProviderFactory =
051                                    (CacheManagerPeerProviderFactory)InstanceFactory.newInstance(
052                                            className);
053                    }
054                    catch (Exception e) {
055                            throw new RuntimeException(e);
056                    }
057            }
058    
059            @Override
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                    String[] values = PropsUtil.getArray(portalPropertyKey);
070    
071                    if (_log.isInfoEnabled()) {
072                            _log.info(
073                                    "portalPropertyKey " + portalPropertyKey + " has value " +
074                                            Arrays.toString(values));
075                    }
076    
077                    Properties portalProperties = new Properties();
078    
079                    for (String value : values) {
080                            String[] valueParts = StringUtil.split(value, CharPool.EQUAL);
081    
082                            if (valueParts.length != 2) {
083                                    if (_log.isWarnEnabled()) {
084                                            _log.warn("Ignore malformed value " + value);
085                                    }
086                            }
087    
088                            portalProperties.put(
089                                    valueParts[0], _htmlUtil.unescape(valueParts[1]));
090                    }
091    
092                    if (_log.isDebugEnabled()) {
093                            _log.debug(PropertiesUtil.list(portalProperties));
094                    }
095    
096                    return _cacheManagerPeerProviderFactory.createCachePeerProvider(
097                            cacheManager, portalProperties);
098            }
099    
100            private static Log _log = LogFactoryUtil.getLog(
101                    LiferayCacheManagerPeerProviderFactory.class);
102    
103            private static HtmlImpl _htmlUtil = new HtmlImpl();
104    
105            private CacheManagerPeerProviderFactory _cacheManagerPeerProviderFactory;
106    
107    }