Coverage Report - org.mule.impl.container.MultiContainerContext
 
Classes in this File Line Coverage Branch Coverage Complexity
MultiContainerContext
62%
38/61
44%
14/32
3.556
 
 1  
 /*
 2  
  * $Id: MultiContainerContext.java 10404 2008-01-18 17:06:25Z 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.impl.container;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.umo.lifecycle.InitialisationException;
 15  
 import org.mule.umo.manager.ContainerException;
 16  
 import org.mule.umo.manager.ObjectNotFoundException;
 17  
 import org.mule.umo.manager.UMOContainerContext;
 18  
 
 19  
 import java.io.Reader;
 20  
 import java.util.Iterator;
 21  
 import java.util.TreeMap;
 22  
 
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 
 26  
 /**
 27  
  * <code>MultiContainerContext</code> is a container that hosts other containers
 28  
  * from which components are queried.
 29  
  */
 30  
 public class MultiContainerContext implements UMOContainerContext
 31  
 {
 32  
     /**
 33  
      * logger used by this class
 34  
      */
 35  4
     protected static final Log logger = LogFactory.getLog(MultiContainerContext.class);
 36  
 
 37  392
     private String name = "multi";
 38  392
     private TreeMap containers = new TreeMap();
 39  
 
 40  
     public MultiContainerContext()
 41  392
     {
 42  392
         addContainer(new MuleContainerContext());
 43  392
         addContainer(new DescriptorContainerContext());
 44  392
     }
 45  
 
 46  
     public void setName(String name)
 47  
     {
 48  
         // noop
 49  0
     }
 50  
 
 51  
     public String getName()
 52  
     {
 53  0
         return name;
 54  
     }
 55  
 
 56  
     public void addContainer(UMOContainerContext container)
 57  
     {
 58  784
         if (containers.containsKey(container.getName()))
 59  
         {
 60  0
             throw new IllegalArgumentException(
 61  
                 CoreMessages.containerAlreadyRegistered(container.getName()).toString());
 62  
         }
 63  784
         containers.put(container.getName(), container);
 64  784
     }
 65  
 
 66  
     public UMOContainerContext removeContainer(String name)
 67  
     {
 68  0
         return (UMOContainerContext) containers.remove(name);
 69  
     }
 70  
 
 71  
     public Object getComponent(Object key) throws ObjectNotFoundException
 72  
     {
 73  70
         ContainerKeyPair realKey = null;
 74  70
         StringBuffer cause = new StringBuffer();
 75  70
         Throwable finalCause = null;
 76  
         
 77  
         // first see if a particular container has been requested
 78  70
         if (key instanceof String)
 79  
         {
 80  0
             realKey = new ContainerKeyPair(null, key);
 81  
         }
 82  
         else
 83  
         {
 84  70
             realKey = (ContainerKeyPair) key;
 85  
         }
 86  
 
 87  70
         if (realKey == null)
 88  
         {
 89  0
             throw new ObjectNotFoundException(null);
 90  
         }
 91  
         
 92  70
         Object component = null;
 93  
         UMOContainerContext container;
 94  70
         if (realKey.getContainerName() != null)
 95  
         {
 96  0
             container = (UMOContainerContext) containers.get(realKey.getContainerName());
 97  0
             if (container != null)
 98  
             {
 99  0
                 return container.getComponent(realKey);
 100  
             }
 101  
             else
 102  
             {
 103  0
                 throw new ObjectNotFoundException("Container: " + realKey.getContainerName());
 104  
             }
 105  
         }
 106  
 
 107  70
         for (Iterator iterator = containers.values().iterator(); iterator.hasNext();)
 108  
         {
 109  140
             container = (UMOContainerContext) iterator.next();
 110  
             try
 111  
             {
 112  140
                 component = container.getComponent(realKey);
 113  
             }
 114  70
             catch (ObjectNotFoundException e)
 115  
             {
 116  70
                 if (e.getCause() != null)
 117  
                 {
 118  0
                     finalCause = e.getCause();
 119  0
                     if (logger.isDebugEnabled())
 120  
                     {
 121  0
                         logger.debug("Object: '" + realKey + "' not found in container: " + container.getName());
 122  
                     }
 123  
                 }
 124  
                 else
 125  
                 {
 126  70
                     finalCause = e;
 127  70
                     if (logger.isDebugEnabled())
 128  
                     {
 129  0
                         logger.debug("Object: '" + realKey + "' not found in container: " + container.getName());
 130  
                     }
 131  
                 }
 132  
 
 133  70
                 if (cause.length() > 0)
 134  
                 {
 135  0
                     cause.append("; ");
 136  
                 }
 137  70
                 cause.append(finalCause.toString());
 138  70
             }
 139  140
             if (component != null)
 140  
             {
 141  70
                 if (logger.isDebugEnabled())
 142  
                 {
 143  0
                     logger.debug("Object: '" + realKey + "' found in container: " + container.getName());
 144  
                 }
 145  
                 break;
 146  
             }
 147  
         }
 148  
         
 149  70
         if (component == null)
 150  
         {
 151  0
             if (realKey.isRequired())
 152  
             {
 153  0
                 throw new ObjectNotFoundException(realKey.toString() + " " + cause, finalCause);
 154  
             }
 155  0
             else if (logger.isDebugEnabled())
 156  
             {
 157  0
                 logger.debug("Component reference not found: " + realKey.toFullString());
 158  0
                 return null;
 159  
             }
 160  
         }
 161  70
         return component;
 162  
     }
 163  
 
 164  
     public void configure(Reader configuration, String doctype, String encoding) throws ContainerException
 165  
     {
 166  
         // noop
 167  0
     }
 168  
 
 169  
     public void dispose()
 170  
     {
 171  
         UMOContainerContext container;
 172  392
         for (Iterator iterator = containers.values().iterator(); iterator.hasNext();)
 173  
         {
 174  784
             container = (UMOContainerContext) iterator.next();
 175  784
             container.dispose();
 176  
         }
 177  392
         containers.clear();
 178  392
         containers = null;
 179  392
     }
 180  
 
 181  
     public void initialise() throws InitialisationException
 182  
     {
 183  
         // no op
 184  0
     }
 185  
 
 186  
 }