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.ThreadPoolExecutor;
018    import com.liferay.portal.kernel.security.pacl.PACLConstants;
019    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
020    import com.liferay.registry.Registry;
021    import com.liferay.registry.RegistryUtil;
022    import com.liferay.registry.ServiceTracker;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class PortalExecutorManagerUtil {
028    
029            public static ThreadPoolExecutor getPortalExecutor(String name) {
030                    PortalRuntimePermission.checkThreadPoolExecutor(name);
031    
032                    return getPortalExecutorManager().getPortalExecutor(name);
033            }
034    
035            public static ThreadPoolExecutor getPortalExecutor(
036                    String name, boolean createIfAbsent) {
037    
038                    PortalRuntimePermission.checkThreadPoolExecutor(name);
039    
040                    return getPortalExecutorManager().getPortalExecutor(
041                            name, createIfAbsent);
042            }
043    
044            public static PortalExecutorManager getPortalExecutorManager() {
045                    PortalRuntimePermission.checkGetBeanProperty(
046                            PortalExecutorManagerUtil.class);
047    
048                    try {
049                            while (_instance._serviceTracker.getService() == null) {
050                                    Thread.sleep(500);
051                            }
052                    }
053                    catch (InterruptedException e) {
054                    }
055    
056                    return _instance._serviceTracker.getService();
057            }
058    
059            public static ThreadPoolExecutor registerPortalExecutor(
060                    String name, ThreadPoolExecutor threadPoolExecutor) {
061    
062                    PortalRuntimePermission.checkThreadPoolExecutor(name);
063    
064                    return getPortalExecutorManager().registerPortalExecutor(
065                            name, threadPoolExecutor);
066            }
067    
068            public static void shutdown() {
069                    PortalRuntimePermission.checkThreadPoolExecutor(
070                            PACLConstants.PORTAL_RUNTIME_PERMISSION_THREAD_POOL_ALL_EXECUTORS);
071    
072                    getPortalExecutorManager().shutdown();
073            }
074    
075            public static void shutdown(boolean interrupt) {
076                    PortalRuntimePermission.checkThreadPoolExecutor(
077                            PACLConstants.PORTAL_RUNTIME_PERMISSION_THREAD_POOL_ALL_EXECUTORS);
078    
079                    getPortalExecutorManager().shutdown(interrupt);
080            }
081    
082            private PortalExecutorManagerUtil() {
083                    Registry registry = RegistryUtil.getRegistry();
084    
085                    _serviceTracker = registry.trackServices(PortalExecutorManager.class);
086    
087                    _serviceTracker.open();
088            }
089    
090            private static final PortalExecutorManagerUtil _instance =
091                    new PortalExecutorManagerUtil();
092    
093            private final ServiceTracker<PortalExecutorManager, PortalExecutorManager>
094                    _serviceTracker;
095    
096    }