View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * 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   **/
18  package org.codehaus.activemq.transport;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import javax.jms.JMSException;
24  import java.net.URI;
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  /***
30   * An abstract base class useful for implementation inheritence
31   *
32   * @version $Revision: 1.7 $
33   */
34  public abstract class TransportServerChannelSupport implements TransportServerChannel {
35  
36      private static final Log log = LogFactory.getLog(TransportServerChannelSupport.class);
37  
38      private String url;
39      private TransportChannelListener transportChannelListener;
40      private List channels = new ArrayList();
41  
42      public TransportServerChannelSupport(URI url) {
43          this(url.toString());
44      }
45  
46      public TransportServerChannelSupport(String url) {
47          this.url = url;
48      }
49  
50      public void start() throws JMSException {
51          if (transportChannelListener == null) {
52              throw new JMSException("Must have a TransportChannelListener attached!");
53          }
54      }
55  
56      public synchronized void stop() throws JMSException {
57          for (Iterator iter = channels.iterator(); iter.hasNext();) {
58              TransportChannel channel = (TransportChannel) iter.next();
59              channel.stop();
60          }
61      }
62  
63      public TransportChannelListener getTransportChannelListener() {
64          return transportChannelListener;
65      }
66  
67      public void setTransportChannelListener(TransportChannelListener listener) {
68          this.transportChannelListener = listener;
69      }
70  
71      public String getUrl() {
72          return url;
73      }
74  
75      public void setUrl(String url) {
76          this.url = url;
77      }
78  
79      protected synchronized void addClient(TransportChannel channel) {
80          if (transportChannelListener == null) {
81              log.warn("No listener attached, cannot add channel: " + channel);
82          }
83          else {
84              transportChannelListener.addClient(channel);
85              channel.setTransportChannelListener(transportChannelListener);
86              channels.add(channel);
87          }
88      }
89  }