1
2
3
4
5
6
7
8
9
10
11 package org.mule.extras.hivemind;
12
13 import org.mule.impl.container.AbstractContainerContext;
14 import org.mule.impl.container.ContainerKeyPair;
15 import org.mule.umo.lifecycle.InitialisationException;
16 import org.mule.umo.manager.ContainerException;
17 import org.mule.umo.manager.ObjectNotFoundException;
18
19 import java.io.Reader;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.Registry;
25 import org.apache.hivemind.impl.RegistryBuilder;
26
27
28
29
30
31 public class HiveMindContext extends AbstractContainerContext
32 {
33 private static final Log logger = LogFactory.getLog(HiveMindContext.class);
34
35
36
37
38 private Registry registry;
39
40 public HiveMindContext()
41 {
42 super("hivemind");
43 logger.debug("HiveMindContext built");
44 }
45
46 protected Registry getRegistry()
47 {
48 return this.registry;
49 }
50
51
52
53
54
55
56 public Object getComponent(Object key) throws ObjectNotFoundException
57 {
58
59 if (registry == null)
60 {
61 throw new IllegalStateException("HiveMind registry has not been set");
62 }
63 if (key == null)
64 {
65 throw new ObjectNotFoundException("Component not found for null key");
66 }
67 if (key instanceof ContainerKeyPair)
68 {
69 key = ((ContainerKeyPair)key).getKey();
70 }
71 Object component = null;
72
73 if (key instanceof String)
74 {
75 try
76 {
77 component = registry.getService((String)key, Object.class);
78 logger.debug("Called " + key + " obtained " + component.getClass().getName());
79 }
80 catch (ApplicationRuntimeException are)
81 {
82 throw new ObjectNotFoundException("Component not found for " + key, are);
83 }
84 }
85 else if (key instanceof Class)
86 {
87 try
88 {
89 component = registry.getService((Class)key);
90 logger.debug("Called " + ((Class)key).getName() + " obtained "
91 + component.getClass().getName());
92 }
93 catch (ApplicationRuntimeException are)
94 {
95 throw new ObjectNotFoundException("Component not found for " + key, are);
96 }
97 }
98
99 if (component == null)
100 {
101 logger.debug("Component not found for key" + key);
102 throw new ObjectNotFoundException("Component not found for key: " + key.toString());
103 }
104 return component;
105 }
106
107
108
109
110 public void configure(Reader configuration) throws ContainerException
111 {
112 logger.info("HiveMindContext don't need any configuration fragment. Configuration ignored");
113 }
114
115
116
117
118 public void initialise() throws InitialisationException {
119 if (registry == null)
120 {
121 logger.debug("About to initilise the registry...");
122 registry = RegistryBuilder.constructDefaultRegistry();
123 logger.debug(" ... registry initialized");
124 }
125 else
126 {
127 logger.debug("Registry already initialized...");
128 }
129 }
130
131
132
133
134
135 public void dispose()
136 {
137 if (registry != null)
138 {
139 registry.shutdown();
140 logger.debug("Registry halted");
141 }
142 }
143 }