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.util.JavaDetector;
018    import com.liferay.portal.kernel.util.ReflectionUtil;
019    import com.liferay.portal.util.PropsValues;
020    
021    import java.lang.reflect.Field;
022    
023    import java.util.concurrent.BlockingQueue;
024    import java.util.concurrent.DelayQueue;
025    import java.util.concurrent.RunnableScheduledFuture;
026    import java.util.concurrent.ScheduledThreadPoolExecutor;
027    import java.util.concurrent.ThreadPoolExecutor;
028    
029    import net.sf.ehcache.CacheManager;
030    import net.sf.ehcache.config.Configuration;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class CacheManagerUtil {
036    
037            public static CacheManager createCacheManager(Configuration configuration) {
038                    CacheManager cacheManager = new CacheManager(configuration);
039    
040                    try {
041                            ScheduledThreadPoolExecutor scheduledThreadPoolExecutor =
042                                    (ScheduledThreadPoolExecutor)_statisticsExecutorField.get(
043                                            cacheManager);
044    
045                            BlockingQueue<Runnable> blockingQueue = null;
046    
047                            // This odd logic is a workaround for
048                            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6522773
049    
050                            if (JavaDetector.isJDK6()) {
051                                    blockingQueue = (BlockingQueue<Runnable>)_workQueueField.get(
052                                            scheduledThreadPoolExecutor);;
053    
054                                    _workQueueField.set(
055                                            scheduledThreadPoolExecutor,
056                                            new DelayQueue<RunnableScheduledFuture<?>>() {
057    
058                                                    @Override
059                                                    public int remainingCapacity() {
060                                                            return 0;
061                                                    }
062    
063                                            });
064                            }
065    
066                            scheduledThreadPoolExecutor.setCorePoolSize(
067                                    PropsValues.EHCACHE_CACHE_MANAGER_STATISTICS_THREAD_POOL_SIZE);
068    
069                            if (JavaDetector.isJDK6()) {
070                                    while (
071                                            scheduledThreadPoolExecutor.getPoolSize() >
072                                                    PropsValues.
073                                                            EHCACHE_CACHE_MANAGER_STATISTICS_THREAD_POOL_SIZE);
074    
075                                    _workQueueField.set(scheduledThreadPoolExecutor, blockingQueue);
076                            }
077                    }
078                    catch (Exception e) {
079                            throw new RuntimeException(e);
080                    }
081    
082                    return cacheManager;
083            }
084    
085            private static Field _statisticsExecutorField;
086            private static Field _workQueueField;
087    
088            static {
089                    try {
090                            _statisticsExecutorField = ReflectionUtil.getDeclaredField(
091                                    CacheManager.class, "statisticsExecutor");
092    
093                            if (JavaDetector.isJDK6()) {
094                                    _workQueueField = ReflectionUtil.getDeclaredField(
095                                            ThreadPoolExecutor.class, "workQueue");
096                            }
097                    }
098                    catch (Exception e) {
099                            throw new ExceptionInInitializerError(e);
100                    }
101            }
102    
103    }