001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.jbi.management;
018
019 import java.lang.reflect.Method;
020 import java.util.ArrayList;
021 import java.util.List;
022
023 import javax.management.MBeanOperationInfo;
024 import javax.management.MBeanParameterInfo;
025
026 /**
027 * A Helper class to build an MBeanOperationInfo
028 *
029 * @version $Revision: 564607 $
030 */
031 public class OperationInfoHelper {
032 private List<MBeanOperationInfo> list = new ArrayList<MBeanOperationInfo>();
033
034 /**
035 * Add an operation
036 *
037 * @param theObject
038 * @param name
039 * @param description
040 * @return array of MBeanParameterInfos
041 */
042 public ParameterHelper addOperation(Object theObject, String name, String description) {
043 return addOperation(theObject, name, 0, description);
044 }
045
046 /**
047 * Add an operation
048 *
049 * @param theObject
050 * @param name
051 * @param numberParams
052 * @param description
053 * @return array of MBeanParameterInfos
054 */
055 public ParameterHelper addOperation(Object theObject, String name, int numberParams, String description) {
056 Method method = getMethod(theObject.getClass(), name, numberParams);
057 MBeanOperationInfo opInfo = new MBeanOperationInfo(description, method);
058 list.add(opInfo);
059 MBeanParameterInfo[] result = opInfo.getSignature();
060 return new ParameterHelper(result);
061 }
062
063 /**
064 * Get array of MBeanOperationInfos registered
065 *
066 * @return MBeanOperationInfos
067 */
068 public MBeanOperationInfo[] getOperationInfos() {
069 MBeanOperationInfo[] result = new MBeanOperationInfo[list.size()];
070 list.toArray(result);
071 return result;
072 }
073
074 /**
075 * clear the internal list
076 */
077 public void clear() {
078 list.clear();
079 }
080
081 /**
082 * Join two MBeanOperationInfo[] arrays
083 *
084 * @param ops1
085 * @param ops2
086 * @return new MBeanOperationInfo array containing contents of ops1 and ops2
087 */
088 public static MBeanOperationInfo[] join(MBeanOperationInfo[] ops1, MBeanOperationInfo[] ops2) {
089 MBeanOperationInfo[] result = null;
090 int length = 0;
091 int startPos = 0;
092 if (ops1 != null) {
093 length = ops1.length;
094 }
095 if (ops2 != null) {
096 length += ops2.length;
097 }
098 result = new MBeanOperationInfo[length];
099 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 getMethod(Class<? extends Object> theClass, String name, int numParams) {
110 Method result = null;
111 Method[] 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 }