1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | 0 | public class MuleTomcatListener implements LifecycleListener |
28 | |
{ |
29 | |
|
30 | 0 | private static Log log = LogFactory.getLog(MuleTomcatListener.class); |
31 | |
|
32 | |
protected MuleContext muleContext; |
33 | |
|
34 | |
public void lifecycleEvent(LifecycleEvent event) |
35 | |
{ |
36 | 0 | if (Lifecycle.BEFORE_START_EVENT.equals(event.getType())) |
37 | |
{ |
38 | 0 | if (log.isDebugEnabled()) |
39 | |
{ |
40 | 0 | log.debug("BEFORE_START_EVENT"); |
41 | |
} |
42 | 0 | doStart(); |
43 | 0 | return; |
44 | |
} |
45 | |
|
46 | 0 | if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType())) |
47 | |
{ |
48 | 0 | if (log.isDebugEnabled()) |
49 | |
{ |
50 | 0 | log.debug("BEFORE_STOP_EVENT"); |
51 | |
} |
52 | 0 | doStop(); |
53 | 0 | return; |
54 | |
} |
55 | |
|
56 | 0 | if (log.isDebugEnabled()) |
57 | |
{ |
58 | 0 | log.debug("not our event: " + event.getType()); |
59 | |
} |
60 | 0 | } |
61 | |
|
62 | |
protected void doStart() |
63 | |
{ |
64 | 0 | log.info("Starting Mule"); |
65 | 0 | DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); |
66 | |
try |
67 | |
{ |
68 | 0 | muleContext = muleContextFactory.createMuleContext(); |
69 | 0 | muleContext.start(); |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 0 | DeployableMuleXmlContextListener.setMuleContext(muleContext); |
75 | |
} |
76 | 0 | catch (Exception e) |
77 | |
{ |
78 | 0 | log.error("Failed to start Mule", e); |
79 | 0 | } |
80 | 0 | } |
81 | |
|
82 | |
protected void doStop() |
83 | |
{ |
84 | 0 | log.info("Stopping Mule"); |
85 | |
try |
86 | |
{ |
87 | 0 | muleContext.stop(); |
88 | |
} |
89 | 0 | catch (MuleException e) |
90 | |
{ |
91 | |
|
92 | |
|
93 | 0 | System.err.println("Failed to stop Mule: " + e); |
94 | 0 | } |
95 | 0 | muleContext.dispose(); |
96 | 0 | } |
97 | |
} |