001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.server;
023:
024: import javax.management.Attribute;
025: import javax.management.AttributeList;
026: import javax.management.AttributeNotFoundException;
027: import javax.management.DynamicMBean;
028: import javax.management.InvalidAttributeValueException;
029: import javax.management.MBeanException;
030: import javax.management.MBeanInfo;
031: import javax.management.MBeanRegistration;
032: import javax.management.MBeanServer;
033: import javax.management.NotCompliantMBeanException;
034: import javax.management.ObjectName;
035: import javax.management.ReflectionException;
036:
037: /**
038: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
039: * @version $Revision: 57200 $
040: *
041: */
042: public class RawDynamicInvoker extends AbstractMBeanInvoker {
043:
044: private DynamicMBean typedRes = null;
045:
046: public RawDynamicInvoker(DynamicMBean resource) {
047: super (resource);
048: this .typedRes = resource;
049: }
050:
051: // DynamicMBean overrides ----------------------------------------
052:
053: public void setAttribute(Attribute attribute)
054: throws AttributeNotFoundException,
055: InvalidAttributeValueException, MBeanException,
056: ReflectionException {
057: ClassLoader mbeanTCL = resourceEntry.getClassLoader();
058: final ClassLoader ccl = TCLAction.UTIL.getContextClassLoader();
059: boolean setCl = ccl != mbeanTCL && mbeanTCL != null;
060: if (setCl) {
061: TCLAction.UTIL.setContextClassLoader(mbeanTCL);
062: }
063:
064: try {
065: typedRes.setAttribute(attribute);
066: } finally {
067: if (setCl) {
068: TCLAction.UTIL.setContextClassLoader(ccl);
069: }
070: }
071: }
072:
073: public AttributeList setAttributes(AttributeList attributes) {
074: ClassLoader mbeanTCL = resourceEntry.getClassLoader();
075: final ClassLoader ccl = TCLAction.UTIL.getContextClassLoader();
076: boolean setCl = ccl != mbeanTCL && mbeanTCL != null;
077: if (setCl) {
078: TCLAction.UTIL.setContextClassLoader(mbeanTCL);
079: }
080:
081: try {
082: return typedRes.setAttributes(attributes);
083: } finally {
084: if (setCl) {
085: TCLAction.UTIL.setContextClassLoader(ccl);
086: }
087: }
088: }
089:
090: public Object getAttribute(String name)
091: throws AttributeNotFoundException, MBeanException,
092: ReflectionException {
093: ClassLoader mbeanTCL = resourceEntry.getClassLoader();
094: final ClassLoader ccl = TCLAction.UTIL.getContextClassLoader();
095: boolean setCl = ccl != mbeanTCL && mbeanTCL != null;
096: if (setCl) {
097: TCLAction.UTIL.setContextClassLoader(mbeanTCL);
098: }
099:
100: try {
101: return typedRes.getAttribute(name);
102: } finally {
103: if (setCl) {
104: TCLAction.UTIL.setContextClassLoader(ccl);
105: }
106: }
107: }
108:
109: public AttributeList getAttributes(String[] attributes) {
110: ClassLoader mbeanTCL = resourceEntry.getClassLoader();
111: final ClassLoader ccl = TCLAction.UTIL.getContextClassLoader();
112: boolean setCl = ccl != mbeanTCL && mbeanTCL != null;
113: if (setCl) {
114: TCLAction.UTIL.setContextClassLoader(mbeanTCL);
115: }
116:
117: try {
118: return typedRes.getAttributes(attributes);
119: } finally {
120: if (setCl) {
121: TCLAction.UTIL.setContextClassLoader(ccl);
122: }
123: }
124: }
125:
126: public Object invoke(String name, Object[] args, String[] signature)
127: throws MBeanException, ReflectionException {
128: ClassLoader mbeanTCL = resourceEntry.getClassLoader();
129: final ClassLoader ccl = TCLAction.UTIL.getContextClassLoader();
130: boolean setCl = ccl != mbeanTCL && mbeanTCL != null;
131: if (setCl) {
132: TCLAction.UTIL.setContextClassLoader(mbeanTCL);
133: }
134:
135: try {
136: return typedRes.invoke(name, args, signature);
137: } finally {
138: if (setCl) {
139: TCLAction.UTIL.setContextClassLoader(ccl);
140: }
141: }
142: }
143:
144: public MBeanInfo getMBeanInfo() {
145: ClassLoader mbeanTCL = resourceEntry.getClassLoader();
146: final ClassLoader ccl = TCLAction.UTIL.getContextClassLoader();
147: boolean setCl = ccl != mbeanTCL && mbeanTCL != null;
148: if (setCl) {
149: TCLAction.UTIL.setContextClassLoader(mbeanTCL);
150: }
151:
152: try {
153: return typedRes.getMBeanInfo();
154: } finally {
155: if (setCl) {
156: TCLAction.UTIL.setContextClassLoader(ccl);
157: }
158: }
159: }
160:
161: // MBeanRegistration overrides -----------------------------------
162: public ObjectName preRegister(MBeanServer server, ObjectName oname)
163: throws Exception {
164: this .resourceEntry = AbstractMBeanInvoker.getMBeanEntry();
165:
166: try {
167: this .info = getMBeanInfo();
168: } catch (Exception e) {
169: throw new NotCompliantMBeanException(
170: "Cannot obtain MBeanInfo, for: " + oname);
171: }
172:
173: ClassLoader mbeanTCL = resourceEntry.getClassLoader();
174: final ClassLoader ccl = TCLAction.UTIL.getContextClassLoader();
175: boolean setCl = ccl != mbeanTCL && mbeanTCL != null;
176: if (setCl) {
177: TCLAction.UTIL.setContextClassLoader(mbeanTCL);
178: }
179:
180: try {
181: if (getResource() instanceof MBeanRegistration)
182: return ((MBeanRegistration) getResource()).preRegister(
183: server, oname);
184: else
185: return oname;
186: } finally {
187: if (setCl) {
188: TCLAction.UTIL.setContextClassLoader(ccl);
189: }
190: }
191: }
192:
193: public void postRegister(Boolean b) {
194: ClassLoader mbeanTCL = resourceEntry.getClassLoader();
195: final ClassLoader ccl = TCLAction.UTIL.getContextClassLoader();
196: boolean setCl = ccl != mbeanTCL && mbeanTCL != null;
197: if (setCl) {
198: TCLAction.UTIL.setContextClassLoader(mbeanTCL);
199: }
200:
201: try {
202: if (getResource() instanceof MBeanRegistration)
203: ((MBeanRegistration) getResource()).postRegister(b);
204: } finally {
205: TCLAction.UTIL.setContextClassLoader(ccl);
206: }
207: }
208:
209: public void preDeregister() throws Exception {
210: ClassLoader mbeanTCL = resourceEntry.getClassLoader();
211: final ClassLoader ccl = TCLAction.UTIL.getContextClassLoader();
212: boolean setCl = ccl != mbeanTCL && mbeanTCL != null;
213: if (setCl) {
214: TCLAction.UTIL.setContextClassLoader(mbeanTCL);
215: }
216:
217: try {
218: if (getResource() instanceof MBeanRegistration)
219: ((MBeanRegistration) getResource()).preDeregister();
220: } finally {
221: if (setCl) {
222: TCLAction.UTIL.setContextClassLoader(ccl);
223: }
224: }
225: }
226:
227: public void postDeregister() {
228: ClassLoader mbeanTCL = resourceEntry.getClassLoader();
229: final ClassLoader ccl = TCLAction.UTIL.getContextClassLoader();
230: boolean setCl = ccl != mbeanTCL && mbeanTCL != null;
231: if (setCl) {
232: TCLAction.UTIL.setContextClassLoader(mbeanTCL);
233: }
234:
235: try {
236: if (getResource() instanceof MBeanRegistration)
237: ((MBeanRegistration) getResource()).postDeregister();
238: } finally {
239: if (setCl) {
240: TCLAction.UTIL.setContextClassLoader(ccl);
241: }
242: }
243: }
244: }
|