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