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 com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
020    import com.liferay.portal.kernel.util.ProxyFactory;
021    
022    import java.util.Collections;
023    import java.util.List;
024    
025    /**
026     * @author Tina Tian
027     * @author Raymond Aug??
028     */
029    public class ClusterExecutorUtil {
030    
031            public static void addClusterEventListener(
032                    ClusterEventListener clusterEventListener) {
033    
034                    ClusterExecutor clusterExecutor = getClusterExecutor();
035    
036                    if (clusterExecutor == null) {
037                            return;
038                    }
039    
040                    clusterExecutor.addClusterEventListener(clusterEventListener);
041            }
042    
043            public static FutureClusterResponses execute(
044                    ClusterRequest clusterRequest) {
045    
046                    ClusterExecutor clusterExecutor = getClusterExecutor();
047    
048                    if (clusterExecutor == null) {
049                            return null;
050                    }
051    
052                    return clusterExecutor.execute(clusterRequest);
053            }
054    
055            public static ClusterExecutor getClusterExecutor() {
056                    PortalRuntimePermission.checkGetBeanProperty(ClusterExecutorUtil.class);
057    
058                    if ((_clusterExecutor == null) || !_clusterExecutor.isEnabled()) {
059                            if (_log.isWarnEnabled()) {
060                                    _log.warn("ClusterExecutorUtil is not initialized");
061                            }
062    
063                            return null;
064                    }
065    
066                    return _clusterExecutor;
067            }
068    
069            public static List<ClusterNode> getClusterNodes() {
070                    ClusterExecutor clusterExecutor = getClusterExecutor();
071    
072                    if (clusterExecutor == null) {
073                            return Collections.emptyList();
074                    }
075    
076                    return clusterExecutor.getClusterNodes();
077            }
078    
079            public static ClusterNode getLocalClusterNode() {
080                    ClusterExecutor clusterExecutor = getClusterExecutor();
081    
082                    if (clusterExecutor == null) {
083                            return null;
084                    }
085    
086                    return clusterExecutor.getLocalClusterNode();
087            }
088    
089            public static boolean isClusterNodeAlive(String clusterNodeId) {
090                    ClusterExecutor clusterExecutor = getClusterExecutor();
091    
092                    if (clusterExecutor == null) {
093                            return false;
094                    }
095    
096                    return clusterExecutor.isClusterNodeAlive(clusterNodeId);
097            }
098    
099            public static boolean isEnabled() {
100                    ClusterExecutor clusterExecutor = getClusterExecutor();
101    
102                    if (clusterExecutor == null) {
103                            return false;
104                    }
105    
106                    return true;
107            }
108    
109            public static void removeClusterEventListener(
110                    ClusterEventListener clusterEventListener) {
111    
112                    ClusterExecutor clusterExecutor = getClusterExecutor();
113    
114                    if (clusterExecutor == null) {
115                            return;
116                    }
117    
118                    clusterExecutor.removeClusterEventListener(clusterEventListener);
119            }
120    
121            private static final Log _log = LogFactoryUtil.getLog(
122                    ClusterExecutorUtil.class);
123    
124            private static final ClusterExecutor _clusterExecutor =
125                    ProxyFactory.newServiceTrackedInstance(ClusterExecutor.class);
126    
127    }