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