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.portal.kernel.util.ProxyFactory;
022    
023    /**
024     * @author Shuyang Zhou
025     * @author Raymond Aug??
026     */
027    public class ClusterLinkUtil {
028    
029            public static Address getAddress(Message message) {
030                    return (Address)message.get(_ADDRESS);
031            }
032    
033            public static ClusterLink getClusterLink() {
034                    PortalRuntimePermission.checkGetBeanProperty(ClusterLinkUtil.class);
035    
036                    if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
037                            if (_log.isWarnEnabled()) {
038                                    _log.warn("ClusterLinkUtil is not initialized");
039                            }
040    
041                            return null;
042                    }
043    
044                    return _clusterLink;
045            }
046    
047            public static void sendMulticastMessage(
048                    Message message, Priority priority) {
049    
050                    ClusterLink clusterLink = getClusterLink();
051    
052                    if (clusterLink == null) {
053                            return;
054                    }
055    
056                    clusterLink.sendMulticastMessage(message, priority);
057            }
058    
059            public static void sendMulticastMessage(Object payload, Priority priority) {
060                    Message message = new Message();
061    
062                    message.setPayload(payload);
063    
064                    sendMulticastMessage(message, priority);
065            }
066    
067            public static void sendUnicastMessage(
068                    Address address, Message message, Priority priority) {
069    
070                    ClusterLink clusterLink = getClusterLink();
071    
072                    if (clusterLink == null) {
073                            return;
074                    }
075    
076                    clusterLink.sendUnicastMessage(address, message, priority);
077            }
078    
079            public static Message setAddress(Message message, Address address) {
080                    message.put(_ADDRESS, address);
081    
082                    return message;
083            }
084    
085            private static final String _ADDRESS = "CLUSTER_ADDRESS";
086    
087            private static final Log _log = LogFactoryUtil.getLog(
088                    ClusterLinkUtil.class);
089    
090            private static final ClusterLink _clusterLink =
091                    ProxyFactory.newServiceTrackedInstance(ClusterLink.class);
092    
093    }