1 48 package org.exolab.jms.client; 49 50 51 import java.io.Externalizable ; 52 import java.io.IOException ; 53 import java.io.ObjectInput ; 54 import java.io.ObjectOutput ; 55 import java.util.StringTokenizer ; 56 57 import javax.jms.JMSException ; 58 import javax.jms.Topic ; 59 import javax.naming.Reference ; 60 import javax.naming.Referenceable ; 61 import javax.naming.StringRefAddr ; 62 63 64 73 public class JmsTopic 74 extends JmsDestination 75 implements Topic , Externalizable , Referenceable { 76 77 80 static final long serialVersionUID = 1; 81 82 public static final String WILDCARD = "*"; 84 85 public static final String ALL_WILDCARD = "**"; 87 88 public static final String SEPARATOR = "."; 90 91 92 95 public JmsTopic() { 96 } 97 98 103 public JmsTopic(String name) { 104 super(name); 105 } 106 107 113 public String getTopicName() 114 throws JMSException { 115 return getName(); 116 } 117 118 119 public boolean equals(Object object) { 121 boolean result = false; 122 123 if ((object instanceof JmsTopic) && 124 (((JmsTopic) object).getName().equals(this.getName()))) { 125 result = true; 126 } 127 128 return result; 129 } 130 131 public void writeExternal(ObjectOutput stream) 133 throws IOException { 134 stream.writeLong(serialVersionUID); 135 super.writeExternal(stream); 136 } 137 138 public void readExternal(ObjectInput stream) 140 throws IOException , ClassNotFoundException { 141 long version = stream.readLong(); 142 if (version == serialVersionUID) { 143 super.readExternal(stream); 144 } else { 145 throw new IOException ("JmsTopic with version " + 146 version + " is not supported."); 147 } 148 } 149 150 public int hashCode() { 152 return getName().hashCode(); 153 } 154 155 160 public boolean isWildCard() { 161 return isWildCard(this.getName()); 162 } 163 164 171 public static boolean isWildCard(String topic) { 172 int pos = topic.indexOf(ALL_WILDCARD); 175 176 if (pos >= 0 && (pos != topic.length() - 2)) { 177 return false; 178 } 179 180 pos = topic.indexOf(WILDCARD); 181 182 if (pos >= 0) { 185 StringTokenizer tokens = new StringTokenizer (topic, SEPARATOR); 186 String token = null; 187 188 while (tokens.hasMoreTokens()) { 189 token = tokens.nextToken(); 190 if (token.indexOf(WILDCARD) >= 0) { 192 if (!(token.equals(WILDCARD) || 193 token.equals(ALL_WILDCARD))) { 194 return false; 195 } 196 } 197 } 198 } 199 200 return (pos >= 0); 201 } 202 203 230 public boolean match(JmsTopic destination) { 231 boolean matches = false; 232 String topic = destination.getName(); 233 String wildcard = this.getName(); 234 if (wildcard.equals(ALL_WILDCARD)) { 235 matches = true; 237 } else { 238 StringTokenizer wildTokens = 239 new StringTokenizer (wildcard, SEPARATOR); 240 StringTokenizer topicTokens = 241 new StringTokenizer (topic, SEPARATOR); 242 String wildToken = null; 243 String topicToken = null; 244 int tokenCountDiff = 245 topicTokens.countTokens() - wildTokens.countTokens(); 246 if ((tokenCountDiff == 0) || 247 (tokenCountDiff == -1) || 248 (tokenCountDiff > 0 && wildcard.indexOf(ALL_WILDCARD) >= 0)) { 249 while (wildTokens.hasMoreTokens() && 250 topicTokens.hasMoreTokens()) { 251 wildToken = wildTokens.nextToken(); 252 topicToken = topicTokens.nextToken(); 253 if (wildToken.equals(ALL_WILDCARD)) { 254 matches = true; 256 break; 257 } else if (wildToken.equals(WILDCARD)) { 258 matches = true; 260 continue; 261 } else if (wildToken.equals(topicToken)) { 262 matches = true; 264 continue; 265 } else { 266 matches = false; 268 break; 269 } 270 } 271 } 272 } 273 274 return matches; 275 } 276 277 public Reference getReference() { 279 Reference reference = null; 280 281 reference = new Reference (JmsTopic.class.getName(), 283 new StringRefAddr ("name", getName()), 284 JmsDestinationFactory.class.getName(), null); 285 286 reference.add(new StringRefAddr ("persistent", 288 (getPersistent() ? "true" : "false"))); 289 290 return reference; 291 } 292 } 293 294 | Popular Tags |