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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.nio.intraband.Datagram;
022    import com.liferay.portal.kernel.nio.intraband.DatagramReceiveHandler;
023    import com.liferay.portal.kernel.nio.intraband.Intraband;
024    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
025    import com.liferay.portal.kernel.process.ProcessCallable;
026    
027    import java.io.Serializable;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class BootstrapRPCDatagramReceiveHandler
033            implements DatagramReceiveHandler {
034    
035            @Override
036            public void receive(
037                    RegistrationReference registrationReference, Datagram datagram) {
038    
039                    Deserializer deserializer = new Deserializer(
040                            datagram.getDataByteBuffer());
041    
042                    try {
043                            ProcessCallable<? extends Serializable> processCallable =
044                                    deserializer.readObject();
045    
046                            Serializer serializer = new Serializer();
047    
048                            Serializable result = processCallable.call();
049    
050                            serializer.writeObject(result);
051    
052                            Intraband intraband = registrationReference.getIntraband();
053    
054                            intraband.sendDatagram(
055                                    registrationReference,
056                                    Datagram.createResponseDatagram(
057                                            datagram, serializer.toByteBuffer()));
058                    }
059                    catch (Exception e) {
060                            _log.error("Unable to execute", e);
061                    }
062            }
063    
064            private static Log _log = LogFactoryUtil.getLog(
065                    BootstrapRPCDatagramReceiveHandler.class);
066    
067    }