001 /**
002 *
003 * Copyright 2004 Protique Ltd
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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 **/
018
019 package org.activemq.transport;
020 import java.lang.ref.WeakReference;
021 import java.net.InetAddress;
022 import java.net.InetSocketAddress;
023 import java.net.URI;
024 import java.net.UnknownHostException;
025 import java.util.Iterator;
026 import java.util.List;
027
028 import javax.jms.JMSException;
029
030 import org.apache.commons.logging.Log;
031 import org.apache.commons.logging.LogFactory;
032
033 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
034
035 /**
036 * An abstract base class useful for implementation inheritance
037 *
038 * @version $Revision: 1.1.1.1 $
039 */
040 public abstract class TransportServerChannelSupport implements TransportServerChannel {
041 private static final Log log = LogFactory.getLog(TransportServerChannelSupport.class);
042 private String url;
043 private TransportChannelListener transportChannelListener;
044 private List channels = new CopyOnWriteArrayList();
045
046 public TransportServerChannelSupport(URI url) {
047 this(url.toString());
048 }
049
050 public TransportServerChannelSupport(String url) {
051 this.url = url;
052 }
053
054 public void start() throws JMSException {
055 if (transportChannelListener == null) {
056 throw new JMSException("Must have a TransportChannelListener attached!");
057 }
058 }
059
060 public synchronized void stop() throws JMSException {
061 for (Iterator iter = channels.iterator();iter.hasNext();) {
062 WeakReference channelRef = (WeakReference) iter.next();
063 TransportChannel channel = (TransportChannel) channelRef.get();
064 if( channel!=null )
065 channel.stop();
066 }
067 }
068
069 public TransportChannelListener getTransportChannelListener() {
070 return transportChannelListener;
071 }
072
073 public void setTransportChannelListener(TransportChannelListener listener) {
074 this.transportChannelListener = listener;
075 }
076
077 public String getUrl() {
078 return url;
079 }
080
081 public void setUrl(String url) {
082 this.url = url;
083 }
084
085 public InetSocketAddress getSocketAddress() {
086 return null;
087 }
088
089 /**
090 * Add a channel
091 * @param channel
092 */
093 public synchronized void addClient(TransportChannel channel) {
094 if (transportChannelListener == null) {
095 log.warn("No listener attached, cannot add channel: " + channel);
096 }
097 else {
098 transportChannelListener.addClient(channel);
099 channel.setTransportChannelListener(transportChannelListener);
100 channels.add(new WeakReference(channel));
101 }
102 }
103
104 /**
105 * remove a channel
106 * @param channel
107 */
108 public synchronized void removeClient(TransportChannel channel){
109 for (Iterator iter = channels.iterator();iter.hasNext();) {
110 WeakReference channelRef = (WeakReference) iter.next();
111 TransportChannel c = (TransportChannel) channelRef.get();
112 if( c!=null && c.equals(channel)){
113 channels.remove(channelRef);
114 break;
115 }
116 }
117 }
118
119 protected String resolveHostName(String hostName) {
120 String result = hostName;
121 try {
122 //hostname can be null for vm:// protocol ...
123 if (hostName != null && (hostName.equalsIgnoreCase("localhost") || hostName.equals("127.0.0.1"))) {
124 result = InetAddress.getLocalHost().getHostName();
125 }
126 }
127 catch (UnknownHostException e) {
128 log.debug("failed to resolve hostname", e);
129 }
130 return result;
131 }
132 }