001    /**
002     * Copyright (c) 2000-present 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.test;
016    
017    import com.liferay.portal.kernel.nio.intraband.BaseIntraband;
018    import com.liferay.portal.kernel.nio.intraband.CompletionHandler;
019    import com.liferay.portal.kernel.nio.intraband.Datagram;
020    import com.liferay.portal.kernel.nio.intraband.DatagramHelper;
021    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
022    
023    import java.io.IOException;
024    
025    import java.nio.channels.Channel;
026    import java.nio.channels.GatheringByteChannel;
027    import java.nio.channels.ScatteringByteChannel;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class MockIntraband extends BaseIntraband {
033    
034            public MockIntraband() {
035                    this(10000);
036            }
037    
038            public MockIntraband(long defaultTimeout) {
039                    super(defaultTimeout);
040            }
041    
042            public Datagram getDatagram() {
043                    return _datagram;
044            }
045    
046            public RegistrationReference getRegistrationReference() {
047                    return _registrationReference;
048            }
049    
050            @Override
051            public RegistrationReference registerChannel(Channel duplexChannel) {
052                    return new MockRegistrationReference(
053                            (ScatteringByteChannel)duplexChannel,
054                            (GatheringByteChannel)duplexChannel);
055            }
056    
057            @Override
058            public RegistrationReference registerChannel(
059                    ScatteringByteChannel scatteringByteChannel,
060                    GatheringByteChannel gatheringByteChannel) {
061    
062                    return new MockRegistrationReference(
063                            scatteringByteChannel, gatheringByteChannel);
064            }
065    
066            public void setIOException(IOException ioException) {
067                    _ioException = ioException;
068            }
069    
070            @Override
071            protected void doSendDatagram(
072                    RegistrationReference registrationReference, Datagram datagram) {
073    
074                    _registrationReference = registrationReference;
075                    _datagram = datagram;
076    
077                    CompletionHandler<?> completionHandler =
078                            DatagramHelper.getCompletionHandler(datagram);
079    
080                    if (completionHandler == null) {
081                            return;
082                    }
083    
084                    if (_ioException == null) {
085                            Datagram responseDatagram = processDatagram(datagram);
086    
087                            if (responseDatagram != null) {
088                                    completionHandler.replied(null, responseDatagram);
089                            }
090                    }
091                    else {
092                            completionHandler.failed(null, _ioException);
093                    }
094            }
095    
096            protected Datagram processDatagram(Datagram datagram) {
097                    return null;
098            }
099    
100            private Datagram _datagram;
101            private IOException _ioException;
102            private RegistrationReference _registrationReference;
103    
104    }