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.portal.kernel.util.ProxyFactory;
021    
022    import java.util.concurrent.Future;
023    
024    /**
025     * @author Michael C. Han
026     */
027    @ProviderType
028    public class ClusterMasterExecutorUtil {
029    
030            public static void addClusterMasterTokenTransitionListener(
031                    ClusterMasterTokenTransitionListener
032                            clusterMasterTokenTransitionListener) {
033    
034                    ClusterMasterExecutor clusterMasterExecutor =
035                            getClusterMasterExecutor();
036    
037                    if (clusterMasterExecutor == null) {
038                            return;
039                    }
040    
041                    clusterMasterExecutor.addClusterMasterTokenTransitionListener(
042                            clusterMasterTokenTransitionListener);
043            }
044    
045            public static <T> Future<T> executeOnMaster(MethodHandler methodHandler) {
046                    ClusterMasterExecutor clusterMasterExecutor =
047                            getClusterMasterExecutor();
048    
049                    if (clusterMasterExecutor == null) {
050                            return null;
051                    }
052    
053                    return clusterMasterExecutor.executeOnMaster(methodHandler);
054            }
055    
056            public static ClusterMasterExecutor getClusterMasterExecutor() {
057                    return _instance;
058            }
059    
060            public static boolean isEnabled() {
061                    ClusterMasterExecutor clusterMasterExecutor =
062                            getClusterMasterExecutor();
063    
064                    if (clusterMasterExecutor == null) {
065                            return false;
066                    }
067    
068                    return clusterMasterExecutor.isEnabled();
069            }
070    
071            public static boolean isMaster() {
072                    ClusterMasterExecutor clusterMasterExecutor =
073                            getClusterMasterExecutor();
074    
075                    if (clusterMasterExecutor == null) {
076                            return false;
077                    }
078    
079                    return clusterMasterExecutor.isMaster();
080            }
081    
082            public static void removeClusterMasterTokenTransitionListener(
083                    ClusterMasterTokenTransitionListener
084                            clusterMasterTokenTransitionListener) {
085    
086                    ClusterMasterExecutor clusterMasterExecutor =
087                            getClusterMasterExecutor();
088    
089                    if (clusterMasterExecutor == null) {
090                            return;
091                    }
092    
093                    clusterMasterExecutor.removeClusterMasterTokenTransitionListener(
094                            clusterMasterTokenTransitionListener);
095            }
096    
097            private static final ClusterMasterExecutor _instance =
098                    ProxyFactory.newServiceTrackedInstance(ClusterMasterExecutor.class);
099    
100    }