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    
021    import java.util.Collections;
022    import java.util.List;
023    
024    /**
025     * @author Tina Tian
026     * @author Raymond Aug??
027     */
028    public class ClusterExecutorUtil {
029    
030            public static void addClusterEventListener(
031                    ClusterEventListener clusterEventListener) {
032    
033                    ClusterExecutor clusterExecutor = getClusterExecutor();
034    
035                    if (clusterExecutor == null) {
036                            return;
037                    }
038    
039                    clusterExecutor.addClusterEventListener(clusterEventListener);
040            }
041    
042            public static void destroy() {
043                    ClusterExecutor clusterExecutor = getClusterExecutor();
044    
045                    if (clusterExecutor == null) {
046                            return;
047                    }
048    
049                    clusterExecutor.destroy();
050            }
051    
052            public static FutureClusterResponses execute(
053                    ClusterRequest clusterRequest) {
054    
055                    ClusterExecutor clusterExecutor = getClusterExecutor();
056    
057                    if (clusterExecutor == null) {
058                            return null;
059                    }
060    
061                    return clusterExecutor.execute(clusterRequest);
062            }
063    
064            public static FutureClusterResponses execute(
065                    ClusterRequest clusterRequest,
066                    ClusterResponseCallback clusterResponseCallback) {
067    
068                    ClusterExecutor clusterExecutor = getClusterExecutor();
069    
070                    if (clusterExecutor == null) {
071                            return null;
072                    }
073    
074                    return clusterExecutor.execute(clusterRequest, clusterResponseCallback);
075            }
076    
077            public static ClusterExecutor getClusterExecutor() {
078                    PortalRuntimePermission.checkGetBeanProperty(ClusterExecutorUtil.class);
079    
080                    if ((_clusterExecutor == null) || !_clusterExecutor.isEnabled()) {
081                            if (_log.isWarnEnabled()) {
082                                    _log.warn("ClusterExecutorUtil has not been initialized");
083                            }
084    
085                            return null;
086                    }
087    
088                    return _clusterExecutor;
089            }
090    
091            public static List<Address> getClusterNodeAddresses() {
092                    ClusterExecutor clusterExecutor = getClusterExecutor();
093    
094                    if (clusterExecutor == null) {
095                            return Collections.emptyList();
096                    }
097    
098                    return clusterExecutor.getClusterNodeAddresses();
099            }
100    
101            public static List<ClusterNode> getClusterNodes() {
102                    ClusterExecutor clusterExecutor = getClusterExecutor();
103    
104                    if (clusterExecutor == null) {
105                            return Collections.emptyList();
106                    }
107    
108                    return clusterExecutor.getClusterNodes();
109            }
110    
111            public static ClusterNode getLocalClusterNode() {
112                    ClusterExecutor clusterExecutor = getClusterExecutor();
113    
114                    if (clusterExecutor == null) {
115                            return null;
116                    }
117    
118                    return clusterExecutor.getLocalClusterNode();
119            }
120    
121            public static Address getLocalClusterNodeAddress() {
122                    ClusterExecutor clusterExecutor = getClusterExecutor();
123    
124                    if (clusterExecutor == null) {
125                            return null;
126                    }
127    
128                    return clusterExecutor.getLocalClusterNodeAddress();
129            }
130    
131            public static void initialize() {
132                    ClusterExecutor clusterExecutor = getClusterExecutor();
133    
134                    if (clusterExecutor == null) {
135                            return;
136                    }
137    
138                    clusterExecutor.initialize();
139            }
140    
141            public static boolean isClusterNodeAlive(Address address) {
142                    ClusterExecutor clusterExecutor = getClusterExecutor();
143    
144                    if (clusterExecutor == null) {
145                            return false;
146                    }
147    
148                    return clusterExecutor.isClusterNodeAlive(address);
149            }
150    
151            public static boolean isClusterNodeAlive(String clusterNodeId) {
152                    ClusterExecutor clusterExecutor = getClusterExecutor();
153    
154                    if (clusterExecutor == null) {
155                            return false;
156                    }
157    
158                    return clusterExecutor.isClusterNodeAlive(clusterNodeId);
159            }
160    
161            public static boolean isEnabled() {
162                    ClusterExecutor clusterExecutor = getClusterExecutor();
163    
164                    if (clusterExecutor == null) {
165                            return false;
166                    }
167    
168                    return true;
169            }
170    
171            public static void removeClusterEventListener(
172                    ClusterEventListener clusterEventListener) {
173    
174                    ClusterExecutor clusterExecutor = getClusterExecutor();
175    
176                    if (clusterExecutor == null) {
177                            return;
178                    }
179    
180                    clusterExecutor.removeClusterEventListener(clusterEventListener);
181            }
182    
183            public void setClusterExecutor(ClusterExecutor clusterExecutor) {
184                    PortalRuntimePermission.checkSetBeanProperty(getClass());
185    
186                    _clusterExecutor = clusterExecutor;
187            }
188    
189            private static final Log _log = LogFactoryUtil.getLog(
190                    ClusterExecutorUtil.class);
191    
192            private static ClusterExecutor _clusterExecutor;
193    
194    }