KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > TransportSupport


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;
19
20 import java.io.IOException JavaDoc;
21
22 import org.apache.activemq.util.ServiceSupport;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * A useful base class for transport implementations.
28  *
29  * @version $Revision: 1.1 $
30  */

31 public abstract class TransportSupport extends ServiceSupport implements Transport {
32     private static final Log log = LogFactory.getLog(TransportSupport.class);
33
34     TransportListener transportListener;
35
36     /**
37      * Returns the current transport listener
38      */

39     public TransportListener getTransportListener() {
40         return transportListener;
41     }
42
43     /**
44      * Registers an inbound command listener
45      *
46      * @param commandListener
47      */

48     public void setTransportListener(TransportListener commandListener) {
49         this.transportListener = commandListener;
50     }
51
52     /**
53      * narrow acceptance
54      *
55      * @param target
56      * @return 'this' if assignable
57      */

58     public Object JavaDoc narrow(Class JavaDoc target) {
59         boolean assignableFrom = target.isAssignableFrom(getClass());
60         if (assignableFrom) {
61             return this;
62         }
63         return null;
64     }
65
66     public FutureResponse asyncRequest(Object JavaDoc command, ResponseCallback responseCallback) throws IOException JavaDoc {
67         throw new AssertionError JavaDoc("Unsupported Method");
68     }
69
70     public Object JavaDoc request(Object JavaDoc command) throws IOException JavaDoc {
71         throw new AssertionError JavaDoc("Unsupported Method");
72     }
73     
74     public Object JavaDoc request(Object JavaDoc command,int timeout) throws IOException JavaDoc {
75         throw new AssertionError JavaDoc("Unsupported Method");
76     }
77
78     /**
79      * Process the inbound command
80      */

81     public void doConsume(Object JavaDoc command) {
82         if (command != null) {
83             if (transportListener != null) {
84                 transportListener.onCommand(command);
85             }
86             else {
87                 log.error("No transportListener available to process inbound command: " + command);
88             }
89         }
90     }
91
92     /**
93      * Passes any IO exceptions into the transport listener
94      */

95     public void onException(IOException JavaDoc e) {
96         if (transportListener != null) {
97             transportListener.onException(e);
98         }
99     }
100
101     protected void checkStarted() throws IOException JavaDoc {
102         if (!isStarted()) {
103             throw new IOException JavaDoc("The transport is not running.");
104         }
105     }
106
107 }
108
Popular Tags