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.kernel.executor;
016    
017    import com.liferay.portal.kernel.concurrent.ThreadPoolExecutor;
018    import com.liferay.portal.kernel.security.pacl.PACLConstants;
019    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
020    
021    import java.util.concurrent.Callable;
022    import java.util.concurrent.ExecutionException;
023    import java.util.concurrent.Future;
024    import java.util.concurrent.TimeUnit;
025    import java.util.concurrent.TimeoutException;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class PortalExecutorManagerUtil {
031    
032            public static <T> Future<T> execute(String name, Callable<T> callable) {
033                    PortalRuntimePermission.checkThreadPoolExecutor(name);
034    
035                    return getPortalExecutorManager().execute(name, callable);
036            }
037    
038            public static <T> T execute(
039                            String name, Callable<T> callable, long timeout, TimeUnit timeUnit)
040                    throws ExecutionException, InterruptedException, TimeoutException {
041    
042                    PortalRuntimePermission.checkThreadPoolExecutor(name);
043    
044                    return getPortalExecutorManager().execute(
045                            name, callable, timeout, timeUnit);
046            }
047    
048            public static ThreadPoolExecutor getPortalExecutor(String name) {
049                    PortalRuntimePermission.checkThreadPoolExecutor(name);
050    
051                    return getPortalExecutorManager().getPortalExecutor(name);
052            }
053    
054            public static ThreadPoolExecutor getPortalExecutor(
055                    String name, boolean createIfAbsent) {
056    
057                    PortalRuntimePermission.checkThreadPoolExecutor(name);
058    
059                    return getPortalExecutorManager().getPortalExecutor(
060                            name, createIfAbsent);
061            }
062    
063            public static PortalExecutorManager getPortalExecutorManager() {
064                    PortalRuntimePermission.checkGetBeanProperty(
065                            PortalExecutorManagerUtil.class);
066    
067                    return _portalExecutorManager;
068            }
069    
070            public static ThreadPoolExecutor registerPortalExecutor(
071                    String name, ThreadPoolExecutor threadPoolExecutor) {
072    
073                    PortalRuntimePermission.checkThreadPoolExecutor(name);
074    
075                    return getPortalExecutorManager().registerPortalExecutor(
076                            name, threadPoolExecutor);
077            }
078    
079            public static void shutdown() {
080                    PortalRuntimePermission.checkThreadPoolExecutor(
081                            PACLConstants.PORTAL_RUNTIME_PERMISSION_THREAD_POOL_ALL_EXECUTORS);
082    
083                    getPortalExecutorManager().shutdown();
084            }
085    
086            public static void shutdown(boolean interrupt) {
087                    PortalRuntimePermission.checkThreadPoolExecutor(
088                            PACLConstants.PORTAL_RUNTIME_PERMISSION_THREAD_POOL_ALL_EXECUTORS);
089    
090                    getPortalExecutorManager().shutdown(interrupt);
091            }
092    
093            public static void shutdown(String name) {
094                    PortalRuntimePermission.checkThreadPoolExecutor(name);
095    
096                    getPortalExecutorManager().shutdown(name);
097            }
098    
099            public static void shutdown(String name, boolean interrupt) {
100                    PortalRuntimePermission.checkThreadPoolExecutor(name);
101    
102                    getPortalExecutorManager().shutdown(name, interrupt);
103            }
104    
105            public void setPortalExecutorManager(
106                    PortalExecutorManager portalExecutorManager) {
107    
108                    PortalRuntimePermission.checkSetBeanProperty(getClass());
109    
110                    _portalExecutorManager = portalExecutorManager;
111            }
112    
113            private static PortalExecutorManager _portalExecutorManager;
114    
115    }