View Javadoc

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