1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.management.agent; |
12 | |
|
13 | |
import javax.management.MBeanException; |
14 | |
import javax.management.MBeanRegistration; |
15 | |
import javax.management.MBeanServer; |
16 | |
import javax.management.NotCompliantMBeanException; |
17 | |
import javax.management.ObjectName; |
18 | |
import javax.management.ReflectionException; |
19 | |
import javax.management.StandardMBean; |
20 | |
|
21 | |
import org.apache.commons.logging.Log; |
22 | |
import org.apache.commons.logging.LogFactory; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class ClassloaderSwitchingMBeanWrapper extends StandardMBean implements MBeanRegistration |
32 | |
{ |
33 | 0 | protected Log logger = LogFactory.getLog(getClass()); |
34 | |
|
35 | |
private ClassLoader executionClassLoader; |
36 | |
|
37 | |
public <T> ClassloaderSwitchingMBeanWrapper(T implementation, Class<T> mbeanInterface, ClassLoader executionClassLoader) |
38 | |
throws NotCompliantMBeanException |
39 | |
{ |
40 | 0 | super(implementation, mbeanInterface); |
41 | 0 | this.executionClassLoader = executionClassLoader; |
42 | 0 | } |
43 | |
|
44 | |
@Override |
45 | |
public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException |
46 | |
{ |
47 | 0 | ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); |
48 | |
try |
49 | |
{ |
50 | 0 | Thread.currentThread().setContextClassLoader(executionClassLoader); |
51 | 0 | return super.invoke(actionName, params, signature); |
52 | |
} |
53 | 0 | catch (MBeanException mbex) |
54 | |
{ |
55 | 0 | throw mbex; |
56 | |
} |
57 | 0 | catch (ReflectionException rex) |
58 | |
{ |
59 | 0 | throw rex; |
60 | |
} |
61 | 0 | catch (Exception ex) |
62 | |
{ |
63 | 0 | logger.error(String.format("MBean operation '%s' failed", actionName), ex); |
64 | 0 | if (ex instanceof RuntimeException) |
65 | |
{ |
66 | 0 | throw (RuntimeException) ex; |
67 | |
} |
68 | |
|
69 | 0 | throw new RuntimeException(ex); |
70 | |
} |
71 | |
finally |
72 | |
{ |
73 | 0 | Thread.currentThread().setContextClassLoader(oldCl); |
74 | |
} |
75 | |
} |
76 | |
|
77 | |
public ClassLoader getExecutionClassLoader() |
78 | |
{ |
79 | 0 | return executionClassLoader; |
80 | |
} |
81 | |
|
82 | |
public void setExecutionClassLoader(ClassLoader executionClassLoader) |
83 | |
{ |
84 | 0 | this.executionClassLoader = executionClassLoader; |
85 | 0 | } |
86 | |
|
87 | |
public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception |
88 | |
{ |
89 | 0 | if (getImplementation() instanceof MBeanRegistration) |
90 | |
{ |
91 | 0 | return ((MBeanRegistration) getImplementation()).preRegister(server, name); |
92 | |
} |
93 | |
|
94 | 0 | return name; |
95 | |
} |
96 | |
|
97 | |
public void postRegister(Boolean registrationDone) |
98 | |
{ |
99 | 0 | if (getImplementation() instanceof MBeanRegistration) |
100 | |
{ |
101 | 0 | ((MBeanRegistration) getImplementation()).postRegister(registrationDone); |
102 | |
} |
103 | 0 | } |
104 | |
|
105 | |
public void preDeregister() throws Exception |
106 | |
{ |
107 | 0 | if (getImplementation() instanceof MBeanRegistration) |
108 | |
{ |
109 | 0 | ((MBeanRegistration) getImplementation()).preDeregister(); |
110 | |
} |
111 | 0 | } |
112 | |
|
113 | |
public void postDeregister() |
114 | |
{ |
115 | 0 | if (getImplementation() instanceof MBeanRegistration) |
116 | |
{ |
117 | 0 | ((MBeanRegistration) getImplementation()).postDeregister(); |
118 | |
} |
119 | 0 | } |
120 | |
} |