View Javadoc

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