1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.archive.hcc.util.jmx;
25
26 import java.lang.reflect.InvocationTargetException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import javax.management.MBeanException;
31 import javax.management.ReflectionException;
32 import javax.management.openmbean.OpenMBeanOperationInfo;
33
34 public class OpenMBeanInvocationManager {
35 private List<MBeanOperation> operations = new ArrayList<MBeanOperation>();
36
37 private OpenMBeanOperationInfo[] info = new OpenMBeanOperationInfo[0];
38
39 public Object invoke(String actionName, Object[] params, String[] signature)
40 throws MBeanException,
41 ReflectionException {
42
43 MBeanOperation operation = getOperation(actionName, params, signature);
44
45 if (operation != null) {
46 MBeanInvocation invocation = operation.createInvocation(
47 params,
48 signature);
49 return invocation.invoke();
50 }
51
52 throw new ReflectionException(new InvocationTargetException(
53 null,
54 "no operation found with name and " + "signature: actionName="
55 + actionName + "; params=" + params + "; signature"));
56
57 }
58
59 protected MBeanOperation getOperation(
60 String actionName,
61 Object[] params,
62 String[] signature) throws ReflectionException {
63
64 for (MBeanOperation operation : operations) {
65 if (operation.matches(actionName, signature)) {
66 return operation;
67 }
68
69 }
70
71 throw new ReflectionException(new InvocationTargetException(
72 null,
73 "The method with the specified signature was not found:"
74 + actionName + "; signature=" + signature));
75
76 }
77
78 public void addMBeanOperation(MBeanOperation operation) {
79 this.operations.add(operation);
80 setInfo();
81 }
82
83 public OpenMBeanOperationInfo[] getInfo() {
84 return this.info;
85 }
86
87 private void setInfo() {
88 OpenMBeanOperationInfo[] newInfo = new OpenMBeanOperationInfo[this.operations
89 .size()];
90 for (int i = 0; i < newInfo.length; i++) {
91 newInfo[i] = this.operations.get(i).getInfo();
92 }
93
94 this.info = newInfo;
95 }
96 }