001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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    
018    package org.apache.geronimo.system.jmx;
019    
020    import java.util.Iterator;
021    import java.util.List;
022    import java.util.Set;
023    import javax.management.MBeanAttributeInfo;
024    import javax.management.MBeanConstructorInfo;
025    import javax.management.MBeanInfo;
026    import javax.management.MBeanNotificationInfo;
027    import javax.management.MBeanOperationInfo;
028    import javax.management.MBeanParameterInfo;
029    
030    import org.apache.geronimo.gbean.GAttributeInfo;
031    import org.apache.geronimo.gbean.GBeanInfo;
032    import org.apache.geronimo.gbean.GOperationInfo;
033    import org.apache.geronimo.kernel.management.NotificationType;
034    
035    
036    /**
037     * Helper class for JMX Operations
038     *
039     * @version $Rev: 430508 $ $Date: 2006-08-10 12:56:47 -0700 (Thu, 10 Aug 2006) $
040     */
041    public final class JMXUtil {
042        private JMXUtil() {
043        }
044    
045        public static MBeanInfo toMBeanInfo(GBeanInfo gBeanInfo) {
046            String className = gBeanInfo.getClassName();
047            String description = "No description available";
048    
049            // attributes
050            Set gbeanAttributes = gBeanInfo.getAttributes();
051            MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[gbeanAttributes.size()];
052            int a = 0;
053            for (Iterator iterator = gbeanAttributes.iterator(); iterator.hasNext();) {
054                GAttributeInfo gAttributeInfo = (GAttributeInfo) iterator.next();
055                attributes[a] = new MBeanAttributeInfo(gAttributeInfo.getName(), gAttributeInfo.getType(), "no description available", gAttributeInfo.isReadable(), gAttributeInfo.isWritable(), isIs(gAttributeInfo));
056                a++;
057            }
058    
059            //we don't expose managed constructors
060            MBeanConstructorInfo[] constructors = new MBeanConstructorInfo[0];
061    
062            // operations
063            Set gbeanOperations = gBeanInfo.getOperations();
064            MBeanOperationInfo[] operations = new MBeanOperationInfo[gbeanOperations.size()];
065            int o = 0;
066            for (Iterator iterator = gbeanOperations.iterator(); iterator.hasNext();) {
067                GOperationInfo gOperationInfo = (GOperationInfo) iterator.next();
068                //list of class names
069                List gparameters = gOperationInfo.getParameterList();
070                MBeanParameterInfo[] parameters = new MBeanParameterInfo[gparameters.size()];
071                int p = 0;
072                for (Iterator piterator = gparameters.iterator(); piterator.hasNext();) {
073                    String type = (String) piterator.next();
074                    parameters[p] = new MBeanParameterInfo("parameter" + p, type, "no description available");
075                    p++;
076                }
077                operations[o] = new MBeanOperationInfo(gOperationInfo.getName(), "no description available", parameters, "java.lang.Object", MBeanOperationInfo.UNKNOWN);
078                o++;
079            }
080    
081            MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[1];
082            notifications[0] = new MBeanNotificationInfo(NotificationType.TYPES, "javax.management.Notification", "J2EE Notifications");
083    
084            MBeanInfo mbeanInfo = new MBeanInfo(className, description, attributes, constructors, operations, notifications);
085            return mbeanInfo;
086        }
087    
088        private static boolean isIs(GAttributeInfo gAttributeInfo) {
089            String getterName = gAttributeInfo.getGetterName();
090            if (getterName == null) {
091                return false;
092            }
093            return getterName.startsWith("is");
094        }
095    }