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