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.module.tomcat;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.config.builders.DeployableMuleXmlContextListener;
12  import org.mule.context.DefaultMuleContextFactory;
13  
14  import org.apache.catalina.Lifecycle;
15  import org.apache.catalina.LifecycleEvent;
16  import org.apache.catalina.LifecycleListener;
17  import org.apache.juli.logging.Log;
18  import org.apache.juli.logging.LogFactory;
19  
20  /**
21   *
22   */
23  public class MuleTomcatListener implements LifecycleListener
24  {
25  
26      private static Log log = LogFactory.getLog(MuleTomcatListener.class);
27  
28      protected MuleContext muleContext;
29  
30      public void lifecycleEvent(LifecycleEvent event)
31      {
32          if (Lifecycle.BEFORE_START_EVENT.equals(event.getType()))
33          {
34              if (log.isDebugEnabled())
35              {
36                  log.debug("BEFORE_START_EVENT");
37              }
38              doStart();
39              return;
40          }
41  
42          if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType()))
43          {
44              if (log.isDebugEnabled())
45              {
46                  log.debug("BEFORE_STOP_EVENT");
47              }
48              doStop();
49              return;
50          }
51  
52          if (log.isDebugEnabled())
53          {
54              log.debug("not our event: " + event.getType());
55          }
56      }
57  
58      protected void doStart()
59      {
60          log.info("Starting Mule");
61          DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
62          try
63          {
64              muleContext = muleContextFactory.createMuleContext();
65              muleContext.start();
66  
67              // Make single shared instance of mule context
68              // available to DeployableMuleXmlContextListener to support
69              // hot-deployment of Mule configurations in web applications.
70              DeployableMuleXmlContextListener.setMuleContext(muleContext);
71          }
72          catch (Exception e)
73          {
74              log.error("Failed to start Mule", e);
75          }
76      }
77  
78      protected void doStop()
79      {
80          log.info("Stopping Mule");
81          try
82          {
83              muleContext.stop();
84          }
85          catch (MuleException e)
86          {
87              // sigh, ridiculous juli bugs - logger would have already been disposed
88              // by a shutdown handler by now
89              System.err.println("Failed to stop Mule: " + e);
90          }
91          muleContext.dispose();
92      }
93  }