1 25 package org.objectweb.joram.client.jms; 26 27 import java.net.ConnectException ; 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.List ; 31 import java.util.ListIterator ; 32 import java.util.Properties ; 33 import java.util.Vector ; 34 35 import javax.naming.*; 36 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 import org.objectweb.joram.client.jms.admin.DeadMQueue; 40 import org.objectweb.joram.client.jms.admin.User; 41 import org.objectweb.joram.client.jms.admin.AdministeredObject; 42 import org.objectweb.joram.client.jms.admin.AdminModule; 43 import org.objectweb.joram.client.jms.admin.AdminException; 44 import org.objectweb.joram.client.jms.admin.XmlSerializer; 45 import org.objectweb.joram.shared.admin.*; 46 47 import org.objectweb.util.monolog.api.BasicLevel; 48 import org.objectweb.joram.shared.JoramTracing; 49 50 import fr.dyade.aaa.util.management.MXWrapper; 51 52 56 public abstract class Destination 57 extends AdministeredObject 58 implements javax.jms.Destination , DestinationMBean { 59 60 protected String agentId; 61 62 63 protected String adminName; 64 65 private String type; 66 67 public Destination() {} 69 70 public Destination(String type) { 71 this.type = type; 72 } 73 74 protected Destination(String name, String type) { 75 agentId = name; 76 this.type = type; 77 } 78 79 80 public String getName() { 81 return agentId; 82 } 83 84 85 public final String getAdminName() { 86 return adminName; 87 } 88 89 public final String getType() { 90 return type; 91 } 92 93 97 public boolean equals(Object obj) { 98 if (! (obj instanceof Destination)) 99 return false; 100 101 return (getName().equals(((Destination) obj).getName())); 102 } 103 104 109 public String toString() { 110 StringBuffer strbuf = new StringBuffer (); 111 strbuf.append(type).append(agentId); 112 if (adminName != null) 113 strbuf.append('(').append(adminName).append(')'); 114 return strbuf.toString(); 115 } 116 117 125 public String toXml(int indent, int serverId) throws ConnectException , AdminException { 126 StringBuffer strbuf = new StringBuffer (); 127 128 strbuf.append(XmlSerializer.indent(indent)); 129 130 if (getType().equals("queue")) { 131 strbuf.append("<Queue "); 132 } else if (getType().equals("topic")) { 133 strbuf.append("<Topic "); 134 } else { 135 return ""; 136 } 137 strbuf.append(XmlSerializer.xmlAttribute(getAdminName(), "name")); 138 strbuf.append(XmlSerializer.xmlAttribute(String.valueOf(serverId), "serverId")); 139 DeadMQueue dmq = getDMQ(); 140 if (dmq != null) { 141 strbuf.append(XmlSerializer.xmlAttribute(dmq.getAdminName(), "dmq")); 142 strbuf.append(XmlSerializer.xmlAttribute(String.valueOf(dmq.getThreshold()), "threshold")); 143 } 144 145 strbuf.append(">\n"); 146 147 indent+=2; 148 149 if (isFreelyReadable()) { 150 strbuf.append(XmlSerializer.indent(indent)); 151 strbuf.append("<freeReader/>\n"); 152 } 153 154 if (isFreelyWriteable()) { 155 strbuf.append(XmlSerializer.indent(indent)); 156 strbuf.append("<freeWriter/>\n"); 157 } 158 159 List readers = getReaders(); 160 for (ListIterator iterator = readers.listIterator(); iterator.hasNext(); ) { 161 User user = (User) (iterator.next()); 162 strbuf.append(XmlSerializer.indent(indent)); 163 strbuf.append("<reader "); 164 strbuf.append(XmlSerializer.xmlAttribute(user.getName(), "user")); 165 strbuf.append("/>\n"); 166 } 167 168 List writers = getWriters(); 169 for (ListIterator iterator = writers.listIterator(); iterator.hasNext(); ) { 170 User user = (User) (iterator.next()); 171 strbuf.append(XmlSerializer.indent(indent)); 172 strbuf.append("<writer "); 173 strbuf.append(XmlSerializer.xmlAttribute(user.getName(), "user")); 174 strbuf.append("/>\n"); 175 } 176 177 178 strbuf.append(XmlSerializer.indent(indent)); 179 strbuf.append("<jndi "); 180 strbuf.append(XmlSerializer.xmlAttribute(getAdminName(), "name")); 181 strbuf.append("/>\n"); 182 183 indent-=2; 184 185 strbuf.append(XmlSerializer.indent(indent)); 186 187 if (getType().equals("queue")) { 188 strbuf.append("</Queue>\n"); 189 } else if (getType().equals("topic")) { 190 strbuf.append("</Topic>\n"); 191 } 192 193 return strbuf.toString(); 194 } 195 196 199 public boolean isQueue() { 200 return (this instanceof javax.jms.Queue ); 201 } 202 203 public static final String QUEUE = 204 "org.objectweb.joram.mom.dest.Queue"; 205 public static final String TOPIC = 206 "org.objectweb.joram.mom.dest.Topic"; 207 public static final String DEAD_MQUEUE = 208 "org.objectweb.joram.mom.dest.DeadMQueue"; 209 public static final String CLUSTER_QUEUE = 210 "org.objectweb.joram.mom.dest.ClusterQueue"; 211 public static final String BRIDGE_QUEUE = 212 "org.objectweb.joram.mom.dest.BridgeQueue"; 213 public static final String BRIDGE_TOPIC = 214 "org.objectweb.joram.mom.dest.BridgeTopic"; 215 public static final String MAIL_QUEUE = 216 "com.scalagent.joram.mom.dest.mail.JavaMailQueue"; 217 public static final String MAIL_TOPIC = 218 "com.scalagent.joram.mom.dest.mail.JavaMailTopic"; 219 public static final String SCHEDULER_QUEUE = 220 "com.scalagent.joram.mom.dest.scheduler.SchedulerQueue"; 221 222 238 protected static void doCreate( 239 int serverId, 240 String name, 241 String className, 242 Properties props, 243 Destination dest, 244 String expectedType) 245 throws ConnectException , AdminException { 246 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 247 JoramTracing.dbgClient.log( 248 BasicLevel.DEBUG, 249 "Destination.doCreate(" + 250 serverId + ',' + name + ',' + 251 className + ',' + props + ',' + 252 dest + ',' + expectedType + ')'); 253 254 CreateDestinationRequest cdr = 255 new CreateDestinationRequest(serverId, 256 name, 257 className, 258 props, 259 expectedType); 260 CreateDestinationReply reply = 261 (CreateDestinationReply) AdminModule.doRequest(cdr); 262 dest.agentId = reply.getId(); 263 dest.adminName = name; 264 dest.type = reply.getType(); 265 } 266 267 274 public void delete() 275 throws ConnectException , AdminException, javax.jms.JMSException { 276 AdminModule.doRequest(new DeleteDestination(getName())); 277 if (MXWrapper.mxserver != null) { 278 StringBuffer buff = new StringBuffer (); 279 buff.append("type="); 280 buff.append(getType()); 281 buff.append(",name="); 282 buff.append(getAdminName()); 283 try { 284 MXWrapper.unregisterMBean("joramClient",buff.toString()); 285 } catch (Exception e) { 286 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 287 JoramTracing.dbgClient.log(BasicLevel.DEBUG, 288 "unregisterMBean",e); 289 } 290 } 291 } 292 293 301 public void setFreeReading() throws ConnectException , AdminException 302 { 303 AdminModule.doRequest(new SetReader(null, getName())); 304 } 305 306 314 public void setFreeWriting() throws ConnectException , AdminException 315 { 316 AdminModule.doRequest(new SetWriter(null, getName())); 317 } 318 319 327 public void unsetFreeReading() throws ConnectException , AdminException 328 { 329 AdminModule.doRequest(new UnsetReader(null, getName())); 330 } 331 332 340 public void unsetFreeWriting() throws ConnectException , AdminException 341 { 342 AdminModule.doRequest(new UnsetWriter(null, getName())); 343 } 344 345 355 public void setReader(User user) throws ConnectException , AdminException 356 { 357 AdminModule.doRequest(new SetReader(user.getProxyId(), getName())); 358 } 359 360 361 public void addReader(String proxyId) 362 throws ConnectException , AdminException { 363 AdminModule.doRequest(new SetReader(proxyId, getName())); 364 } 365 366 376 public void setWriter(User user) throws ConnectException , AdminException 377 { 378 AdminModule.doRequest(new SetWriter(user.getProxyId(), getName())); 379 } 380 381 382 public void addWriter(String proxyId) 383 throws ConnectException , AdminException { 384 AdminModule.doRequest(new SetWriter(proxyId, getName())); 385 } 386 387 397 public void unsetReader(User user) 398 throws ConnectException , AdminException { 399 AdminModule.doRequest(new UnsetReader(user.getProxyId(), getName())); 400 } 401 402 403 public void removeReader(String proxyId) 404 throws ConnectException , AdminException { 405 AdminModule.doRequest(new UnsetReader(proxyId, getName())); 406 } 407 408 418 public void unsetWriter(User user) 419 throws ConnectException , AdminException { 420 AdminModule.doRequest(new UnsetWriter(user.getProxyId(), getName())); 421 } 422 423 424 public void removeWriter(String proxyId) 425 throws ConnectException , AdminException { 426 AdminModule.doRequest(new UnsetWriter(proxyId, getName())); 427 } 428 429 443 public void setDMQ(DeadMQueue dmq) throws ConnectException , AdminException 444 { 445 if (dmq == null) 446 AdminModule.doRequest(new UnsetDestinationDMQ(getName())); 447 else 448 AdminModule.doRequest(new SetDestinationDMQ(getName(), dmq.getName())); 449 } 450 451 461 public List getReaders() throws ConnectException , AdminException 462 { 463 Monitor_GetReaders request = new Monitor_GetReaders(getName()); 464 Monitor_GetUsersRep reply = 465 (Monitor_GetUsersRep) AdminModule.doRequest(request); 466 467 Vector list = new Vector (); 468 Hashtable users = reply.getUsers(); 469 String name; 470 for (Enumeration names = users.keys(); names.hasMoreElements();) { 471 name = (String ) names.nextElement(); 472 list.add(new User(name, (String ) users.get(name))); 473 } 474 return list; 475 } 476 477 478 public List getReaderList() throws ConnectException , AdminException { 479 Vector list = new Vector (); 480 List readers = getReaders(); 481 for (ListIterator iterator = readers.listIterator(); iterator.hasNext(); ) { 482 list.add(iterator.next().toString()); 483 } 484 return list; 485 } 486 487 497 public List getWriters() throws ConnectException , AdminException 498 { 499 Monitor_GetWriters request = new Monitor_GetWriters(getName()); 500 Monitor_GetUsersRep reply = 501 (Monitor_GetUsersRep) AdminModule.doRequest(request); 502 503 Vector list = new Vector (); 504 Hashtable users = reply.getUsers(); 505 String name; 506 for (Enumeration names = users.keys(); names.hasMoreElements();) { 507 name = (String ) names.nextElement(); 508 list.add(new User(name, (String ) users.get(name))); 509 } 510 return list; 511 } 512 513 514 public List getWriterList() throws ConnectException , AdminException { 515 Vector list = new Vector (); 516 List readers = getWriters(); 517 for (ListIterator iterator = readers.listIterator(); iterator.hasNext(); ) { 518 list.add(iterator.next().toString()); 519 } 520 return list; 521 } 522 523 532 public boolean isFreelyReadable() throws ConnectException , AdminException 533 { 534 Monitor_GetFreeAccess request = new Monitor_GetFreeAccess(getName()); 535 Monitor_GetFreeAccessRep reply; 536 reply = (Monitor_GetFreeAccessRep) AdminModule.doRequest(request); 537 538 return reply.getFreeReading(); 539 } 540 541 542 public void setFreelyReadable(boolean b) 543 throws ConnectException , AdminException { 544 if (b) 545 setFreeReading(); 546 else 547 unsetFreeReading(); 548 } 549 550 559 public boolean isFreelyWriteable() throws ConnectException , AdminException 560 { 561 Monitor_GetFreeAccess request = new Monitor_GetFreeAccess(getName()); 562 Monitor_GetFreeAccessRep reply; 563 reply = (Monitor_GetFreeAccessRep) AdminModule.doRequest(request); 564 565 return reply.getFreeWriting(); 566 } 567 568 569 public void setFreelyWriteable(boolean b) 570 throws ConnectException , AdminException { 571 if (b) 572 setFreeWriting(); 573 else 574 unsetFreeWriting(); 575 } 576 577 586 public DeadMQueue getDMQ() throws ConnectException , AdminException 587 { 588 Monitor_GetDMQSettings request = new Monitor_GetDMQSettings(getName()); 589 Monitor_GetDMQSettingsRep reply; 590 reply = (Monitor_GetDMQSettingsRep) AdminModule.doRequest(request); 591 592 if (reply.getDMQName() == null) { 593 return null; 594 } else { 595 return new DeadMQueue(reply.getDMQName()); 596 } 597 } 598 599 public static Destination newInstance( 600 String id, 601 String name, 602 String type) throws AdminException { 603 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 604 JoramTracing.dbgClient.log(BasicLevel.DEBUG, 605 "Destination.newInstance(" + 606 id + ',' + name + ',' + type + ')'); 607 Destination dest; 608 if (Queue.isQueue(type)) { 609 if (TemporaryQueue.isTemporaryQueue(type)) { 610 dest = new TemporaryQueue(id, null); 611 } else if (DeadMQueue.isDeadMQueue(type)) { 612 dest = new DeadMQueue(id); 613 } else { 614 dest = new Queue(id); 615 } 616 } else if (Topic.isTopic(type)) { 617 if (TemporaryTopic.isTemporaryTopic(type)) { 618 dest = new TemporaryTopic(id, null); 619 } else { 620 dest = new Topic(id); 621 } 622 } else throw new AdminException("Unknown destination type"); 623 dest.adminName = name; 624 return dest; 625 } 626 627 public static boolean isAssignableTo(String realType, 628 String resultingType) { 629 return realType.startsWith(resultingType); 630 } 631 632 public Hashtable getStatistic() 633 throws ConnectException , AdminException { 634 Monitor_GetStat request = 635 new Monitor_GetStat(agentId); 636 Monitor_GetStatRep reply = 637 (Monitor_GetStatRep) AdminModule.doRequest(request); 638 return reply.getStats(); 639 } 640 641 642 public void toReference(Reference ref) throws NamingException { 643 ref.add(new StringRefAddr("dest.agentId", agentId)); 644 ref.add(new StringRefAddr("dest.adminName", adminName)); 645 } 646 647 648 public void fromReference(Reference ref) throws NamingException { 649 agentId = (String ) ref.get("dest.agentId").getContent(); 650 adminName = (String ) ref.get("dest.adminName").getContent(); 651 } 652 653 657 public Hashtable code() { 658 Hashtable h = new Hashtable (); 659 h.put("agentId", getName()); 660 h.put("type", type); 661 return h; 662 } 663 664 public void decode(Hashtable h) { 665 agentId = (String ) h.get("agentId"); 666 type = (String ) h.get("type"); 667 } 668 } 669 | Popular Tags |