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    package org.activemq.message;
019    
020    import java.io.Serializable;
021    
022    /**
023     * Describes a Message consumer
024     *
025     * @version $Revision: 1.1.1.1 $
026     */
027    public class ConsumerInfo extends AbstractPacket implements Serializable{
028        static final long serialVersionUID = 3489666609L;
029        private ActiveMQDestination destination;
030        private String clientId;
031        private short sessionId;
032        private String consumerName;
033        private String selector;
034        private long startTime;
035        private boolean started;
036        private int consumerNo;
037        private boolean noLocal;
038        private boolean browser;
039        private int prefetchNumber = 100;
040        private transient String consumerKey;
041        private String consumerId;
042    
043        
044        
045        /**
046         * @return Returns the sessionId.
047         */
048        public short getSessionId() {
049            return sessionId;
050        }
051    
052        /**
053         * @param sessionId The sessionId to set.
054         */
055        public void setSessionId(short sessionId) {
056            this.sessionId = sessionId;
057        }
058    
059        /**
060         * Return the type of Packet
061         *
062         * @return integer representation of the type of Packet
063         */
064        public int getPacketType() {
065            return CONSUMER_INFO;
066        }
067    
068        /**
069         * Test for equality
070         *
071         * @param obj object to test
072         * @return true if equivalent
073         */
074        public boolean equals(Object obj) {
075            boolean result = false;
076            if (obj != null && obj instanceof ConsumerInfo) {
077                ConsumerInfo that = (ConsumerInfo) obj;
078                result = this.getConsumerId().equals(that.getConsumerId());
079            }
080            return result;
081        }
082    
083        /**
084         * @return hash code for instance
085         */
086        public int hashCode() {
087            return getConsumerId().hashCode();
088        }
089    
090        /**
091         * @return Returns the clientId.
092         */
093        public String getClientId() {
094            return this.clientId;
095        }
096    
097        /**
098         * @param newClientId The clientId to set.
099         */
100        public void setClientId(String newClientId) {
101            this.clientId = newClientId;
102        }
103    
104        /**
105         * @return Returns the destination.
106         */
107        public ActiveMQDestination getDestination() {
108            return this.destination;
109        }
110    
111        /**
112         * @param newDestination The destination to set.
113         */
114        public void setDestination(ActiveMQDestination newDestination) {
115            this.destination = newDestination;
116        }
117    
118        /**
119         * @return Returns the selector.
120         */
121        public String getSelector() {
122            return this.selector;
123        }
124    
125        /**
126         * @param newSelector The selector to set.
127         */
128        public void setSelector(String newSelector) {
129            this.selector = newSelector;
130        }
131    
132        /**
133         * @return Returns the started.
134         */
135        public boolean isStarted() {
136            return this.started;
137        }
138    
139        /**
140         * @param flag to indicate if started
141         */
142        public void setStarted(boolean flag) {
143            this.started = flag;
144        }
145    
146        /**
147         * @return Returns the startTime.
148         */
149        public long getStartTime() {
150            return this.startTime;
151        }
152    
153        /**
154         * @param newStartTime The startTime to set.
155         */
156        public void setStartTime(long newStartTime) {
157            this.startTime = newStartTime;
158        }
159    
160        /**
161         * @return Returns the consumerNo.
162         */
163        public int getConsumerNo() {
164            return this.consumerNo;
165        }
166    
167        /**
168         * @param newConsumerNo The consumerNo to set.
169         */
170        public void setConsumerNo(int newConsumerNo) {
171            this.consumerNo = newConsumerNo;
172        }
173    
174        /**
175         * @return Returns the consumer name.
176         */
177        public String getConsumerName() {
178            return this.consumerName;
179        }
180    
181        /**
182         * @param newconsumerName The consumerName to set.
183         */
184        public void setConsumerName(String newconsumerName) {
185            this.consumerName = newconsumerName;
186        }
187    
188        /**
189         * @return Returns true if the Consumer is a durable Topic subscriber
190         */
191        public boolean isDurableTopic() {
192            return this.destination.isTopic() && !this.destination.isTemporary() && this.consumerName != null
193                    && this.consumerName.length() > 0;
194        }
195    
196        /**
197         * @return Returns the noLocal.
198         */
199        public boolean isNoLocal() {
200            return noLocal;
201        }
202    
203        /**
204         * @param noLocal The noLocal to set.
205         */
206        public void setNoLocal(boolean noLocal) {
207            this.noLocal = noLocal;
208        }
209    
210        /**
211         * @return Returns the browser.
212         */
213        public boolean isBrowser() {
214            return browser;
215        }
216    
217        /**
218         * @param browser The browser to set.
219         */
220        public void setBrowser(boolean browser) {
221            this.browser = browser;
222        }
223    
224        /**
225         * @return Returns the prefetchNumber.
226         */
227        public int getPrefetchNumber() {
228            return prefetchNumber;
229        }
230    
231        /**
232         * @param prefetchNumber The prefetchNumber to set.
233         */
234        public void setPrefetchNumber(int prefetchNumber) {
235            this.prefetchNumber = prefetchNumber;
236        }
237    
238    
239        /**
240         * Creates a primary key for the consumer info which uniquely
241         * describes the consumer using a combination of clientID and
242         * consumerName
243         *
244         * @return the consumerKey
245         */
246        public String getConsumerKey() {
247            if (consumerKey == null){
248                consumerKey = generateConsumerKey(clientId,consumerName);
249            }
250            return consumerKey;
251        }
252        
253        /**
254         * @return Returns the consumerIdentifier.
255         */
256        public String getConsumerId() {
257            if (consumerId == null){
258                consumerId = clientId + "." + sessionId + "." + consumerNo;
259            }
260            return consumerId;
261        }
262        /**
263         * @param consumerIdentifier The consumerIdentifier to set.
264         */
265        public void setConsumerId(String consumerIdentifier) {
266            this.consumerId = consumerIdentifier;
267        }
268        
269        
270        /**
271         * Generate a primary key for a consumer from the clientId and consumerName
272         * @param clientId
273         * @param consumerName
274         * @return
275         */
276        public static String generateConsumerKey(String clientId, String consumerName){
277            return "[" + clientId + ":" + consumerName + "]";
278        }
279    
280        /**
281         * @return true if the consumer is interested in advisory messages
282         */
283        public boolean isAdvisory(){
284            return destination != null && destination.isAdvisory();
285        }
286        
287        /**
288         * @return a pretty print
289         */
290        public String toString() {
291            return super.toString() + " ConsumerInfo{ " +
292                    "browser = " + browser +
293                    ", destination = " + destination +
294                    ", consumerIdentifier = '" + getConsumerId() + "' " +
295                    ", clientId = '" + clientId + "' " +
296                    ", sessionId = '" + sessionId + "' " +
297                    ", consumerName = '" + consumerName + "' " +
298                    ", selector = '" + selector + "' " +
299                    ", startTime = " + startTime +
300                    ", started = " + started +
301                    ", consumerNo = " + consumerNo +
302                    ", noLocal = " + noLocal +
303                    ", prefetchNumber = " + prefetchNumber +
304                    ", consumerKey = '" + getConsumerKey() + "' " +
305                    " }";
306        }
307    }