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