Coverage Report - org.mule.registry.AbstractRegistryBroker
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractRegistryBroker
0%
0/65
0%
0/36
0
 
 1  
 /*
 2  
  * $Id: AbstractRegistryBroker.java 20320 2010-11-24 15:03:31Z 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.lifecycle.Disposable;
 14  
 import org.mule.api.lifecycle.Initialisable;
 15  
 import org.mule.api.lifecycle.InitialisationException;
 16  
 import org.mule.api.lifecycle.LifecycleException;
 17  
 import org.mule.api.registry.RegistrationException;
 18  
 import org.mule.api.registry.Registry;
 19  
 import org.mule.api.registry.RegistryBroker;
 20  
 
 21  
 import java.util.ArrayList;
 22  
 import java.util.Collection;
 23  
 import java.util.HashMap;
 24  
 import java.util.Iterator;
 25  
 import java.util.Map;
 26  
 
 27  0
 public abstract class AbstractRegistryBroker implements RegistryBroker
 28  
 {
 29  
     public void initialise() throws InitialisationException
 30  
     {
 31  0
         for (Registry registry : getRegistries())
 32  
         {
 33  0
             registry.initialise();
 34  
         }
 35  0
     }
 36  
 
 37  
     public void dispose()
 38  
     {
 39  0
         for (Registry registry : getRegistries())
 40  
         {
 41  0
             registry.dispose();
 42  
         }
 43  0
     }
 44  
 
 45  
     public void fireLifecycle(String phase) throws LifecycleException
 46  
     {
 47  0
         if (Initialisable.PHASE_NAME.equals(phase))
 48  
         {
 49  0
             initialise();
 50  
         }
 51  0
         else if (Disposable.PHASE_NAME.equals(phase))
 52  
         {
 53  0
             dispose();
 54  
         }
 55  
         else
 56  
         {
 57  0
             for (Registry registry : getRegistries())
 58  
             {
 59  0
                 registry.fireLifecycle(phase);
 60  
             }
 61  
         }
 62  
 
 63  0
     }
 64  
 
 65  
     public String getRegistryId()
 66  
     {
 67  0
         return this.toString();
 68  
     }
 69  
 
 70  
     public boolean isReadOnly()
 71  
     {
 72  0
         return false;
 73  
     }
 74  
 
 75  
     public boolean isRemote()
 76  
     {
 77  0
         return false;
 78  
     }
 79  
 
 80  
     abstract protected Collection<Registry> getRegistries();
 81  
 
 82  
     ////////////////////////////////////////////////////////////////////////////////
 83  
     // Delegating methods
 84  
     ////////////////////////////////////////////////////////////////////////////////
 85  
 
 86  
 
 87  
     @SuppressWarnings("unchecked")
 88  
     public <T> T get(String key)
 89  
     {
 90  0
         return (T) lookupObject(key);
 91  
     }
 92  
 
 93  
     @SuppressWarnings("unchecked")
 94  
     public <T> T lookupObject(String key)
 95  
     {
 96  0
         Object obj = null;
 97  0
         Iterator it = getRegistries().iterator();
 98  0
         while (obj == null && it.hasNext())
 99  
         {
 100  0
             obj = ((Registry) it.next()).lookupObject(key);
 101  
         }
 102  0
         return (T) obj;
 103  
     }
 104  
 
 105  
     public <T> T lookupObject(Class<T> type) throws RegistrationException
 106  
     {
 107  
         Object object;
 108  0
         for (Registry registry : getRegistries())
 109  
         {
 110  0
             object = registry.lookupObject(type);
 111  0
             if (object != null)
 112  
             {
 113  0
                 return (T) object;
 114  
             }
 115  
         }
 116  0
         return null;
 117  
     }
 118  
 
 119  
     public <T> Collection<T> lookupObjects(Class<T> type)
 120  
     {
 121  0
         Collection<T> objects = new ArrayList<T>();
 122  
 
 123  0
         Iterator it = getRegistries().iterator();
 124  0
         while (it.hasNext())
 125  
         {
 126  0
             objects.addAll(((Registry) it.next()).lookupObjects(type));
 127  
         }
 128  0
         return objects;
 129  
     }
 130  
 
 131  
     public <T> Map<String, T> lookupByType(Class<T> type)
 132  
     {
 133  0
         Map<String, T> results = new HashMap<String, T>();
 134  0
         for (Registry registry : getRegistries())
 135  
         {
 136  0
             results.putAll(registry.lookupByType(type));
 137  
         }
 138  
 
 139  0
         return results;
 140  
     }
 141  
     
 142  
     public <T> Collection<T> lookupObjectsForLifecycle(Class<T> type)
 143  
     {
 144  0
         Collection<T> objects = new ArrayList<T>();
 145  
 
 146  0
         Iterator it = getRegistries().iterator();
 147  0
         while (it.hasNext())
 148  
         {
 149  0
             objects.addAll(((Registry) it.next()).lookupObjectsForLifecycle(type));
 150  
         }
 151  0
         return objects;
 152  
     }
 153  
     
 154  
     public void registerObject(String key, Object value) throws RegistrationException
 155  
     {
 156  0
         registerObject(key, value, null);
 157  0
     }
 158  
 
 159  
     public void registerObject(String key, Object value, Object metadata) throws RegistrationException
 160  
     {
 161  0
         Iterator it = getRegistries().iterator();
 162  
         Registry reg;
 163  0
         while (it.hasNext())
 164  
         {
 165  0
             reg = (Registry) it.next();
 166  0
             if (!reg.isReadOnly())
 167  
             {
 168  0
                 reg.registerObject(key, value, metadata);
 169  0
                 break;
 170  
             }
 171  
         }
 172  0
     }
 173  
 
 174  
     public void registerObjects(Map objects) throws RegistrationException
 175  
     {
 176  0
         Iterator it = objects.keySet().iterator();
 177  
         Object key;
 178  0
         while (it.hasNext())
 179  
         {
 180  0
             key = it.next();
 181  0
             registerObject((String) key, objects.get(key));
 182  
         }
 183  0
     }
 184  
 
 185  
     public void unregisterObject(String key) throws RegistrationException
 186  
     {
 187  0
         unregisterObject(key, null);
 188  0
     }
 189  
 
 190  
     public void unregisterObject(String key, Object metadata) throws RegistrationException
 191  
     {
 192  0
         Iterator it = getRegistries().iterator();
 193  
         Registry reg;
 194  0
         while (it.hasNext())
 195  
         {
 196  0
             reg = (Registry) it.next();
 197  0
             if (!reg.isReadOnly() && reg.lookupObject(key) != null)
 198  
             {
 199  0
                 reg.unregisterObject(key, metadata);
 200  0
                 break;
 201  
             }
 202  
         }
 203  0
     }
 204  
 }