1
2
3
4
5
6
7
8
9
10 package org.mule;
11
12 import org.mule.api.MuleContext;
13 import org.mule.api.MuleException;
14 import org.mule.config.ExceptionHelper;
15 import org.mule.config.i18n.CoreMessages;
16 import org.mule.config.i18n.Message;
17 import org.mule.util.StringMessageUtils;
18
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.List;
22
23 import org.apache.commons.logging.Log;
24
25
26
27
28 public class MuleShutdownHook extends Thread
29 {
30 private Log logger;
31 private Throwable exception = null;
32
33 public MuleShutdownHook(Log logger)
34 {
35 super();
36 this.logger = logger;
37 }
38
39 public MuleShutdownHook(Log logger, Throwable exception)
40 {
41 this(logger);
42 this.exception = exception;
43 }
44
45
46
47
48
49
50 public void run()
51 {
52 if (exception != null)
53 {
54 shutdown(exception);
55 }
56 else
57 {
58 shutdown();
59 }
60 }
61
62 protected void shutdown(Throwable t)
63 {
64 Message msg = CoreMessages.fatalErrorWhileRunning();
65 MuleException muleException = ExceptionHelper.getRootMuleException(t);
66 if (muleException != null)
67 {
68 logger.fatal(muleException.getDetailedMessage());
69 }
70 else
71 {
72 logger.fatal(msg.toString() + " " + t.getMessage(), t);
73 }
74
75 List msgs = new ArrayList();
76 msgs.add(msg.getMessage());
77 Throwable root = ExceptionHelper.getRootException(t);
78 msgs.add(root.getMessage() + " (" + root.getClass().getName() + ")");
79 msgs.add(" ");
80 msgs.add(CoreMessages.fatalErrorInShutdown());
81 MuleContext context = MuleServer.getMuleContext();
82 if(context!=null)
83 {
84 msgs.add(CoreMessages.serverStartedAt(context.getStartDate()));
85 }
86 msgs.add(CoreMessages.serverShutdownAt(new Date()));
87
88 String shutdownMessage = StringMessageUtils.getBoilerPlate(msgs, '*', 86);
89
90 if (logger.isFatalEnabled())
91 {
92 logger.fatal(shutdownMessage);
93 }
94 else
95 {
96 System.err.println(shutdownMessage);
97 }
98 }
99
100 protected void shutdown()
101 {
102 logger.info("Mule server shutting down due to normal shutdown request");
103 List msgs = new ArrayList();
104 msgs.add(CoreMessages.normalShutdown());
105
106 MuleContext context = MuleServer.getMuleContext();
107
108 if(context!=null)
109 {
110 msgs.add(CoreMessages.serverStartedAt(context.getStartDate()));
111 }
112 msgs.add(CoreMessages.serverShutdownAt(new Date()));
113 String shutdownMessage = StringMessageUtils.getBoilerPlate(msgs, '*', 86);
114 if (logger.isInfoEnabled())
115 {
116 logger.info(shutdownMessage);
117 }
118 else
119 {
120 System.out.println(shutdownMessage);
121 }
122 }
123
124 public Throwable getException()
125 {
126 return exception;
127 }
128
129 public void setException(Throwable exception)
130 {
131 this.exception = exception;
132 }
133
134 public boolean equals(Object o)
135 {
136 if (this == o)
137 {
138 return true;
139 }
140 if (o != null && getClass().equals(o.getClass()))
141 {
142 return true;
143 }
144
145 return false;
146 }
147
148 public int hashCode()
149 {
150 return getClass().getName().hashCode();
151 }
152 }
153