View Javadoc

1   /*
2    * $Id: MultiContainerContext.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.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          // TODO MULE-863: possible class cast exception below.  Document?
79          if (key instanceof String)
80          {
81              realKey = new ContainerKeyPair(null, key);
82          }
83          else
84          {
85              realKey = (ContainerKeyPair) key;
86          }
87  
88          if (realKey == null)
89          {
90              throw new ObjectNotFoundException(null);
91          }
92          
93          Object component = null;
94          UMOContainerContext container;
95          if (realKey.getContainerName() != null)
96          {
97              container = (UMOContainerContext) containers.get(realKey.getContainerName());
98              if (container != null)
99              {
100                 return container.getComponent(realKey);
101             }
102             else
103             {
104                 throw new ObjectNotFoundException("Container: " + realKey.getContainerName());
105             }
106         }
107 
108         for (Iterator iterator = containers.values().iterator(); iterator.hasNext();)
109         {
110             container = (UMOContainerContext) iterator.next();
111             try
112             {
113                 component = container.getComponent(realKey);
114             }
115             catch (ObjectNotFoundException e)
116             {
117                 if (e.getCause() != null)
118                 {
119                     finalCause = e.getCause();
120                     if (logger.isDebugEnabled())
121                     {
122                         logger.debug("Object: '" + realKey + "' not found in container: " + container.getName());
123                     }
124                 }
125                 else
126                 {
127                     finalCause = e;
128                     if (logger.isDebugEnabled())
129                     {
130                         logger.debug("Object: '" + realKey + "' not found in container: " + container.getName());
131                     }
132                 }
133 
134                 if (cause.length() > 0)
135                 {
136                     cause.append("; ");
137                 }
138                 cause.append(finalCause.toString());
139             }
140             if (component != null)
141             {
142                 if (logger.isDebugEnabled())
143                 {
144                     logger.debug("Object: '" + realKey + "' found in container: " + container.getName());
145                 }
146                 break;
147             }
148         }
149         
150         if (component == null)
151         {
152             if (realKey.isRequired())
153             {
154                 throw new ObjectNotFoundException(realKey.toString() + " " + cause, finalCause);
155             }
156             else if (logger.isDebugEnabled())
157             {
158                 logger.debug("Component reference not found: " + realKey.toFullString());
159                 return null;
160             }
161         }
162         return component;
163     }
164 
165     public void configure(Reader configuration, String doctype, String encoding) throws ContainerException
166     {
167         // noop
168     }
169 
170     public void dispose()
171     {
172         UMOContainerContext container;
173         for (Iterator iterator = containers.values().iterator(); iterator.hasNext();)
174         {
175             container = (UMOContainerContext) iterator.next();
176             container.dispose();
177         }
178         containers.clear();
179         containers = null;
180     }
181 
182     public void initialise() throws InitialisationException
183     {
184         // no op
185     }
186 
187 }