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