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.net.URI;
021    
022    /**
023     * A TransportStatusEvent is raised when the state of the underlying transport channel changes
024     * 
025     * @version $Revision: 1.2 $
026     */
027    public class TransportStatusEvent {
028        /**
029         * The channel has been intially and successfully connected
030         */
031        public static final int CONNECTED = 1;
032        /**
033         * The channel has been disconnected, but maybe reconnected
034         */
035        public static final int DISCONNECTED = 2;
036        /**
037         * The channel has successfully reconnected after a disconnect
038         */
039        public static final int RECONNECTED = 3;
040        /**
041         * The channel has failed
042         */
043        public static final int FAILED = 4;
044        
045        /**
046         * The channel has been STOPPED
047         */
048        public static final int STOPPED = 5;
049        
050        private URI remoteURI;
051        private int channelStatus;
052        private TransportChannel transportChannel;
053    
054        /**
055         * Default Constructor
056         */
057        public TransportStatusEvent() {
058        }
059        
060        /**
061         * Constructs an event with the given channel status.
062         * @param tc
063         * @param channelStatus the channel status
064         */
065        public TransportStatusEvent(TransportChannel tc, int channelStatus) {
066            this.transportChannel = tc;
067            this.channelStatus = channelStatus;
068        }
069    
070        /**
071         * @return a pretty print of this
072         */
073        public String toString() {
074            StringBuffer rc = new StringBuffer();
075            rc.append("Channel: ");
076            if( transportChannel !=null )
077                rc.append(transportChannel);
078            else if( remoteURI!=null )
079                rc.append(remoteURI);
080            rc.append(" has ");
081            rc.append(getStatusAsString(channelStatus));        
082            return rc.toString(); 
083        }
084    
085        private String getStatusAsString(int status) {
086            String result = null;
087            switch (status) {
088                case CONNECTED :
089                    result = "connected";
090                    break;
091                case DISCONNECTED :
092                    result = "disconnected";
093                    break;
094                case RECONNECTED :
095                    result = "reconnected";
096                    break;
097                case FAILED :
098                    result = "failed";
099                    break;
100                case STOPPED:
101                    result = "stopped";
102                    break;
103                default :
104                    result = "unknown";
105            }
106            return result;
107        }
108    
109        /**
110         * @return Returns the channelStatus.
111         */
112        public int getChannelStatus() {
113            return channelStatus;
114        }
115    
116        /**
117         * @param channelStatus The channelStatus to set.
118         */
119        public void setChannelStatus(int channelStatus) {
120            this.channelStatus = channelStatus;
121        }
122        
123        /**
124         * @return Returns the transportChannel.
125         */
126        public TransportChannel getTransportChannel() {
127            return transportChannel;
128        }
129        /**
130         * @param transportChannel The transportChannel to set.
131         */
132        public void setTransportChannel(TransportChannel transportChannel) {
133            this.transportChannel = transportChannel;
134        }
135    
136        /**
137         * @return Returns the remoteURI.
138         */
139        public URI getRemoteURI() {
140            return remoteURI;
141        }
142    
143        /**
144         * @param remoteURI The remoteURI to set.
145         */
146        public void setRemoteURI(URI remoteURI) {
147            this.remoteURI = remoteURI;
148        }
149    }