001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq;
023:
024: import java.io.Serializable;
025:
026: import javax.jms.InvalidSelectorException;
027: import javax.jms.JMSException;
028:
029: import org.jboss.mq.selectors.Selector;
030:
031: /**
032: * This class contains all the data needed to for a the provider to to
033: * determine if a message can be routed to a consumer.
034: *
035: * @author Hiram Chirino (Cojonudo14@hotmail.com)
036: * @author David Maplesden (David.Maplesden@orion.co.nz)
037: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
038: * @version $Revision: 57198 $
039: */
040: public class Subscription implements Serializable {
041: // Constants -----------------------------------------------------
042:
043: /** The serialVersionID */
044: private static final long serialVersionUID = -4045603824932803577L;
045:
046: // Attributes ----------------------------------------------------
047:
048: /** This gets set to a unique value at the SpyConnection. */
049: public int subscriptionId;
050:
051: /** The queue we want to subscribe to. */
052: public SpyDestination destination;
053:
054: /** The selector which will filter out messages. */
055: public String messageSelector;
056:
057: /** Should this message destroy the subscription? */
058: public boolean destroyDurableSubscription;
059:
060: /** Topics might not want locally produced messages. */
061: public boolean noLocal;
062:
063: /** The message selector */
064: public transient Selector selector;
065:
066: /** The connection token */
067: public transient ConnectionToken connectionToken;
068:
069: /** The client consumer */
070: public transient Object clientConsumer;
071:
072: // Static --------------------------------------------------------
073:
074: // Constructors --------------------------------------------------
075:
076: // Public --------------------------------------------------------
077:
078: /**
079: * Determines the consumer would accept the message.
080: *
081: * @return the selector
082: * @throws InvalidSelectorException for an invalid selector
083: */
084: public Selector getSelector() throws InvalidSelectorException {
085: if (messageSelector == null
086: || messageSelector.trim().length() == 0)
087: return null;
088:
089: if (selector == null)
090: selector = new Selector(messageSelector);
091:
092: return selector;
093: }
094:
095: /**
096: * Determines the consumer would accept the message.
097: *
098: * @param header the message header
099: * @return true when accepted, false otherwise
100: * @throws JMSException for any error
101: */
102: public boolean accepts(SpyMessage.Header header)
103: throws JMSException {
104: if (header.jmsDestination instanceof SpyTopic
105: && noLocal
106: && header.producerClientId.equals(connectionToken
107: .getClientID()))
108: return false;
109:
110: Selector ms = getSelector();
111: if (ms != null && !ms.test(header))
112: return false;
113: return true;
114: }
115:
116: /**
117: * Clone the subscription
118: *
119: * @return the cloned subscription
120: */
121: public Subscription myClone() {
122: Subscription result = new Subscription();
123: //only need to clone non-transient fields for our purposes.
124:
125: result.subscriptionId = subscriptionId;
126: result.destination = destination;
127: result.messageSelector = messageSelector;
128: result.destroyDurableSubscription = destroyDurableSubscription;
129: result.noLocal = noLocal;
130:
131: return result;
132: }
133:
134: // Object overrides ----------------------------------------------
135:
136: public String toString() {
137: StringBuffer buffer = new StringBuffer(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: }
|