001    /**
002     * Copyright (c) 2000-2013 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 com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
019    import com.liferay.portal.kernel.util.MethodHandler;
020    
021    import java.util.concurrent.Future;
022    
023    /**
024     * @author Michael C. Han
025     */
026    public class ClusterMasterExecutorUtil {
027    
028            public static <T> Future<T> executeOnMaster(MethodHandler methodHandler)
029                    throws SystemException {
030    
031                    ClusterMasterExecutor clusterMasterExecutor =
032                            getClusterMasterExecutor();
033    
034                    if (clusterMasterExecutor == null) {
035                            return null;
036                    }
037    
038                    return _clusterMasterExecutor.executeOnMaster(methodHandler);
039            }
040    
041            public static ClusterMasterExecutor getClusterMasterExecutor() {
042                    return _clusterMasterExecutor;
043            }
044    
045            public static void initialize() {
046                    ClusterMasterExecutor clusterMasterExecutor =
047                            getClusterMasterExecutor();
048    
049                    if (clusterMasterExecutor == null) {
050                            return;
051                    }
052    
053                    _clusterMasterExecutor.initialize();
054            }
055    
056            public static boolean isMaster() {
057                    ClusterMasterExecutor clusterMasterExecutor =
058                            getClusterMasterExecutor();
059    
060                    if (clusterMasterExecutor == null) {
061                            return false;
062                    }
063    
064                    return _clusterMasterExecutor.isMaster();
065            }
066    
067            public static void registerClusterMasterTokenTransitionListener(
068                    ClusterMasterTokenTransitionListener
069                            clusterMasterTokenTransitionListener) {
070    
071                    ClusterMasterExecutor clusterMasterExecutor =
072                            getClusterMasterExecutor();
073    
074                    if (clusterMasterExecutor == null) {
075                            return;
076                    }
077    
078                    _clusterMasterExecutor.registerClusterMasterTokenTransitionListener(
079                            clusterMasterTokenTransitionListener);
080            }
081    
082            public static void unregisterClusterMasterTokenTransitionListener(
083                    ClusterMasterTokenTransitionListener
084                            clusterMasterTokenTransitionListener) {
085    
086                    ClusterMasterExecutor clusterMasterExecutor =
087                            getClusterMasterExecutor();
088    
089                    if (clusterMasterExecutor == null) {
090                            return;
091                    }
092    
093                    _clusterMasterExecutor.unregisterClusterMasterTokenTransitionListener(
094                            clusterMasterTokenTransitionListener);
095            }
096    
097            public void setClusterMasterExecutor(
098                    ClusterMasterExecutor clusterMasterExecutor) {
099    
100                    PortalRuntimePermission.checkSetBeanProperty(getClass());
101    
102                    _clusterMasterExecutor = clusterMasterExecutor;
103            }
104    
105            private static ClusterMasterExecutor _clusterMasterExecutor;
106    
107    }