001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.transport.mock;
018
019import java.io.IOException;
020import java.net.URI;
021import org.apache.activemq.transport.DefaultTransportListener;
022import org.apache.activemq.transport.FutureResponse;
023import org.apache.activemq.transport.ResponseCallback;
024import org.apache.activemq.transport.Transport;
025import org.apache.activemq.transport.TransportFilter;
026import org.apache.activemq.transport.TransportListener;
027
028/**
029 * 
030 */
031public class MockTransport extends DefaultTransportListener implements Transport {
032
033    protected Transport next;
034    protected TransportListener transportListener;
035
036    public MockTransport(Transport next) {
037        this.next = next;
038    }
039
040    /**
041     */
042    public synchronized void setTransportListener(TransportListener channelListener) {
043        this.transportListener = channelListener;
044        if (channelListener == null) {
045            getNext().setTransportListener(null);
046        } else {
047            getNext().setTransportListener(this);
048        }
049    }
050
051    /**
052     * @see org.apache.activemq.Service#start()
053     * @throws IOException if the next channel has not been set.
054     */
055    public void start() throws Exception {
056        if (getNext() == null) {
057            throw new IOException("The next channel has not been set.");
058        }
059        if (transportListener == null) {
060            throw new IOException("The command listener has not been set.");
061        }
062        getNext().start();
063    }
064
065    /**
066     * @see org.apache.activemq.Service#stop()
067     */
068    public void stop() throws Exception {
069        getNext().stop();
070    }
071
072    @Override
073    public void onCommand(Object command) {
074        getTransportListener().onCommand(command);
075    }
076
077    /**
078     * @return Returns the getNext().
079     */
080    public synchronized Transport getNext() {
081        return next;
082    }
083
084    /**
085     * @return Returns the packetListener.
086     */
087    public synchronized TransportListener getTransportListener() {
088        return transportListener;
089    }
090
091    @Override
092    public String toString() {
093        return getNext().toString();
094    }
095
096    public void oneway(Object command) throws IOException {
097        getNext().oneway(command);
098    }
099
100    public FutureResponse asyncRequest(Object command, ResponseCallback responseCallback) throws IOException {
101        return getNext().asyncRequest(command, null);
102    }
103
104    public Object request(Object command) throws IOException {
105        return getNext().request(command);
106    }
107
108    public Object request(Object command, int timeout) throws IOException {
109        return getNext().request(command, timeout);
110    }
111
112    @Override
113    public void onException(IOException error) {
114        getTransportListener().onException(error);
115    }
116
117    public <T> T narrow(Class<T> target) {
118        if (target.isAssignableFrom(getClass())) {
119            return target.cast(this);
120        }
121        return getNext().narrow(target);
122    }
123
124    public synchronized void setNext(Transport next) {
125        this.next = next;
126    }
127
128    public void install(TransportFilter filter) {
129        filter.setTransportListener(this);
130        getNext().setTransportListener(filter);
131        setNext(filter);
132    }
133
134    public String getRemoteAddress() {
135        return getNext().getRemoteAddress();
136    }
137
138    /**
139     * @see org.apache.activemq.transport.Transport#isFaultTolerant()
140     */
141    public boolean isFaultTolerant() {
142        return getNext().isFaultTolerant();
143    }
144
145        public boolean isDisposed() {
146                return getNext().isDisposed();
147        }
148        
149        public boolean isConnected() {
150       return getNext().isConnected();
151    }
152
153        public void reconnect(URI uri) throws IOException {
154                getNext().reconnect(uri);
155        }
156
157    public int getReceiveCounter() {
158        return getNext().getReceiveCounter();
159    }
160    
161
162    public boolean isReconnectSupported() {
163        return getNext().isReconnectSupported();
164    }
165
166    public boolean isUpdateURIsSupported() {
167        return getNext().isUpdateURIsSupported();
168    }
169    public void updateURIs(boolean reblance,URI[] uris) throws IOException {
170       getNext().updateURIs(reblance,uris);
171    }
172}