View Javadoc

1   /*
2    * $Id: DefaultMuleContextBuilder.java 11483 2008-03-22 19:28:06Z rossmason $
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.context;
12  
13  import org.mule.DefaultMuleContext;
14  import org.mule.api.MuleContext;
15  import org.mule.api.config.MuleConfiguration;
16  import org.mule.api.config.ThreadingProfile;
17  import org.mule.api.context.MuleContextBuilder;
18  import org.mule.api.context.WorkManager;
19  import org.mule.api.context.notification.ConnectionNotificationListener;
20  import org.mule.api.context.notification.CustomNotificationListener;
21  import org.mule.api.context.notification.ExceptionNotificationListener;
22  import org.mule.api.context.notification.ManagementNotificationListener;
23  import org.mule.api.context.notification.ModelNotificationListener;
24  import org.mule.api.context.notification.MuleContextNotificationListener;
25  import org.mule.api.context.notification.RegistryNotificationListener;
26  import org.mule.api.context.notification.RoutingNotificationListener;
27  import org.mule.api.context.notification.SecurityNotificationListener;
28  import org.mule.api.context.notification.ServiceNotificationListener;
29  import org.mule.api.context.notification.TransactionNotificationListener;
30  import org.mule.api.lifecycle.LifecycleManager;
31  import org.mule.config.DefaultMuleConfiguration;
32  import org.mule.context.notification.ConnectionNotification;
33  import org.mule.context.notification.CustomNotification;
34  import org.mule.context.notification.ExceptionNotification;
35  import org.mule.context.notification.ManagementNotification;
36  import org.mule.context.notification.ModelNotification;
37  import org.mule.context.notification.MuleContextNotification;
38  import org.mule.context.notification.RegistryNotification;
39  import org.mule.context.notification.RoutingNotification;
40  import org.mule.context.notification.SecurityNotification;
41  import org.mule.context.notification.ServerNotificationManager;
42  import org.mule.context.notification.ServiceNotification;
43  import org.mule.context.notification.TransactionNotification;
44  import org.mule.lifecycle.GenericLifecycleManager;
45  import org.mule.lifecycle.phases.MuleContextDisposePhase;
46  import org.mule.lifecycle.phases.MuleContextInitialisePhase;
47  import org.mule.lifecycle.phases.MuleContextStartPhase;
48  import org.mule.lifecycle.phases.MuleContextStopPhase;
49  import org.mule.util.ClassUtils;
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 LifecycleManager lifecycleManager;
71  
72      protected WorkManager workManager;
73  
74      protected WorkListener workListener;
75  
76      protected ServerNotificationManager notificationManager;
77  
78      /**
79       * {@inheritDoc}
80       */
81      public MuleContext buildMuleContext()
82      {
83          logger.debug("Building new DefaultMuleContext instance with MuleContextBuilder: " + this);
84          MuleContext muleContext = new DefaultMuleContext(getMuleConfiguration(),
85                                                           getWorkManager(),
86                                                           getWorkListener(),
87                                                           getLifecycleManager(),
88                                                           getNotificationManager());
89          return muleContext;
90      }
91  
92      public void setMuleConfiguration(MuleConfiguration config)
93      {
94          this.config = config;
95      }
96      
97      public void setWorkManager(WorkManager workManager)
98      {
99          this.workManager = workManager;
100     }
101 
102     public void setWorkListener(WorkListener workListener)
103     {
104         this.workListener = workListener;
105     }
106     
107     public void setNotificationManager(ServerNotificationManager notificationManager)
108     {
109         this.notificationManager = notificationManager;
110     }
111 
112     public void setLifecycleManager(LifecycleManager lifecycleManager)
113     {
114         this.lifecycleManager = lifecycleManager;
115     }
116     
117     protected MuleConfiguration getMuleConfiguration()
118     {
119         if (config != null)
120         {
121             return config;
122         }
123         else
124         {
125             return new DefaultMuleConfiguration();
126         }
127     }
128 
129     protected LifecycleManager getLifecycleManager()
130     {
131         if (lifecycleManager != null)
132         {
133             return lifecycleManager;
134         }
135         else
136         {
137             LifecycleManager lifecycleManager = new GenericLifecycleManager();
138             lifecycleManager.registerLifecycle(new MuleContextInitialisePhase());
139             lifecycleManager.registerLifecycle(new MuleContextStartPhase());
140             lifecycleManager.registerLifecycle(new MuleContextStopPhase());
141             lifecycleManager.registerLifecycle(new MuleContextDisposePhase());
142             return lifecycleManager;
143         }
144     }
145 
146     protected WorkManager getWorkManager()
147     {
148         if (workManager != null)
149         {
150             return workManager;
151         }
152         else
153         {
154             return new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, "MuleServer");
155         }
156     }
157 
158     protected WorkListener getWorkListener()
159     {
160         if (workListener != null)
161         {
162             return workListener;
163         }
164         else
165         {
166             return new DefaultWorkListener();
167         }
168     }
169 
170     protected ServerNotificationManager getNotificationManager()
171     {
172         if (notificationManager != null)
173         {
174             return notificationManager;
175         }
176         else
177         {
178             ServerNotificationManager notificationManager = new ServerNotificationManager();
179             notificationManager.addInterfaceToType(MuleContextNotificationListener.class,
180                 MuleContextNotification.class);
181             notificationManager.addInterfaceToType(ModelNotificationListener.class, ModelNotification.class);
182             notificationManager.addInterfaceToType(RoutingNotificationListener.class, RoutingNotification.class);
183             notificationManager.addInterfaceToType(ServiceNotificationListener.class,
184                 ServiceNotification.class);
185             notificationManager.addInterfaceToType(SecurityNotificationListener.class,
186                 SecurityNotification.class);
187             notificationManager.addInterfaceToType(ManagementNotificationListener.class,
188                 ManagementNotification.class);
189             notificationManager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class);
190             notificationManager.addInterfaceToType(ConnectionNotificationListener.class,
191                 ConnectionNotification.class);
192             notificationManager.addInterfaceToType(RegistryNotificationListener.class,
193                 RegistryNotification.class);
194             notificationManager.addInterfaceToType(ExceptionNotificationListener.class,
195                 ExceptionNotification.class);
196             notificationManager.addInterfaceToType(TransactionNotificationListener.class,
197                 TransactionNotification.class);
198             return notificationManager;
199         }
200     }
201 
202     public String toString()
203     {
204         return ClassUtils.getClassName(getClass()) + 
205             "{muleConfiguration=" + config +
206             ", lifecycleManager=" + lifecycleManager + 
207             ", workManager=" + workManager + 
208             ", workListener=" + workListener + 
209             ", notificationManager=" + notificationManager + "}";
210     }
211 }