View Javadoc

1   /*
2    * $Id: DefaultMuleContextBuilder.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.context;
12  
13  import org.mule.DefaultMuleContext;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleRuntimeException;
16  import org.mule.api.config.MuleConfiguration;
17  import org.mule.api.config.ThreadingProfile;
18  import org.mule.api.context.MuleContextBuilder;
19  import org.mule.api.context.WorkManager;
20  import org.mule.api.context.notification.ConnectionNotificationListener;
21  import org.mule.api.context.notification.CustomNotificationListener;
22  import org.mule.api.context.notification.ExceptionNotificationListener;
23  import org.mule.api.context.notification.ManagementNotificationListener;
24  import org.mule.api.context.notification.ModelNotificationListener;
25  import org.mule.api.context.notification.MuleContextNotificationListener;
26  import org.mule.api.context.notification.RegistryNotificationListener;
27  import org.mule.api.context.notification.RoutingNotificationListener;
28  import org.mule.api.context.notification.SecurityNotificationListener;
29  import org.mule.api.context.notification.ServiceNotificationListener;
30  import org.mule.api.context.notification.TransactionNotificationListener;
31  import org.mule.api.lifecycle.LifecycleManager;
32  import org.mule.config.DefaultMuleConfiguration;
33  import org.mule.config.i18n.Message;
34  import org.mule.config.i18n.MessageFactory;
35  import org.mule.context.notification.ConnectionNotification;
36  import org.mule.context.notification.CustomNotification;
37  import org.mule.context.notification.ExceptionNotification;
38  import org.mule.context.notification.ManagementNotification;
39  import org.mule.context.notification.ModelNotification;
40  import org.mule.context.notification.MuleContextNotification;
41  import org.mule.context.notification.RegistryNotification;
42  import org.mule.context.notification.RoutingNotification;
43  import org.mule.context.notification.SecurityNotification;
44  import org.mule.context.notification.ServerNotificationManager;
45  import org.mule.context.notification.ServiceNotification;
46  import org.mule.context.notification.TransactionNotification;
47  import org.mule.lifecycle.MuleContextLifecycleManager;
48  import org.mule.util.ClassUtils;
49  import org.mule.util.SplashScreen;
50  import org.mule.work.DefaultWorkListener;
51  import org.mule.work.MuleWorkManager;
52  
53  import javax.resource.spi.work.WorkListener;
54  
55  import org.apache.commons.logging.Log;
56  import org.apache.commons.logging.LogFactory;
57  
58  /**
59   * Implementation of {@link MuleContextBuilder} that uses {@link DefaultMuleContext}
60   * as the default {@link MuleContext} implementation and builds it with defaults
61   * values for {@link MuleConfiguration}, {@link LifecycleManager}, {@link WorkManager}, 
62   * {@link WorkListener} and {@link ServerNotificationManager}.
63   */
64  public class DefaultMuleContextBuilder implements MuleContextBuilder
65  {
66      protected static final Log logger = LogFactory.getLog(DefaultMuleContextBuilder.class);
67      
68      protected MuleConfiguration config;
69  
70      protected MuleContextLifecycleManager lifecycleManager;
71  
72      protected WorkManager workManager;
73  
74      protected WorkListener workListener;
75  
76      protected ServerNotificationManager notificationManager;
77  
78      protected SplashScreen startupScreen;
79  
80      protected SplashScreen shutdownScreen;
81  
82      /**
83       * {@inheritDoc}
84       */
85      public MuleContext buildMuleContext()
86      {
87          logger.debug("Building new DefaultMuleContext instance with MuleContextBuilder: " + this);
88          MuleContextLifecycleManager manager = getLifecycleManager();
89          DefaultMuleContext muleContext = new DefaultMuleContext(getMuleConfiguration(),
90                                                           getWorkManager(),
91                                                           getWorkListener(),
92                                                           manager,
93                                                           getNotificationManager());
94          manager.setMuleContext(muleContext);
95          muleContext.setExecutionClassLoader(Thread.currentThread().getContextClassLoader());
96          return muleContext;
97      }
98  
99      public void setMuleConfiguration(MuleConfiguration config)
100     {
101         this.config = config;
102     }
103     
104     public void setWorkManager(WorkManager workManager)
105     {
106         this.workManager = workManager;
107     }
108 
109     public void setWorkListener(WorkListener workListener)
110     {
111         this.workListener = workListener;
112     }
113     
114     public void setNotificationManager(ServerNotificationManager notificationManager)
115     {
116         this.notificationManager = notificationManager;
117     }
118     
119     protected MuleConfiguration getMuleConfiguration()
120     {
121         if (config != null)
122         {
123             return config;
124         }
125         else
126         {
127             return createMuleConfiguration();
128         }
129     }
130 
131     protected MuleContextLifecycleManager getLifecycleManager()
132     {
133         if (lifecycleManager != null)
134         {
135             return lifecycleManager;
136         }
137         else
138         {
139             return createLifecycleManager();
140         }
141     }
142 
143     public void setLifecycleManager(LifecycleManager manager)
144     {
145         if (!(manager instanceof MuleContextLifecycleManager))
146         {
147             Message msg = MessageFactory.createStaticMessage(
148                 "lifecycle manager for MuleContext must be a MuleContextLifecycleManager");
149             throw new MuleRuntimeException(msg);
150         }
151         
152         lifecycleManager = (MuleContextLifecycleManager) manager;
153     }
154 
155     protected WorkManager getWorkManager()
156     {
157         if (workManager != null)
158         {
159             return workManager;
160         }
161         else
162         {
163             return createWorkManager();
164         }
165     }
166 
167     protected WorkListener getWorkListener()
168     {
169         if (workListener != null)
170         {
171             return workListener;
172         }
173         else
174         {
175             return createWorkListener();
176         }
177     }
178 
179     protected ServerNotificationManager getNotificationManager()
180     {
181         if (notificationManager != null)
182         {
183             return notificationManager;
184         }
185         else
186         {
187             return createNotificationManager();
188         }
189     }
190 
191     public SplashScreen getStartupScreen()
192     {
193         return startupScreen;
194     }
195 
196     public void setStartupScreen(SplashScreen startupScreen)
197     {
198         this.startupScreen = startupScreen;
199     }
200 
201     public SplashScreen getShutdownScreen()
202     {
203         return shutdownScreen;
204     }
205 
206     public void setShutdownScreen(SplashScreen shutdownScreen)
207     {
208         this.shutdownScreen = shutdownScreen;
209     }
210 
211     protected DefaultMuleConfiguration createMuleConfiguration()
212     {
213         return new DefaultMuleConfiguration();
214     }
215 
216     protected MuleContextLifecycleManager createLifecycleManager()
217     {
218         return new MuleContextLifecycleManager();
219     }
220 
221     protected MuleWorkManager createWorkManager()
222     {
223         return new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, "MuleServer", getMuleConfiguration().getShutdownTimeout());
224     }
225 
226     protected DefaultWorkListener createWorkListener()
227     {
228         return new DefaultWorkListener();
229     }
230 
231     protected ServerNotificationManager createNotificationManager()
232     {
233         ServerNotificationManager manager = new ServerNotificationManager();
234         manager.addInterfaceToType(MuleContextNotificationListener.class,
235                                    MuleContextNotification.class);
236         manager.addInterfaceToType(ModelNotificationListener.class, ModelNotification.class);
237         manager.addInterfaceToType(RoutingNotificationListener.class, RoutingNotification.class);
238         manager.addInterfaceToType(ServiceNotificationListener.class,
239                                    ServiceNotification.class);
240         manager.addInterfaceToType(SecurityNotificationListener.class,
241                                    SecurityNotification.class);
242         manager.addInterfaceToType(ManagementNotificationListener.class,
243                                    ManagementNotification.class);
244         manager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class);
245         manager.addInterfaceToType(ConnectionNotificationListener.class,
246                                    ConnectionNotification.class);
247         manager.addInterfaceToType(RegistryNotificationListener.class,
248                                    RegistryNotification.class);
249         manager.addInterfaceToType(ExceptionNotificationListener.class,
250                                    ExceptionNotification.class);
251         manager.addInterfaceToType(TransactionNotificationListener.class,
252                                    TransactionNotification.class);
253         return manager;
254     }
255 
256     @Override
257     public String toString()
258     {
259         return ClassUtils.getClassName(getClass()) +
260             "{muleConfiguration=" + config +
261             ", lifecycleManager=" + lifecycleManager +
262             ", workManager=" + workManager +
263             ", workListener=" + workListener +
264             ", notificationManager=" + notificationManager + "}";
265     }
266 }