Coverage Report - org.mule.context.notification.Policy
 
Classes in this File Line Coverage Branch Coverage Complexity
Policy
0%
0/58
0%
0/54
0
 
 1  
 /*
 2  
  * $Id: Policy.java 20321 2010-11-24 15:21:24Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.context.notification;
 12  
 
 13  
 import org.mule.api.context.notification.ServerNotification;
 14  
 import org.mule.api.context.notification.ServerNotificationListener;
 15  
 
 16  
 import java.util.ArrayList;
 17  
 import java.util.Collection;
 18  
 import java.util.HashMap;
 19  
 import java.util.Iterator;
 20  
 import java.util.Map;
 21  
 import java.util.Set;
 22  
 
 23  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 24  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap;
 25  
 
 26  
 /**
 27  
  * For a particular configuration, this describes what events should be delivered where.
 28  
  * It is read-only and a lazy instance is cached by the
 29  
  * {@link Configuration}
 30  
  */
 31  
 class Policy
 32  
 {
 33  
 
 34  
     // map from event to set of senders
 35  0
     private Map<Class<? extends ServerNotification>, Collection<Sender>> eventToSenders = new HashMap<Class<? extends ServerNotification>, Collection<Sender>>();
 36  
 
 37  
     // these are cumulative - set values should never change, they are just a cache of known info
 38  
     // they are co and contra-variant wrt to exact event type (see code below).
 39  0
     private ConcurrentMap knownEventsExact = new ConcurrentHashMap();
 40  0
     private ConcurrentMap knownEventsSuper = new ConcurrentHashMap();
 41  
 
 42  
     /**
 43  
      * For each listener, we check each interface and see what events can be delivered.
 44  
      */
 45  
     Policy(Map<Class<? extends ServerNotificationListener>, Set<Class<? extends ServerNotification>>> interfaceToEvents, 
 46  
         Set<ListenerSubscriptionPair> listenerSubscriptionPairs, 
 47  
         Set<Class<? extends ServerNotificationListener>> disabledInterfaces, 
 48  
         Set<Class<? extends ServerNotification>> disabledEvents)
 49  0
     {
 50  0
         for (ListenerSubscriptionPair pair : listenerSubscriptionPairs)
 51  
         {
 52  0
             ServerNotificationListener listener = pair.getListener();
 53  0
             for (Class<? extends ServerNotificationListener> iface : interfaceToEvents.keySet())
 54  
             {
 55  0
                 if (notASubclassOfAnyClassInSet(disabledInterfaces, iface))
 56  
                 {
 57  0
                     if (iface.isAssignableFrom(listener.getClass()))
 58  
                     {
 59  0
                         Set<Class<? extends ServerNotification>> events = interfaceToEvents.get(iface);
 60  0
                         for (Class<? extends ServerNotification> event : events)
 61  
                         {
 62  0
                             if (notASubclassOfAnyClassInSet(disabledEvents, event))
 63  
                             {
 64  0
                                 knownEventsExact.put(event, Boolean.TRUE);
 65  0
                                 knownEventsSuper.put(event, Boolean.TRUE);
 66  0
                                 if (!eventToSenders.containsKey(event))
 67  
                                 {
 68  
                                     // use a collection with predictable iteration order
 69  0
                                     eventToSenders.put(event, new ArrayList<Sender>());
 70  
                                 }
 71  0
                                 eventToSenders.get(event).add(new Sender(pair));
 72  
                             }
 73  
                         }
 74  0
                     }
 75  
                 }
 76  
             }
 77  0
         }
 78  0
     }
 79  
 
 80  
     protected static boolean notASubclassOfAnyClassInSet(Set set,  Class clazz)
 81  
     {
 82  0
         for (Iterator iterator = set.iterator(); iterator.hasNext();)
 83  
         {
 84  0
             Class disabled = (Class) iterator.next();
 85  0
             if (disabled.isAssignableFrom(clazz))
 86  
             {
 87  0
                 return false;
 88  
             }
 89  0
         }
 90  0
         return true;
 91  
     }
 92  
 
 93  
     protected static boolean notASuperclassOfAnyClassInSet(Set set, Class clazz)
 94  
     {
 95  0
         for (Iterator iterator = set.iterator(); iterator.hasNext();)
 96  
         {
 97  0
             Class disabled = (Class) iterator.next();
 98  0
             if (clazz.isAssignableFrom(disabled))
 99  
             {
 100  0
                 return false;
 101  
             }
 102  0
         }
 103  0
         return true;
 104  
     }
 105  
 
 106  
     void dispatch(ServerNotification notification)
 107  
     {
 108  0
         if (null != notification)
 109  
         {
 110  0
             Class notfnClass = notification.getClass();
 111  
             // search if we don't know about this event, or if we do know it is used
 112  0
             if ((!knownEventsExact.containsKey(notfnClass))
 113  
                     || ((Boolean) knownEventsExact.get(notfnClass)).booleanValue())
 114  
             {
 115  0
                 boolean found = false;
 116  0
                 for (Class<? extends ServerNotification> event : eventToSenders.keySet())
 117  
                 {
 118  0
                     if (event.isAssignableFrom(notfnClass))
 119  
                     {
 120  0
                         found = true;
 121  0
                         for (Iterator senders = ((Collection) eventToSenders.get(event)).iterator(); senders.hasNext();)
 122  
                         {
 123  0
                             ((Sender) senders.next()).dispatch(notification);
 124  
                         }
 125  
                     }
 126  
                 }
 127  0
                 knownEventsExact.put(notfnClass, Boolean.valueOf(found));
 128  
             }
 129  
         }
 130  0
     }
 131  
 
 132  
     /**
 133  
      * This returns a very "conservative" value - it is true if the notification or any subclass would be
 134  
      * accepted.  So if it returns false then you can be sure that there is no need to send the
 135  
      * notification.  On the other hand, if it returns true there is no guarantee that the notification
 136  
      * "really" will be dispatched to any listener.
 137  
      *
 138  
      * @param notfnClass Either the notification class being generated or some superclass
 139  
      * @return false if there is no need to dispatch the notification
 140  
      */
 141  
     boolean isNotificationEnabled(Class notfnClass)
 142  
     {
 143  0
         if (!knownEventsSuper.containsKey(notfnClass))
 144  
         {
 145  0
             boolean found = false;
 146  
             // this is exhaustive because we initialise to include all events handled.
 147  0
             for (Iterator events = knownEventsSuper.keySet().iterator(); events.hasNext() && !found;)
 148  
             {
 149  0
                 Class event = (Class) events.next();
 150  0
                 found = ((Boolean) knownEventsSuper.get(event)).booleanValue() && notfnClass.isAssignableFrom(event);
 151  0
             }
 152  0
             knownEventsSuper.put(notfnClass, Boolean.valueOf(found));
 153  
         }
 154  0
         if (!knownEventsExact.containsKey(notfnClass))
 155  
         {
 156  0
             boolean found = false;
 157  0
             for (Iterator events = eventToSenders.keySet().iterator(); events.hasNext() && !found;)
 158  
             {
 159  0
                 Class event = (Class) events.next();
 160  0
                 found = event.isAssignableFrom(notfnClass);
 161  0
             }
 162  0
             knownEventsExact.put(notfnClass, Boolean.valueOf(found));
 163  
 
 164  
         }
 165  0
         return ((Boolean) knownEventsSuper.get(notfnClass)).booleanValue()
 166  
                 || ((Boolean) knownEventsExact.get(notfnClass)).booleanValue();
 167  
     }
 168  
 
 169  
 }