1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.activemq.transport;
18
19 import java.io.IOException;
20 import java.net.URI;
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 <T> T narrow(Class<T> target) {
59 boolean assignableFrom = target.isAssignableFrom(getClass());
60 if (assignableFrom) {
61 return target.cast(this);
62 }
63 return null;
64 }
65
66 public FutureResponse asyncRequest(Object command, ResponseCallback responseCallback) throws IOException {
67 throw new AssertionError("Unsupported Method");
68 }
69
70 public Object request(Object command) throws IOException {
71 throw new AssertionError("Unsupported Method");
72 }
73
74 public Object request(Object command, int timeout) throws IOException {
75 throw new AssertionError("Unsupported Method");
76 }
77
78 /**
79 * Process the inbound command
80 */
81 public void doConsume(Object command) {
82 if (command != null) {
83 if (transportListener != null) {
84 transportListener.onCommand(command);
85 } else {
86 LOG.error("No transportListener available to process inbound command: " + command);
87 }
88 }
89 }
90
91 /**
92 * Passes any IO exceptions into the transport listener
93 */
94 public void onException(IOException e) {
95 if (transportListener != null) {
96 transportListener.onException(e);
97 }
98 }
99
100 protected void checkStarted() throws IOException {
101 if (!isStarted()) {
102 throw new IOException("The transport is not running.");
103 }
104 }
105
106 public boolean isFaultTolerant() {
107 return false;
108 }
109
110
111 public void reconnect(URI uri) throws IOException {
112 throw new IOException("Not supported");
113 }
114
115 public boolean isDisposed() {
116 return isStopped();
117 }
118
119 public boolean isConnected() {
120 return isStarted();
121 }
122
123 }