Clover coverage report - Code Coverage for hivemind release 1.0-rc-1
Coverage timestamp: Wed Aug 25 2004 13:06:02 EDT
file stats: LOC: 193   Methods: 7
NCLOC: 136   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
EventLinkerImpl.java 100% 92.9% 100% 95.5%
coverage coverage
 1   
 //  Copyright 2004 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.hivemind.service.impl;
 16   
 
 17   
 import java.beans.BeanInfo;
 18   
 import java.beans.EventSetDescriptor;
 19   
 import java.beans.IntrospectionException;
 20   
 import java.beans.Introspector;
 21   
 import java.lang.reflect.Method;
 22   
 import java.util.HashMap;
 23   
 import java.util.Map;
 24   
 
 25   
 import org.apache.commons.logging.Log;
 26   
 import org.apache.hivemind.ErrorHandler;
 27   
 import org.apache.hivemind.HiveMind;
 28   
 import org.apache.hivemind.Location;
 29   
 import org.apache.hivemind.impl.BaseLocatable;
 30   
 import org.apache.hivemind.service.EventLinker;
 31   
 
 32   
 /**
 33   
  * Implementation of {@link org.apache.hivemind.service.EventLinker}. Will output warnings
 34   
  * whenever a consumer can't be registered for at least one event set (which can happen
 35   
  * when the consumer does not implement the necessary interfaces).
 36   
  *
 37   
  * @author Howard Lewis Ship
 38   
  */
 39   
 public class EventLinkerImpl extends BaseLocatable implements EventLinker
 40   
 {
 41   
     private Log _log;
 42   
     private ErrorHandler _errorHandler;
 43   
 
 44   
     /**
 45   
      * Map of {@link java.beans.EventSetDescriptor}[], keyed on producer class.
 46   
      */
 47   
     private Map _producerEventSets;
 48   
 
 49  9
     public EventLinkerImpl(Log log, ErrorHandler errorHandler)
 50   
     {
 51  9
         _log = log;
 52  9
         _errorHandler = errorHandler;
 53   
     }
 54   
 
 55  10
     public void addEventListener(
 56   
         Object producer,
 57   
         String eventSetName,
 58   
         Object consumer,
 59   
         Location location)
 60   
     {
 61  10
         EventSetDescriptor[] sets = getEventSets(producer);
 62  10
         boolean nameMatch = HiveMind.isNonBlank(eventSetName);
 63  10
         Class consumerClass = consumer.getClass();
 64   
 
 65  10
         int count = 0;
 66  10
         for (int i = 0; i < sets.length; i++)
 67   
         {
 68  9
             EventSetDescriptor set = sets[i];
 69  9
             String name = set.getName();
 70   
 
 71  9
             if (nameMatch)
 72   
             {
 73  3
                 if (!eventSetName.equals(name))
 74  1
                     continue;
 75   
 
 76  2
                 if (isAssignable(set, consumerClass))
 77  1
                     addEventListener(producer, set, consumer, location);
 78   
                 else
 79   
                 {
 80  1
                     _errorHandler.error(
 81   
                         _log,
 82   
                         ServiceMessages.notCompatibleWithEvent(consumer, set, producer),
 83   
                         location,
 84   
                         null);
 85   
                 }
 86   
 
 87  2
                 return;
 88   
             }
 89   
 
 90   
             // Not matching on name, add anything that fits!
 91   
 
 92  6
             if (isAssignable(set, consumerClass))
 93   
             {
 94  5
                 addEventListener(producer, set, consumer, location);
 95  5
                 count++;
 96   
             }
 97   
         }
 98   
 
 99  8
         if (count == 0)
 100   
         {
 101  3
             if (nameMatch)
 102  1
                 _errorHandler.error(
 103   
                     _log,
 104   
                     ServiceMessages.noSuchEventSet(producer, eventSetName),
 105   
                     location,
 106   
                     null);
 107   
             else
 108  2
                 _errorHandler.error(
 109   
                     _log,
 110   
                     ServiceMessages.noEventMatches(consumer, producer),
 111   
                     location,
 112   
                     null);
 113   
         }
 114   
     }
 115   
 
 116  8
     private boolean isAssignable(EventSetDescriptor set, Class consumerClass)
 117   
     {
 118  8
         return set.getListenerType().isAssignableFrom(consumerClass);
 119   
     }
 120   
 
 121  6
     private void addEventListener(
 122   
         Object producer,
 123   
         EventSetDescriptor set,
 124   
         Object consumer,
 125   
         Location location)
 126   
     {
 127  6
         Method m = set.getAddListenerMethod();
 128   
 
 129  6
         try
 130   
         {
 131  6
             m.invoke(producer, new Object[] { consumer });
 132   
         }
 133   
         catch (Exception ex)
 134   
         {
 135  0
             _errorHandler.error(
 136   
                 _log,
 137   
                 ServiceMessages.unableToAddListener(producer, set, consumer, location, ex),
 138   
                 location,
 139   
                 ex);
 140   
 
 141   
         }
 142   
     }
 143   
 
 144  10
     private EventSetDescriptor[] getEventSets(Object producer)
 145   
     {
 146  10
         return getEventSets(producer.getClass());
 147   
     }
 148   
 
 149  10
     private synchronized EventSetDescriptor[] getEventSets(Class producerClass)
 150   
     {
 151  10
         EventSetDescriptor[] result = null;
 152   
 
 153  10
         if (_producerEventSets == null)
 154  9
             _producerEventSets = new HashMap();
 155   
         else
 156  1
             result = (EventSetDescriptor[]) _producerEventSets.get(producerClass);
 157   
 
 158  10
         if (result == null)
 159   
         {
 160  9
             result = findEventSets(producerClass);
 161   
 
 162  9
             _producerEventSets.put(producerClass, result);
 163   
         }
 164   
 
 165  10
         return result;
 166   
     }
 167   
 
 168  9
     private EventSetDescriptor[] findEventSets(Class producerClass)
 169   
     {
 170  9
         try
 171   
         {
 172  9
             BeanInfo beanInfo = Introspector.getBeanInfo(producerClass);
 173   
 
 174   
             // Will return an empty array (not null) when the class contains
 175   
             // no event sets.
 176   
 
 177  9
             return beanInfo.getEventSetDescriptors();
 178   
         }
 179   
         catch (IntrospectionException ex)
 180   
         {
 181  0
             _errorHandler.error(
 182   
                 _log,
 183   
                 ServiceMessages.unableToIntrospectClass(producerClass, ex),
 184   
                 null,
 185   
                 ex);
 186   
 
 187  0
             return new EventSetDescriptor[0];
 188   
         }
 189   
 
 190   
     }
 191   
 
 192   
 }
 193