001    /**
002     * Copyright (c) 2000-present 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.configurator.impl;
016    
017    import com.liferay.portal.cache.configurator.PortalCacheConfigurator;
018    import com.liferay.portal.dao.orm.hibernate.region.LiferayEhcacheRegionFactory;
019    import com.liferay.portal.dao.orm.hibernate.region.SingletonLiferayEhcacheRegionFactory;
020    import com.liferay.portal.kernel.cache.PortalCacheManager;
021    import com.liferay.portal.kernel.cache.PortalCacheProvider;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.AggregateClassLoader;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.util.ClassLoaderUtil;
027    
028    import java.io.Serializable;
029    
030    import java.net.URL;
031    
032    import java.util.Collection;
033    
034    /**
035     * @author Miguel Pastor
036     */
037    public class PortalCacheConfiguratorImpl implements PortalCacheConfigurator {
038    
039            @Override
040            public void reconfigureCaches(ClassLoader classLoader, URL url)
041                    throws Exception {
042    
043                    if (Validator.isNull(url)) {
044                            return;
045                    }
046    
047                    ClassLoader aggregateClassLoader =
048                            AggregateClassLoader.getAggregateClassLoader(
049                                    new ClassLoader[] {
050                                            ClassLoaderUtil.getPortalClassLoader(), classLoader
051                                    });
052    
053                    ClassLoader contextClassLoader =
054                            ClassLoaderUtil.getContextClassLoader();
055    
056                    try {
057                            ClassLoaderUtil.setContextClassLoader(aggregateClassLoader);
058    
059                            Collection<PortalCacheManager<? extends Serializable, ?>>
060                                    portalCacheManagers =
061                                            PortalCacheProvider.getPortalCacheManagers();
062    
063                            for (PortalCacheManager<? extends Serializable, ?>
064                                            portalCacheManager : portalCacheManagers) {
065    
066                                    if (_log.isInfoEnabled()) {
067                                            _log.info(
068                                                    "Reconfiguring caches in cache manager " +
069                                                            portalCacheManager.getName() + " using " + url);
070                                    }
071    
072                                    portalCacheManager.reconfigureCaches(url);
073                            }
074                    }
075                    finally {
076                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
077                    }
078            }
079    
080            @Override
081            public void reconfigureHibernateCache(URL url) {
082                    if (Validator.isNull(url)) {
083                            return;
084                    }
085    
086                    LiferayEhcacheRegionFactory liferayEhcacheRegionFactory =
087                            SingletonLiferayEhcacheRegionFactory.getInstance();
088    
089                    if (_log.isInfoEnabled()) {
090                            _log.info("Reconfiguring Hibernate caches using " + url);
091                    }
092    
093                    liferayEhcacheRegionFactory.reconfigureCaches(url);
094            }
095    
096            private static final Log _log = LogFactoryUtil.getLog(
097                    PortalCacheConfiguratorImpl.class);
098    
099    }