KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > capability > DispatcherFactory


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.capability;
23
24 import org.jboss.mx.metadata.AttributeOperationResolver;
25 import org.jboss.mx.metadata.MethodMapper;
26
27 import org.jboss.mx.server.ServerConstants;
28 import org.jboss.mx.util.PropertyAccess;
29
30 import javax.management.DynamicMBean JavaDoc;
31 import javax.management.IntrospectionException JavaDoc;
32 import javax.management.MBeanAttributeInfo JavaDoc;
33 import javax.management.MBeanInfo JavaDoc;
34 import javax.management.MBeanOperationInfo JavaDoc;
35 import javax.management.Descriptor JavaDoc;
36 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38
39 /**
40  * Creates and binds a dispatcher
41  *
42  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
43  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>
44  */

45 public class DispatcherFactory
46       implements ServerConstants
47 {
48
49    /**
50     * Creates a Dispatcher for an arbitrary resource. Useful for when you don't care
51     * about the AttributeOperationResolver.
52     */

53    public static DynamicMBean JavaDoc create(MBeanInfo JavaDoc info, Object JavaDoc resource) throws IntrospectionException JavaDoc
54    {
55       return create(info, resource, new AttributeOperationResolver(info));
56    }
57
58    /**
59     * Creates a dispatcher for an arbitrary resource using the named AttributeOperationResolver.
60     */

61    public static DynamicMBean JavaDoc create(MBeanInfo JavaDoc info, Object JavaDoc resource, AttributeOperationResolver resolver) throws IntrospectionException JavaDoc
62    {
63       if (null == info)
64       {
65          throw new IllegalArgumentException JavaDoc("info cannot be null");
66       }
67
68       if (null == resolver)
69       {
70          throw new IllegalArgumentException JavaDoc("resolver cannot be null");
71       }
72
73       if (null == resource)
74       {
75          throw new IllegalArgumentException JavaDoc("resource cannot be null");
76       }
77
78       MethodMapper mmap = new MethodMapper(resource.getClass());
79       ReflectedMBeanDispatcher dispatcher = new ReflectedMBeanDispatcher(info, resolver, resource);
80
81       String JavaDoc flag = PropertyAccess.getProperty(OPTIMIZE_REFLECTED_DISPATCHER, "false");
82       if (flag.equalsIgnoreCase("true"))
83       {
84          // FIXME: subclassing for now so I can rely on the reflection based implementation for the parts
85
// that aren't implemented yet
86
dispatcher = OptimizedMBeanDispatcher.create(info, resource /*, parent classloader */);
87       }
88
89       MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
90       for (int i = 0; i < attributes.length; i++)
91       {
92          MBeanAttributeInfo JavaDoc attribute = attributes[i];
93          Method JavaDoc getter = null;
94          Method JavaDoc setter = null;
95
96          if (attribute.isReadable())
97          {
98             if (attribute instanceof ModelMBeanAttributeInfo JavaDoc)
99             {
100                ModelMBeanAttributeInfo JavaDoc mmbAttribute = (ModelMBeanAttributeInfo JavaDoc) attribute;
101                Descriptor JavaDoc desc = mmbAttribute.getDescriptor();
102                if (desc != null && desc.getFieldValue("getMethod") != null)
103                {
104                   getter = mmap.lookupGetter(mmbAttribute);
105                   if (getter == null)
106                   {
107                      throw new IntrospectionException JavaDoc("no getter method found for attribute: " + attribute.getName());
108                   }
109                }
110             }
111             else
112             {
113                getter = mmap.lookupGetter(attribute);
114                if (getter == null)
115                {
116                   throw new IntrospectionException JavaDoc("no getter method found for attribute: " + attribute.getName());
117                }
118             }
119          }
120
121          if (attribute.isWritable())
122          {
123             if (attribute instanceof ModelMBeanAttributeInfo JavaDoc)
124             {
125                ModelMBeanAttributeInfo JavaDoc mmbAttribute = (ModelMBeanAttributeInfo JavaDoc) attribute;
126                Descriptor JavaDoc desc = mmbAttribute.getDescriptor();
127                if (desc != null && desc.getFieldValue("setMethod") != null)
128                {
129                   setter = mmap.lookupSetter(mmbAttribute);
130                   if (setter == null)
131                   {
132                      throw new IntrospectionException JavaDoc("no setter method found for attribute: " + attribute.getName());
133                   }
134                }
135             }
136             else
137             {
138                setter = mmap.lookupSetter(attribute);
139                if (setter == null)
140                {
141                   throw new IntrospectionException JavaDoc("no setter method found for attribute: " + attribute.getName());
142                }
143             }
144          }
145
146          dispatcher.bindAttributeAt(i, getter, setter);
147       }
148
149       MBeanOperationInfo JavaDoc[] operations = info.getOperations();
150       for (int i = 0; i < operations.length; i++)
151       {
152          MBeanOperationInfo JavaDoc operation = operations[i];
153          Method JavaDoc method = mmap.lookupOperation(operation);
154          if (method == null)
155          {
156             throw new IntrospectionException JavaDoc("no method found for operation: " + operation.getName()); // FIXME better error!
157
}
158
159          dispatcher.bindOperationAt(i, method);
160       }
161
162       return dispatcher;
163    }
164 }
165
Popular Tags