KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > Subscription


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq;
23
24 import java.io.Serializable JavaDoc;
25
26 import javax.jms.InvalidSelectorException JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28
29 import org.jboss.mq.selectors.Selector;
30
31 /**
32  * This class contains all the data needed to for a the provider to to
33  * determine if a message can be routed to a consumer.
34  *
35  * @author Hiram Chirino (Cojonudo14@hotmail.com)
36  * @author David Maplesden (David.Maplesden@orion.co.nz)
37  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
38  * @version $Revision: 37459 $
39  */

40 public class Subscription implements Serializable JavaDoc
41 {
42    // Constants -----------------------------------------------------
43

44    /** The serialVersionID */
45    private static final long serialVersionUID = -4045603824932803577L;
46    
47    // Attributes ----------------------------------------------------
48

49    /** This gets set to a unique value at the SpyConnection. */
50    public int subscriptionId;
51
52    /** The queue we want to subscribe to. */
53    public SpyDestination destination;
54
55    /** The selector which will filter out messages. */
56    public String JavaDoc messageSelector;
57
58    /** Should this message destroy the subscription? */
59    public boolean destroyDurableSubscription;
60
61    /** Topics might not want locally produced messages. */
62    public boolean noLocal;
63
64    /** The message selector */
65    public transient Selector selector;
66    
67    /** The connection token */
68    public transient ConnectionToken connectionToken;
69    
70    /** The client consumer */
71    public transient Object JavaDoc clientConsumer;
72    
73    // Static --------------------------------------------------------
74

75    // Constructors --------------------------------------------------
76

77    // Public --------------------------------------------------------
78

79    /**
80     * Determines the consumer would accept the message.
81     *
82     * @return the selector
83     * @throws InvalidSelectorException for an invalid selector
84     */

85    public Selector getSelector() throws InvalidSelectorException JavaDoc
86    {
87       if (messageSelector == null || messageSelector.trim().length() == 0)
88          return null;
89
90       if (selector == null)
91          selector = new Selector(messageSelector);
92
93       return selector;
94    }
95
96    /**
97     * Determines the consumer would accept the message.
98     *
99     * @param header the message header
100     * @return true when accepted, false otherwise
101     * @throws JMSException for any error
102     */

103    public boolean accepts(SpyMessage.Header header) throws JMSException JavaDoc
104    {
105       if (header.jmsDestination instanceof SpyTopic && noLocal && header.producerClientId.equals(connectionToken.getClientID()))
106          return false;
107
108       Selector ms = getSelector();
109       if (ms != null && !ms.test(header))
110          return false;
111       return true;
112    }
113
114    /**
115     * Clone the subscription
116     *
117     * @return the cloned subscription
118     */

119    public Subscription myClone()
120    {
121       Subscription result = new Subscription();
122       //only need to clone non-transient fields for our purposes.
123

124       result.subscriptionId = subscriptionId;
125       result.destination = destination;
126       result.messageSelector = messageSelector;
127       result.destroyDurableSubscription = destroyDurableSubscription;
128       result.noLocal = noLocal;
129
130       return result;
131    }
132    
133    // Object overrides ----------------------------------------------
134

135    public String JavaDoc toString()
136    {
137       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(100);
138       buffer.append("Subscription[subId=").append(subscriptionId);
139       if (connectionToken != null)
140          buffer.append("connection=").append(connectionToken);
141       buffer.append(" destination=").append(destination);
142       buffer.append(" messageSelector=").append(messageSelector);
143       if (noLocal)
144          buffer.append(" NoLocal");
145       else
146          buffer.append(" Local");
147       if (destroyDurableSubscription)
148          buffer.append(" Destroy");
149       else
150          buffer.append(" Create");
151
152       buffer.append(']');
153       return buffer.toString();
154    }
155    
156    // Package protected ---------------------------------------------
157

158    // Protected -----------------------------------------------------
159

160    // Private -------------------------------------------------------
161

162    // Inner classes -------------------------------------------------
163

164 }
Popular Tags