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.rpc;
016    
017    import com.liferay.portal.kernel.io.Deserializer;
018    import com.liferay.portal.kernel.io.Serializer;
019    import com.liferay.portal.kernel.nio.intraband.BaseAsyncDatagramReceiveHandler;
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.process.ProcessCallable;
024    
025    import java.io.Serializable;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class RPCDatagramReceiveHandler extends BaseAsyncDatagramReceiveHandler {
031    
032            @Override
033            protected void doReceive(
034                            RegistrationReference registrationReference, Datagram datagram)
035                    throws Exception {
036    
037                    Deserializer deserializer = new Deserializer(
038                            datagram.getDataByteBuffer());
039    
040                    ProcessCallable<? extends Serializable> processCallable =
041                            deserializer.readObject();
042    
043                    Serializer serializer = new Serializer();
044    
045                    Serializable result = processCallable.call();
046    
047                    serializer.writeObject(result);
048    
049                    Intraband intraband = registrationReference.getIntraband();
050    
051                    intraband.sendDatagram(
052                            registrationReference,
053                            Datagram.createResponseDatagram(
054                                    datagram, serializer.toByteBuffer()));
055            }
056    
057    }