View Javadoc

1   /*
2    * $Id: AbstractModel.java 11517 2008-03-31 21:34:19Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.model;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.component.LifecycleAdapterFactory;
16  import org.mule.api.context.MuleContextAware;
17  import org.mule.api.context.notification.ServerNotification;
18  import org.mule.api.lifecycle.InitialisationException;
19  import org.mule.api.model.EntryPointResolver;
20  import org.mule.api.model.EntryPointResolverSet;
21  import org.mule.api.model.Model;
22  import org.mule.component.DefaultLifecycleAdapterFactory;
23  import org.mule.context.notification.ModelNotification;
24  import org.mule.model.resolvers.DefaultEntryPointResolverSet;
25  import org.mule.model.resolvers.LegacyEntryPointResolverSet;
26  import org.mule.service.DefaultServiceExceptionStrategy;
27  
28  import java.beans.ExceptionListener;
29  import java.util.Collection;
30  import java.util.Iterator;
31  
32  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * <code>MuleModel</code> is the default implementation of the Model. The model
39   * encapsulates and manages the runtime behaviour of a Mule Server instance. It is
40   * responsible for maintaining the UMOs instances and their configuration.
41   */
42  public abstract class AbstractModel implements Model
43  {
44  
45      public static final String DEFAULT_MODEL_NAME = "main";
46  
47      private String name = DEFAULT_MODEL_NAME;
48      private EntryPointResolverSet entryPointResolverSet = null; // values are supplied below as required
49      private LifecycleAdapterFactory lifecycleAdapterFactory = new DefaultLifecycleAdapterFactory();
50      private AtomicBoolean initialised = new AtomicBoolean(false);
51      private AtomicBoolean started = new AtomicBoolean(false);
52      private ExceptionListener exceptionListener = new DefaultServiceExceptionStrategy();
53  
54      protected transient Log logger = LogFactory.getLog(getClass());
55      protected MuleContext muleContext;
56  
57      /*
58       * (non-Javadoc)
59       * 
60       * @see org.mule.api.UMOModel#getName()
61       */
62      public String getName()
63      {
64          return name;
65      }
66  
67      /*
68       * (non-Javadoc)
69       * 
70       * @see org.mule.api.UMOModel#setName(java.lang.String)
71       */
72      public void setName(String name)
73      {
74          this.name = name;
75      }
76  
77      /*
78       * (non-Javadoc)
79       * 
80       * @see org.mule.api.model.Model#getEntryPointResolver()
81       */
82      public EntryPointResolverSet getEntryPointResolverSet()
83      {
84          if (null == entryPointResolverSet)
85          {
86              entryPointResolverSet = new LegacyEntryPointResolverSet();
87          }
88          return entryPointResolverSet;
89      }
90  
91      /*
92       * (non-Javadoc)
93       * 
94       * @see org.mule.api.model.Model#setEntryPointResolver(org.mule.api.model.EntryPointResolver)
95       */
96      public void setEntryPointResolverSet(EntryPointResolverSet entryPointResolverSet)
97      {
98          this.entryPointResolverSet = entryPointResolverSet;
99      }
100 
101     /**
102      * This allows us to configure entry point resolvers incrementally
103      *
104      * @param entryPointResolvers Resolvers to add
105      */
106     public void setEntryPointResolvers(Collection entryPointResolvers)
107     {
108         if (null == entryPointResolverSet)
109         {
110             entryPointResolverSet = new DefaultEntryPointResolverSet();
111         }
112         for (Iterator resolvers = entryPointResolvers.iterator(); resolvers.hasNext();)
113         {
114             entryPointResolverSet.addEntryPointResolver((EntryPointResolver) resolvers.next());
115         }
116     }
117 
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.mule.api.model.Model#getLifecycleAdapterFactory()
122      */
123     public LifecycleAdapterFactory getLifecycleAdapterFactory()
124     {
125         return lifecycleAdapterFactory;
126     }
127 
128     /*
129      * (non-Javadoc)
130      * 
131      * @see org.mule.api.model.Model#setLifecycleAdapterFactory(org.mule.api.lifecycle.LifecycleAdapterFactory)
132      */
133     public void setLifecycleAdapterFactory(LifecycleAdapterFactory lifecycleAdapterFactory)
134     {
135         this.lifecycleAdapterFactory = lifecycleAdapterFactory;
136     }
137 
138     /** Destroys any current components */
139     public void dispose()
140     {
141         fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSING));
142         fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSED));
143     }
144 
145     /**
146      * Stops any registered components
147      *
148      * @throws MuleException if a Service fails tcomponent
149      */
150     public void stop() throws MuleException
151     {
152         fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPING));
153         started.set(false);
154         fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPED));
155     }
156 
157     /**
158      * Starts all registered components
159      *
160      * @throws MuleException if any of the components fail to start
161      */
162     public void start() throws MuleException
163     {
164         if (!initialised.get())
165         {
166             throw new IllegalStateException("Not Initialised");
167         }
168 
169         if (!started.get())
170         {
171             fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTING));
172             started.set(true);
173             fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTED));
174         }
175         else
176         {
177             logger.debug("Model already started");
178         }
179     }
180 
181     public void initialise() throws InitialisationException
182     {
183         if (!initialised.get())
184         {
185             fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISING));
186             initialised.set(true);
187             fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISED));
188         }
189         else
190         {
191             logger.debug("Model already initialised");
192         }
193     }
194 
195     public ExceptionListener getExceptionListener()
196     {
197         return exceptionListener;
198     }
199 
200     public void setExceptionListener(ExceptionListener exceptionListener)
201     {
202         this.exceptionListener = exceptionListener;
203     }
204 
205     void fireNotification(ServerNotification notification)
206     {
207         if (muleContext != null)
208         {
209             muleContext.fireNotification(notification);
210         }
211         else if (logger.isWarnEnabled())
212         {
213             logger.debug("MuleContext is not yet available for firing notifications, ignoring event: " + notification);
214         }
215     }
216 
217     public void setMuleContext(MuleContext context)
218     {
219         this.muleContext = context;
220         //Because we allow a default Exception strategy for the model we need to inject the
221         //muleContext when we get it
222         if(exceptionListener instanceof MuleContextAware)
223         {
224             ((MuleContextAware)exceptionListener).setMuleContext(muleContext);
225         }
226     }
227 
228 }