1 22 package org.jboss.mq.sm; 23 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 28 import javax.jms.InvalidClientIDException ; 29 import javax.jms.InvalidDestinationException ; 30 import javax.jms.JMSException ; 31 import javax.jms.JMSSecurityException ; 32 33 import org.jboss.mq.DurableSubscriptionID; 34 import org.jboss.mq.SpyTopic; 35 import org.jboss.mq.server.JMSDestinationManager; 36 import org.jboss.mq.server.JMSTopic; 37 import org.jboss.system.ServiceMBeanSupport; 38 import org.jboss.util.JBossStringBuilder; 39 40 53 54 public abstract class AbstractStateManager extends ServiceMBeanSupport 55 implements StateManager, AbstractStateManagerMBean 56 { 57 58 private final Set loggedOnClientIds = new HashSet (); 59 60 63 public AbstractStateManager() 64 { 65 } 66 67 public void setDurableSubscription(JMSDestinationManager server, DurableSubscriptionID sub, SpyTopic topic) 68 throws JMSException 69 { 70 boolean debug = log.isDebugEnabled(); 71 if (debug) 72 log.debug("Checking durable subscription: " + sub + ", on topic: " + topic); 73 74 DurableSubscription subscription = getDurableSubscription(sub); 75 76 if (subscription == null) 78 { 79 if (debug) 80 log.debug("The subscription was not previously registered " + sub); 81 if (topic == null) 84 return; 85 JMSTopic dest = (JMSTopic) server.getJMSDestination(topic); 87 if (dest == null) 88 throw new InvalidDestinationException ("Topic does not exist: " + topic); 89 dest.createDurableSubscription(sub); 90 91 subscription = new DurableSubscription(sub.getClientID(), sub.getSubscriptionName(), topic.getName(), sub 93 .getSelector()); 94 saveDurableSubscription(subscription); 96 } 97 else 100 { 101 if (debug) 102 log.debug("The subscription was previously registered: " + subscription); 103 104 String newSelector = sub.getSelector(); 105 String oldSelector = subscription.getSelector(); 106 boolean selectorChanged = false; 107 if ((newSelector == null && oldSelector != null) 108 || (newSelector != null && newSelector.equals(oldSelector) == false)) 109 selectorChanged = true; 110 111 if (topic == null) 116 { 117 if (debug) 118 log.debug("Removing subscription: " + subscription); 119 SpyTopic prevTopic = new SpyTopic(subscription.getTopic()); 121 JMSTopic dest = (JMSTopic) server.getJMSDestination(prevTopic); 122 if (dest == null) 123 throw new InvalidDestinationException ("Topic does not exist: " + prevTopic); 124 127 dest.destroyDurableSubscription(sub); 128 129 removeDurableSubscription(subscription); 131 } 132 else if (!subscription.getTopic().equals(topic.getName()) || selectorChanged) 135 { 136 if (debug) 138 log.debug("But the topic or selector was different, changing the subscription."); 139 SpyTopic prevTopic = new SpyTopic(subscription.getTopic()); 141 JMSTopic dest = (JMSTopic) server.getJMSDestination(prevTopic); 142 if (dest == null) 143 throw new InvalidDestinationException ("Previous topic does not exist: " + prevTopic); 144 dest.destroyDurableSubscription(sub); 145 146 dest = (JMSTopic) server.getJMSDestination(topic); 148 if (dest == null) 149 throw new InvalidDestinationException ("Topic does not exist: " + topic); 150 dest.createDurableSubscription(sub); 151 152 subscription.setTopic(topic.getName()); 154 subscription.setSelector(sub.getSelector()); 155 saveDurableSubscription(subscription); 156 } 157 } 158 } 159 160 public SpyTopic getDurableTopic(DurableSubscriptionID sub) throws JMSException 161 { 162 DurableSubscription subscription = getDurableSubscription(sub); 163 if (subscription == null) 164 throw new InvalidDestinationException ("No durable subscription found for subscription: " 165 + sub.getSubscriptionName()); 166 167 return new SpyTopic(subscription.getTopic()); 168 } 169 170 public String checkUser(String login, String passwd) throws JMSException 171 { 172 String clientId = getPreconfClientId(login, passwd); 173 174 if (clientId != null) 175 { 176 synchronized (loggedOnClientIds) 177 { 178 if (loggedOnClientIds.contains(clientId)) 179 { 180 throw new JMSSecurityException 181 ("The login id has an assigned client id '" + clientId + 182 "', that is already connected to the server!"); 183 } 184 loggedOnClientIds.add(clientId); 185 } 186 } 187 188 return clientId; 189 } 190 191 public void addLoggedOnClientId(String ID) throws JMSException 192 { 193 synchronized (loggedOnClientIds) 194 { 195 if (loggedOnClientIds.contains(ID)) 196 throw new InvalidClientIDException ("This client id '" + ID + "' is already registered!"); 197 } 198 199 checkLoggedOnClientId(ID); 200 201 synchronized (loggedOnClientIds) 202 { 203 loggedOnClientIds.add(ID); 204 } 205 if (log.isTraceEnabled()) 206 log.trace("Client id '" + ID + "' is logged in."); 207 } 208 209 public void removeLoggedOnClientId(String ID) 210 { 211 synchronized (loggedOnClientIds) 212 { 213 loggedOnClientIds.remove(ID); 214 } 215 if (log.isTraceEnabled()) 216 log.trace("Client id '" + ID + "' is logged out."); 217 } 218 219 abstract public Collection getDurableSubscriptionIdsForTopic(SpyTopic topic) throws JMSException ; 220 221 230 abstract protected String getPreconfClientId(String login, String passwd) throws JMSException ; 231 232 239 abstract protected void checkLoggedOnClientId(String clientID) throws JMSException ; 240 241 248 abstract protected DurableSubscription getDurableSubscription(DurableSubscriptionID sub) throws JMSException ; 249 250 262 abstract protected void saveDurableSubscription(DurableSubscription ds) throws JMSException ; 263 264 272 abstract protected void removeDurableSubscription(DurableSubscription ds) throws JMSException ; 273 274 279 protected class DurableSubscription 280 { 281 282 String clientID; 283 284 285 String name; 286 287 288 String topic; 289 290 291 String selector; 292 293 296 public DurableSubscription() 297 { 298 } 299 300 308 public DurableSubscription(String clientID, String name, String topic, String selector) 309 { 310 this.clientID = clientID; 311 this.name = name; 312 this.topic = topic; 313 this.selector = selector; 314 } 315 316 321 public String getClientID() 322 { 323 return clientID; 324 } 325 326 331 public String getName() 332 { 333 return name; 334 } 335 336 341 public String getTopic() 342 { 343 return topic; 344 } 345 346 351 public void setTopic(String topic) 352 { 353 this.topic = topic; 354 } 355 356 361 public String getSelector() 362 { 363 return selector; 364 } 365 366 371 public void setSelector(String selector) 372 { 373 this.selector = selector; 374 } 375 376 public String toString() 377 { 378 JBossStringBuilder buffer = new JBossStringBuilder(); 379 buffer.append("DurableSub[clientID=").append(clientID); 380 buffer.append(" name=").append(name); 381 buffer.append(" topic=").append(topic); 382 buffer.append(" selector=").append(selector); 383 buffer.append(']'); 384 return buffer.toString(); 385 } 386 } 387 } | Popular Tags |