/razpub/src/com/razie/pub/events/PostOffice.java

http://razpub.googlecode.com/ · Java · 131 lines · 58 code · 16 blank · 57 comment · 9 complexity · 8b676830abd6a5c3b1fe8a40aa8f21a2 MD5 · raw file

  1. /**
  2. * Razvan's public code. Copyright 2008 based on Apache license (share alike) see LICENSE.txt for
  3. * details.
  4. */
  5. package com.razie.pub.events;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import com.razie.pub.base.NoStatics;
  9. import com.razie.pub.events.RazDestination.QOS;
  10. import com.razie.pub.events.RazResource.RazResourceLocator;
  11. /**
  12. * just a utility to simplify the client code as much as possible...not always optimal from a
  13. * runtime perspective, but...
  14. *
  15. * @author razvanc
  16. */
  17. public class PostOffice implements RazResourceLocator {
  18. Map<String, RazDestination> destinations = new HashMap<String, RazDestination>();
  19. /** any context anywhere must have a default topic. This is used for all messages without topic. */
  20. public final static String DFLT_TOPIC = "razie/jms/topic/DefaultTopic";
  21. public final static String DFLT_LOCAL_TOPIC = "razie/jms/topic/DefaultLocalTopic";
  22. /** factory stub */
  23. public static PostOffice getInstance() {
  24. PostOffice singleton = (PostOffice) NoStatics.get(PostOffice.class);
  25. if (singleton == null) {
  26. singleton = (PostOffice) NoStatics.put(PostOffice.class, new PostOffice());
  27. // these destinations are default...
  28. singleton.register(PostOffice.DFLT_TOPIC, new RazTopic(PostOffice.DFLT_TOPIC, true, QOS.VOLANS));
  29. singleton.register(PostOffice.DFLT_LOCAL_TOPIC, new RazTopic(PostOffice.DFLT_LOCAL_TOPIC, false,
  30. QOS.VOLANS));
  31. }
  32. return singleton;
  33. }
  34. /**
  35. * main event notification thing - local AND remote
  36. *
  37. * @deprecated
  38. * @param srcId the id of the source of the event. normally of the form: "agentNm"
  39. * @param eventId the id of the event
  40. * @param args optional name-value pairs, i.e. "someparm", "somevalue", ... IF you only want to
  41. * pass an object which is the subject of the event, the convention is that it should be
  42. * named "subject".
  43. */
  44. public static void shout(String srcId, String eventId, Object... args) {
  45. PostOffice.send(PostOffice.DFLT_LOCAL_TOPIC, srcId, eventId, args);
  46. }
  47. /**
  48. * main event notification thing - local only
  49. *
  50. * @deprecated
  51. * @param srcId the id of the source of the event. normally of the form: "agentNm"
  52. * @param eventId the id of the event
  53. * @param args optional name-value pairs, i.e. "someparm", "somevalue", ... IF you only want to
  54. * pass an object which is the subject of the event, the convention is that it should be
  55. * named "subject".
  56. */
  57. public static void shoutLocal(String srcId, String eventId, Object... args) {
  58. PostOffice.send(PostOffice.DFLT_LOCAL_TOPIC, srcId, eventId, args);
  59. }
  60. /**
  61. * accept a message for a destination
  62. *
  63. * @param destName the name of the destination
  64. * @param srcId the id of the source of the event. normally of the form: "agentNm"
  65. * @param eventId the id of the event
  66. * @param args optional name-value pairs, i.e. "someparm", "somevalue", ... IF you only want to
  67. * pass an object which is the subject of the event, the convention is that it should be
  68. * named "subject".
  69. */
  70. public static void send(String destName, String srcId, String eventId, Object... args) {
  71. RazDestination d = getInstance().destinations.get(destName);
  72. if (d != null) {
  73. d.send(srcId, eventId, args);
  74. } else
  75. throw new IllegalStateException("Destination not found: " + destName);
  76. }
  77. public static RazDestination destination(String destName) {
  78. return getInstance().destinations.get(destName);
  79. }
  80. /**
  81. * register a smart/reflective event listener
  82. *
  83. * @param destinationName the name of the destination to register to
  84. * @param l the listener
  85. */
  86. public static void register(String destinationName, EvListener l) {
  87. RazDestination d = getInstance().destinations.get(destinationName);
  88. if (d != null) {
  89. for (String eventId : l.interestedIn())
  90. d.register(eventId, l);
  91. } else
  92. throw new IllegalStateException("Destination not found: " + destinationName);
  93. }
  94. /**
  95. * register a smart/reflective event listener
  96. *
  97. * @param destinationName the name of the destination to register to
  98. * @param eventId the ID of the event to listen to
  99. * @param l the listener
  100. */
  101. public static void register(String destinationName, String eventId, EvListener l) {
  102. RazDestination d = getInstance().destinations.get(destinationName);
  103. if (d != null) {
  104. d.register(eventId, l);
  105. } else
  106. throw new IllegalStateException("Destination not found: " + destinationName);
  107. }
  108. // TODO is this just for testing?
  109. public RazResource locate(String name) {
  110. return destinations.get(name);
  111. }
  112. public void register(String name, RazResource res) {
  113. destinations.put(name, (RazDestination) res);
  114. }
  115. }