1 22 package org.jboss.mq.sm.file; 23 24 import java.io.BufferedInputStream ; 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.PrintStream ; 30 import java.net.URL ; 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.Enumeration ; 34 import java.util.HashSet ; 35 import java.util.Set ; 36 37 import javax.jms.InvalidClientIDException ; 38 import javax.jms.JMSException ; 39 import javax.jms.JMSSecurityException ; 40 41 import org.jboss.mq.DurableSubscriptionID; 42 import org.jboss.mq.SpyJMSException; 43 import org.jboss.mq.SpyTopic; 44 import org.jboss.mq.server.JMSDestinationManager; 45 import org.jboss.mq.server.JMSTopic; 46 import org.jboss.mq.sm.StateManager; 47 import org.jboss.mq.xml.XElement; 48 import org.jboss.mq.xml.XElementException; 49 import org.jboss.system.ServiceMBeanSupport; 50 import org.jboss.system.server.ServerConfigLocator; 51 52 64 public class OldStateManager 65 extends ServiceMBeanSupport 66 implements StateManager, OldStateManagerMBean 67 { 68 XElement stateConfig; 69 70 private final Set loggedOnClientIds = new HashSet (); 71 private String stateFile = "conf/default/jbossmq-state.xml"; 72 private URL systemConfigURL; 73 74 79 public OldStateManager() throws XElementException 80 { 81 } 83 84 protected void createService() throws Exception { 85 systemConfigURL = ServerConfigLocator.locate().getServerConfigURL(); 87 } 88 89 96 public void setStateFile(String newStateFile) 97 { 98 stateFile = newStateFile.trim(); 99 } 100 101 104 public StateManager getInstance() 105 { 106 return this; 107 } 108 109 117 public void setDurableSubscription(JMSDestinationManager server, DurableSubscriptionID sub, SpyTopic topic) 118 throws JMSException 119 { 120 boolean debug = log.isDebugEnabled(); 121 if (debug) 122 log.debug("Checking durable subscription: " + sub + ", on topic: " + topic); 123 try 124 { 125 Enumeration enumeration = stateConfig.getElementsNamed("User"); 127 while (enumeration.hasMoreElements()) 128 { 129 130 XElement user = (XElement)enumeration.nextElement(); 132 if (!user.containsField("Id") || !user.getField("Id").equals(sub.getClientID())) 133 { 134 continue; 135 } 136 137 if (debug) 138 log.debug("Found a matching ClientID configuration section."); 139 140 XElement subscription = null; 141 142 Enumeration enum2 = user.getElementsNamed("DurableSubscription"); 144 while (enum2.hasMoreElements()) 145 { 146 XElement t = (XElement)enum2.nextElement(); 147 if (t.getField("Name").equals(sub.getSubscriptionName())) 148 { 149 subscription = t; 150 break; 151 } 152 } 153 154 if (subscription == null) 156 { 157 if (debug) 158 log.debug("The subscription was not previously registered."); 159 if (topic == null) 161 { 162 return; 163 } 164 165 JMSTopic dest = (JMSTopic)server.getJMSDestination(topic); 166 dest.createDurableSubscription(sub); 167 168 subscription = new XElement("DurableSubscription"); 169 subscription.addField("Name", sub.getSubscriptionName()); 170 subscription.addField("TopicName", topic.getName()); 171 user.addElement(subscription); 172 173 saveConfig(); 174 175 } 176 else 179 { 180 if (debug) 181 log.debug("The subscription was previously registered."); 182 183 if (topic == null) 188 { 189 SpyTopic prevTopic = new SpyTopic(subscription.getField("TopicName")); 191 JMSTopic dest = (JMSTopic)server.getJMSDestination(prevTopic); 192 195 dest.destroyDurableSubscription(sub); 196 197 subscription.removeFromParent(); 199 saveConfig(); 200 } 201 else if (!subscription.getField("TopicName").equals(topic.getName())) 205 { 206 if (debug) 208 log.debug("But the topic was different, changing the subscription."); 209 SpyTopic prevTopic = new SpyTopic(subscription.getField("TopicName")); 211 JMSTopic dest = (JMSTopic)server.getJMSDestination(prevTopic); 212 dest.destroyDurableSubscription(sub); 213 214 dest = (JMSTopic)server.getJMSDestination(topic); 215 dest.createDurableSubscription(sub); 216 217 subscription.setField("TopicName", topic.getName()); 219 saveConfig(); 220 } 221 } 222 return; 223 } 224 225 throw new JMSException ("ClientID '" + sub.getClientID() + 227 "' cannot create durable subscriptions."); 228 } 229 catch (IOException e) 230 { 231 JMSException newE = new SpyJMSException("Could not setup the durable subscription"); 232 newE.setLinkedException(e); 233 throw newE; 234 } 235 catch (XElementException e) 236 { 237 JMSException newE = new SpyJMSException("Could not setup the durable subscription"); 238 newE.setLinkedException(e); 239 throw newE; 240 } 241 242 } 243 244 247 public SpyTopic getDurableTopic(DurableSubscriptionID sub) throws JMSException 248 { 249 try { 250 XElement subscription = getSubscription(sub); 251 if (subscription == null) 252 throw new JMSException ("No durable subscription found for subscription: " + sub.getSubscriptionName()); 253 254 return new SpyTopic(subscription.getField("TopicName")); 255 }catch(XElementException e) 256 { 257 JMSException newE = new SpyJMSException("Could not find durable subscription"); 258 newE.setLinkedException(e); 259 throw newE; 260 } 261 } 262 263 private XElement getSubscription(DurableSubscriptionID sub) throws JMSException ,XElementException { 264 boolean debug = log.isDebugEnabled(); 265 266 XElement subscription = null; 268 Enumeration enumeration = stateConfig.getElementsNamed("User"); 269 while (enumeration.hasMoreElements()) 270 { 271 272 XElement user = (XElement)enumeration.nextElement(); 274 if (!user.containsField("Id") || !user.getField("Id").equals(sub.getClientID())) 275 { 276 continue; 277 } 278 279 if (debug) 280 log.debug("Found a matching ClientID configuration section."); 281 282 284 Enumeration enum2 = user.getElementsNamed("DurableSubscription"); 286 while (enum2.hasMoreElements()) 287 { 288 XElement t = (XElement)enum2.nextElement(); 289 if (t.getField("Name").equals(sub.getSubscriptionName())) 290 { 291 subscription = t; 292 return subscription; 294 } 295 } 296 } 297 return subscription; 299 } 300 301 308 public String getStateFile() 309 { 310 return stateFile; 311 } 312 313 321 public String checkUser(String login, String passwd) throws JMSException 322 { 323 try 324 { 325 synchronized (stateConfig) 326 { 327 328 Enumeration enumeration = stateConfig.getElementsNamed("User"); 329 while (enumeration.hasMoreElements()) 330 { 331 XElement element = (XElement)enumeration.nextElement(); 332 String name = element.getField("Name"); 333 if (!name.equals(login)) 334 { 335 continue; 336 } 337 338 String pw = element.getField("Password"); 339 if (!passwd.equals(pw)) 340 { 341 throw new JMSException ("Bad password"); 342 } 343 344 String clientId = null; 345 if (element.containsField("Id")) 346 { 347 clientId = element.getField("Id"); 348 } 349 350 if (clientId != null) 351 { 352 synchronized (loggedOnClientIds) 353 { 354 if (loggedOnClientIds.contains(clientId)) 355 { 356 throw new JMSSecurityException 357 ("The login id has an assigned client id. " + 358 "That client id is already connected to the server!"); 359 } 360 loggedOnClientIds.add(clientId); 361 } 362 } 363 364 return clientId; 365 } 366 throw new JMSSecurityException ("This user does not exist"); 367 } 368 } 369 catch (XElementException e) 370 { 371 log.error(e); 372 throw new JMSException ("Invalid server user configuration."); 373 } 374 } 375 376 385 public void addLoggedOnClientId(String ID) throws JMSException 386 { 387 388 synchronized (loggedOnClientIds) 390 { 391 if (loggedOnClientIds.contains(ID)) 392 { 393 throw new InvalidClientIDException ("This loggedOnClientIds is already registered !"); 394 } 395 } 396 397 synchronized (stateConfig) 399 { 400 Enumeration enumeration = stateConfig.getElementsNamed("User"); 401 while (enumeration.hasMoreElements()) 402 { 403 XElement element = (XElement)enumeration.nextElement(); 404 try 405 { 406 if (element.containsField("Id") && element.getField("Id").equals(ID)) 407 { 408 throw new InvalidClientIDException ("This loggedOnClientIds is password protected !"); 409 } 410 } 411 catch (XElementException ignore) 412 { 413 } 414 } 415 416 } 417 synchronized (loggedOnClientIds) 418 { 419 loggedOnClientIds.add(ID); 420 } 421 } 422 423 428 public void removeLoggedOnClientId(String ID) 429 { 430 synchronized (loggedOnClientIds) 431 { 432 loggedOnClientIds.remove(ID); 433 } 434 } 435 436 441 public void startService() throws Exception 442 { 443 444 loadConfig(); 445 } 446 447 public Collection getDurableSubscriptionIdsForTopic(SpyTopic topic) 448 throws JMSException 449 { 450 Collection durableSubs = new ArrayList (); 451 try 452 { 453 Enumeration enumeration = stateConfig.getElementsNamed("User/DurableSubscription"); 454 while (enumeration.hasMoreElements()) 455 { 456 XElement element = (XElement)enumeration.nextElement(); 457 458 String clientId = element.getField("../Id"); 459 String name = element.getField("Name"); 460 String topicName = element.getField("TopicName"); 461 if (topic.getName().equals(topicName)) 462 { 463 durableSubs.add(new DurableSubscriptionID(clientId, name, null)); 464 } 466 } 467 } 468 catch (XElementException e) 469 { 470 JMSException jmse = new JMSException ("Error in statemanager xml"); 471 jmse.setLinkedException(e); 472 throw jmse; 473 } return durableSubs; 475 } 476 477 483 523 524 527 public void loadConfig() throws IOException , XElementException 528 { 529 URL configURL = new URL (systemConfigURL, stateFile); 530 if (log.isDebugEnabled()) { 531 log.debug("Loading config from: " + configURL); 532 } 533 534 InputStream in = new BufferedInputStream (configURL.openStream()); 535 try { 536 stateConfig = XElement.createFrom(in); 537 } 538 finally { 539 in.close(); 540 } 541 } 542 543 546 public void saveConfig() throws IOException 547 { 548 URL configURL = new URL (systemConfigURL, stateFile); 549 550 if (configURL.getProtocol().equals("file")) { 551 File file = new File (configURL.getFile()); 552 if (log.isDebugEnabled()) { 553 log.debug("Saving config to: " + file); 554 } 555 556 PrintStream stream = new PrintStream (new FileOutputStream (file)); 557 try { 558 stream.print(stateConfig.toXML(true)); 559 } 560 finally { 561 stream.close(); 562 } 563 } 564 else { 565 log.error("Can not save configuration to non-file URL: " + configURL); 566 } 567 } 568 569 572 public String displayStateConfig() throws Exception 573 { 574 return stateConfig.toString(); 575 } 576 577 596 } 597 | Popular Tags |