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.Datagram;
020    import com.liferay.portal.kernel.nio.intraband.IntraBand;
021    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
022    import com.liferay.portal.kernel.nio.intraband.SystemDataType;
023    import com.liferay.portal.kernel.process.ProcessCallable;
024    
025    import java.io.Serializable;
026    
027    import java.util.concurrent.TimeUnit;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class IntraBandRPCUtil {
033    
034            public static <V extends Serializable> V execute(
035                            RegistrationReference registrationReference,
036                            ProcessCallable<V> processCallable)
037                    throws IntraBandRPCException {
038    
039                    IntraBand intraBand = registrationReference.getIntraBand();
040    
041                    SystemDataType systemDataType = SystemDataType.RPC;
042    
043                    Serializer serializer = new Serializer();
044    
045                    serializer.writeObject(processCallable);
046    
047                    try {
048                            Datagram responseDatagram = intraBand.sendSyncDatagram(
049                                    registrationReference,
050                                    Datagram.createRequestDatagram(
051                                            systemDataType.getValue(), serializer.toByteBuffer()));
052    
053                            Deserializer deserializer = new Deserializer(
054                                    responseDatagram.getDataByteBuffer());
055    
056                            return deserializer.readObject();
057                    }
058                    catch (Exception e) {
059                            throw new IntraBandRPCException(e);
060                    }
061            }
062    
063            public static <V extends Serializable> V execute(
064                            RegistrationReference registrationReference,
065                            ProcessCallable<V> processCallable, long timeout, TimeUnit timeUnit)
066                    throws IntraBandRPCException {
067    
068                    IntraBand intraBand = registrationReference.getIntraBand();
069    
070                    SystemDataType systemDataType = SystemDataType.RPC;
071    
072                    Serializer serializer = new Serializer();
073    
074                    serializer.writeObject(processCallable);
075    
076                    try {
077                            Datagram responseDatagram = intraBand.sendSyncDatagram(
078                                    registrationReference,
079                                    Datagram.createRequestDatagram(
080                                            systemDataType.getValue(), serializer.toByteBuffer()),
081                                    timeout, timeUnit);
082    
083                            Deserializer deserializer = new Deserializer(
084                                    responseDatagram.getDataByteBuffer());
085    
086                            return deserializer.readObject();
087                    }
088                    catch (Exception e) {
089                            throw new IntraBandRPCException(e);
090                    }
091            }
092    
093    }