PageRenderTime 44ms CodeModel.GetById 33ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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