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