Coverage Report - org.mule.context.notification.Configuration
 
Classes in this File Line Coverage Branch Coverage Complexity
Configuration
0%
0/65
0%
0/24
0
 
 1  
 /*
 2  
  * $Id: Configuration.java 19191 2010-08-25 21:05:23Z tcarlson $
 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  
 import org.mule.config.i18n.CoreMessages;
 16  
 import static org.mule.context.notification.ServerNotificationManager.*;
 17  
 
 18  
 import java.util.Collection;
 19  
 import java.util.Collections;
 20  
 import java.util.HashMap;
 21  
 import java.util.HashSet;
 22  
 import java.util.Iterator;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 /**
 30  
  * This acts as a synchronized collection.  No call blocks and all are synchronized.
 31  
  */
 32  0
 class Configuration
 33  
 {
 34  
 
 35  0
     protected Log logger = LogFactory.getLog(getClass());
 36  0
     private Map<Class<? extends ServerNotificationListener>, Set<Class<? extends ServerNotification>>> interfaceToTypes =
 37  
             new HashMap<Class<? extends ServerNotificationListener>, Set<Class<? extends ServerNotification>>>(); // map from interface to collection of events
 38  0
     private Set<ListenerSubscriptionPair> listenerSubscriptionPairs = new HashSet<ListenerSubscriptionPair>();
 39  0
     private Set<Class<? extends ServerNotificationListener>> disabledInterfaces = new HashSet<Class<? extends ServerNotificationListener>>();
 40  0
     private Set<Class<? extends ServerNotification>> disabledNotificationTypes = new HashSet<Class<? extends ServerNotification>>();
 41  0
     private boolean dirty = true;
 42  
     private Policy policy;
 43  
 
 44  
     synchronized void addInterfaceToType(Class<? extends ServerNotificationListener> iface, Class<? extends ServerNotification> type)
 45  
     {
 46  0
         dirty = true;
 47  0
         if (!ServerNotification.class.isAssignableFrom(type))
 48  
         {
 49  0
             throw new IllegalArgumentException(
 50  
                     CoreMessages.propertyIsNotSupportedType("type",
 51  
                             ServerNotification.class, type).getMessage());
 52  
         }
 53  0
         if (!interfaceToTypes.containsKey(iface))
 54  
         {
 55  0
             interfaceToTypes.put(iface, new HashSet<Class<? extends ServerNotification>>());
 56  
         }
 57  0
         Set<Class<? extends ServerNotification>> events = interfaceToTypes.get(iface);
 58  0
         events.add(type);
 59  0
         if (logger.isDebugEnabled())
 60  
         {
 61  0
             logger.debug("Registered event type: " + type);
 62  0
             logger.debug("Binding listener type '" + iface + "' to event type '" + type + "'");
 63  
         }
 64  0
     }
 65  
 
 66  
     /**
 67  
      * @param interfaceToTypes map from interace to a particular event
 68  
      * @throws ClassNotFoundException if the interface is a key, but the corresponding class cannot be loaded
 69  
      */
 70  
     synchronized void addAllInterfaceToTypes(Map<Class<? extends ServerNotificationListener>, Set<Class<? extends ServerNotification>>> interfaceToTypes) throws ClassNotFoundException
 71  
     {
 72  0
         dirty = true;
 73  
        
 74  0
         for (Iterator ifaces = interfaceToTypes.keySet().iterator(); ifaces.hasNext();)
 75  
         {
 76  0
             Object iface = ifaces.next();
 77  0
             addInterfaceToType(toClass(iface), toClass(interfaceToTypes.get(iface)));
 78  0
         }
 79  0
     }
 80  
 
 81  
     synchronized void addListenerSubscriptionPair(ListenerSubscriptionPair pair)
 82  
     {
 83  0
         dirty = true;
 84  0
         if (!listenerSubscriptionPairs.add(pair))
 85  
         {
 86  0
             logger.warn(CoreMessages.notificationListenerSubscriptionAlreadyRegistered(pair));
 87  
         }
 88  0
     }
 89  
 
 90  
     synchronized void addAllListenerSubscriptionPairs(Collection pairs)
 91  
     {
 92  0
         dirty = true;
 93  0
         for (Iterator listener = pairs.iterator(); listener.hasNext();)
 94  
         {
 95  0
             addListenerSubscriptionPair((ListenerSubscriptionPair) listener.next());
 96  
         }
 97  0
     }
 98  
 
 99  
     synchronized void removeListener(ServerNotificationListener listener)
 100  
     {
 101  0
         dirty = true;
 102  0
         Set<ListenerSubscriptionPair> toRemove = new HashSet<ListenerSubscriptionPair>();
 103  0
         for (Iterator listeners = listenerSubscriptionPairs.iterator(); listeners.hasNext();)
 104  
         {
 105  0
             ListenerSubscriptionPair pair = (ListenerSubscriptionPair) listeners.next();
 106  0
             if (pair.getListener().equals(listener))
 107  
             {
 108  0
                 toRemove.add(pair);
 109  
             }
 110  0
         }
 111  0
         listenerSubscriptionPairs.removeAll(toRemove);
 112  0
     }
 113  
 
 114  
     synchronized void removeAllListeners(Collection listeners)
 115  
     {
 116  0
         dirty = true;
 117  0
         for (Iterator listener = listeners.iterator(); listener.hasNext();)
 118  
         {
 119  0
             removeListener((ServerNotificationListener) listener.next());
 120  
         }
 121  0
     }
 122  
 
 123  
     synchronized void disableInterface(Class<? extends ServerNotificationListener> iface)
 124  
     {
 125  0
         dirty = true;
 126  0
         disabledInterfaces.add(iface);
 127  0
     }
 128  
 
 129  
     synchronized void disabledAllInterfaces(Collection<Class<? extends ServerNotificationListener>> interfaces) throws ClassNotFoundException
 130  
     {
 131  0
         dirty = true;
 132  0
         for (Iterator iface = interfaces.iterator(); iface.hasNext();)
 133  
         {
 134  0
             disableInterface(toClass(iface.next()));
 135  
         }
 136  0
     }
 137  
 
 138  
     synchronized void disableType(Class<? extends ServerNotification> type)
 139  
     {
 140  0
         dirty = true;
 141  0
         disabledNotificationTypes.add(type);
 142  0
     }
 143  
 
 144  
     synchronized void disableAllTypes(Collection types) throws ClassNotFoundException
 145  
     {
 146  0
         dirty = true;
 147  0
         for (Iterator event = types.iterator(); event.hasNext();)
 148  
         {
 149  0
             disableType(toClass(event.next()));
 150  
         }
 151  0
     }
 152  
 
 153  
     synchronized Policy getPolicy()
 154  
     {
 155  0
         if (dirty)
 156  
         {
 157  0
             policy = new Policy(interfaceToTypes, listenerSubscriptionPairs, disabledInterfaces, disabledNotificationTypes);
 158  0
             dirty = false;
 159  
         }
 160  0
         return policy;
 161  
     }
 162  
 
 163  
     // for tests -------------------------------
 164  
 
 165  
     Map<Class<? extends ServerNotificationListener>, Set<Class<? extends ServerNotification>>> getInterfaceToTypes()
 166  
     {
 167  0
         return Collections.unmodifiableMap(interfaceToTypes);
 168  
     }
 169  
 
 170  
     Set<ListenerSubscriptionPair> getListeners()
 171  
     {
 172  0
         return Collections.unmodifiableSet(listenerSubscriptionPairs);
 173  
     }
 174  
 
 175  
 }