KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > management > OperationInfoHelper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.management;
18
19 import javax.management.MBeanOperationInfo JavaDoc;
20 import javax.management.MBeanParameterInfo JavaDoc;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * A Helper class to build an MBeanOperationInfo
28  *
29  * @version $Revision: 426415 $
30  */

31 public class OperationInfoHelper {
32     private List JavaDoc list = new ArrayList JavaDoc();
33
34     /**
35      * Add an operation
36      *
37      * @param theObject
38      * @param name
39      * @param description
40      * @return array of MBeanParameterInfos
41      */

42     public ParameterHelper addOperation(Object JavaDoc theObject, String JavaDoc name, String JavaDoc description) {
43         return addOperation(theObject, name, 0, description);
44     }
45
46     /**
47      * Add an operation
48      *
49      * @param theObject
50      * @param name
51      * @param numberParams
52      * @param description
53      * @return array of MBeanParameterInfos
54      */

55     public ParameterHelper addOperation(Object JavaDoc theObject, String JavaDoc name, int numberParams, String JavaDoc description) {
56         Method JavaDoc method = getMethod(theObject.getClass(), name, numberParams);
57         MBeanOperationInfo JavaDoc opInfo = new MBeanOperationInfo JavaDoc(description, method);
58         list.add(opInfo);
59         MBeanParameterInfo JavaDoc[] result = opInfo.getSignature();
60         return new ParameterHelper(result);
61     }
62
63     /**
64      * Get array of MBeanOperationInfos registered
65      *
66      * @return MBeanOperationInfos
67      */

68     public MBeanOperationInfo JavaDoc[] getOperationInfos() {
69         MBeanOperationInfo JavaDoc[] result = new MBeanOperationInfo JavaDoc[list.size()];
70         list.toArray(result);
71         return result;
72     }
73
74     /**
75      * clear the internal list
76      */

77     public void clear() {
78         list.clear();
79     }
80
81     /**
82      * Join two MBeanOperationInfo[] arrays
83      *
84      * @param ops1
85      * @param ops2
86      * @return new MBeanOperationInfo array containing contents of ops1 and ops2
87      */

88     public static MBeanOperationInfo JavaDoc[] join(MBeanOperationInfo JavaDoc[] ops1, MBeanOperationInfo JavaDoc[] ops2) {
89         MBeanOperationInfo JavaDoc[] result = null;
90         int length = 0;
91         int startPos = 0;
92         if (ops1 != null) {
93             length = ops1.length;
94         }
95         if (ops2 != null) {
96             length += ops2.length;
97         }
98         result = new MBeanOperationInfo JavaDoc[length];
99         if (ops1 != null) {
100             System.arraycopy(ops1, 0, result, startPos, ops1.length);
101             startPos = ops1.length;
102         }
103         if (ops2 != null) {
104             System.arraycopy(ops2, 0, result, startPos, ops2.length);
105         }
106         return result;
107     }
108
109     private Method JavaDoc getMethod(Class JavaDoc theClass, String JavaDoc name, int numParams) {
110         Method JavaDoc result = null;
111         Method JavaDoc[] methods = theClass.getMethods();
112         if (methods != null) {
113             for (int i = 0;i < methods.length;i++) {
114                 if (methods[i].getName().equals(name) && methods[i].getParameterTypes().length == numParams) {
115                     result = methods[i];
116                     break;
117                 }
118             }
119             if (result == null){
120                 //do a less exact search
121
for (int i = 0;i < methods.length;i++) {
122                     if (methods[i].getName().equals(name)) {
123                         result = methods[i];
124                         break;
125                     }
126                 }
127             }
128         }
129         return result;
130     }
131 }
Popular Tags