001    /**
002     * Copyright (c) 2000-2012 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.executor;
016    
017    import com.liferay.portal.kernel.concurrent.RejectedExecutionHandler;
018    import com.liferay.portal.kernel.concurrent.ThreadPoolExecutor;
019    import com.liferay.portal.kernel.concurrent.ThreadPoolHandler;
020    import com.liferay.portal.kernel.executor.PortalExecutorFactory;
021    import com.liferay.portal.kernel.util.NamedThreadFactory;
022    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
023    
024    import java.util.concurrent.ThreadFactory;
025    import java.util.concurrent.TimeUnit;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class PortalExecutorFactoryImpl implements PortalExecutorFactory {
031    
032            public void afterPropertiesSet() {
033                    if (_corePoolSize < 0) {
034                            throw new IllegalArgumentException("Core pool size is less than 0");
035                    }
036    
037                    if (_keepAliveTime < 0) {
038                            throw new IllegalArgumentException(
039                                    "Keep alive time is less than 0");
040                    }
041    
042                    if (_maxPoolSize <= 0) {
043                            throw new IllegalArgumentException(
044                                    "Max pool size is less than or equal to 0");
045                    }
046    
047                    if (_maxPoolSize < _corePoolSize) {
048                            throw new IllegalArgumentException(
049                                    "Max pool size is less than core pool size");
050                    }
051    
052                    if (_maxQueueSize <= 0) {
053                            throw new IllegalArgumentException(
054                                    "Max queue size is less than or equal to 0");
055                    }
056    
057                    if (_rejectedExecutionHandler == null) {
058                            throw new IllegalArgumentException(
059                                    "Rejected execution handler is null");
060                    }
061    
062                    if (_threadPoolHandler == null) {
063                            throw new IllegalArgumentException("Thread pool handler is null");
064                    }
065    
066                    if (_timeUnit == null) {
067                            throw new IllegalArgumentException("Time unit is null");
068                    }
069            }
070    
071            public ThreadPoolExecutor createPortalExecutor(String executorName) {
072                    ThreadFactory threadFactory = new NamedThreadFactory(
073                            executorName, Thread.NORM_PRIORITY,
074                            PortalClassLoaderUtil.getClassLoader());
075    
076                    return new ThreadPoolExecutor(
077                            _corePoolSize, _maxPoolSize, _keepAliveTime, _timeUnit,
078                            _allowCoreThreadTimeout, _maxQueueSize, _rejectedExecutionHandler,
079                            threadFactory, _threadPoolHandler);
080            }
081    
082            public void setAllowCoreThreadTimeout(boolean allowCoreThreadTimeout) {
083                    _allowCoreThreadTimeout = allowCoreThreadTimeout;
084            }
085    
086            public void setCorePoolSize(int corePoolSize) {
087                    _corePoolSize = corePoolSize;
088            }
089    
090            public void setKeepAliveTime(long keepAliveTime) {
091                    _keepAliveTime = keepAliveTime;
092            }
093    
094            public void setMaxPoolSize(int maxPoolSize) {
095                    _maxPoolSize = maxPoolSize;
096            }
097    
098            public void setMaxQueueSize(int maxQueueSize) {
099                    _maxQueueSize = maxQueueSize;
100            }
101    
102            public void setRejectedExecutionHandler(
103                    RejectedExecutionHandler rejectedExecutionHandler) {
104    
105                    _rejectedExecutionHandler = rejectedExecutionHandler;
106            }
107    
108            public void setThreadPoolHandler(ThreadPoolHandler threadPoolHandler) {
109                    _threadPoolHandler = threadPoolHandler;
110            }
111    
112            public void setTimeUnit(TimeUnit timeUnit) {
113                    _timeUnit = timeUnit;
114            }
115    
116            private boolean _allowCoreThreadTimeout;
117            private int _corePoolSize;
118            private long _keepAliveTime;
119            private int _maxPoolSize;
120            private int _maxQueueSize;
121            private RejectedExecutionHandler _rejectedExecutionHandler;
122            private ThreadPoolHandler _threadPoolHandler;
123            private TimeUnit _timeUnit;
124    
125    }