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