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.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 (!(payload instanceof ProxyRequest)) {
044                                    throw new Exception(
045                                            "Payload " + payload.getClass() + " is not of type " +
046                                                    ProxyRequest.class.getName());
047                            }
048                            else {
049                                    MessageValuesThreadLocal.populateThreadLocalsFromMessage(
050                                            message);
051    
052                                    ProxyRequest proxyRequest = (ProxyRequest)payload;
053    
054                                    Object result = proxyRequest.execute(_manager);
055    
056                                    proxyResponse.setResult(result);
057                            }
058                    }
059                    catch (Exception e) {
060                            proxyResponse.setException(e);
061                    }
062                    finally {
063                            String responseDestinationName =
064                                    message.getResponseDestinationName();
065    
066                            Exception proxyResponseException = proxyResponse.getException();
067    
068                            if (Validator.isNotNull(responseDestinationName)) {
069                                    Message responseMessage = MessageBusUtil.createResponseMessage(
070                                            message);
071    
072                                    responseMessage.setPayload(proxyResponse);
073    
074                                    if (_log.isDebugEnabled() && (proxyResponseException != null)) {
075                                            _log.debug(proxyResponseException, proxyResponseException);
076                                    }
077    
078                                    _messageBus.sendMessage(
079                                            responseDestinationName, responseMessage);
080                            }
081                            else {
082                                    if (proxyResponseException != null) {
083                                            if (_log.isWarnEnabled()) {
084                                                    _log.warn(
085                                                            proxyResponseException, proxyResponseException);
086                                            }
087                                    }
088    
089                                    message.setResponse(proxyResponse);
090                            }
091                    }
092            }
093    
094            public void setManager(Object manager) {
095                    _manager = manager;
096            }
097    
098            public void setMessageBus(MessageBus messageBus) {
099                    _messageBus = messageBus;
100            }
101    
102            private static final Log _log = LogFactoryUtil.getLog(
103                    ProxyMessageListener.class);
104    
105            private Object _manager;
106            private MessageBus _messageBus;
107    
108    }