KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > container > MultiContainerContext


1 /*
2  * $Id: MultiContainerContext.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.config.i18n.Message;
16 import org.mule.config.i18n.Messages;
17 import org.mule.umo.lifecycle.InitialisationException;
18 import org.mule.umo.manager.ContainerException;
19 import org.mule.umo.manager.ObjectNotFoundException;
20 import org.mule.umo.manager.UMOContainerContext;
21
22 import java.io.Reader JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.TreeMap JavaDoc;
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 JavaDoc name = "multi";
38     private TreeMap JavaDoc containers = new TreeMap JavaDoc();
39
40     public MultiContainerContext()
41     {
42         addContainer(new MuleContainerContext());
43         addContainer(new DescriptorContainerContext());
44     }
45
46     public void setName(String JavaDoc name)
47     {
48         // noop
49
}
50
51     public String JavaDoc 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 JavaDoc(new Message(Messages.CONTAINER_X_ALREADY_REGISTERED,
61                 container.getName()).toString());
62         }
63         containers.put(container.getName(), container);
64     }
65
66     public UMOContainerContext removeContainer(String JavaDoc name)
67     {
68         return (UMOContainerContext)containers.remove(name);
69     }
70
71     public Object JavaDoc getComponent(Object JavaDoc key) throws ObjectNotFoundException
72     {
73         // first see if a particular container has been requested
74
ContainerKeyPair realKey = null;
75         String JavaDoc cause = null;
76         if (key instanceof String JavaDoc)
77         {
78             realKey = new ContainerKeyPair(null, key);
79         }
80         else
81         {
82             realKey = (ContainerKeyPair)key;
83         }
84
85         Object JavaDoc component = null;
86         UMOContainerContext container;
87         if (realKey.getContainerName() != null)
88         {
89             container = (UMOContainerContext)containers.get(realKey.getContainerName());
90             if (container != null)
91             {
92                 return container.getComponent(realKey);
93             }
94             else
95             {
96                 throw new ObjectNotFoundException("Container: " + realKey.getContainerName());
97             }
98         }
99
100         for (Iterator JavaDoc iterator = containers.values().iterator(); iterator.hasNext();)
101         {
102             container = (UMOContainerContext)iterator.next();
103             try
104             {
105                 component = container.getComponent(realKey);
106             }
107             catch (ObjectNotFoundException e)
108             {
109                 if (logger.isDebugEnabled())
110                 {
111                     logger.debug("Object: '" + realKey + "' not found in container: " + container.getName(),
112                         e.getCause());
113                 }
114                 if (e.getCause() != null){
115                     cause = cause + " " + e.getCause().toString();
116                 }
117             }
118             if (component != null)
119             {
120                 if (logger.isDebugEnabled())
121                 {
122                     logger.debug("Object: '" + realKey + "' found in container: " + container.getName());
123                 }
124                 break;
125             }
126         }
127         if (component == null)
128         {
129             if (realKey.isRequired())
130             {
131                 throw new ObjectNotFoundException(realKey.toString() + " " + cause);
132             }
133             else if (logger.isDebugEnabled())
134             {
135                 logger.debug("Component reference not found: " + realKey.toFullString());
136                 return null;
137             }
138         }
139         return component;
140     }
141
142     public void configure(Reader JavaDoc configuration, String JavaDoc doctype, String JavaDoc encoding) throws ContainerException
143     {
144         // noop
145
}
146
147     public void dispose()
148     {
149         UMOContainerContext container;
150         for (Iterator JavaDoc iterator = containers.values().iterator(); iterator.hasNext();)
151         {
152             container = (UMOContainerContext)iterator.next();
153             container.dispose();
154         }
155         containers.clear();
156         containers = null;
157     }
158
159     public void initialise() throws InitialisationException
160     {
161         // no op
162
}
163
164 }
165
Popular Tags