KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > mock > MockTransport


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.mock;
19
20 import java.io.IOException JavaDoc;
21
22 import org.apache.activemq.transport.DefaultTransportListener;
23 import org.apache.activemq.transport.FutureResponse;
24 import org.apache.activemq.transport.ResponseCallback;
25 import org.apache.activemq.transport.Transport;
26 import org.apache.activemq.transport.TransportFilter;
27 import org.apache.activemq.transport.TransportListener;
28
29
30 /**
31  * @version $Revision: 1.5 $
32  */

33 public class MockTransport extends DefaultTransportListener implements Transport {
34
35     protected Transport next;
36     protected TransportListener transportListener;
37
38     public MockTransport(Transport next) {
39         this.next = next;
40     }
41
42     /**
43      */

44     synchronized public void setTransportListener(TransportListener channelListener) {
45         this.transportListener = channelListener;
46         if (channelListener == null)
47             getNext().setTransportListener(null);
48         else
49             getNext().setTransportListener(this);
50     }
51
52
53     /**
54      * @see org.apache.activemq.Service#start()
55      * @throws IOException if the next channel has not been set.
56      */

57     public void start() throws Exception JavaDoc {
58         if( getNext() == null )
59             throw new IOException JavaDoc("The next channel has not been set.");
60         if( transportListener == null )
61             throw new IOException JavaDoc("The command listener has not been set.");
62         getNext().start();
63     }
64
65     /**
66      * @see org.apache.activemq.Service#stop()
67      */

68     public void stop() throws Exception JavaDoc {
69         getNext().stop();
70     }
71
72     public void onCommand(Object JavaDoc command) {
73         getTransportListener().onCommand(command);
74     }
75
76     /**
77      * @return Returns the getNext().
78      */

79     synchronized public Transport getNext() {
80         return next;
81     }
82
83     /**
84      * @return Returns the packetListener.
85      */

86     synchronized public TransportListener getTransportListener() {
87         return transportListener;
88     }
89     
90     public String JavaDoc toString() {
91         return getNext().toString();
92     }
93
94     public void oneway(Object JavaDoc command) throws IOException JavaDoc {
95         getNext().oneway(command);
96     }
97
98     public FutureResponse asyncRequest(Object JavaDoc command, ResponseCallback responseCallback) throws IOException JavaDoc {
99         return getNext().asyncRequest(command, null);
100     }
101
102     public Object JavaDoc request(Object JavaDoc command) throws IOException JavaDoc {
103         return getNext().request(command);
104     }
105     
106     public Object JavaDoc request(Object JavaDoc command,int timeout) throws IOException JavaDoc {
107         return getNext().request(command, timeout);
108     }
109
110     public void onException(IOException JavaDoc error) {
111         getTransportListener().onException(error);
112     }
113
114     public Object JavaDoc narrow(Class JavaDoc target) {
115         if( target.isAssignableFrom(getClass()) ) {
116             return this;
117         }
118         return getNext().narrow(target);
119     }
120
121     synchronized public void setNext(Transport next) {
122         this.next = next;
123     }
124
125     public void install(TransportFilter filter) {
126         filter.setTransportListener(this);
127         getNext().setTransportListener(filter);
128         setNext(filter);
129     }
130
131     public String JavaDoc getRemoteAddress() {
132         return getNext().getRemoteAddress();
133     }
134     
135 }
136
Popular Tags