View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.registry;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
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.LifecycleCallback;
15  import org.mule.api.lifecycle.LifecycleException;
16  import org.mule.api.registry.RegistrationException;
17  import org.mule.api.registry.Registry;
18  import org.mule.api.registry.RegistryBroker;
19  import org.mule.lifecycle.RegistryBrokerLifecycleManager;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  public abstract class AbstractRegistryBroker implements RegistryBroker
28  {
29      protected RegistryBrokerLifecycleManager lifecycleManager;
30  
31      public AbstractRegistryBroker(MuleContext muleContext)
32      {
33          lifecycleManager = new RegistryBrokerLifecycleManager("mule.registry.broker", this, muleContext);
34      }
35  
36      public void initialise() throws InitialisationException
37      {
38          lifecycleManager.fireInitialisePhase(new LifecycleCallback<AbstractRegistryBroker>()
39          {
40              public void onTransition(String phaseName, AbstractRegistryBroker broker) throws MuleException
41              {
42                  for (Registry registry : broker.getRegistries())
43                  {
44                      registry.initialise();
45                  }
46              }
47          });
48      }
49  
50      public void dispose()
51      {
52          lifecycleManager.fireDisposePhase(new LifecycleCallback<AbstractRegistryBroker>()
53          {
54              public void onTransition(String phaseName, AbstractRegistryBroker broker) throws MuleException
55              {
56                  for (Registry registry : broker.getRegistries())
57                  {
58                      registry.dispose();
59                  }
60              }
61          });
62      }
63  
64      public void fireLifecycle(String phase) throws LifecycleException
65      {
66          if (Initialisable.PHASE_NAME.equals(phase))
67          {
68              initialise();
69          }
70          else if (Disposable.PHASE_NAME.equals(phase))
71          {
72              dispose();
73          }
74          else
75          {
76              lifecycleManager.fireLifecycle(phase);
77              for (Registry registry : getRegistries())
78              {
79                  registry.fireLifecycle(phase);
80              }
81          }
82  
83      }
84  
85      public String getRegistryId()
86      {
87          return this.toString();
88      }
89  
90      public boolean isReadOnly()
91      {
92          return false;
93      }
94  
95      public boolean isRemote()
96      {
97          return false;
98      }
99  
100     abstract protected Collection<Registry> getRegistries();
101 
102      ////////////////////////////////////////////////////////////////////////////////
103    // Delegating methods
104     ////////////////////////////////////////////////////////////////////////////////
105 
106 
107     @SuppressWarnings("unchecked")
108     public <T> T get(String key)
109     {
110         return (T) lookupObject(key);
111     }
112 
113     @SuppressWarnings("unchecked")
114     public <T> T lookupObject(String key)
115     {
116         Object obj = null;
117         Iterator it = getRegistries().iterator();
118         while (obj == null && it.hasNext())
119         {
120             obj = ((Registry) it.next()).lookupObject(key);
121         }
122         return (T) obj;
123     }
124 
125     public <T> T lookupObject(Class<T> type) throws RegistrationException
126     {
127         Object object;
128         for (Registry registry : getRegistries())
129         {
130             object = registry.lookupObject(type);
131             if (object != null)
132             {
133                 return (T) object;
134             }
135         }
136         return null;
137     }
138 
139     public <T> Collection<T> lookupObjects(Class<T> type)
140     {
141         Collection<T> objects = new ArrayList<T>();
142 
143         Iterator it = getRegistries().iterator();
144         while (it.hasNext())
145         {
146             objects.addAll(((Registry) it.next()).lookupObjects(type));
147         }
148         return objects;
149     }
150 
151     public <T> Map<String, T> lookupByType(Class<T> type)
152     {
153         Map<String, T> results = new HashMap<String, T>();
154         for (Registry registry : getRegistries())
155         {
156             results.putAll(registry.lookupByType(type));
157         }
158 
159         return results;
160     }
161     
162     public <T> Collection<T> lookupObjectsForLifecycle(Class<T> type)
163     {
164         Collection<T> objects = new ArrayList<T>();
165 
166         Iterator it = getRegistries().iterator();
167         while (it.hasNext())
168         {
169             objects.addAll(((Registry) it.next()).lookupObjectsForLifecycle(type));
170         }
171         return objects;
172     }
173 
174        
175     public void registerObject(String key, Object value) throws RegistrationException
176     {
177         registerObject(key, value, null);
178     }
179 
180     public void registerObject(String key, Object value, Object metadata) throws RegistrationException
181     {
182         Iterator it = getRegistries().iterator();
183         Registry reg;
184         while (it.hasNext())
185         {
186             reg = (Registry) it.next();
187             if (!reg.isReadOnly())
188             {
189                 reg.registerObject(key, value, metadata);
190                 break;
191             }
192         }
193     }
194 
195     public void registerObjects(Map objects) throws RegistrationException
196     {
197         Iterator it = objects.keySet().iterator();
198         Object key;
199         while (it.hasNext())
200         {
201             key = it.next();
202             registerObject((String) key, objects.get(key));
203         }
204     }
205 
206     public void unregisterObject(String key) throws RegistrationException
207     {
208         unregisterObject(key, null);
209     }
210 
211     public void unregisterObject(String key, Object metadata) throws RegistrationException
212     {
213         Iterator it = getRegistries().iterator();
214         Registry reg;
215         while (it.hasNext())
216         {
217             reg = (Registry) it.next();
218             if (!reg.isReadOnly() && reg.lookupObject(key) != null)
219             {
220                 reg.unregisterObject(key, metadata);
221                 break;
222             }
223         }
224     }
225 }