1 22 package org.jboss.mq.sm.file; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.PrintStream ; 27 import java.io.InputStream ; 28 import java.io.BufferedInputStream ; 29 import java.io.IOException ; 30 import java.net.URL ; 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.Enumeration ; 34 35 import javax.jms.InvalidClientIDException ; 36 import javax.jms.JMSException ; 37 import javax.jms.JMSSecurityException ; 38 39 import org.jboss.system.server.ServerConfigLocator; 40 import org.jboss.mq.DurableSubscriptionID; 41 import org.jboss.mq.SpyTopic; 42 import org.jboss.mq.SpyJMSException; 43 import org.jboss.mq.xml.XElement; 44 import org.jboss.mq.xml.XElementException; 45 46 import org.jboss.mq.sm.StateManager; 47 import org.jboss.mq.sm.AbstractStateManager; 48 49 92 public class DynamicStateManager extends AbstractStateManager implements DynamicStateManagerMBean 93 { 94 class DynamicDurableSubscription extends DurableSubscription 95 { 96 XElement element; 97 public DynamicDurableSubscription(XElement element) throws XElementException 98 { 99 super( 100 element.getField("ClientID"), 101 element.getField("Name"), 102 element.getField("TopicName"), 103 element.getOptionalField("Selector")); 104 this.element = element; 105 } 106 XElement getElement() 107 { 108 return element; 109 } 110 111 117 } 118 119 125 boolean hasSecurityManager = true; 126 127 XElement stateConfig = new XElement("StateManager"); 129 130 private String stateFile = "jbossmq-state.xml"; 131 private URL systemConfigURL; 132 133 public DynamicStateManager() 134 { 135 136 } 137 public StateManager getInstance() 141 { 142 return this; 143 } 144 145 protected void createService() throws Exception 146 { 147 systemConfigURL = ServerConfigLocator.locate().getServerConfigURL(); 149 } 150 151 public void startService() throws Exception 152 { 153 loadConfig(); 154 } 155 156 159 public String displayStateConfig() throws Exception 160 { 161 return stateConfig.toString(); 162 } 163 164 171 public void setStateFile(String newStateFile) 172 { 173 stateFile = newStateFile.trim(); 174 } 175 176 183 public String getStateFile() 184 { 185 return stateFile; 186 } 187 188 191 public boolean hasSecurityManager() 192 { 193 return hasSecurityManager; 194 } 195 196 199 public void setHasSecurityManager(boolean hasSecurityManager) 200 { 201 this.hasSecurityManager = hasSecurityManager; 202 } 203 204 207 public void loadConfig() throws IOException , XElementException 208 { 209 URL configURL = new URL (systemConfigURL, stateFile); 210 if (log.isDebugEnabled()) 211 { 212 log.debug("Loading config from: " + configURL); 213 } 214 215 InputStream in = new BufferedInputStream (configURL.openStream()); 216 try 217 { 218 synchronized (stateConfig) 219 { 220 stateConfig = XElement.createFrom(in); 221 } 222 } 223 finally 224 { 225 in.close(); 226 } 227 } 228 229 232 public void saveConfig() throws IOException 233 { 234 URL configURL = new URL (systemConfigURL, stateFile); 235 236 if (configURL.getProtocol().equals("file")) 237 { 238 File file = new File (configURL.getFile()); 239 if (log.isDebugEnabled()) 240 { 241 log.debug("Saving config to: " + file); 242 } 243 244 PrintStream stream = new PrintStream (new FileOutputStream (file)); 245 try 246 { 247 synchronized (stateConfig) 248 { 249 stream.print(stateConfig.toXML(true)); 250 } 251 } 252 finally 253 { 254 stream.close(); 255 } 256 } 257 else 258 { 259 log.error("Can not save configuration to non-file URL: " + configURL); 260 } 261 } 262 270 protected String getPreconfClientId(String login, String passwd) throws JMSException 271 { 272 try 273 { 274 synchronized (stateConfig) 275 { 276 277 Enumeration enumeration = stateConfig.getElementsNamed("Users/User"); 278 while (enumeration.hasMoreElements()) 279 { 280 XElement element = (XElement) enumeration.nextElement(); 281 String name = element.getField("Name"); 282 if (!name.equals(login)) 283 { 284 continue; } 286 287 if (!hasSecurityManager) 289 { 290 String pw = element.getField("Password"); 291 if (!passwd.equals(pw)) 292 { 293 throw new JMSSecurityException ("Bad password"); 294 } 295 } 296 297 String clientId = null; 298 if (element.containsField("Id")) 299 { 300 clientId = element.getField("Id"); 301 } 302 303 return clientId; 305 } 306 if (!hasSecurityManager) 307 throw new JMSSecurityException ("This user does not exist"); 308 else 309 return null; 310 } 311 } 312 catch (XElementException e) 313 { 314 log.error(e); 315 throw new JMSException ("Invalid server user configuration."); 316 } 317 } 318 319 322 protected DurableSubscription getDurableSubscription(DurableSubscriptionID sub) throws JMSException 323 { 324 boolean debug = log.isDebugEnabled(); 325 326 try 328 { 329 synchronized (stateConfig) 330 { 331 332 Enumeration enumeration = stateConfig.getElementsNamed("DurableSubscriptions/DurableSubscription"); 333 while (enumeration.hasMoreElements()) 334 { 335 336 XElement dur = (XElement) enumeration.nextElement(); 338 if (dur.containsField("ClientID") && dur.getField("ClientID").equals(sub.getClientID())) 339 { 340 if (dur.getField("Name").equals(sub.getSubscriptionName())) 342 { 343 if (debug) 345 log.debug("Found a matching ClientID configuration section."); 346 return new DynamicDurableSubscription(dur); 347 } 348 349 } 350 } 351 return null; 353 } 354 } 355 catch (XElementException e) 356 { 357 JMSException newE = new SpyJMSException("Could not find durable subscription"); 358 newE.setLinkedException(e); 359 throw newE; 360 } 361 } 362 363 367 protected void checkLoggedOnClientId(String clientID) throws JMSException 368 { 369 synchronized (stateConfig) 370 { 371 372 Enumeration enumeration = stateConfig.getElementsNamed("Users/User"); 373 while (enumeration.hasMoreElements()) 374 { 375 XElement element = (XElement) enumeration.nextElement(); 376 try 377 { 378 if (element.containsField("Id") && element.getField("Id").equals(clientID)) 379 { 380 throw new InvalidClientIDException ("This loggedOnClientIds is password protected !"); 381 } 382 } 383 catch (XElementException ignore) 384 { 385 } 386 } 387 } 388 389 } 390 protected void saveDurableSubscription(DurableSubscription ds) throws JMSException 391 { 392 try 393 { 394 synchronized (stateConfig) 395 { 396 if (ds instanceof DynamicDurableSubscription) 399 { 400 XElement s = ((DynamicDurableSubscription) ds).getElement(); 401 if (s != null) 402 { 403 s.setField("TopicName", ds.getTopic()); s.setOptionalField("Selector", ds.getSelector()); } 406 else 407 { 408 throw new JMSException ("Can not save a null subscription"); 409 } 410 } 411 else 412 { 413 XElement dur = stateConfig.getElement("DurableSubscriptions"); 414 XElement subscription = new XElement("DurableSubscription"); 415 subscription.addField("ClientID", ds.getClientID()); 416 subscription.addField("Name", ds.getName()); 417 subscription.addField("TopicName", ds.getTopic()); 418 subscription.setOptionalField("Selector", ds.getSelector()); 419 dur.addElement(subscription); 420 } 421 saveConfig(); 422 } 423 } 424 catch (XElementException e) 425 { 426 JMSException newE = new SpyJMSException("Could not save the durable subscription"); 427 newE.setLinkedException(e); 428 throw newE; 429 } 430 catch (IOException e) 431 { 432 JMSException newE = new SpyJMSException("Could not save the durable subscription"); 433 newE.setLinkedException(e); 434 throw newE; 435 } 436 } 437 protected void removeDurableSubscription(DurableSubscription ds) throws JMSException 438 { 439 try 440 { 441 synchronized (stateConfig) 443 { 444 XElement s = ((DynamicDurableSubscription) ds).getElement(); 445 if (s != null) 446 { 447 s.removeFromParent(); 448 saveConfig(); 449 } 450 else 451 { 452 throw new JMSException ("Can not remove a null subscription"); 453 } 454 } 455 } 456 catch (XElementException e) 457 { 458 JMSException newE = new SpyJMSException("Could not remove the durable subscription"); 459 newE.setLinkedException(e); 460 throw newE; 461 } 462 catch (IOException e) 463 { 464 JMSException newE = new SpyJMSException("Could not remove the durable subscription"); 465 newE.setLinkedException(e); 466 throw newE; 467 } 468 } 469 470 public Collection getDurableSubscriptionIdsForTopic(SpyTopic topic) throws JMSException 471 { 472 Collection durableSubs = new ArrayList (); 473 try 474 { 475 synchronized (stateConfig) 476 { 477 478 Enumeration enumeration = stateConfig.getElementsNamed("DurableSubscriptions/DurableSubscription"); 479 while (enumeration.hasMoreElements()) 480 { 481 XElement element = (XElement) enumeration.nextElement(); 482 483 String clientId = element.getField("ClientID"); 484 String name = element.getField("Name"); 485 String topicName = element.getField("TopicName"); 486 String selector = element.getOptionalField("Selector"); 487 if (topic.getName().equals(topicName)) 488 { 489 durableSubs.add(new DurableSubscriptionID(clientId, name, selector)); 490 } 492 } 493 } 494 } 495 catch (XElementException e) 496 { 497 JMSException jmse = new JMSException ("Error in statemanager xml"); 498 jmse.setLinkedException(e); 499 throw jmse; 500 } return durableSubs; 502 } 503 504 508 511 public void addUser(String name, String password, String preconfID) throws Exception 512 { 513 if (findUser(name) != null) 514 throw new Exception ("Can not add, user exist"); 515 516 XElement users = stateConfig.getElement("Users"); 517 XElement user = new XElement("User"); 518 user.addField("Name", name); 519 user.addField("Password", password); 520 if (preconfID != null) 521 user.addField("Id", preconfID); 522 users.addElement(user); 523 saveConfig(); 524 } 525 526 529 public void removeUser(String name) throws Exception 530 { 531 XElement user = findUser(name); 532 if (user == null) 533 throw new Exception ("Cant remove user that does not exist"); 534 535 user.removeFromParent(); 536 537 String [] roles = getRoles(name); 539 if (roles != null) 540 { 541 for (int i = 0; i < roles.length; i++) 542 { 543 try 544 { 545 removeUserFromRole(roles[i], name); 546 } 547 catch (Exception ex) 548 { 549 } 551 } 552 } 553 554 saveConfig(); 555 } 556 557 560 public void addRole(String name) throws Exception 561 { 562 if (findRole(name) != null) 563 throw new Exception ("Cant add role, it already exists"); 564 565 XElement roles = stateConfig.getElement("Roles"); 566 XElement role = new XElement("Role"); 567 role.setAttribute("name", name); 568 roles.addElement(role); 569 saveConfig(); 570 } 571 572 575 public void removeRole(String name) throws Exception 576 { 577 XElement role = findRole(name); 578 if (role == null) 579 throw new Exception ("Cant remove role that does not exist"); 580 581 role.removeFromParent(); 582 saveConfig(); 583 } 584 585 589 public void addUserToRole(String roleName, String user) throws Exception 590 { 591 XElement role = findRole(roleName); 592 if (role == null) 593 throw new Exception ("Cant add to role that does not exist"); 594 595 if (findUser(user) == null) 596 throw new Exception ("Cant add user to role, user does to exist"); 597 598 if (findUserInRole(role, user) != null) 599 throw new Exception ("Cant add user to role, user already part of role"); 600 XElement u = new XElement("UserName"); 602 u.setValue(user); 603 role.addElement(u); 604 saveConfig(); 605 } 606 607 610 public void removeUserFromRole(String roleName, String user) throws Exception 611 { 612 XElement role = findRole(roleName); 613 if (role == null) 614 throw new Exception ("Cant remove user from role that does not exist"); 615 616 XElement u = findUserInRole(role, user); 617 if (u == null) 618 throw new Exception ("Cant remove user from role, user does not exist"); 619 u.removeFromParent(); 620 saveConfig(); 621 622 } 623 624 protected XElement findUser(String user) throws Exception 625 { 626 Enumeration enumeration = stateConfig.getElementsNamed("Users/User"); 627 while (enumeration.hasMoreElements()) 628 { 629 XElement element = (XElement) enumeration.nextElement(); 630 if (element.getField("Name").equals(user)) 631 return element; 632 } 633 return null; 634 } 635 636 protected XElement findRole(String role) throws Exception 637 { 638 Enumeration enumeration = stateConfig.getElementsNamed("Roles/Role"); 639 while (enumeration.hasMoreElements()) 640 { 641 XElement element = (XElement) enumeration.nextElement(); 642 if (element.getAttribute("name").equals(role)) 643 return element; 644 } 645 return null; 646 } 647 648 protected XElement findUserInRole(XElement role, String user) throws Exception 649 { 650 Enumeration enumeration = role.getElementsNamed("UserName"); 651 while (enumeration.hasMoreElements()) 652 { 653 XElement element = (XElement) enumeration.nextElement(); 654 if (user.equals(element.getValue())) 655 return element; 656 } 657 return null; 658 } 659 666 public String [] getRoles(String user) throws Exception 667 { 668 ArrayList roles = new ArrayList (); 669 Enumeration enumeration = stateConfig.getElementsNamed("Roles/Role"); 670 while (enumeration.hasMoreElements()) 671 { 672 XElement element = (XElement) enumeration.nextElement(); 673 XElement u = findUserInRole(element, user); 674 if (u != null) 675 roles.add(element.getAttribute("name")); 676 } 677 return (String []) roles.toArray(new String [roles.size()]); 678 } 679 680 684 public boolean validatePassword(String user, String inputPassword) throws Exception 685 { 686 boolean valid = false; 687 XElement u = findUser(user); 688 if (u != null) 689 { 690 String pw = u.getField("Password"); 691 if (inputPassword != null && inputPassword.equals(pw)) 692 valid = true; 693 } 694 return valid; 695 } 696 697 701 } | Popular Tags |