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.cluster;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.util.MethodHandler;
020    import com.liferay.registry.Registry;
021    import com.liferay.registry.RegistryUtil;
022    import com.liferay.registry.ServiceTracker;
023    
024    import java.util.concurrent.Future;
025    
026    /**
027     * @author Michael C. Han
028     */
029    @ProviderType
030    public class ClusterMasterExecutorUtil {
031    
032            public static void addClusterMasterTokenTransitionListener(
033                    ClusterMasterTokenTransitionListener
034                            clusterMasterTokenTransitionListener) {
035    
036                    ClusterMasterExecutor clusterMasterExecutor =
037                            getClusterMasterExecutor();
038    
039                    if (clusterMasterExecutor == null) {
040                            return;
041                    }
042    
043                    clusterMasterExecutor.addClusterMasterTokenTransitionListener(
044                            clusterMasterTokenTransitionListener);
045            }
046    
047            public static <T> Future<T> executeOnMaster(MethodHandler methodHandler) {
048                    ClusterMasterExecutor clusterMasterExecutor =
049                            getClusterMasterExecutor();
050    
051                    if (clusterMasterExecutor == null) {
052                            return null;
053                    }
054    
055                    return clusterMasterExecutor.executeOnMaster(methodHandler);
056            }
057    
058            public static ClusterMasterExecutor getClusterMasterExecutor() {
059                    return _instance._serviceTracker.getService();
060            }
061    
062            public static boolean isEnabled() {
063                    ClusterMasterExecutor clusterMasterExecutor =
064                            getClusterMasterExecutor();
065    
066                    if (clusterMasterExecutor == null) {
067                            return false;
068                    }
069    
070                    return clusterMasterExecutor.isEnabled();
071            }
072    
073            public static boolean isMaster() {
074                    ClusterMasterExecutor clusterMasterExecutor =
075                            getClusterMasterExecutor();
076    
077                    if (clusterMasterExecutor == null) {
078                            return false;
079                    }
080    
081                    return clusterMasterExecutor.isMaster();
082            }
083    
084            public static void removeClusterMasterTokenTransitionListener(
085                    ClusterMasterTokenTransitionListener
086                            clusterMasterTokenTransitionListener) {
087    
088                    ClusterMasterExecutor clusterMasterExecutor =
089                            getClusterMasterExecutor();
090    
091                    if (clusterMasterExecutor == null) {
092                            return;
093                    }
094    
095                    clusterMasterExecutor.removeClusterMasterTokenTransitionListener(
096                            clusterMasterTokenTransitionListener);
097            }
098    
099            private ClusterMasterExecutorUtil() {
100                    Registry registry = RegistryUtil.getRegistry();
101    
102                    _serviceTracker = registry.trackServices(ClusterMasterExecutor.class);
103    
104                    _serviceTracker.open();
105            }
106    
107            private static final ClusterMasterExecutorUtil _instance =
108                    new ClusterMasterExecutorUtil();
109    
110            private final ServiceTracker<ClusterMasterExecutor, ClusterMasterExecutor>
111                    _serviceTracker;
112    
113    }