001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jmx.export;
018:
019: import javax.management.Attribute;
020: import javax.management.AttributeList;
021: import javax.management.AttributeNotFoundException;
022: import javax.management.InstanceNotFoundException;
023: import javax.management.InvalidAttributeValueException;
024: import javax.management.MBeanException;
025: import javax.management.ReflectionException;
026: import javax.management.RuntimeOperationsException;
027: import javax.management.modelmbean.InvalidTargetObjectTypeException;
028: import javax.management.modelmbean.ModelMBeanInfo;
029: import javax.management.modelmbean.RequiredModelMBean;
030:
031: /**
032: * Extension of the {@link RequiredModelMBean} class that ensures the
033: * {@link Thread#getContextClassLoader() thread context ClassLoader} is switched
034: * for the managed resources {@link ClassLoader} before any invocations occur.
035: *
036: * @author Rob Harrop
037: * @since 2.0
038: * @see RequiredModelMBean
039: */
040: public class SpringModelMBean extends RequiredModelMBean {
041:
042: /**
043: * Stores the {@link ClassLoader} to use for invocations. Defaults
044: * to the current thread {@link ClassLoader}.
045: */
046: private ClassLoader managedResourceClassLoader = Thread
047: .currentThread().getContextClassLoader();
048:
049: /**
050: * Construct a new SpringModelMBean instance with an empty {@link ModelMBeanInfo}.
051: * @see javax.management.modelmbean.RequiredModelMBean#RequiredModelMBean()
052: */
053: public SpringModelMBean() throws MBeanException,
054: RuntimeOperationsException {
055: super ();
056: }
057:
058: /**
059: * Construct a new SpringModelMBean instance with the given {@link ModelMBeanInfo}.
060: * @see javax.management.modelmbean.RequiredModelMBean#RequiredModelMBean(ModelMBeanInfo)
061: */
062: public SpringModelMBean(ModelMBeanInfo mbi) throws MBeanException,
063: RuntimeOperationsException {
064: super (mbi);
065: }
066:
067: /**
068: * Sets managed resource to expose and stores its {@link ClassLoader}.
069: */
070: public void setManagedResource(Object managedResource,
071: String managedResourceType) throws MBeanException,
072: InstanceNotFoundException, InvalidTargetObjectTypeException {
073:
074: this .managedResourceClassLoader = managedResource.getClass()
075: .getClassLoader();
076: super .setManagedResource(managedResource, managedResourceType);
077: }
078:
079: /**
080: * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
081: * managed resources {@link ClassLoader} before allowing the invocation to occur.
082: * @see javax.management.modelmbean.ModelMBean#invoke
083: */
084: public Object invoke(final String opName, final Object[] opArgs,
085: final String[] sig) throws MBeanException,
086: ReflectionException {
087:
088: ClassLoader currentClassLoader = Thread.currentThread()
089: .getContextClassLoader();
090: try {
091: Thread.currentThread().setContextClassLoader(
092: this .managedResourceClassLoader);
093: return super .invoke(opName, opArgs, sig);
094: } finally {
095: Thread.currentThread().setContextClassLoader(
096: currentClassLoader);
097: }
098: }
099:
100: /**
101: * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
102: * managed resources {@link ClassLoader} before allowing the invocation to occur.
103: * @see javax.management.modelmbean.ModelMBean#getAttribute
104: */
105: public Object getAttribute(final String attrName)
106: throws AttributeNotFoundException, MBeanException,
107: ReflectionException {
108:
109: ClassLoader currentClassLoader = Thread.currentThread()
110: .getContextClassLoader();
111: try {
112: Thread.currentThread().setContextClassLoader(
113: this .managedResourceClassLoader);
114: return super .getAttribute(attrName);
115: } finally {
116: Thread.currentThread().setContextClassLoader(
117: currentClassLoader);
118: }
119: }
120:
121: /**
122: * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
123: * managed resources {@link ClassLoader} before allowing the invocation to occur.
124: * @see javax.management.modelmbean.ModelMBean#getAttributes
125: */
126: public AttributeList getAttributes(String[] attrNames) {
127: ClassLoader currentClassLoader = Thread.currentThread()
128: .getContextClassLoader();
129: try {
130: Thread.currentThread().setContextClassLoader(
131: this .managedResourceClassLoader);
132: return super .getAttributes(attrNames);
133: } finally {
134: Thread.currentThread().setContextClassLoader(
135: currentClassLoader);
136: }
137: }
138:
139: /**
140: * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
141: * managed resources {@link ClassLoader} before allowing the invocation to occur.
142: * @see javax.management.modelmbean.ModelMBean#setAttribute
143: */
144: public void setAttribute(Attribute attribute)
145: throws AttributeNotFoundException,
146: InvalidAttributeValueException, MBeanException,
147: ReflectionException {
148:
149: ClassLoader currentClassLoader = Thread.currentThread()
150: .getContextClassLoader();
151: try {
152: Thread.currentThread().setContextClassLoader(
153: this .managedResourceClassLoader);
154: super .setAttribute(attribute);
155: } finally {
156: Thread.currentThread().setContextClassLoader(
157: currentClassLoader);
158: }
159: }
160:
161: /**
162: * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
163: * managed resources {@link ClassLoader} before allowing the invocation to occur.
164: * @see javax.management.modelmbean.ModelMBean#setAttributes
165: */
166: public AttributeList setAttributes(AttributeList attributes) {
167: ClassLoader currentClassLoader = Thread.currentThread()
168: .getContextClassLoader();
169: try {
170: Thread.currentThread().setContextClassLoader(
171: this.managedResourceClassLoader);
172: return super.setAttributes(attributes);
173: } finally {
174: Thread.currentThread().setContextClassLoader(
175: currentClassLoader);
176: }
177: }
178:
179: }
|