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 java.security.cert.X509Certificate;
022
023import org.apache.activemq.transport.DefaultTransportListener;
024import org.apache.activemq.transport.FutureResponse;
025import org.apache.activemq.transport.ResponseCallback;
026import org.apache.activemq.transport.Transport;
027import org.apache.activemq.transport.TransportFilter;
028import org.apache.activemq.transport.TransportListener;
029import org.apache.activemq.wireformat.WireFormat;
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    @Override
041    public synchronized void setTransportListener(TransportListener channelListener) {
042        this.transportListener = channelListener;
043        if (channelListener == null) {
044            getNext().setTransportListener(null);
045        } else {
046            getNext().setTransportListener(this);
047        }
048    }
049
050    /**
051     * @see org.apache.activemq.Service#start()
052     * @throws IOException
053     *         if the next channel has not been set.
054     */
055    @Override
056    public void start() throws Exception {
057        if (getNext() == null) {
058            throw new IOException("The next channel has not been set.");
059        }
060        if (transportListener == null) {
061            throw new IOException("The command listener has not been set.");
062        }
063        getNext().start();
064    }
065
066    /**
067     * @see org.apache.activemq.Service#stop()
068     */
069    @Override
070    public void stop() throws Exception {
071        getNext().stop();
072    }
073
074    @Override
075    public void onCommand(Object command) {
076        getTransportListener().onCommand(command);
077    }
078
079    /**
080     * @return Returns the getNext().
081     */
082    public synchronized Transport getNext() {
083        return next;
084    }
085
086    /**
087     * @return Returns the packetListener.
088     */
089    @Override
090    public synchronized TransportListener getTransportListener() {
091        return transportListener;
092    }
093
094    @Override
095    public String toString() {
096        return getNext().toString();
097    }
098
099    @Override
100    public void oneway(Object command) throws IOException {
101        getNext().oneway(command);
102    }
103
104    @Override
105    public FutureResponse asyncRequest(Object command, ResponseCallback responseCallback) throws IOException {
106        return getNext().asyncRequest(command, null);
107    }
108
109    @Override
110    public Object request(Object command) throws IOException {
111        return getNext().request(command);
112    }
113
114    @Override
115    public Object request(Object command, int timeout) throws IOException {
116        return getNext().request(command, timeout);
117    }
118
119    @Override
120    public void onException(IOException error) {
121        getTransportListener().onException(error);
122    }
123
124    @Override
125    public <T> T narrow(Class<T> target) {
126        if (target.isAssignableFrom(getClass())) {
127            return target.cast(this);
128        }
129        return getNext().narrow(target);
130    }
131
132    public synchronized void setNext(Transport next) {
133        this.next = next;
134    }
135
136    public void install(TransportFilter filter) {
137        filter.setTransportListener(this);
138        getNext().setTransportListener(filter);
139        setNext(filter);
140    }
141
142    @Override
143    public String getRemoteAddress() {
144        return getNext().getRemoteAddress();
145    }
146
147    /**
148     * @see org.apache.activemq.transport.Transport#isFaultTolerant()
149     */
150    @Override
151    public boolean isFaultTolerant() {
152        return getNext().isFaultTolerant();
153    }
154
155    @Override
156    public boolean isDisposed() {
157        return getNext().isDisposed();
158    }
159
160    @Override
161    public boolean isConnected() {
162        return getNext().isConnected();
163    }
164
165    @Override
166    public void reconnect(URI uri) throws IOException {
167        getNext().reconnect(uri);
168    }
169
170    @Override
171    public int getReceiveCounter() {
172        return getNext().getReceiveCounter();
173    }
174
175    @Override
176    public boolean isReconnectSupported() {
177        return getNext().isReconnectSupported();
178    }
179
180    @Override
181    public boolean isUpdateURIsSupported() {
182        return getNext().isUpdateURIsSupported();
183    }
184
185    @Override
186    public void updateURIs(boolean reblance, URI[] uris) throws IOException {
187        getNext().updateURIs(reblance, uris);
188    }
189
190    @Override
191    public X509Certificate[] getPeerCertificates() {
192        return getNext().getPeerCertificates();
193    }
194
195    @Override
196    public void setPeerCertificates(X509Certificate[] certificates) {
197        getNext().setPeerCertificates(certificates);
198    }
199
200    @Override
201    public WireFormat getWireFormat() {
202        return getNext().getWireFormat();
203    }
204}