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