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.PropsUtil;
025    import com.liferay.portal.util.PropsValues;
026    
027    import java.io.IOException;
028    
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 + " " + this.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            public CacheManagerPeerProvider createCachePeerProvider(
060                    CacheManager cacheManager, Properties properties) {
061    
062                    String portalPropertyKey = properties.getProperty("portalPropertyKey");
063    
064                    if (Validator.isNull(portalPropertyKey)) {
065                            throw new RuntimeException("portalPropertyKey is null");
066                    }
067    
068                    String portalPropertiesString = PropsUtil.getProperties().getProperty(
069                            portalPropertyKey);
070    
071                    if (_log.isInfoEnabled()) {
072                            _log.info(
073                                    "portalPropertyKey " + portalPropertyKey + " has value " +
074                                            portalPropertiesString);
075                    }
076    
077                    portalPropertiesString = StringUtil.replace(
078                            portalPropertiesString, StringPool.COMMA, StringPool.NEW_LINE);
079    
080                    Properties portalProperties = null;
081    
082                    try {
083                            portalProperties = PropertiesUtil.load(
084                                    portalPropertiesString);
085                    }
086                    catch (IOException ioe) {
087                            _log.error(ioe, ioe);
088    
089                            throw new RuntimeException(ioe.getMessage());
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 CacheManagerPeerProviderFactory _cacheManagerPeerProviderFactory;
104    
105    }