1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22 package org.jboss.mx.modelmbean;
23
24 import javax.management.Attribute;
25 import javax.management.AttributeList;
26 import javax.management.AttributeNotFoundException;
27 import javax.management.DynamicMBean;
28 import javax.management.InvalidAttributeValueException;
29 import javax.management.MBeanException;
30 import javax.management.ReflectionException;
31 import javax.management.RuntimeMBeanException;
32 import javax.management.RuntimeOperationsException;
33 import javax.management.ServiceNotFoundException;
34 import javax.management.modelmbean.RequiredModelMBean;
35
36 import org.jboss.mx.server.RawDynamicInvoker;
37
38 /** An invoker that handles the 'ops' that are part of the RequiredModelMBean
39 * that must be handled at that level rather than its delegate.
40 *
41 * @author Scott.Stark@jboss.org
42 * @version $Revison:$
43 */
44 public class RequiredModelMBeanInvoker extends RawDynamicInvoker
45 {
46 RequiredModelMBean mbean;
47
48 public RequiredModelMBeanInvoker(DynamicMBean resource)
49 {
50 super(resource);
51 mbean = (RequiredModelMBean) resource;
52 }
53
54 public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException
55 {
56 try
57 {
58 return super.getAttribute(name);
59 }
60 catch (ReflectionException e)
61 {
62 // Another inconsistency
63 Exception ex = e.getTargetException();
64 if ((ex instanceof ClassNotFoundException) == false &&
65 (ex instanceof NoSuchMethodException) == false)
66 {
67 log.debug("Rewrapping reflection exception: ", e);
68 throw new MBeanException(new ServiceNotFoundException(ex.getMessage()), e.getMessage());
69 }
70 else
71 throw e;
72 }
73 }
74
75 public void setAttribute(Attribute attribute)
76 throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException
77 {
78 // Another inconsistency
79 if (attribute == null)
80 throw new RuntimeOperationsException(new IllegalArgumentException("Null attribute"));
81 try
82 {
83 super.setAttribute(attribute);
84 }
85 catch (ReflectionException e)
86 {
87 // Another inconsistency
88 Exception ex = e.getTargetException();
89 if ((ex instanceof ClassNotFoundException) == false &&
90 (ex instanceof NoSuchMethodException) == false)
91 {
92 log.debug("Rewrapping reflection exception: ", e);
93 throw new MBeanException(new ServiceNotFoundException(ex.getMessage()), e.getMessage());
94 }
95 else
96 throw e;
97 }
98 }
99
100 public AttributeList getAttributes(String[] attributes)
101 {
102 if (attributes == null)
103 throw new RuntimeOperationsException(new IllegalArgumentException("Null attributes"));
104 return super.getAttributes(attributes);
105 }
106
107 public AttributeList setAttributes(AttributeList attributes)
108 {
109 if (attributes == null)
110 throw new RuntimeOperationsException(new IllegalArgumentException("Null attributes"));
111 return super.setAttributes(attributes);
112 }
113
114 public Object invoke(String name, Object[] args, String[] signature) throws
115 MBeanException, ReflectionException
116 {
117 Object value;
118
119 if (name == null)
120 throw new RuntimeOperationsException(new IllegalArgumentException("Null operation"));
121 else if( name.equals("getNotificationInfo") )
122 value = mbean.getNotificationInfo();
123 else
124 {
125 try
126 {
127 value = super.invoke(name, args, signature);
128 }
129 catch (RuntimeMBeanException e)
130 {
131 // For some reason (not mentioned in the spec) these have to
132 // be wrapped in MBeanExceptions for RMM
133 throw new MBeanException(e.getTargetException(), e.getMessage());
134 }
135 catch (ReflectionException e)
136 {
137 // Another inconsistency
138 Exception ex = e.getTargetException();
139 if ((ex instanceof ClassNotFoundException) == false &&
140 (ex instanceof NoSuchMethodException) == false)
141 {
142 log.debug("Rewrapping reflection exception: ", e);
143 throw new MBeanException(new ServiceNotFoundException(ex.getMessage()), e.getMessage());
144 }
145 else
146 throw e;
147 }
148 }
149 return value;
150 }
151 }