KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > interceptor > AttributeDispatcher


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * 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.interceptor;
23
24 import java.lang.reflect.Method JavaDoc;
25
26 import javax.management.Descriptor JavaDoc;
27 import javax.management.InvalidAttributeValueException JavaDoc;
28 import javax.management.MBeanException JavaDoc;
29 import javax.management.ServiceNotFoundException JavaDoc;
30 import javax.management.modelmbean.InvalidTargetObjectTypeException JavaDoc;
31
32 import org.jboss.mx.modelmbean.ModelMBeanConstants;
33 import org.jboss.mx.server.Invocation;
34 import org.jboss.mx.server.MBeanInvoker;
35
36 /** A dispatcher used by the AbstractMBeanInvoker class for the attribute
37  * getter and setter dispatch.
38  *
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 37459 $
41  */

42 public class AttributeDispatcher
43    extends ReflectedDispatcher
44 {
45    private Method JavaDoc getter;
46    private Method JavaDoc setter;
47
48    public AttributeDispatcher(Method JavaDoc getter, Method JavaDoc setter, boolean dynamic)
49    {
50       super(dynamic);
51       setName("Attribute Dispatcher");
52       this.getter = getter;
53       this.setter = setter;
54    }
55    
56    /** Dispatch the attribute set or get. A get is identified by a dispatch
57     * with a null args value.
58     *
59     * @return the result of the attribute accessor invocation
60     * @throws InvocationException
61     */

62    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
63    {
64       Object JavaDoc target = invocation.getTarget();
65       
66       Object JavaDoc value = null;
67       Object JavaDoc[] args = invocation.getArgs();
68       // Getter
69
if( args == null )
70       {
71          Method JavaDoc getMethod = getter;
72          if (dynamic)
73          {
74             Descriptor JavaDoc d = invocation.getDescriptor();
75             if (d != null)
76             {
77                Object JavaDoc descriptorTarget = d.getFieldValue(ModelMBeanConstants.TARGET_OBJECT);
78                if (descriptorTarget != null)
79                {
80                   String JavaDoc targetType = (String JavaDoc) d.getFieldValue(ModelMBeanConstants.TARGET_TYPE);
81                   if (ModelMBeanConstants.OBJECT_REF.equalsIgnoreCase(targetType) == false)
82                      throw new InvalidTargetObjectTypeException JavaDoc("Target type is " + targetType);
83                   target = descriptorTarget;
84                }
85                String JavaDoc getMethodString = (String JavaDoc) d.getFieldValue(ModelMBeanConstants.GET_METHOD);
86                if (getMethodString != null && (getMethod == null || getMethodString.equals(getMethod.getName()) == false))
87                {
88                   MBeanInvoker invoker = invocation.getInvoker();
89                   Object JavaDoc object = invoker.invoke(getMethodString, new Object JavaDoc[0], new String JavaDoc[0]);
90                   checkAssignable(getMethodString, invocation.getAttributeTypeClass(), object);
91                   return object;
92                }
93             }
94          }
95          if (target == null)
96             throw new MBeanException JavaDoc(new ServiceNotFoundException JavaDoc("No Target"));
97          try
98          {
99             value = getMethod.invoke(target, args);
100          }
101          catch (Throwable JavaDoc t)
102          {
103             handleInvocationExceptions(t);
104             return null;
105          }
106       }
107       // Setter
108
else
109       {
110          Method JavaDoc setMethod = setter;
111          if (dynamic)
112          {
113             Descriptor JavaDoc d = invocation.getDescriptor();
114             if (d != null)
115             {
116                Object JavaDoc descriptorTarget = d.getFieldValue(ModelMBeanConstants.TARGET_OBJECT);
117                if (descriptorTarget != null)
118                {
119                   String JavaDoc targetType = (String JavaDoc) d.getFieldValue(ModelMBeanConstants.TARGET_TYPE);
120                   if (ModelMBeanConstants.OBJECT_REF.equalsIgnoreCase(targetType) == false)
121                      throw new InvalidTargetObjectTypeException JavaDoc("Target type is " + targetType);
122                   target = descriptorTarget;
123                }
124                String JavaDoc setMethodString = (String JavaDoc) d.getFieldValue(ModelMBeanConstants.SET_METHOD);
125                if (setMethodString != null && (setMethod == null || setMethodString.equals(setMethod.getName()) == false))
126                {
127                   MBeanInvoker invoker = invocation.getInvoker();
128                   return invoker.invoke(setMethodString, new Object JavaDoc[] { args[0] }, new String JavaDoc[] { invocation.getAttributeType() });
129                }
130             }
131          }
132          if (target == null)
133             throw new MBeanException JavaDoc(new ServiceNotFoundException JavaDoc("No Target"));
134          try
135          {
136             value = setMethod.invoke(target, args);
137          }
138          catch (Throwable JavaDoc t)
139          {
140             handleInvocationExceptions(t);
141             return null;
142          }
143       }
144       return value;
145    }
146    
147    protected void checkAssignable(String JavaDoc context, Class JavaDoc clazz, Object JavaDoc value) throws InvalidAttributeValueException JavaDoc, ClassNotFoundException JavaDoc
148    {
149       if (value != null && clazz.isAssignableFrom(value.getClass()) == false)
150          throw new InvalidAttributeValueException JavaDoc(context + " has class " + value.getClass() + " loaded from " + value.getClass().getClassLoader() +
151             " that is not assignable to attribute class " + clazz + " loaded from " + clazz.getClassLoader());
152    }
153 }
154    
Popular Tags