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