View Javadoc

1   /*
2    * $Id: AbstractModel.java 22772 2011-08-27 15:20:15Z 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.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.exception.MessagingExceptionHandler;
18  import org.mule.api.lifecycle.InitialisationException;
19  import org.mule.api.lifecycle.LifecycleState;
20  import org.mule.api.model.EntryPointResolver;
21  import org.mule.api.model.EntryPointResolverSet;
22  import org.mule.api.model.Model;
23  import org.mule.component.DefaultComponentLifecycleAdapterFactory;
24  import org.mule.exception.DefaultMessagingExceptionStrategy;
25  import org.mule.lifecycle.EmptyLifecycleCallback;
26  import org.mule.model.resolvers.DefaultEntryPointResolverSet;
27  import org.mule.model.resolvers.LegacyEntryPointResolverSet;
28  import org.mule.util.ClassUtils;
29  
30  import java.util.Collection;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /**
36   * <code>MuleModel</code> is the default implementation of the Model. The model
37   * encapsulates and manages the runtime behaviour of a Mule Server instance. It is
38   * responsible for maintaining the service instances and their configuration.
39   */
40  public abstract class AbstractModel implements Model
41  {
42  
43      public static final String DEFAULT_MODEL_NAME = "main";
44  
45      private String name = DEFAULT_MODEL_NAME;
46      protected MuleContext muleContext;
47      private EntryPointResolverSet entryPointResolverSet = null; // values are supplied below as required
48      private LifecycleAdapterFactory lifecycleAdapterFactory = new DefaultComponentLifecycleAdapterFactory();
49      private MessagingExceptionHandler exceptionListener = new DefaultMessagingExceptionStrategy(muleContext);
50  
51      protected transient Log logger = LogFactory.getLog(getClass());
52  
53      protected ModelLifecycleManager lifecycleManager = new ModelLifecycleManager(this);
54  
55      public String getName()
56      {
57          return name;
58      }
59  
60      public void setName(String name)
61      {
62          this.name = name;
63      }
64  
65      public LifecycleState getLifecycleState()
66      {
67          return lifecycleManager.getState();
68      }
69  
70      public EntryPointResolverSet getEntryPointResolverSet()
71      {
72          if (null == entryPointResolverSet)
73          {
74              entryPointResolverSet = new LegacyEntryPointResolverSet();
75          }
76          return entryPointResolverSet;
77      }
78  
79      public void setEntryPointResolverSet(EntryPointResolverSet entryPointResolverSet)
80      {
81          this.entryPointResolverSet = entryPointResolverSet;
82      }
83  
84      /**
85       * This allows us to configure entry point resolvers incrementally
86       *
87       * @param entryPointResolvers Resolvers to add
88       */
89      public void setEntryPointResolvers(Collection<EntryPointResolver> entryPointResolvers)
90      {
91          if (null == entryPointResolverSet)
92          {
93              entryPointResolverSet = new DefaultEntryPointResolverSet();
94          }
95          
96          for (EntryPointResolver resolver : entryPointResolvers)
97          {
98              entryPointResolverSet.addEntryPointResolver(resolver);
99          }
100     }
101 
102     public LifecycleAdapterFactory getLifecycleAdapterFactory()
103     {
104         return lifecycleAdapterFactory;
105     }
106 
107     public void setLifecycleAdapterFactory(LifecycleAdapterFactory lifecycleAdapterFactory)
108     {
109         this.lifecycleAdapterFactory = lifecycleAdapterFactory;
110     }
111 
112     /** Destroys any current components */
113     public void dispose()
114     {
115         if(getLifecycleState().isStarted())
116         {
117             try
118             {
119                 stop();
120             }
121             catch (MuleException e)
122             {
123                 logger.error("Failed to stop model cleanly as part of a dispoae call: " + getName(), e);
124             }
125         }
126         try
127         {
128             lifecycleManager.fireDisposePhase(new EmptyLifecycleCallback<AbstractModel>());
129         }
130         catch (MuleException e)
131         {
132             logger.error("Failed to dispose model: " + getName(), e);
133         }
134 
135     }
136 
137     /**
138      * Stops any registered components
139      *
140      * @throws MuleException if a Service fails tcomponent
141      */
142     public void stop() throws MuleException
143     {
144         lifecycleManager.fireStopPhase(new EmptyLifecycleCallback<AbstractModel>());
145     }
146 
147     /**
148      * Starts all registered components
149      *
150      * @throws MuleException if any of the components fail to start
151      */
152     public void start() throws MuleException
153     {
154         lifecycleManager.fireStartPhase(new EmptyLifecycleCallback<AbstractModel>());
155     }
156 
157     public void initialise() throws InitialisationException
158     {
159         try
160         {
161             lifecycleManager.fireInitialisePhase(new EmptyLifecycleCallback<AbstractModel>());
162         }
163         catch (InitialisationException e)
164         {
165             throw e;
166         }
167         catch (MuleException e)
168         {
169             throw new InitialisationException(e, this);
170         }
171     }
172 
173     public MessagingExceptionHandler getExceptionListener()
174     {
175         return exceptionListener;
176     }
177 
178     public void setExceptionListener(MessagingExceptionHandler exceptionListener)
179     {
180         this.exceptionListener = exceptionListener;
181     }
182 
183 
184 
185     public void setMuleContext(MuleContext context)
186     {
187         this.muleContext = context;
188         //Because we allow a default Exception strategy for the model we need to inject the
189         //muleContext when we get it
190         if (exceptionListener instanceof MuleContextAware)
191         {
192             ((MuleContextAware)exceptionListener).setMuleContext(muleContext);
193         }
194     }
195 
196     public MuleContext getMuleContext()
197     {
198         return muleContext;
199     }
200 
201     @Override
202     public String toString()
203     {
204         return String.format("%s{%s}", ClassUtils.getSimpleName(this.getClass()), getName());
205     }
206 }