KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > SpringModelMBean


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jmx.export;
18
19 import javax.management.Attribute JavaDoc;
20 import javax.management.AttributeList JavaDoc;
21 import javax.management.AttributeNotFoundException JavaDoc;
22 import javax.management.InstanceNotFoundException JavaDoc;
23 import javax.management.InvalidAttributeValueException JavaDoc;
24 import javax.management.MBeanException JavaDoc;
25 import javax.management.ReflectionException JavaDoc;
26 import javax.management.RuntimeOperationsException JavaDoc;
27 import javax.management.modelmbean.InvalidTargetObjectTypeException JavaDoc;
28 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
29 import javax.management.modelmbean.RequiredModelMBean JavaDoc;
30
31 /**
32  * Extension of the {@link RequiredModelMBean} class that ensures the
33  * {@link Thread#getContextClassLoader() thread context ClassLoader} is switched
34  * for the managed resources {@link ClassLoader} before any invocations occur.
35  *
36  * @author Rob Harrop
37  * @since 2.0
38  * @see RequiredModelMBean
39  */

40 public class SpringModelMBean extends RequiredModelMBean JavaDoc {
41
42     /**
43      * Stores the {@link ClassLoader} to use for invocations. Defaults
44      * to the current thread {@link ClassLoader}.
45      */

46     private ClassLoader JavaDoc managedResourceClassLoader = Thread.currentThread().getContextClassLoader();
47
48
49     /**
50      * Construct a new SpringModelMBean instance with an empty {@link ModelMBeanInfo}.
51      * @see javax.management.modelmbean.RequiredModelMBean#RequiredModelMBean()
52      */

53     public SpringModelMBean() throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
54         super();
55     }
56
57     /**
58      * Construct a new SpringModelMBean instance with the given {@link ModelMBeanInfo}.
59      * @see javax.management.modelmbean.RequiredModelMBean#RequiredModelMBean(ModelMBeanInfo)
60      */

61     public SpringModelMBean(ModelMBeanInfo JavaDoc mbi) throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
62         super(mbi);
63     }
64
65
66     /**
67      * Sets managed resource to expose and stores its {@link ClassLoader}.
68      */

69     public void setManagedResource(Object JavaDoc managedResource, String JavaDoc managedResourceType)
70             throws MBeanException JavaDoc, InstanceNotFoundException JavaDoc, InvalidTargetObjectTypeException JavaDoc {
71
72         this.managedResourceClassLoader = managedResource.getClass().getClassLoader();
73         super.setManagedResource(managedResource, managedResourceType);
74     }
75
76
77     /**
78      * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
79      * managed resources {@link ClassLoader} before allowing the invocation to occur.
80      * @see javax.management.modelmbean.ModelMBean#invoke
81      */

82     public Object JavaDoc invoke(final String JavaDoc opName, final Object JavaDoc[] opArgs, final String JavaDoc[] sig)
83             throws MBeanException JavaDoc, ReflectionException JavaDoc {
84
85         ClassLoader JavaDoc currentClassLoader = Thread.currentThread().getContextClassLoader();
86         try {
87             Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader);
88             return super.invoke(opName, opArgs, sig);
89         }
90         finally {
91             Thread.currentThread().setContextClassLoader(currentClassLoader);
92         }
93     }
94
95     /**
96      * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
97      * managed resources {@link ClassLoader} before allowing the invocation to occur.
98      * @see javax.management.modelmbean.ModelMBean#getAttribute
99      */

100     public Object JavaDoc getAttribute(final String JavaDoc attrName)
101             throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
102
103         ClassLoader JavaDoc currentClassLoader = Thread.currentThread().getContextClassLoader();
104         try {
105             Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader);
106             return super.getAttribute(attrName);
107         }
108         finally {
109             Thread.currentThread().setContextClassLoader(currentClassLoader);
110         }
111     }
112
113     /**
114      * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
115      * managed resources {@link ClassLoader} before allowing the invocation to occur.
116      * @see javax.management.modelmbean.ModelMBean#getAttributes
117      */

118     public AttributeList JavaDoc getAttributes(String JavaDoc[] attrNames) {
119         ClassLoader JavaDoc currentClassLoader = Thread.currentThread().getContextClassLoader();
120         try {
121             Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader);
122             return super.getAttributes(attrNames);
123         }
124         finally {
125             Thread.currentThread().setContextClassLoader(currentClassLoader);
126         }
127     }
128
129     /**
130      * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
131      * managed resources {@link ClassLoader} before allowing the invocation to occur.
132      * @see javax.management.modelmbean.ModelMBean#setAttribute
133      */

134     public void setAttribute(Attribute JavaDoc attribute)
135             throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
136
137         ClassLoader JavaDoc currentClassLoader = Thread.currentThread().getContextClassLoader();
138         try {
139             Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader);
140             super.setAttribute(attribute);
141         }
142         finally {
143             Thread.currentThread().setContextClassLoader(currentClassLoader);
144         }
145     }
146
147     /**
148      * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
149      * managed resources {@link ClassLoader} before allowing the invocation to occur.
150      * @see javax.management.modelmbean.ModelMBean#setAttributes
151      */

152     public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes) {
153         ClassLoader JavaDoc currentClassLoader = Thread.currentThread().getContextClassLoader();
154         try {
155             Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader);
156             return super.setAttributes(attributes);
157         }
158         finally {
159             Thread.currentThread().setContextClassLoader(currentClassLoader);
160         }
161     }
162
163 }
164
Popular Tags