KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > server > interceptor > ContextClassLoaderMBeanServerInterceptor


1 /*
2  * Copyright (C) MX4J.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.server.interceptor;
10
11 import java.security.PrivilegedAction JavaDoc;
12 import java.security.AccessController JavaDoc;
13
14 import javax.management.Attribute JavaDoc;
15 import javax.management.AttributeList JavaDoc;
16 import javax.management.AttributeNotFoundException JavaDoc;
17 import javax.management.InvalidAttributeValueException JavaDoc;
18 import javax.management.ListenerNotFoundException JavaDoc;
19 import javax.management.MBeanException JavaDoc;
20 import javax.management.MBeanInfo JavaDoc;
21 import javax.management.MBeanRegistrationException JavaDoc;
22 import javax.management.NotificationFilter JavaDoc;
23 import javax.management.NotificationListener JavaDoc;
24 import javax.management.ReflectionException JavaDoc;
25
26 import mx4j.server.MBeanMetaData;
27
28 /**
29  * This interceptor sets the context class loader to the proper value for incoming calls.
30  * It saves the current context class loader, set the context class loader to be the MBean's class loader for
31  * the current call, and on return re-set the context class loader to the previous value
32  *
33  * @author <a HREF="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
34  * @version $Revision: 1.9 $
35  */

