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 */ 017 package org.apache.activemq.transport.mock; 018 019 import java.io.IOException; 020 import java.net.URI; 021 022 import org.apache.activemq.transport.DefaultTransportListener; 023 import org.apache.activemq.transport.FutureResponse; 024 import org.apache.activemq.transport.ResponseCallback; 025 import org.apache.activemq.transport.Transport; 026 import org.apache.activemq.transport.TransportFilter; 027 import org.apache.activemq.transport.TransportListener; 028 029 /** 030 * @version $Revision: 1.5 $ 031 */ 032 public class MockTransport extends DefaultTransportListener implements Transport { 033 034 protected Transport next; 035 protected TransportListener transportListener; 036 037 public MockTransport(Transport next) { 038 this.next = next; 039 } 040 041 /** 042 */ 043 public synchronized void setTransportListener(TransportListener channelListener) { 044 this.transportListener = channelListener; 045 if (channelListener == null) { 046 getNext().setTransportListener(null); 047 } else { 048 getNext().setTransportListener(this); 049 } 050 } 051 052 /** 053 * @see org.apache.activemq.Service#start() 054 * @throws IOException if the next channel has not been set. 055 */ 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 public void stop() throws Exception { 070 getNext().stop(); 071 } 072 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 public String toString() { 092 return getNext().toString(); 093 } 094 095 public void oneway(Object command) throws IOException { 096 getNext().oneway(command); 097 } 098 099 public FutureResponse asyncRequest(Object command, ResponseCallback responseCallback) throws IOException { 100 return getNext().asyncRequest(command, null); 101 } 102 103 public Object request(Object command) throws IOException { 104 return getNext().request(command); 105 } 106 107 public Object request(Object command, int timeout) throws IOException { 108 return getNext().request(command, timeout); 109 } 110 111 public void onException(IOException error) { 112 getTransportListener().onException(error); 113 } 114 115 public <T> T narrow(Class<T> target) { 116 if (target.isAssignableFrom(getClass())) { 117 return target.cast(this); 118 } 119 return getNext().narrow(target); 120 } 121 122 public synchronized void setNext(Transport next) { 123 this.next = next; 124 } 125 126 public void install(TransportFilter filter) { 127 filter.setTransportListener(this); 128 getNext().setTransportListener(filter); 129 setNext(filter); 130 } 131 132 public String getRemoteAddress() { 133 return getNext().getRemoteAddress(); 134 } 135 136 /** 137 * @see org.apache.activemq.transport.Transport#isFaultTolerant() 138 */ 139 public boolean isFaultTolerant() { 140 return getNext().isFaultTolerant(); 141 } 142 143 public boolean isDisposed() { 144 return getNext().isDisposed(); 145 } 146 147 public boolean isConnected() { 148 return getNext().isConnected(); 149 } 150 151 public void reconnect(URI uri) throws IOException { 152 getNext().reconnect(uri); 153 } 154 155 public int getReceiveCounter() { 156 return getNext().getReceiveCounter(); 157 } 158 }