KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > server > CachingReflectionMBeanInvoker


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.server;
10
11 import java.lang.reflect.Method JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14 import javax.management.MBeanAttributeInfo JavaDoc;
15 import javax.management.MBeanOperationInfo JavaDoc;
16 import javax.management.ReflectionException JavaDoc;
17
18 import mx4j.util.MethodTernaryTree;
19
20 /**
21  * Caching MBeanInvoker that uses reflection to invoke on MBean instances.
22  * Attributes and operations lookup is cached to speedup invocations.
23  *
24  * @version $Revision: 1.3 $
25  */

26 public class CachingReflectionMBeanInvoker extends ReflectionMBeanInvoker
27 {
28    private final Map JavaDoc attributes = new HashMap JavaDoc();
29    private final Map JavaDoc attributeNames = new HashMap JavaDoc();
30    private final MethodTernaryTree operations = new MethodTernaryTree();
31    private final MethodTernaryTree methods = new MethodTernaryTree();
32
33    protected MBeanOperationInfo JavaDoc getStandardOperationInfo(MBeanMetaData metadata, String JavaDoc method, String JavaDoc[] signature)
34    {
35       MBeanOperationInfo JavaDoc oper = null;
36       synchronized (operations)
37       {
38          oper = (MBeanOperationInfo JavaDoc)operations.get(method, signature);
39       }
40       if (oper != null) return oper;
41
42       // The MBeanOperationInfo is not in the cache, look it up
43
MBeanOperationInfo JavaDoc info = super.getStandardOperationInfo(metadata, method, signature);
44       if (info != null)
45       {
46          synchronized (operations)
47          {
48             operations.put(method, signature, oper);
49          }
50       }
51       return info;
52    }
53
54    protected MBeanAttributeInfo JavaDoc getStandardAttributeInfo(MBeanMetaData metadata, String JavaDoc attribute, boolean forWrite)
55    {
56       MBeanAttributeInfo JavaDoc attr = null;
57       synchronized (attributes)
58       {
59          attr = (MBeanAttributeInfo JavaDoc)attributes.get(attribute);
60       }
61
62       if (attr == null)
63       {
64          attr = super.getStandardAttributeInfo(metadata, attribute, forWrite);
65          if (attr == null) return null;
66
67          synchronized (attributes)
68          {
69             attributes.put(attribute, attr);
70          }
71       }
72
73       if (forWrite && attr.isWritable()) return attr;
74       if (!forWrite && attr.isReadable()) return attr;
75
76       return null;
77    }
78
79    protected String JavaDoc getMethodForAttribute(MBeanAttributeInfo JavaDoc attribute, boolean getter)
80    {
81       AttributeName attributeName = null;
82       String JavaDoc name = attribute.getName();
83       synchronized (attributeNames)
84       {
85          attributeName = (AttributeName)attributeNames.get(name);
86       }
87       if (attributeName == null)
88       {
89          attributeName = new AttributeName(super.getMethodForAttribute(attribute, true), super.getMethodForAttribute(attribute, false));
90          synchronized (attributeNames)
91          {
92             attributeNames.put(name, attributeName);
93          }
94       }
95
96       if (getter) return attributeName.getter;
97       return attributeName.setter;
98    }
99
100    protected Method JavaDoc getStandardManagementMethod(MBeanMetaData metadata, String JavaDoc name, String JavaDoc[] signature) throws ReflectionException JavaDoc
101    {
102       Method JavaDoc method = null;
103       synchronized (methods)
104       {
105          method = (Method JavaDoc)methods.get(name, signature);
106       }
107       if (method != null) return method;
108
109       // Method is not in cache, look it up
110
method = super.getStandardManagementMethod(metadata, name, signature);
111       synchronized (methods)
112       {
113          methods.put(name, signature, method);
114       }
115       return method;
116    }
117
118    private static class AttributeName
119    {
120       private final String JavaDoc getter;
121       private final String JavaDoc setter;
122
123       public AttributeName(String JavaDoc getter, String JavaDoc setter)
124       {
125          this.getter = getter;
126          this.setter = setter;
127       }
128    }
129 }
130
Popular Tags