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