Collaboration.java :  » Workflow-Engines » osbl-1_0 » org » concern » controller » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » org » concern » controller » Collaboration.java
package org.concern.controller;

import org.concern.*;

import java.util.*;
import java.lang.ref.WeakReference;

/**
 * @author hengels
 * @version $Revision$
 */
public class Collaboration
    implements org.concern.Collaboration
{
    Map<String, Set<SetAwareWeakReference<Callable>>> callables = new HashMap<String, Set<SetAwareWeakReference<Callable>>>();
    Map<String, Set<SetAwareWeakReference<Notifiable>>> notifiables = new HashMap<String, Set<SetAwareWeakReference<Notifiable>>>();

    public void register(String name, Callable callable) {
        synchronized (callables) {
            Set<SetAwareWeakReference<Callable>> set = callables.get(name);
            if (set == null) {
                set = new HashSet<SetAwareWeakReference<Callable>>();
                callables.put(name, set);
            }
            set.add(new SetAwareWeakReference<Callable>(callable));

            // maintainance
            for (Iterator<Map.Entry<String,Set<SetAwareWeakReference<Callable>>>> iterator = callables.entrySet().iterator(); iterator.hasNext();) {
                Map.Entry<String, Set<SetAwareWeakReference<Callable>>> entry = iterator.next();

                for (Iterator<SetAwareWeakReference<Callable>> iterator2 = entry.getValue().iterator(); iterator2.hasNext();) {
                    SetAwareWeakReference<Callable> setAwareWeakReference = iterator2.next();
                    if (setAwareWeakReference.get() == null)
                        iterator2.remove();
                }
                if (entry.getValue().isEmpty())
                    iterator.remove();
            }
        }
    }

    public void register(String name, Notifiable notifiable) {
        synchronized (notifiables) {
            Set<SetAwareWeakReference<Notifiable>> set = notifiables.get(name);
            if (set == null) {
                set = new HashSet<SetAwareWeakReference<Notifiable>>();
                notifiables.put(name, set);
            }
            set.add(new SetAwareWeakReference<Notifiable>(notifiable));

            // maintainance
            for (Iterator<Map.Entry<String,Set<SetAwareWeakReference<Notifiable>>>> iterator = notifiables.entrySet().iterator(); iterator.hasNext();) {
                Map.Entry<String, Set<SetAwareWeakReference<Notifiable>>> entry = iterator.next();

                for (Iterator<SetAwareWeakReference<Notifiable>> iterator2 = entry.getValue().iterator(); iterator2.hasNext();) {
                    SetAwareWeakReference<Notifiable> setAwareWeakReference = iterator2.next();
                    if (setAwareWeakReference.get() == null)
                        iterator2.remove();
                }
                if (entry.getValue().isEmpty())
                    iterator.remove();
            }
        }
    }

    public void call(Message message) throws RejectCallException {
        Set<SetAwareWeakReference<Callable>> set = callables.get(message.getName());
        if (set == null)
            return;

        for (Iterator<SetAwareWeakReference<Callable>> iterator = set.iterator(); iterator.hasNext();) {
            SetAwareWeakReference<Callable> setAwareWeakReference = iterator.next();

            Callable callable = setAwareWeakReference.get();
            if (callable != null)
                callable.call(message);
        }
    }

    public void notify(Message message) {
        Set<SetAwareWeakReference<Notifiable>> set = notifiables.get(message.getName());
        if (set == null)
            return;

        for (Iterator<SetAwareWeakReference<Notifiable>> iterator = set.iterator(); iterator.hasNext();) {
            SetAwareWeakReference<Notifiable> setAwareWeakReference = iterator.next();

            Notifiable notifiable = setAwareWeakReference.get();
            if (notifiable != null)
                notifiable.notify(message);
        }
    }

    private static class SetAwareWeakReference<T>
        extends WeakReference<T>
    {
        public SetAwareWeakReference(T referent) {
            super(referent);
        }

        public boolean equals(Object obj) {
            Object o = get();
            return o != null && o.equals(obj);
        }

        public int hashCode() {
            Object o = get();
            return o != null ? o.hashCode() : -1;
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.