001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }