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.messaging.proxy;
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.messaging.MessageBus;
021    import com.liferay.portal.kernel.messaging.MessageBusUtil;
022    import com.liferay.portal.kernel.messaging.MessageListener;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    /**
026     * @author Micha Kiener
027     * @author Michael C. Han
028     * @author Brian Wing Shun Chan
029     * @author Igor Spasic
030     */
031    public class ProxyMessageListener implements MessageListener {
032    
033            @Override
034            public void receive(Message message) {
035                    ProxyResponse proxyResponse = new ProxyResponse();
036    
037                    try {
038                            Object payload = message.getPayload();
039    
040                            if (payload == null) {
041                                    throw new Exception("Payload is null");
042                            }
043                            else if (!ProxyRequest.class.isAssignableFrom(payload.getClass())) {
044                                    throw new Exception(
045                                            "Payload " + payload.getClass() + " is not of type " +
046                                                    ProxyRequest.class.getName());
047                            }
048                            else {
049                                    ProxyRequest proxyRequest = (ProxyRequest)payload;
050    
051                                    Object result = proxyRequest.execute(_manager);
052    
053                                    proxyResponse.setResult(result);
054                            }
055                    }
056                    catch (Exception e) {
057                            proxyResponse.setException(e);
058                    }
059                    finally {
060                            String responseDestinationName =
061                                    message.getResponseDestinationName();
062    
063                            Exception proxyResponseException = proxyResponse.getException();
064    
065                            if (Validator.isNotNull(responseDestinationName)) {
066                                    Message responseMessage = MessageBusUtil.createResponseMessage(
067                                            message);
068    
069                                    responseMessage.setPayload(proxyResponse);
070    
071                                    if (_log.isDebugEnabled() && (proxyResponseException != null)) {
072                                            _log.debug(proxyResponseException, proxyResponseException);
073                                    }
074    
075                                    _messageBus.sendMessage(
076                                            responseDestinationName, responseMessage);
077                            }
078                            else {
079                                    if (proxyResponseException != null) {
080                                            if (_log.isWarnEnabled()) {
081                                                    _log.warn(
082                                                            proxyResponseException, proxyResponseException);
083                                            }
084                                    }
085    
086                                    message.setResponse(proxyResponse);
087                            }
088                    }
089            }
090    
091            public void setManager(Object manager) {
092                    _manager = manager;
093            }
094    
095            public void setMessageBus(MessageBus messageBus) {
096                    _messageBus = messageBus;
097            }
098    
099            private static Log _log = LogFactoryUtil.getLog(ProxyMessageListener.class);
100    
101            private Object _manager;
102            private MessageBus _messageBus;
103    
104    }