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 isEnabled() {
057                    ClusterMasterExecutor clusterMasterExecutor =
058                            getClusterMasterExecutor();
059    
060                    if (clusterMasterExecutor == null) {
061                            return false;
062                    }
063    
064                    return _clusterMasterExecutor.isEnabled();
065            }
066    
067            public static boolean isMaster() {
068                    ClusterMasterExecutor clusterMasterExecutor =
069                            getClusterMasterExecutor();
070    
071                    if (clusterMasterExecutor == null) {
072                            return false;
073                    }
074    
075                    return _clusterMasterExecutor.isMaster();
076            }
077    
078            public static void registerClusterMasterTokenTransitionListener(
079                    ClusterMasterTokenTransitionListener
080                            clusterMasterTokenTransitionListener) {
081    
082                    ClusterMasterExecutor clusterMasterExecutor =
083                            getClusterMasterExecutor();
084    
085                    if (clusterMasterExecutor == null) {
086                            return;
087                    }
088    
089                    _clusterMasterExecutor.registerClusterMasterTokenTransitionListener(
090                            clusterMasterTokenTransitionListener);
091            }
092    
093            public static void unregisterClusterMasterTokenTransitionListener(
094                    ClusterMasterTokenTransitionListener
095                            clusterMasterTokenTransitionListener) {
096    
097                    ClusterMasterExecutor clusterMasterExecutor =
098                            getClusterMasterExecutor();
099    
100                    if (clusterMasterExecutor == null) {
101                            return;
102                    }
103    
104                    _clusterMasterExecutor.unregisterClusterMasterTokenTransitionListener(
105                            clusterMasterTokenTransitionListener);
106            }
107    
108            public void setClusterMasterExecutor(
109                    ClusterMasterExecutor clusterMasterExecutor) {
110    
111                    PortalRuntimePermission.checkSetBeanProperty(getClass());
112    
113                    _clusterMasterExecutor = clusterMasterExecutor;
114            }
115    
116            private static ClusterMasterExecutor _clusterMasterExecutor;
117    
118    }