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,
043: String description) {
044: return addOperation(theObject, name, 0, description);
045: }
046:
047: /**
048: * Add an operation
049: *
050: * @param theObject
051: * @param name
052: * @param numberParams
053: * @param description
054: * @return array of MBeanParameterInfos
055: */
056: public ParameterHelper addOperation(Object theObject, String name,
057: int numberParams, String description) {
058: Method method = getMethod(theObject.getClass(), name,
059: numberParams);
060: MBeanOperationInfo opInfo = new MBeanOperationInfo(description,
061: method);
062: list.add(opInfo);
063: MBeanParameterInfo[] result = opInfo.getSignature();
064: return new ParameterHelper(result);
065: }
066:
067: /**
068: * Get array of MBeanOperationInfos registered
069: *
070: * @return MBeanOperationInfos
071: */
072: public MBeanOperationInfo[] getOperationInfos() {
073: MBeanOperationInfo[] result = new MBeanOperationInfo[list
074: .size()];
075: list.toArray(result);
076: return result;
077: }
078:
079: /**
080: * clear the internal list
081: */
082: public void clear() {
083: list.clear();
084: }
085:
086: /**
087: * Join two MBeanOperationInfo[] arrays
088: *
089: * @param ops1
090: * @param ops2
091: * @return new MBeanOperationInfo array containing contents of ops1 and ops2
092: */
093: public static MBeanOperationInfo[] join(MBeanOperationInfo[] ops1,
094: MBeanOperationInfo[] ops2) {
095: MBeanOperationInfo[] result = null;
096: int length = 0;
097: int startPos = 0;
098: if (ops1 != null) {
099: length = ops1.length;
100: }
101: if (ops2 != null) {
102: length += ops2.length;
103: }
104: result = new MBeanOperationInfo[length];
105: if (ops1 != null) {
106: System.arraycopy(ops1, 0, result, startPos, ops1.length);
107: startPos = ops1.length;
108: }
109: if (ops2 != null) {
110: System.arraycopy(ops2, 0, result, startPos, ops2.length);
111: }
112: return result;
113: }
114:
115: private Method getMethod(Class<? extends Object> theClass,
116: String name, int numParams) {
117: Method result = null;
118: Method[] methods = theClass.getMethods();
119: if (methods != null) {
120: for (int i = 0; i < methods.length; i++) {
121: if (methods[i].getName().equals(name)
122: && methods[i].getParameterTypes().length == numParams) {
123: result = methods[i];
124: break;
125: }
126: }
127: if (result == null) {
128: // do a less exact search
129: for (int i = 0; i < methods.length; i++) {
130: if (methods[i].getName().equals(name)) {
131: result = methods[i];
132: break;
133: }
134: }
135: }
136: }
137: return result;
138: }
139: }
|