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
32
33
34 public class HiveMindContext extends AbstractContainerContext
35 {
36 private static final Log logger = LogFactory.getLog(HiveMindContext.class);
37
38
39
40
41 private Registry registry;
42
43 public HiveMindContext()
44 {
45 super("hivemind");
46 logger.debug("HiveMindContext built");
47 }
48
49 protected Registry getRegistry()
50 {
51 return this.registry;
52 }
53
54
55
56
57
58
59 public Object getComponent(Object key) throws ObjectNotFoundException
60 {
61
62 if (registry == null)
63 {
64 throw new IllegalStateException("HiveMind registry has not been set");
65 }
66 if (key == null)
67 {
68 throw new ObjectNotFoundException("Component not found for null key");
69 }
70 if (key instanceof ContainerKeyPair)
71 {
72 key = ((ContainerKeyPair)key).getKey();
73 }
74 Object component = null;
75
76 if (key instanceof String)
77 {
78 try
79 {
80 component = registry.getService((String)key, Object.class);
81 logger.debug("Called " + key + " obtained " + component.getClass().getName());
82 }
83 catch (ApplicationRuntimeException are)
84 {
85 throw new ObjectNotFoundException("Component not found for " + key, are);
86 }
87 }
88 else if (key instanceof Class)
89 {
90 try
91 {
92 component = registry.getService((Class)key);
93 logger.debug("Called " + ((Class)key).getName() + " obtained "
94 + component.getClass().getName());
95 }
96 catch (ApplicationRuntimeException are)
97 {
98 throw new ObjectNotFoundException("Component not found for " + key, are);
99 }
100 }
101
102 if (component == null)
103 {
104 logger.debug("Component not found for key" + key);
105 throw new ObjectNotFoundException("Component not found for key: " + key.toString());
106 }
107 return component;
108 }
109
110
111
112
113 public void configure(Reader configuration) throws ContainerException
114 {
115 logger.info("HiveMindContext don't need any configuration fragment. Configuration ignored");
116 }
117
118
119
120
121 public void initialise() throws InitialisationException {
122 if (registry == null)
123 {
124 logger.debug("About to initilise the registry...");
125 registry = RegistryBuilder.constructDefaultRegistry();
126 logger.debug(" ... registry initialized");
127 }
128 else
129 {
130 logger.debug("Registry already initialized...");
131 }
132 }
133
134
135
136
137
138 public void dispose()
139 {
140 if (registry != null)
141 {
142 registry.shutdown();
143 logger.debug("Registry halted");
144 }
145 }
146 }