Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable

Thursday, May 20, 2010

GWT-GXT Building message-event framework/bus

I decided to go with the simplest event model, based on message broad-cast. The fundamental principle is to have a central universal listener and receiver, all interested parties would hang-around this guy.


|--------|
| |
Sender1 -------\ | | |------ Listener 1
Sender2 --------\-----| Event- |------|------ Listener 2
Sender3 --------/-----| Bus |------|------ Listener 3
Sender4 -------/ | | |------ Listener 4
| |
|--------|


Sender


package org.myxyz.listeners;

//import com.google.gwt.event.shared.HandlerManager;
//import com.google.gwt.event.shared.HandlerRegistration;

import org.myxyz.custom.X10HandlerManager;
import org.myxyz.custom.X10HandlerRegistration;

public class MessageSender{

/**
*
*/
private static final long serialVersionUID = -382336739840212254L;

private MessageExchange exchange = MessageExchange.getMessageExchange();
private X10HandlerManager sender = null;

public MessageSender() {
sender = new X10HandlerManager(this);
X10HandlerRegistration handlerRegistration = sender.addHandler(MessageEvent.getType(), exchange);
exchange.registerSender(this, handlerRegistration);
}

/**
* Fire an event.
*/
public void fireEvent(MessageEvent event) {
sender.fireEvent(event);
}

}



MessageListener


package org.myxyz.listeners;

import com.google.gwt.event.shared.EventHandler;

public abstract class MessageListener<T> implements EventHandler{

private MessageExchange exchange = MessageExchange.getMessageExchange();
private T source = null;

public MessageListener(){

}

public MessageListener(T source){
exchange.addHandler(this);
this.source = source;
}

public T getSource(){
return source;
}

public abstract void messageRecieved(MessageEvent pEvent);

}



MessageEvent


package org.myxyz.listeners;

import org.myxyz.custom.X10GwtEvent;

//import com.google.gwt.event.shared.GwtEvent;

@SuppressWarnings("unchecked")
public class MessageEvent extends X10GwtEvent<MessageListener> {

private static final X10GwtEvent.Type<MessageListener> TYPE = new X10GwtEvent.Type<MessageListener>();
private String eventType = null;
private Object newValue = null;
private Object oldValue = null;
private Object source = null;

public static Type<MessageListener> getType() {
return TYPE;
}

public MessageEvent(String eventType) {
super();
this.eventType = eventType;
}

public MessageEvent(String eventType, Object oldValue, Object newValue) {
super();
this.eventType = eventType;
this.oldValue = oldValue;
this.newValue = newValue;
}

public MessageEvent(String eventType, Object oldValue, Object newValue, Object source) {
super();
this.eventType = eventType;
this.oldValue = oldValue;
this.newValue = newValue;
this.source = source;
}

@Override
protected void dispatch(MessageListener handler) {
handler.messageRecieved(this);
}

@Override
public Type<MessageListener> getAssociatedType() {
return TYPE;
}

public String getEventType() {
return eventType;
}

public Object getNewValue() {
return newValue;
}

public Object getOldValue() {
return oldValue;
}

public Object getSource() {
return source;
}
}



MessageExchange



package org.myxyz.listeners;

import java.util.HashMap;
import java.util.Iterator;

import com.google.gwt.event.shared.EventHandler;
import org.myxyz.custom.X10HandlerManager;
import org.myxyz.custom.X10HandlerRegistration;

@SuppressWarnings("unchecked")
public class MessageExchange extends MessageListener {

/**
*
*/
private static final long serialVersionUID = 1623583201346713682L;
private static final MessageExchange singleton = new MessageExchange();

private X10HandlerManager dispatcher = new X10HandlerManager(this);
private HashMap<MessageSender,X10HandlerRegistration> registeredSenders = new HashMap<MessageSender,X10HandlerRegistration>(10);
private HashMap<MessageListener,X10HandlerRegistration> registeredListeners = new HashMap<MessageListener,X10HandlerRegistration>(10);

public static MessageExchange getMessageExchange(){
return singleton;
}

private MessageExchange() {
}

public void messageRecieved(MessageEvent pEvent) {
dispatcher.fireEvent(pEvent);
}

public synchronized <H extends EventHandler> X10HandlerRegistration addHandler(MessageListener handler) {
X10HandlerRegistration handlerRegistration = registeredListeners.get(handler);
if(handlerRegistration == null) {
handlerRegistration = dispatcher.addHandler(MessageEvent.getType(), handler);
registeredListeners.put(handler, handlerRegistration);
}
return handlerRegistration;
}

public int getHandlerCount() {
return dispatcher.getHandlerCount(MessageEvent.getType());
}

public synchronized <H extends EventHandler> void removeHandler(MessageListener handler) {
if(registeredListeners.get(handler) != null){
registeredListeners.get(handler).removeHandler();
registeredListeners.remove(handler);
}
}

public synchronized void registerSender(MessageSender sender, X10HandlerRegistration handlerRegistration){
if(handlerRegistration != null) {
registeredSenders.put(sender, handlerRegistration);
}
}

public synchronized void unRegisterSender(MessageSender sender){
X10HandlerRegistration handlerRegistration = registeredSenders.get(sender);
if(handlerRegistration != null) {
handlerRegistration.removeHandler();
registeredSenders.remove(sender);
}
}

@Override
protected void finalize() throws Throwable {
for (Iterator<X10HandlerRegistration> iterator = registeredListeners.values().iterator(); iterator.hasNext();) {
X10HandlerRegistration handlerRegistration = iterator.next();
handlerRegistration.removeHandler();
}
for (Iterator<X10HandlerRegistration> iterator = registeredSenders.values().iterator(); iterator.hasNext();) {
X10HandlerRegistration handlerRegistration = iterator.next();
handlerRegistration.removeHandler();
}
}

}

No comments:

Popular Posts

Labels

About Me

Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.