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.nio.intraband.messaging;
016    
017    import com.liferay.portal.kernel.io.Serializer;
018    import com.liferay.portal.kernel.messaging.Message;
019    import com.liferay.portal.kernel.messaging.MessageListener;
020    import com.liferay.portal.kernel.nio.intraband.Datagram;
021    import com.liferay.portal.kernel.nio.intraband.IntraBand;
022    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
023    import com.liferay.portal.kernel.nio.intraband.SystemDataType;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class IntraBandBridgeMessageListener implements MessageListener {
029    
030            public IntraBandBridgeMessageListener(
031                    RegistrationReference registrationReference) {
032    
033                    _registrationReference = registrationReference;
034    
035                    _intraBand = registrationReference.getIntraBand();
036    
037                    SystemDataType systemDataType = SystemDataType.MESSAGE;
038    
039                    _messageType = systemDataType.getValue();
040            }
041    
042            public void receive(Message message) {
043                    Serializer serializer = new Serializer();
044    
045                    serializer.writeObject(message);
046    
047                    _intraBand.sendDatagram(
048                            _registrationReference,
049                            Datagram.createRequestDatagram(
050                                    _messageType, serializer.toByteBuffer()));
051            }
052    
053            private final IntraBand _intraBand;
054            private final byte _messageType;
055            private final RegistrationReference _registrationReference;
056    
057    }