001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.management.jmx.export.runtime;
019:
020: import java.lang.reflect.Method;
021:
022: import javax.management.Descriptor;
023: import javax.management.modelmbean.ModelMBeanInfo;
024:
025: import org.apache.cxf.management.annotation.ManagedAttribute;
026: import org.apache.cxf.management.annotation.ManagedNotification;
027: import org.apache.cxf.management.annotation.ManagedNotifications;
028: import org.apache.cxf.management.annotation.ManagedOperation;
029: import org.apache.cxf.management.annotation.ManagedOperationParameter;
030: import org.apache.cxf.management.annotation.ManagedOperationParameters;
031: import org.apache.cxf.management.annotation.ManagedResource;
032:
033: public class ModelMBeanAssembler {
034: private ModelMBeanInfoSupporter supporter = new ModelMBeanInfoSupporter();
035:
036: public ManagedResource getManagedResource(Class<?> clazz) {
037: return clazz.getAnnotation(ManagedResource.class);
038: }
039:
040: public ManagedAttribute getManagedAttribute(Method method) {
041: return method.getAnnotation(ManagedAttribute.class);
042: }
043:
044: public ManagedOperation getManagedOperation(Method method) {
045: return method.getAnnotation(ManagedOperation.class);
046: }
047:
048: public ManagedOperationParameter[] getManagedOperationParameters(
049: Method method) {
050: ManagedOperationParameters params = method
051: .getAnnotation(ManagedOperationParameters.class);
052: ManagedOperationParameter[] result = null;
053: if (params == null) {
054: result = new ManagedOperationParameter[0];
055: } else {
056: result = params.value();
057: }
058: return result;
059: }
060:
061: public ManagedNotification[] getManagedNotifications(Class<?> clazz) {
062: ManagedNotifications notificationsAnn = clazz
063: .getAnnotation(ManagedNotifications.class);
064: ManagedNotification[] result = null;
065: if (null == notificationsAnn) {
066: return new ManagedNotification[0];
067: }
068: result = notificationsAnn.value();
069: return result;
070: }
071:
072: public String getAttributeName(String methodName) {
073: if (methodName.indexOf("set") == 0) {
074: return methodName.substring(3);
075: }
076: if (methodName.indexOf("get") == 0) {
077: return methodName.substring(3);
078: }
079: if (methodName.indexOf("is") == 0) {
080: return methodName.substring(2);
081: }
082: return null;
083: }
084:
085: public static boolean checkMethod(Method[] methods,
086: String methodName) {
087: boolean result = false;
088: for (int i = 0; i < methods.length; i++) {
089: if (methods[i].getName().compareTo(methodName) == 0) {
090: result = true;
091: break;
092: }
093: }
094: return result;
095: }
096:
097: public static String getAttributeType(Method[] methods,
098: String attributeName) {
099: String result = null;
100: String searchMethod = "get" + attributeName;
101: for (int i = 0; i < methods.length; i++) {
102: if (methods[i].getName().compareTo(searchMethod) == 0) {
103: result = methods[i].getReturnType().getName();
104: break;
105: }
106: }
107: // check it is "is " attribute
108: if (null == result) {
109: searchMethod = "is" + attributeName;
110: for (int i = 0; i < methods.length; i++) {
111: if (methods[i].getName().compareTo(searchMethod) == 0) {
112: result = methods[i].getReturnType().getName();
113: break;
114: }
115: }
116: }
117: return result;
118: }
119:
120: class ManagedAttributeInfo {
121: String fname;
122: String ftype;
123: String description;
124: boolean read;
125: boolean write;
126: boolean is;
127: };
128:
129: //get the attribut information for the method
130: public ManagedAttributeInfo getAttributInfo(Method[] methods,
131: String attributName, String attributType,
132: ManagedAttribute managedAttribute) {
133: ManagedAttributeInfo mai = new ManagedAttributeInfo();
134: mai.fname = attributName;
135: mai.ftype = attributType;
136: mai.description = managedAttribute.description();
137: mai.is = checkMethod(methods, "is" + attributName);
138: mai.write = checkMethod(methods, "set" + attributName);
139:
140: if (mai.is) {
141: mai.read = true;
142: } else {
143: mai.read = checkMethod(methods, "get" + attributName);
144: }
145:
146: return mai;
147:
148: }
149:
150: Method findMethodByName(Method methods[], String methodName) {
151: for (int i = 0; i < methods.length; i++) {
152: if (methods[i].getName().compareTo(methodName) == 0) {
153: return methods[i];
154: } else {
155: continue;
156: }
157:
158: }
159: return null;
160:
161: }
162:
163: void addAttributeOperation(Method method) {
164: Descriptor operationDescriptor = supporter
165: .buildAttributeOperationDescriptor(method.getName());
166:
167: Class<?>[] types = method.getParameterTypes();
168:
169: String[] paramTypes = new String[types.length];
170: String[] paramNames = new String[types.length];
171: String[] paramDescs = new String[types.length];
172:
173: for (int j = 0; j < types.length; j++) {
174: paramTypes[j] = types[j].getName();
175: paramDescs[j] = "";
176: paramNames[j] = types[j].getName();
177: }
178:
179: supporter.addModelMBeanMethod(method.getName(), paramTypes,
180: paramNames, paramDescs, "", method.getReturnType()
181: .getName(), operationDescriptor);
182: }
183:
184: public ModelMBeanInfo getModelMbeanInfo(Class<?> clazz) {
185: supporter.clear();
186: ManagedResource mr = getManagedResource(clazz);
187: if (mr == null) {
188: // the class is not need to expose to jmx
189: return null;
190: }
191: // Clazz get all the method which should be managemed
192: Descriptor mbeanDescriptor = supporter.buildMBeanDescriptor(mr);
193:
194: // add the notification
195: ManagedNotification[] mns = getManagedNotifications(clazz);
196: for (int k = 0; k < mns.length; k++) {
197: supporter.addModelMBeanNotification(mns[k]
198: .notificationTypes(), mns[k].name(), mns[k]
199: .description(), null);
200: }
201:
202: Method[] methods = clazz.getDeclaredMethods();
203:
204: for (int i = 0; i < methods.length; i++) {
205: ManagedAttribute ma = getManagedAttribute(methods[i]);
206: //add Attribute to the ModelMBean
207: if (ma != null) {
208: String attributeName = getAttributeName(methods[i]
209: .getName());
210: if (!supporter.checkAttribute(attributeName)) {
211: String attributeType = getAttributeType(methods,
212: attributeName);
213: ManagedAttributeInfo mai = getAttributInfo(methods,
214: attributeName, attributeType, ma);
215: Descriptor attributeDescriptor = supporter
216: .buildAttributeDescriptor(ma,
217: attributeName, mai.is, mai.read,
218: mai.write);
219:
220: // should setup the description
221: supporter.addModelMBeanAttribute(mai.fname,
222: mai.ftype, mai.read, mai.write, mai.is,
223: mai.description, attributeDescriptor);
224:
225: Method method;
226: // add the attribute methode to operation
227: if (mai.read) {
228: if (mai.is) {
229: method = findMethodByName(methods, "is"
230: + attributeName);
231: } else {
232: method = findMethodByName(methods, "get"
233: + attributeName);
234: }
235: addAttributeOperation(method);
236: }
237: if (mai.write) {
238: method = findMethodByName(methods, "set"
239: + attributeName);
240: addAttributeOperation(method);
241: }
242: }
243:
244: } else {
245: // add Operation to the ModelMBean
246: ManagedOperation mo = getManagedOperation(methods[i]);
247:
248: if (mo != null) {
249: Class<?>[] types = methods[i].getParameterTypes();
250: ManagedOperationParameter[] mop = getManagedOperationParameters(methods[i]);
251: String[] paramTypes = new String[types.length];
252: String[] paramNames = new String[types.length];
253: String[] paramDescs = new String[types.length];
254:
255: for (int j = 0; j < types.length; j++) {
256: paramTypes[j] = types[j].getName();
257: if (j < mop.length) {
258: paramDescs[j] = mop[j].description();
259: paramNames[j] = mop[j].name();
260: } else {
261: paramDescs[j] = "";
262: paramNames[j] = types[j].getName();
263: }
264: }
265: Descriptor operationDescriptor = supporter
266: .buildOperationDescriptor(mo, methods[i]
267: .getName());
268: supporter.addModelMBeanMethod(methods[i].getName(),
269: paramTypes, paramNames, paramDescs, mo
270: .description(), methods[i]
271: .getReturnType().getName(),
272: operationDescriptor);
273: }
274: }
275:
276: }
277: return supporter.buildModelMBeanInfo(mbeanDescriptor);
278: }
279: }
|