Coverage Report - org.mule.extras.hivemind.HiveMindContext
 
Classes in this File Line Coverage Branch Coverage Complexity
HiveMindContext
92%
37/40
89%
16/18
3.5
 
 1  
 /*
 2  
  * $Id: HiveMindContext.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.extras.hivemind;
 12  
 
 13  
 import org.mule.impl.container.AbstractContainerContext;
 14  
 import org.mule.impl.container.ContainerKeyPair;
 15  
 import org.mule.umo.lifecycle.InitialisationException;
 16  
 import org.mule.umo.manager.ContainerException;
 17  
 import org.mule.umo.manager.ObjectNotFoundException;
 18  
 
 19  
 import java.io.Reader;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 import org.apache.hivemind.ApplicationRuntimeException;
 24  
 import org.apache.hivemind.Registry;
 25  
 import org.apache.hivemind.impl.RegistryBuilder;
 26  
 
 27  
 /**
 28  
  * <code>HiveMindContext</code> is a HiveMind Context that can expose HiveMind
 29  
  * managed services for use in the Mule framework.
 30  
  */
 31  
 public class HiveMindContext extends AbstractContainerContext
 32  
 {
 33  6
     private static final Log logger = LogFactory.getLog(HiveMindContext.class);
 34  
 
 35  
     /**
 36  
      * the hivemind registry that manages services
 37  
      */
 38  
     private Registry registry;
 39  
 
 40  
     public HiveMindContext()
 41  
     {
 42  22
         super("hivemind");
 43  22
         logger.debug("HiveMindContext built");
 44  22
     }
 45  
 
 46  
     protected Registry getRegistry()
 47  
     {
 48  2
         return this.registry;
 49  
     }
 50  
 
 51  
     /*
 52  
      * (non-Javadoc)
 53  
      * 
 54  
      * @see org.mule.model.UMOContainerContext#getComponent(java.lang.Object)
 55  
      */
 56  
     public Object getComponent(Object key) throws ObjectNotFoundException
 57  
     {
 58  
 
 59  18
         if (registry == null)
 60  
         {
 61  2
             throw new IllegalStateException("HiveMind registry has not been set");
 62  
         }
 63  16
         if (key == null)
 64  
         {
 65  2
             throw new ObjectNotFoundException("Component not found for null key");
 66  
         }
 67  14
         if (key instanceof ContainerKeyPair)
 68  
         {
 69  0
             key = ((ContainerKeyPair)key).getKey();
 70  
         }
 71  14
         Object component = null;
 72  
 
 73  14
         if (key instanceof String)
 74  
         {
 75  
             try
 76  
             {
 77  6
                 component = registry.getService((String)key, Object.class);
 78  4
                 logger.debug("Called " + key + " obtained  " + component.getClass().getName());
 79  
             }
 80  2
             catch (ApplicationRuntimeException are)
 81  
             {
 82  2
                 throw new ObjectNotFoundException("Component not found for " + key, are);
 83  4
             }
 84  
         }
 85  8
         else if (key instanceof Class)
 86  
         {
 87  
             try
 88  
             {
 89  6
                 component = registry.getService((Class)key);
 90  2
                 logger.debug("Called " + ((Class)key).getName() + " obtained  "
 91  
                              + component.getClass().getName());
 92  
             }
 93  2
             catch (ApplicationRuntimeException are)
 94  
             {
 95  2
                 throw new ObjectNotFoundException("Component not found for " + key, are);
 96  2
             }
 97  
         }
 98  
 
 99  8
         if (component == null)
 100  
         {
 101  2
             logger.debug("Component not found for key" + key);
 102  2
             throw new ObjectNotFoundException("Component not found for key: " + key.toString());
 103  
         }
 104  6
         return component;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Just log that we don't need any configuration fragment.
 109  
      */
 110  
     public void configure(Reader configuration) throws ContainerException
 111  
     {
 112  0
         logger.info("HiveMindContext don't need any configuration fragment. Configuration ignored");
 113  0
     }
 114  
 
 115  
     /**
 116  
      * Here we build the registry from the standard deployment descriptors location.
 117  
      */
 118  
     public void initialise() throws InitialisationException {
 119  24
         if (registry == null)
 120  
         {
 121  20
             logger.debug("About to initilise the registry...");
 122  20
             registry = RegistryBuilder.constructDefaultRegistry();
 123  20
             logger.debug(" ... registry initialized");
 124  
         }
 125  
         else
 126  
         {
 127  4
             logger.debug("Registry already initialized...");
 128  
         }
 129  24
     }
 130  
 
 131  
     /**
 132  
      * Shutdown the registry so to notify every
 133  
      * {@link org.apache.hivemind.events.RegistryShutdownListener}.
 134  
      */
 135  
     public void dispose()
 136  
     {
 137  2
         if (registry != null)
 138  
         {
 139  2
             registry.shutdown();
 140  2
             logger.debug("Registry halted");
 141  
         }
 142  2
     }
 143  
 }