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.messaging.Message;
020    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
021    import com.liferay.registry.Registry;
022    import com.liferay.registry.RegistryUtil;
023    import com.liferay.registry.ServiceTracker;
024    
025    /**
026     * @author Shuyang Zhou
027     * @author Raymond Aug??
028     */
029    public class ClusterLinkUtil {
030    
031            public static Address getAddress(Message message) {
032                    return (Address)message.get(_ADDRESS);
033            }
034    
035            public static ClusterLink getClusterLink() {
036                    PortalRuntimePermission.checkGetBeanProperty(ClusterLinkUtil.class);
037    
038                    ClusterLink clusterLink = _instance._serviceTracker.getService();
039    
040                    if ((clusterLink == null) || !clusterLink.isEnabled()) {
041                            if (_log.isWarnEnabled()) {
042                                    _log.warn("ClusterLinkUtil has not been initialized");
043                            }
044    
045                            return null;
046                    }
047    
048                    return clusterLink;
049            }
050    
051            public static void sendMulticastMessage(
052                    Message message, Priority priority) {
053    
054                    ClusterLink clusterLink = getClusterLink();
055    
056                    if (clusterLink == null) {
057                            return;
058                    }
059    
060                    clusterLink.sendMulticastMessage(message, priority);
061            }
062    
063            public static void sendMulticastMessage(Object payload, Priority priority) {
064                    Message message = new Message();
065    
066                    message.setPayload(payload);
067    
068                    sendMulticastMessage(message, priority);
069            }
070    
071            public static void sendUnicastMessage(
072                    Address address, Message message, Priority priority) {
073    
074                    ClusterLink clusterLink = getClusterLink();
075    
076                    if (clusterLink == null) {
077                            return;
078                    }
079    
080                    clusterLink.sendUnicastMessage(address, message, priority);
081            }
082    
083            public static Message setAddress(Message message, Address address) {
084                    message.put(_ADDRESS, address);
085    
086                    return message;
087            }
088    
089            private ClusterLinkUtil() {
090                    Registry registry = RegistryUtil.getRegistry();
091    
092                    _serviceTracker = registry.trackServices(ClusterLink.class);
093    
094                    _serviceTracker.open();
095            }
096    
097            private static final String _ADDRESS = "CLUSTER_ADDRESS";
098    
099            private static final Log _log = LogFactoryUtil.getLog(
100                    ClusterLinkUtil.class);
101    
102            private static final ClusterLinkUtil _instance = new ClusterLinkUtil();
103    
104            private final ServiceTracker<ClusterLink, ClusterLink> _serviceTracker;
105    
106    }