Coverage Report - org.mule.config.spring.ServerNotificationManagerConfigurator
 
Classes in this File Line Coverage Branch Coverage Complexity
ServerNotificationManagerConfigurator
0%
0/47
0%
0/22
0
 
 1  
 /*
 2  
  * $Id: ServerNotificationManagerConfigurator.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.config.spring;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.context.MuleContextAware;
 15  
 import org.mule.api.context.notification.ServerNotificationListener;
 16  
 import org.mule.context.notification.ListenerSubscriptionPair;
 17  
 import org.mule.context.notification.ServerNotificationManager;
 18  
 
 19  
 import java.util.Collection;
 20  
 import java.util.HashSet;
 21  
 import java.util.Map;
 22  
 import java.util.Set;
 23  
 
 24  
 import org.springframework.beans.BeansException;
 25  
 import org.springframework.beans.factory.SmartFactoryBean;
 26  
 import org.springframework.context.ApplicationContext;
 27  
 import org.springframework.context.ApplicationContextAware;
 28  
 
 29  0
 public class ServerNotificationManagerConfigurator
 30  
     implements MuleContextAware, SmartFactoryBean, ApplicationContextAware
 31  
 {
 32  
 
 33  
     private MuleContext muleContext;
 34  
     private ApplicationContext applicationContext;
 35  
 
 36  
     private Boolean dynamic;
 37  
     private Map interfaceToEvents;
 38  
     private Collection interfaces;
 39  
     private Collection<ListenerSubscriptionPair> pairs;
 40  
 
 41  
     public void setMuleContext(MuleContext context)
 42  
     {
 43  0
         this.muleContext = context;
 44  0
     }
 45  
 
 46  
     public Object getObject() throws Exception
 47  
     {
 48  0
         ServerNotificationManager notificationManager = muleContext.getNotificationManager();
 49  0
         if (dynamic != null)
 50  
         {
 51  0
             notificationManager.setNotificationDynamic(dynamic.booleanValue());
 52  
         }
 53  0
         if (interfaceToEvents != null)
 54  
         {
 55  0
             notificationManager.setInterfaceToTypes(interfaceToEvents);
 56  
         }
 57  0
         if (interfaces != null)
 58  
         {
 59  0
             notificationManager.setDisabledInterfaces(interfaces);
 60  
         }
 61  
 
 62  
         // Merge:
 63  
         // i) explicitly configured notification listeners,
 64  
         // ii) any singleton beans defined in spring that implement ServerNotificationListener.
 65  0
         Set<ListenerSubscriptionPair> subs = getMergedListeners(notificationManager);
 66  0
         for (ListenerSubscriptionPair sub : subs)
 67  
         {
 68  
             // Do this to avoid warnings when the Spring context is refreshed
 69  0
             if(!notificationManager.isListenerRegistered(sub.getListener()))
 70  
             {
 71  0
                 notificationManager.addListenerSubscriptionPair(sub);
 72  
             }
 73  
         }
 74  0
         return notificationManager;
 75  
     }
 76  
 
 77  
     protected Set<ListenerSubscriptionPair> getMergedListeners(ServerNotificationManager notificationManager)
 78  
     {
 79  0
         Set<ListenerSubscriptionPair> mergedListeners = new HashSet<ListenerSubscriptionPair>();
 80  
 
 81  
         // Any singleton bean defined in spring that implements
 82  
         // ServerNotificationListener or a subclass.
 83  0
         String[] listenerBeans = applicationContext.getBeanNamesForType(ServerNotificationListener.class,
 84  
             false, true);
 85  0
         Set<ListenerSubscriptionPair> adhocListeners = new HashSet<ListenerSubscriptionPair>();
 86  0
         for (String name : listenerBeans)
 87  
         {
 88  0
             adhocListeners.add(new ListenerSubscriptionPair(
 89  
                 (ServerNotificationListener<?>) applicationContext.getBean(name), null));
 90  
         }
 91  
 
 92  0
         if (pairs != null)
 93  
         {
 94  0
             mergedListeners.addAll(pairs);
 95  
 
 96  0
             for (ListenerSubscriptionPair candidate : adhocListeners)
 97  
             {
 98  0
                 boolean explicityDefined = false;
 99  0
                 for (ListenerSubscriptionPair explicitListener : pairs)
 100  
                 {
 101  0
                     if (candidate.getListener().equals(explicitListener.getListener()))
 102  
                     {
 103  0
                         explicityDefined = true;
 104  0
                         break;
 105  
                     }
 106  
                 }
 107  0
                 if (!explicityDefined)
 108  
                 {
 109  0
                     mergedListeners.add(candidate);
 110  
                 }
 111  0
             }
 112  
         }
 113  
         else
 114  
         {
 115  0
             mergedListeners.addAll(adhocListeners);
 116  
         }
 117  
 
 118  0
         return mergedListeners;
 119  
     }
 120  
 
 121  
     public Class getObjectType()
 122  
     {
 123  0
         return ServerNotificationManager.class;
 124  
     }
 125  
 
 126  
     public boolean isSingleton()
 127  
     {
 128  0
         return true;
 129  
     }
 130  
 
 131  
     public void setNotificationDynamic(boolean dynamic)
 132  
     {
 133  0
         this.dynamic = new Boolean(dynamic);
 134  0
     }
 135  
 
 136  
     public void setInterfaceToTypes(Map interfaceToEvents) throws ClassNotFoundException
 137  
     {
 138  0
         this.interfaceToEvents = interfaceToEvents;
 139  0
     }
 140  
 
 141  
     public void setAllListenerSubscriptionPairs(Collection pairs)
 142  
     {
 143  0
         this.pairs = pairs;
 144  0
     }
 145  
 
 146  
     public void setDisabledInterfaces(Collection interfaces) throws ClassNotFoundException
 147  
     {
 148  0
         this.interfaces = interfaces;
 149  0
     }
 150  
 
 151  
     public boolean isEagerInit()
 152  
     {
 153  0
         return true;
 154  
     }
 155  
 
 156  
     public boolean isPrototype()
 157  
     {
 158  0
         return false;
 159  
     }
 160  
 
 161  
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
 162  
     {
 163  0
         this.applicationContext = applicationContext;
 164  
 
 165  0
     }
 166  
 
 167  
 }