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