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