36 public class ContextClassLoaderMBeanServerInterceptor extends DefaultMBeanServerInterceptor
37 {
38    public ContextClassLoaderMBeanServerInterceptor()
39    {
40       // Disabled by default
41
setEnabled(false);
42    }
43
44    public String JavaDoc getType()
45    {
46       return "contextclassloader";
47    }
48
49    public void addNotificationListener(MBeanMetaData metadata, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
50    {
51       if (isEnabled())
52       {
53          ClassLoader JavaDoc context = getContextClassLoader();
54          if (metadata.getClassLoader() != context)
55          {
56             try
57             {
58                setContextClassLoader(metadata.getClassLoader());
59                super.addNotificationListener(metadata, listener, filter, handback);
60                return;
61             }
62             finally
63             {
64                setContextClassLoader(context);
65             }
66          }
67       }
68
69       super.addNotificationListener(metadata, listener, filter, handback);
70    }
71
72    public void removeNotificationListener(MBeanMetaData metadata, NotificationListener JavaDoc listener) throws ListenerNotFoundException JavaDoc
73    {
74       if (isEnabled())
75       {
76          ClassLoader JavaDoc context = getContextClassLoader();
77          if (metadata.getClassLoader() != context)
78          {
79             try
80             {
81                setContextClassLoader(metadata.getClassLoader());
82                super.removeNotificationListener(metadata, listener);
83                return;
84             }
85             finally
86             {
87                setContextClassLoader(context);
88             }
89          }
90       }
91
92       super.removeNotificationListener(metadata, listener);
93    }
94
95    public void removeNotificationListener(MBeanMetaData metadata, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback) throws ListenerNotFoundException JavaDoc
96    {
97       if (isEnabled())
98       {
99          ClassLoader JavaDoc context = getContextClassLoader();
100          if (metadata.getClassLoader() != context)
101          {
102             try
103             {
104                setContextClassLoader(metadata.getClassLoader());
105                super.removeNotificationListener(metadata, listener, filter, handback);
106                return;
107             }
108             finally
109             {
110                setContextClassLoader(context);
111             }
112          }
113       }
114
115       super.removeNotificationListener(metadata, listener, filter, handback);
116    }
117
118    public void instantiate(MBeanMetaData metadata, String JavaDoc className, String JavaDoc[] params, Object JavaDoc[] args) throws ReflectionException JavaDoc, MBeanException JavaDoc
119    {
120       if (isEnabled())
121       {
122          ClassLoader JavaDoc context = getContextClassLoader();
123          if (metadata.getClassLoader() != context)
124          {
125             try
126             {
127                setContextClassLoader(metadata.getClassLoader());
128                super.instantiate(metadata, className, params, args);
129                return;
130             }
131             finally
132             {
133                setContextClassLoader(context);
134             }
135          }
136       }
137
138       super.instantiate(metadata, className, params, args);
139    }
140
141    public void registration(MBeanMetaData metadata, int operation) throws MBeanRegistrationException JavaDoc
142    {
143       if (isEnabled())
144       {
145          ClassLoader JavaDoc context = getContextClassLoader();
146          if (metadata.getClassLoader() != context)
147          {
148             try
149             {
150                setContextClassLoader(metadata.getClassLoader());
151                super.registration(metadata, operation);
152                return;
153             }
154             finally
155             {
156                setContextClassLoader(context);
157             }
158          }
159       }
160
161       super.registration(metadata, operation);
162    }
163
164    public MBeanInfo JavaDoc getMBeanInfo(MBeanMetaData metadata)
165    {
166       if (isEnabled())
167       {
168          ClassLoader JavaDoc context = getContextClassLoader();
169          if (metadata.getClassLoader() != context)
170          {
171             try
172             {
173                setContextClassLoader(metadata.getClassLoader());
174                return super.getMBeanInfo(metadata);
175             }
176             finally
177             {
178                setContextClassLoader(context);
179             }
180          }
181       }
182
183       return super.getMBeanInfo(metadata);
184    }
185
186    public Object JavaDoc invoke(MBeanMetaData metadata, String JavaDoc method, String JavaDoc[] params, Object JavaDoc[] args) throws MBeanException JavaDoc, ReflectionException JavaDoc
187    {
188       if (isEnabled())
189       {
190          ClassLoader JavaDoc context = getContextClassLoader();
191          if (metadata.getClassLoader() != context)
192          {
193             try
194             {
195                setContextClassLoader(metadata.getClassLoader());
196                return super.invoke(metadata, method, params, args);
197             }
198             finally
199             {
200                setContextClassLoader(context);
201             }
202          }
203       }
204
205       return super.invoke(metadata, method, params, args);
206    }
207
208    public AttributeList JavaDoc getAttributes(MBeanMetaData metadata, String JavaDoc[] attributes)
209    {
210       if (isEnabled())
211       {
212          ClassLoader JavaDoc context = getContextClassLoader();
213          if (metadata.getClassLoader() != context)
214          {
215             try
216             {
217                setContextClassLoader(metadata.getClassLoader());
218                return super.getAttributes(metadata, attributes);
219             }
220             finally
221             {
222                setContextClassLoader(context);
223             }
224          }
225       }
226
227       return super.getAttributes(metadata, attributes);
228    }
229
230    public AttributeList JavaDoc setAttributes(MBeanMetaData metadata, AttributeList JavaDoc attributes)
231    {
232       if (isEnabled())
233       {
234          ClassLoader JavaDoc context = getContextClassLoader();
235          if (metadata.getClassLoader() != context)
236          {
237             try
238             {
239                setContextClassLoader(metadata.getClassLoader());
240                return super.setAttributes(metadata, attributes);
241             }
242             finally
243             {
244                setContextClassLoader(context);
245             }
246          }
247       }
248
249       return super.setAttributes(metadata, attributes);
250    }
251
252    public Object JavaDoc getAttribute(MBeanMetaData metadata, String JavaDoc attribute) throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, ReflectionException JavaDoc
253    {
254       if (isEnabled())
255       {
256          ClassLoader JavaDoc context = getContextClassLoader();
257          if (metadata.getClassLoader() != context)
258          {
259             try
260             {
261                setContextClassLoader(metadata.getClassLoader());
262                return super.getAttribute(metadata, attribute);
263             }
264             finally
265             {
266                setContextClassLoader(context);
267             }
268          }
269       }
270
271       return super.getAttribute(metadata, attribute);
272    }
273
274    public void setAttribute(MBeanMetaData metadata, Attribute JavaDoc attribute) throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, ReflectionException JavaDoc
275    {
276       if (isEnabled())
277       {
278          ClassLoader JavaDoc context = getContextClassLoader();
279          if (metadata.getClassLoader() != context)
280          {
281             try
282             {
283                setContextClassLoader(metadata.getClassLoader());
284                super.setAttribute(metadata, attribute);
285                return;
286             }
287             finally
288             {
289                setContextClassLoader(context);
290             }
291          }
292       }
293
294       super.setAttribute(metadata, attribute);
295    }
296
297    private ClassLoader JavaDoc getContextClassLoader()
298    {
299       return Thread.currentThread().getContextClassLoader();
300    }
301
302    private void setContextClassLoader(final ClassLoader JavaDoc cl)
303    {
304       AccessController.doPrivileged(new PrivilegedAction JavaDoc()
305       {
306          public Object JavaDoc run()
307          {
308             Thread.currentThread().setContextClassLoader(cl);
309             return null;
310          }
311       });
312    }
313 }
314
Popular Tags