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.messaging;
016    
017    import com.liferay.portal.kernel.cluster.ClusterLinkUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.messaging.Message;
021    import com.liferay.portal.kernel.messaging.MessageBusUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class ClusterForwardMessageListener implements ClusterMessageListener {
028    
029            @Override
030            public void receive(Message message) {
031                    String destinationName = message.getDestinationName();
032    
033                    if (Validator.isNotNull(destinationName)) {
034                            if (_log.isInfoEnabled()) {
035                                    _log.info(
036                                            "Forwarding cluster link message " + message + " to " +
037                                                    destinationName);
038                            }
039    
040                            ClusterLinkUtil.setForwardMessage(message);
041    
042                            MessageBusUtil.sendMessage(destinationName, message);
043                    }
044                    else {
045                            if (_log.isErrorEnabled()) {
046                                    _log.error(
047                                            "Forwarded cluster link message has no destination " +
048                                                    message);
049                            }
050                    }
051            }
052    
053            private static Log _log = LogFactoryUtil.getLog(
054                    ClusterForwardMessageListener.class);
055    
056    }