001    /**
002     * Copyright (c) 2000-2012 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.cluster;
016    
017    import com.liferay.portal.kernel.cluster.Address;
018    import com.liferay.portal.kernel.cluster.ClusterLink;
019    import com.liferay.portal.kernel.cluster.Priority;
020    import com.liferay.portal.kernel.cluster.messaging.ClusterForwardMessageListener;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.messaging.Message;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.util.PropsUtil;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.util.ArrayList;
029    import java.util.Collections;
030    import java.util.List;
031    import java.util.Properties;
032    
033    import org.jgroups.ChannelException;
034    import org.jgroups.JChannel;
035    
036    /**
037     * @author Shuyang Zhou
038     */
039    public class ClusterLinkImpl extends ClusterBase implements ClusterLink {
040    
041            @Override
042            public void destroy() {
043                    if (!PropsValues.CLUSTER_LINK_ENABLED) {
044                            return;
045                    }
046    
047                    for (JChannel jChannel : _transportChannels) {
048                            jChannel.close();
049                    }
050            }
051    
052            public List<Address> getLocalTransportAddresses() {
053                    if (!PropsValues.CLUSTER_LINK_ENABLED) {
054                            return Collections.emptyList();
055                    }
056    
057                    List<Address> addresses = new ArrayList<Address>(
058                            _localTransportAddresses.size());
059    
060                    for (org.jgroups.Address address : _localTransportAddresses) {
061                            addresses.add(new AddressImpl(address));
062                    }
063    
064                    return addresses;
065            }
066    
067            public List<Address> getTransportAddresses(Priority priority) {
068                    if (!PropsValues.CLUSTER_LINK_ENABLED) {
069                            return Collections.emptyList();
070                    }
071    
072                    JChannel jChannel = getChannel(priority);
073    
074                    return getAddresses(jChannel);
075            }
076    
077            public void sendMulticastMessage(Message message, Priority priority) {
078                    if (!PropsValues.CLUSTER_LINK_ENABLED) {
079                            return;
080                    }
081    
082                    JChannel jChannel = getChannel(priority);
083    
084                    try {
085                            jChannel.send(null, null, message);
086                    }
087                    catch (ChannelException ce) {
088                            _log.error("Unable to send multicast message " + message, ce);
089                    }
090            }
091    
092            public void sendUnicastMessage(
093                    Address address, Message message, Priority priority) {
094    
095                    if (!PropsValues.CLUSTER_LINK_ENABLED) {
096                            return;
097                    }
098    
099                    org.jgroups.Address jGroupsAddress =
100                            (org.jgroups.Address)address.getRealAddress();
101    
102                    JChannel jChannel = getChannel(priority);
103    
104                    try {
105                            jChannel.send(jGroupsAddress, null, message);
106                    }
107                    catch (ChannelException ce) {
108                            _log.error("Unable to send unicast message:" + message, ce);
109                    }
110            }
111    
112            public void setClusterForwardMessageListener(
113                    ClusterForwardMessageListener clusterForwardMessageListener) {
114    
115                    _clusterForwardMessageListener = clusterForwardMessageListener;
116            }
117    
118            protected JChannel getChannel(Priority priority) {
119                    int channelIndex =
120                            priority.ordinal() * _channelCount / _MAX_CHANNEL_COUNT;
121    
122                    if (_log.isDebugEnabled()) {
123                            _log.debug(
124                                    "Select channel number " + channelIndex + " for priority " +
125                                            priority);
126                    }
127    
128                    return _transportChannels.get(channelIndex);
129            }
130    
131            @Override
132            protected void initChannels() throws ChannelException {
133                    Properties transportProperties = PropsUtil.getProperties(
134                            PropsKeys.CLUSTER_LINK_CHANNEL_PROPERTIES_TRANSPORT, true);
135    
136                    _channelCount = transportProperties.size();
137    
138                    if ((_channelCount <= 0) || (_channelCount > _MAX_CHANNEL_COUNT)) {
139                            throw new IllegalArgumentException(
140                                    "Channel count must be between 1 and " + _MAX_CHANNEL_COUNT);
141                    }
142    
143                    _localTransportAddresses = new ArrayList<org.jgroups.Address>(
144                            _channelCount);
145                    _transportChannels = new ArrayList<JChannel>(_channelCount);
146    
147                    List<String> keys = new ArrayList<String>(_channelCount);
148    
149                    for (Object key : transportProperties.keySet()) {
150                            keys.add((String)key);
151                    }
152    
153                    Collections.sort(keys);
154    
155                    for (int i = 0; i < keys.size(); i++) {
156                            String customName = keys.get(i);
157    
158                            String value = transportProperties.getProperty(customName);
159    
160                            JChannel jChannel = createJChannel(
161                                    value,
162                                    new ClusterForwardReceiver(
163                                            _localTransportAddresses, _clusterForwardMessageListener),
164                                            _LIFERAY_TRANSPORT_CHANNEL + i);
165    
166                            _localTransportAddresses.add(jChannel.getLocalAddress());
167                            _transportChannels.add(jChannel);
168                    }
169            }
170    
171            private static final String _LIFERAY_TRANSPORT_CHANNEL =
172                    "LIFERAY-TRANSPORT-CHANNEL-";
173    
174            private static final int _MAX_CHANNEL_COUNT = Priority.values().length;
175    
176            private static Log _log = LogFactoryUtil.getLog(ClusterLinkImpl.class);
177    
178            private int _channelCount;
179            private ClusterForwardMessageListener _clusterForwardMessageListener;
180            private List<org.jgroups.Address> _localTransportAddresses;
181            private List<JChannel> _transportChannels;
182    
183    }