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.ehcache;
016    
017    import com.liferay.portal.kernel.cache.BootstrapLoader;
018    import com.liferay.portal.kernel.cache.CacheListener;
019    import com.liferay.portal.kernel.cache.CacheManagerListener;
020    import com.liferay.portal.kernel.cache.CallbackFactory;
021    import com.liferay.portal.kernel.cache.PortalCacheManager;
022    import com.liferay.portal.kernel.cache.PortalCacheProvider;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.util.InstanceFactory;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.util.ClassLoaderUtil;
028    
029    import java.io.Serializable;
030    
031    import java.util.Properties;
032    
033    import net.sf.ehcache.bootstrap.BootstrapCacheLoaderFactory;
034    import net.sf.ehcache.distribution.CacheReplicator;
035    import net.sf.ehcache.event.CacheEventListener;
036    import net.sf.ehcache.event.CacheEventListenerFactory;
037    import net.sf.ehcache.event.CacheManagerEventListenerFactory;
038    
039    /**
040     * @author Tina Tian
041     */
042    public class EhcacheCallbackFactory implements CallbackFactory {
043    
044            public static final CallbackFactory INSTANCE = new EhcacheCallbackFactory();
045    
046            @Override
047            public BootstrapLoader createBootstrapLoader(Properties properties) {
048                    String className = properties.getProperty(
049                            EhcacheConstants.BOOTSTRAP_CACHE_LOADER_FACTORY_CLASS_NAME);
050    
051                    if (Validator.isNull(className)) {
052                            return null;
053                    }
054    
055                    try {
056                            BootstrapCacheLoaderFactory<?> bootstrapCacheLoaderFactory =
057                                    (BootstrapCacheLoaderFactory<?>)InstanceFactory.newInstance(
058                                            ClassLoaderUtil.getPortalClassLoader(), className);
059    
060                            return new EhcacheBootstrapLoaderAdapter(
061                                    bootstrapCacheLoaderFactory.createBootstrapCacheLoader(
062                                            properties));
063                    }
064                    catch (Exception e) {
065                            _log.error(
066                                    "Unable to instantiate bootstrap cache loader factory " +
067                                            className,
068                                    e);
069    
070                            return null;
071                    }
072            }
073    
074            @Override
075            public <K extends Serializable, V> CacheListener<K, V> createCacheListener(
076                    Properties properties) {
077    
078                    String className = properties.getProperty(
079                            EhcacheConstants.CACHE_EVENT_LISTENER_FACTORY_CLASS_NAME);
080    
081                    if (Validator.isNull(className)) {
082                            return null;
083                    }
084    
085                    try {
086                            CacheEventListenerFactory cacheEventListenerFactory =
087                                    (CacheEventListenerFactory)InstanceFactory.newInstance(
088                                            ClassLoaderUtil.getPortalClassLoader(), className);
089    
090                            CacheEventListener cacheEventListener =
091                                    cacheEventListenerFactory.createCacheEventListener(properties);
092    
093                            if (cacheEventListener instanceof CacheReplicator) {
094                                    return (CacheListener<K, V>)
095                                            new EhcacheCacheReplicatorAdapter<K, Serializable>(
096                                                    cacheEventListener);
097                            }
098    
099                            return new EhcacheCacheListenerAdapter<K, V>(cacheEventListener);
100                    }
101                    catch (Exception e) {
102                            _log.error(
103                                    "Unable to instantiate cache event listener factory " +
104                                            className,
105                                    e);
106    
107                            return null;
108                    }
109            }
110    
111            @Override
112            public CacheManagerListener createCacheManagerListener(
113                    Properties properties) {
114    
115                    String className = properties.getProperty(
116                            EhcacheConstants.CACHE_MANAGER_LISTENER_FACTORY_CLASS_NAME);
117    
118                    if (Validator.isNull(className)) {
119                            return null;
120                    }
121    
122                    String portalCacheManagerName = properties.getProperty(
123                            EhcacheConstants.PORTAL_CACHE_MANAGER_NAME);
124    
125                    if (Validator.isNull(portalCacheManagerName)) {
126                            return null;
127                    }
128    
129                    PortalCacheManager<?, ?> portalCacheManager =
130                            PortalCacheProvider.getPortalCacheManager(portalCacheManagerName);
131    
132                    if (!(portalCacheManager instanceof EhcachePortalCacheManager)) {
133                            throw new IllegalArgumentException(
134                                    "PortalCacheManager with name " + portalCacheManagerName +
135                                            " is not a " + EhcachePortalCacheManager.class.getName());
136                    }
137    
138                    EhcachePortalCacheManager<?, ?> ehcachePortalCacheManager =
139                            (EhcachePortalCacheManager<?, ?>)portalCacheManager;
140    
141                    try {
142                            CacheManagerEventListenerFactory cacheManagerEventListenerFactory =
143                                    (CacheManagerEventListenerFactory)
144                                            InstanceFactory.newInstance(
145                                                    ClassLoaderUtil.getPortalClassLoader(), className);
146    
147                            return new EhcacheCacheManagerListenerAdapter(
148                                    cacheManagerEventListenerFactory.
149                                            createCacheManagerEventListener(
150                                                    ehcachePortalCacheManager.getEhcacheManager(),
151                                                    properties));
152                    }
153                    catch (Exception e) {
154                            _log.error(
155                                    "Unable to instantiate cache manager event listener " +
156                                            "factory " + className,
157                                    e);
158    
159                            return null;
160                    }
161            }
162    
163            private EhcacheCallbackFactory() {
164            }
165    
166            private static final Log _log = LogFactoryUtil.getLog(
167                    EhcacheCallbackFactory.class);
168    
169    }