View Javadoc

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      protected static final Log logger = LogFactory.getLog(MultiContainerContext.class);
36  
37      private String name = "multi";
38      private TreeMap containers = new TreeMap();
39  
40      public MultiContainerContext()
41      {
42          addContainer(new MuleContainerContext());
43          addContainer(new DescriptorContainerContext());
44      }
45  
46      public void setName(String name)
47      {
48          // noop
49      }
50  
51      public String getName()
52      {
53          return name;
54      }
55  
56      public void addContainer(UMOContainerContext container)
57      {
58          if (containers.containsKey(container.getName()))
59          {
60              throw new IllegalArgumentException(
61                  CoreMessages.containerAlreadyRegistered(container.getName()).toString());
62          }
63          containers.put(container.getName(), container);
64      }
65  
66      public UMOContainerContext removeContainer(String name)
67      {
68          return (UMOContainerContext) containers.remove(name);
69      }
70  
71      public Object getComponent(Object key) throws ObjectNotFoundException
72      {
73          ContainerKeyPair realKey = null;
74          StringBuffer cause = new StringBuffer();
75          Throwable finalCause = null;
76          
77          // first see if a particular container has been requested
78          if (key instanceof String)
79          {
80              realKey = new ContainerKeyPair(null, key);
81          }
82          else
83          {
84              realKey = (ContainerKeyPair) key;
85          }
86  
87          if (realKey == null)
88          {
89              throw new ObjectNotFoundException(null);
90          }
91          
92          Object component = null;
93          UMOContainerContext container;
94          if (realKey.getContainerName() != null)
95          {
96              container = (UMOContainerContext) containers.get(realKey.getContainerName());
97              if (container != null)
98              {
99                  return container.getComponent(realKey);
100             }
101             else
102             {
103                 throw new ObjectNotFoundException("Container: " + realKey.getContainerName());
104             }
105         }
106 
107         for (Iterator iterator = containers.values().iterator(); iterator.hasNext();)
108         {
109             container = (UMOContainerContext) iterator.next();
110             try
111             {
112                 component = container.getComponent(realKey);
113             }
114             catch (ObjectNotFoundException e)
115             {
116                 if (e.getCause() != null)
117                 {
118                     finalCause = e.getCause();
119                     if (logger.isDebugEnabled())
120                     {
121                         logger.debug("Object: '" + realKey + "' not found in container: " + container.getName());
122                     }
123                 }
124                 else
125                 {
126                     finalCause = e;
127                     if (logger.isDebugEnabled())
128                     {
129                         logger.debug("Object: '" + realKey + "' not found in container: " + container.getName());
130                     }
131                 }
132 
133                 if (cause.length() > 0)
134                 {
135                     cause.append("; ");
136                 }
137                 cause.append(finalCause.toString());
138             }
139             if (component != null)
140             {
141                 if (logger.isDebugEnabled())
142                 {
143                     logger.debug("Object: '" + realKey + "' found in container: " + container.getName());
144                 }
145                 break;
146             }
147         }
148         
149         if (component == null)
150         {
151             if (realKey.isRequired())
152             {
153                 throw new ObjectNotFoundException(realKey.toString() + " " + cause, finalCause);
154             }
155             else if (logger.isDebugEnabled())
156             {
157                 logger.debug("Component reference not found: " + realKey.toFullString());
158                 return null;
159             }
160         }
161         return component;
162     }
163 
164     public void configure(Reader configuration, String doctype, String encoding) throws ContainerException
165     {
166         // noop
167     }
168 
169     public void dispose()
170     {
171         UMOContainerContext container;
172         for (Iterator iterator = containers.values().iterator(); iterator.hasNext();)
173         {
174             container = (UMOContainerContext) iterator.next();
175             container.dispose();
176         }
177         containers.clear();
178         containers = null;
179     }
180 
181     public void initialise() throws InitialisationException
182     {
183         // no op
184     }
185 
186 }