View Javadoc

1   /*
2    * $Id: AbstractRegistry.java 19191 2010-08-25 21:05:23Z tcarlson $
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.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      /** the unique id for this Registry */
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      public final synchronized void dispose()
56      {
57          if(lifecycleManager.getState().isStarted())
58          {
59              try
60              {
61                  getLifecycleManager().fireLifecycle(Stoppable.PHASE_NAME);
62              }
63              catch (LifecycleException e)
64              {
65                  logger.error("Failed to shut down registry cleanly: " + getRegistryId(), e);
66              }
67          }
68          //Fire dispose lifecycle before calling doDispose() that that registries can clear any object caches once all objects
69          //are disposed
70          try
71          {
72              getLifecycleManager().fireLifecycle(Disposable.PHASE_NAME);
73          }
74          catch (LifecycleException e)
75          {
76              logger.error("Failed to shut down registry cleanly: " + getRegistryId(), e);
77          }
78  
79          try
80          {
81              doDispose();
82          }
83          catch (Exception e)
84          {
85              logger.error("Failed to cleanly dispose: " + e.getMessage(), e);
86          }
87      }
88  
89      protected RegistryLifecycleManager createLifecycleManager()
90      {
91          return new RegistryLifecycleManager(getRegistryId(), this, muleContext);
92      }
93  
94      abstract protected void doInitialise() throws InitialisationException;
95  
96      abstract protected void doDispose();
97  
98      public final void initialise() throws InitialisationException
99      {
100         if (id == null)
101         {
102             logger.warn("No unique id has been set on this registry");
103             id = UUID.getUUID();
104         }
105         try
106         {
107             doInitialise();
108         }
109         catch (InitialisationException e)
110         {
111             throw e;
112         }
113         catch (Exception e)
114         {
115             throw new InitialisationException(e, this);
116         }
117         try
118         {
119             fireLifecycle(Initialisable.PHASE_NAME);
120         }
121         catch (InitialisationException e)
122         {
123             throw e;
124         }
125         catch (LifecycleException e)
126         {
127             throw new InitialisationException(e, this);
128         }
129     }
130 
131     public RegistryLifecycleManager getLifecycleManager()
132     {
133         return lifecycleManager;
134     }
135 
136     public void fireLifecycle(String phase) throws LifecycleException
137     {
138         //Implicitly call stop if necessary when disposing
139         if(Disposable.PHASE_NAME.equals(phase) && lifecycleManager.getState().isStarted())
140         {
141             getLifecycleManager().fireLifecycle(Stoppable.PHASE_NAME);
142         }
143         getLifecycleManager().fireLifecycle(phase);
144     }
145 
146     @SuppressWarnings("unchecked")
147     public <T> T get(String key)
148     {
149         return (T) lookupObject(key);
150     }
151 
152     public <T> T lookupObject(Class<T> type) throws RegistrationException
153     {
154         // Accumulate objects from all registries.
155         Collection<T> objects = lookupObjects(type);
156         
157         if (objects.size() == 1)
158         {
159             return objects.iterator().next();
160         }
161         else if (objects.size() > 1)
162         {
163             throw new RegistrationException(MessageFactory.createStaticMessage("More than one object of type " + type + " registered but only one expected."));
164         }
165         else
166         {
167             return null;
168         }
169     }
170 
171     // /////////////////////////////////////////////////////////////////////////
172     // Registry Metadata
173     // /////////////////////////////////////////////////////////////////////////
174 
175     public final String getRegistryId()
176     {
177         return id;
178     }
179 }