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