001: package org.concern.controller;
002:
003: import org.concern.*;
004:
005: import java.util.*;
006: import java.lang.ref.WeakReference;
007:
008: /**
009: * @author hengels
010: * @version $Revision$
011: */
012: public class Collaboration implements org.concern.Collaboration {
013: Map<String, Set<SetAwareWeakReference<Callable>>> callables = new HashMap<String, Set<SetAwareWeakReference<Callable>>>();
014: Map<String, Set<SetAwareWeakReference<Notifiable>>> notifiables = new HashMap<String, Set<SetAwareWeakReference<Notifiable>>>();
015:
016: public void register(String name, Callable callable) {
017: synchronized (callables) {
018: Set<SetAwareWeakReference<Callable>> set = callables
019: .get(name);
020: if (set == null) {
021: set = new HashSet<SetAwareWeakReference<Callable>>();
022: callables.put(name, set);
023: }
024: set.add(new SetAwareWeakReference<Callable>(callable));
025:
026: // maintainance
027: for (Iterator<Map.Entry<String, Set<SetAwareWeakReference<Callable>>>> iterator = callables
028: .entrySet().iterator(); iterator.hasNext();) {
029: Map.Entry<String, Set<SetAwareWeakReference<Callable>>> entry = iterator
030: .next();
031:
032: for (Iterator<SetAwareWeakReference<Callable>> iterator2 = entry
033: .getValue().iterator(); iterator2.hasNext();) {
034: SetAwareWeakReference<Callable> setAwareWeakReference = iterator2
035: .next();
036: if (setAwareWeakReference.get() == null)
037: iterator2.remove();
038: }
039: if (entry.getValue().isEmpty())
040: iterator.remove();
041: }
042: }
043: }
044:
045: public void register(String name, Notifiable notifiable) {
046: synchronized (notifiables) {
047: Set<SetAwareWeakReference<Notifiable>> set = notifiables
048: .get(name);
049: if (set == null) {
050: set = new HashSet<SetAwareWeakReference<Notifiable>>();
051: notifiables.put(name, set);
052: }
053: set.add(new SetAwareWeakReference<Notifiable>(notifiable));
054:
055: // maintainance
056: for (Iterator<Map.Entry<String, Set<SetAwareWeakReference<Notifiable>>>> iterator = notifiables
057: .entrySet().iterator(); iterator.hasNext();) {
058: Map.Entry<String, Set<SetAwareWeakReference<Notifiable>>> entry = iterator
059: .next();
060:
061: for (Iterator<SetAwareWeakReference<Notifiable>> iterator2 = entry
062: .getValue().iterator(); iterator2.hasNext();) {
063: SetAwareWeakReference<Notifiable> setAwareWeakReference = iterator2
064: .next();
065: if (setAwareWeakReference.get() == null)
066: iterator2.remove();
067: }
068: if (entry.getValue().isEmpty())
069: iterator.remove();
070: }
071: }
072: }
073:
074: public void call(Message message) throws RejectCallException {
075: Set<SetAwareWeakReference<Callable>> set = callables
076: .get(message.getName());
077: if (set == null)
078: return;
079:
080: for (Iterator<SetAwareWeakReference<Callable>> iterator = set
081: .iterator(); iterator.hasNext();) {
082: SetAwareWeakReference<Callable> setAwareWeakReference = iterator
083: .next();
084:
085: Callable callable = setAwareWeakReference.get();
086: if (callable != null)
087: callable.call(message);
088: }
089: }
090:
091: public void notify(Message message) {
092: Set<SetAwareWeakReference<Notifiable>> set = notifiables
093: .get(message.getName());
094: if (set == null)
095: return;
096:
097: for (Iterator<SetAwareWeakReference<Notifiable>> iterator = set
098: .iterator(); iterator.hasNext();) {
099: SetAwareWeakReference<Notifiable> setAwareWeakReference = iterator
100: .next();
101:
102: Notifiable notifiable = setAwareWeakReference.get();
103: if (notifiable != null)
104: notifiable.notify(message);
105: }
106: }
107:
108: private static class SetAwareWeakReference<T> extends
109: WeakReference<T> {
110: public SetAwareWeakReference(T referent) {
111: super (referent);
112: }
113:
114: public boolean equals(Object obj) {
115: Object o = get();
116: return o != null && o.equals(obj);
117: }
118:
119: public int hashCode() {
120: Object o = get();
121: return o != null ? o.hashCode() : -1;
122: }
123: }
124: }
|