Coverage Report - org.mule.registry.AbstractRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractRegistry
0%
0/53
0%
0/14
0
 
 1  
 /*
 2  
  * $Id: AbstractRegistry.java 20277 2010-11-19 18:52:43Z dfeist $
 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.registry;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.MuleRuntimeException;
 15  
 import org.mule.api.lifecycle.Disposable;
 16  
 import org.mule.api.lifecycle.Initialisable;
 17  
 import org.mule.api.lifecycle.InitialisationException;
 18  
 import org.mule.api.lifecycle.LifecycleException;
 19  
 import org.mule.api.lifecycle.Stoppable;
 20  
 import org.mule.api.registry.RegistrationException;
 21  
 import org.mule.api.registry.Registry;
 22  
 import org.mule.config.i18n.CoreMessages;
 23  
 import org.mule.config.i18n.MessageFactory;
 24  
 import org.mule.lifecycle.RegistryLifecycleManager;
 25  
 import org.mule.util.UUID;
 26  
 
 27  
 import java.util.Collection;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 
 32  
 
 33  
 public abstract class AbstractRegistry implements Registry
 34  
 {
 35  
     /** the unique id for this Registry */
 36  
     private String id;
 37  
 
 38  0
     protected transient Log logger = LogFactory.getLog(getClass());
 39  
 
 40  
     protected MuleContext muleContext;
 41  
 
 42  
     protected RegistryLifecycleManager lifecycleManager;
 43  
 
 44  
     protected AbstractRegistry(String id, MuleContext muleContext)
 45  0
     {
 46  0
         if (id == null)
 47  
         {
 48  0
             throw new MuleRuntimeException(CoreMessages.objectIsNull("RegistryID"));
 49  
         }
 50  0
         this.id = id;
 51  0
         this.muleContext = muleContext;
 52  0
         lifecycleManager = createLifecycleManager();
 53  0
     }
 54  
 
 55  
     public final synchronized void dispose()
 56  
     {
 57  0
         if(lifecycleManager.getState().isStarted())
 58  
         {
 59  
             try
 60  
             {
 61  0
                 getLifecycleManager().fireLifecycle(Stoppable.PHASE_NAME);
 62  
             }
 63  0
             catch (LifecycleException e)
 64  
             {
 65  0
                 logger.error("Failed to shut down registry cleanly: " + getRegistryId(), e);
 66  0
             }
 67  
         }
 68  
         //Fire dispose lifecycle before calling doDispose() that that registries can clear any object caches once all objects
 69  
         //are disposed
 70  
         try
 71  
         {
 72  0
             getLifecycleManager().fireLifecycle(Disposable.PHASE_NAME);
 73  
         }
 74  0
         catch (LifecycleException e)
 75  
         {
 76  0
             logger.error("Failed to shut down registry cleanly: " + getRegistryId(), e);
 77  0
         }
 78  
 
 79  
         try
 80  
         {
 81  0
             doDispose();
 82  
         }
 83  0
         catch (Exception e)
 84  
         {
 85  0
             logger.error("Failed to cleanly dispose: " + e.getMessage(), e);
 86  0
         }
 87  0
     }
 88  
 
 89  
     protected RegistryLifecycleManager createLifecycleManager()
 90  
     {
 91  0
         return new RegistryLifecycleManager(getRegistryId(), this, muleContext);
 92  
     }
 93  
 
 94  
     abstract protected void doInitialise() throws InitialisationException;
 95  
 
 96  
     abstract protected void doDispose();
 97  
 
 98  
     public final void initialise() throws InitialisationException
 99  
     {
 100  0
         if (id == null)
 101  
         {
 102  0
             logger.warn("No unique id has been set on this registry");
 103  0
             id = UUID.getUUID();
 104  
         }
 105  
         try
 106  
         {
 107  0
             doInitialise();
 108  
         }
 109  0
         catch (InitialisationException e)
 110  
         {
 111  0
             throw e;
 112  
         }
 113  0
         catch (Exception e)
 114  
         {
 115  0
             throw new InitialisationException(e, this);
 116  0
         }
 117  
         try
 118  
         {
 119  0
             fireLifecycle(Initialisable.PHASE_NAME);
 120  
         }
 121  0
         catch (InitialisationException e)
 122  
         {
 123  0
             throw e;
 124  
         }
 125  0
         catch (LifecycleException e)
 126  
         {
 127  0
             throw new InitialisationException(e, this);
 128  0
         }
 129  0
     }
 130  
 
 131  
     public RegistryLifecycleManager getLifecycleManager()
 132  
     {
 133  0
         return lifecycleManager;
 134  
     }
 135  
 
 136  
     public void fireLifecycle(String phase) throws LifecycleException
 137  
     {
 138  
         //Implicitly call stop if necessary when disposing
 139  0
         if(Disposable.PHASE_NAME.equals(phase) && lifecycleManager.getState().isStarted())
 140  
         {
 141  0
             getLifecycleManager().fireLifecycle(Stoppable.PHASE_NAME);
 142  
         }
 143  0
         getLifecycleManager().fireLifecycle(phase);
 144  0
     }
 145  
 
 146  
     @SuppressWarnings("unchecked")
 147  
     public <T> T get(String key)
 148  
     {
 149  0
         return (T) lookupObject(key);
 150  
     }
 151  
 
 152  
     public <T> T lookupObject(Class<T> type) throws RegistrationException
 153  
     {
 154  
         // Accumulate objects from all registries.
 155  0
         Collection<T> objects = lookupObjects(type);
 156  
         
 157  0
         if (objects.size() == 1)
 158  
         {
 159  0
             return objects.iterator().next();
 160  
         }
 161  0
         else if (objects.size() > 1)
 162  
         {
 163  0
             throw new RegistrationException(MessageFactory.createStaticMessage("More than one object of type " + type + " registered but only one expected."));
 164  
         }
 165  
         else
 166  
         {
 167  0
             return null;
 168  
         }
 169  
     }
 170  
     
 171  
     public <T> Collection<T> lookupObjectsForLifecycle(Class<T> type)
 172  
     {
 173  
         // By default use the normal lookup. If a registry implementation needs a
 174  
         // different lookup implementation for lifecycle it should override this
 175  
         // method
 176  0
         return lookupObjects(type);
 177  
     }
 178  
 
 179  
 
 180  
     // /////////////////////////////////////////////////////////////////////////
 181  
     // Registry Metadata
 182  
     // /////////////////////////////////////////////////////////////////////////
 183  
 
 184  
     public final String getRegistryId()
 185  
     {
 186  0
         return id;
 187  
     }
 188  
 }