1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.registry; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.MuleRuntimeException; |
11 | |
import org.mule.api.lifecycle.Disposable; |
12 | |
import org.mule.api.lifecycle.Initialisable; |
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.api.lifecycle.LifecycleException; |
15 | |
import org.mule.api.lifecycle.Stoppable; |
16 | |
import org.mule.api.registry.RegistrationException; |
17 | |
import org.mule.api.registry.Registry; |
18 | |
import org.mule.config.i18n.CoreMessages; |
19 | |
import org.mule.config.i18n.MessageFactory; |
20 | |
import org.mule.lifecycle.RegistryLifecycleManager; |
21 | |
import org.mule.util.UUID; |
22 | |
|
23 | |
import java.util.Collection; |
24 | |
|
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
|
28 | |
|
29 | |
public abstract class AbstractRegistry implements Registry |
30 | |
{ |
31 | |
|
32 | |
private String id; |
33 | |
|
34 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
35 | |
|
36 | |
protected MuleContext muleContext; |
37 | |
|
38 | |
protected RegistryLifecycleManager lifecycleManager; |
39 | |
|
40 | |
protected AbstractRegistry(String id, MuleContext muleContext) |
41 | 0 | { |
42 | 0 | if (id == null) |
43 | |
{ |
44 | 0 | throw new MuleRuntimeException(CoreMessages.objectIsNull("RegistryID")); |
45 | |
} |
46 | 0 | this.id = id; |
47 | 0 | this.muleContext = muleContext; |
48 | 0 | lifecycleManager = createLifecycleManager(); |
49 | 0 | } |
50 | |
|
51 | |
public final synchronized void dispose() |
52 | |
{ |
53 | 0 | if(lifecycleManager.getState().isStarted()) |
54 | |
{ |
55 | |
try |
56 | |
{ |
57 | 0 | getLifecycleManager().fireLifecycle(Stoppable.PHASE_NAME); |
58 | |
} |
59 | 0 | catch (LifecycleException e) |
60 | |
{ |
61 | 0 | logger.error("Failed to shut down registry cleanly: " + getRegistryId(), e); |
62 | 0 | } |
63 | |
} |
64 | |
|
65 | |
|
66 | |
try |
67 | |
{ |
68 | 0 | getLifecycleManager().fireLifecycle(Disposable.PHASE_NAME); |
69 | |
} |
70 | 0 | catch (LifecycleException e) |
71 | |
{ |
72 | 0 | logger.error("Failed to shut down registry cleanly: " + getRegistryId(), e); |
73 | 0 | } |
74 | |
|
75 | |
try |
76 | |
{ |
77 | 0 | doDispose(); |
78 | |
} |
79 | 0 | catch (Exception e) |
80 | |
{ |
81 | 0 | logger.error("Failed to cleanly dispose: " + e.getMessage(), e); |
82 | 0 | } |
83 | 0 | } |
84 | |
|
85 | |
protected RegistryLifecycleManager createLifecycleManager() |
86 | |
{ |
87 | 0 | return new RegistryLifecycleManager(getRegistryId(), this, muleContext); |
88 | |
} |
89 | |
|
90 | |
abstract protected void doInitialise() throws InitialisationException; |
91 | |
|
92 | |
abstract protected void doDispose(); |
93 | |
|
94 | |
public final void initialise() throws InitialisationException |
95 | |
{ |
96 | 0 | if (id == null) |
97 | |
{ |
98 | 0 | logger.warn("No unique id has been set on this registry"); |
99 | 0 | id = UUID.getUUID(); |
100 | |
} |
101 | |
try |
102 | |
{ |
103 | 0 | doInitialise(); |
104 | |
} |
105 | 0 | catch (InitialisationException e) |
106 | |
{ |
107 | 0 | throw e; |
108 | |
} |
109 | 0 | catch (Exception e) |
110 | |
{ |
111 | 0 | throw new InitialisationException(e, this); |
112 | 0 | } |
113 | |
try |
114 | |
{ |
115 | 0 | fireLifecycle(Initialisable.PHASE_NAME); |
116 | |
} |
117 | 0 | catch (InitialisationException e) |
118 | |
{ |
119 | 0 | throw e; |
120 | |
} |
121 | 0 | catch (LifecycleException e) |
122 | |
{ |
123 | 0 | throw new InitialisationException(e, this); |
124 | 0 | } |
125 | 0 | } |
126 | |
|
127 | |
public RegistryLifecycleManager getLifecycleManager() |
128 | |
{ |
129 | 0 | return lifecycleManager; |
130 | |
} |
131 | |
|
132 | |
public void fireLifecycle(String phase) throws LifecycleException |
133 | |
{ |
134 | |
|
135 | 0 | if(Disposable.PHASE_NAME.equals(phase) && lifecycleManager.getState().isStarted()) |
136 | |
{ |
137 | 0 | getLifecycleManager().fireLifecycle(Stoppable.PHASE_NAME); |
138 | |
} |
139 | 0 | getLifecycleManager().fireLifecycle(phase); |
140 | 0 | } |
141 | |
|
142 | |
@SuppressWarnings("unchecked") |
143 | |
public <T> T get(String key) |
144 | |
{ |
145 | 0 | return (T) lookupObject(key); |
146 | |
} |
147 | |
|
148 | |
public <T> T lookupObject(Class<T> type) throws RegistrationException |
149 | |
{ |
150 | |
|
151 | 0 | Collection<T> objects = lookupObjects(type); |
152 | |
|
153 | 0 | if (objects.size() == 1) |
154 | |
{ |
155 | 0 | return objects.iterator().next(); |
156 | |
} |
157 | 0 | else if (objects.size() > 1) |
158 | |
{ |
159 | 0 | throw new RegistrationException(MessageFactory.createStaticMessage("More than one object of type " + type + " registered but only one expected.")); |
160 | |
} |
161 | |
else |
162 | |
{ |
163 | 0 | return null; |
164 | |
} |
165 | |
} |
166 | |
|
167 | |
public <T> Collection<T> lookupObjectsForLifecycle(Class<T> type) |
168 | |
{ |
169 | |
|
170 | |
|
171 | |
|
172 | 0 | return lookupObjects(type); |
173 | |
} |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public final String getRegistryId() |
181 | |
{ |
182 | 0 | return id; |
183 | |
} |
184 | |
} |