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.util.ReflectionUtil;
018    import com.liferay.portal.util.PropsValues;
019    
020    import java.lang.reflect.Field;
021    
022    import java.util.concurrent.ScheduledThreadPoolExecutor;
023    
024    import net.sf.ehcache.CacheManager;
025    import net.sf.ehcache.config.Configuration;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class CacheManagerUtil {
031    
032            public static CacheManager createCacheManager(Configuration configuration) {
033                    CacheManager cacheManager = new CacheManager(configuration);
034    
035                    try {
036                            ScheduledThreadPoolExecutor scheduledThreadPoolExecutor =
037                                    (ScheduledThreadPoolExecutor)_STATISTICS_EXECUTOR_FIELD.get(
038                                            cacheManager);
039    
040                            scheduledThreadPoolExecutor.setCorePoolSize(
041                                    PropsValues.EHCACHE_CACHE_MANAGER_STATISTICS_THREAD_POOL_SIZE);
042                    }
043                    catch (Exception e) {
044                            throw new RuntimeException(e);
045                    }
046    
047                    return cacheManager;
048            }
049    
050            private static final Field _STATISTICS_EXECUTOR_FIELD;
051    
052            static {
053                    try {
054                            _STATISTICS_EXECUTOR_FIELD = ReflectionUtil.getDeclaredField(
055                                    CacheManager.class, "statisticsExecutor");
056                    }
057                    catch (Exception e) {
058                            throw new ExceptionInInitializerError(e);
059                    }
060            }
061    
062    }