| 1 | // jademx - JADE management using JMX |
| 2 | // Copyright 2005 Caboodle Networks, Inc. |
| 3 | // |
| 4 | // This library is free software; you can redistribute it and/or |
| 5 | // modify it under the terms of the GNU Lesser General Public |
| 6 | // License as published by the Free Software Foundation; either |
| 7 | // version 2.1 of the License, or (at your option) any later version. |
| 8 | // |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | // Lesser General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU Lesser General Public |
| 15 | // License along with this library; if not, write to the Free Software |
| 16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 17 | |
| 18 | package jade.jademx.util; |
| 19 | |
| 20 | //import jade.util.Logger; |
| 21 | |
| 22 | import java.lang.reflect.Array; |
| 23 | |
| 24 | import javax.management.MBeanAttributeInfo; |
| 25 | import javax.management.MBeanConstructorInfo; |
| 26 | import javax.management.MBeanInfo; |
| 27 | import javax.management.MBeanNotificationInfo; |
| 28 | import javax.management.MBeanOperationInfo; |
| 29 | import javax.management.MBeanParameterInfo; |
| 30 | |
| 31 | /** |
| 32 | * MBean utility class. |
| 33 | * @author David Bernstein, <a href="http://www.caboodlenetworks.com" |
| 34 | * >Caboodle Networks, Inc.</a> |
| 35 | */ |
| 36 | public class MBeanUtil { |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * private to prevent instantiation |
| 41 | */ |
| 42 | private MBeanUtil() { |
| 43 | } |
| 44 | |
| 45 | ///** my logger */ |
| 46 | //private final static Logger logger = |
| 47 | // Logger.getMyLogger(MBeanUtil.class.getName()); |
| 48 | |
| 49 | /** |
| 50 | * do two method signatures (by class names) match |
| 51 | * @param signature1 first signature to compare |
| 52 | * @param signature2 second signature to compare |
| 53 | * @return whether signatures are equal |
| 54 | */ |
| 55 | public static boolean signaturesEqual( String signature1[], String signature2[]) { |
| 56 | boolean equal = false; |
| 57 | if ( ( null == signature1 ) && ( null == signature2 ) ) { |
| 58 | equal = true; |
| 59 | } |
| 60 | else if ( ( null != signature1 ) && ( null == signature2 ) ) { |
| 61 | // intentionally empty |
| 62 | } |
| 63 | else if ( ( null == signature1 ) && ( null != signature2 ) ) { |
| 64 | // intentionally empty |
| 65 | } |
| 66 | else { |
| 67 | int pCount1 = signature1.length; |
| 68 | int pCount2 = signature2.length; |
| 69 | if ( pCount1 == pCount2 ) { |
| 70 | equal = true; |
| 71 | for ( int i = 0; i < pCount1; i++ ) { |
| 72 | if ( ! signature1[i].equals( signature2[i] ) ) { |
| 73 | equal = false; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | return equal; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * merge two 1-dimensional arrays consisting of elements of given class. |
| 84 | * will return other array if one array is empty. |
| 85 | * @param c element class |
| 86 | * @param a1 first array to merge |
| 87 | * @param a2 second array to merge |
| 88 | * @return merged array |
| 89 | */ |
| 90 | private static Object mergeArrays( Class c, Object a1, Object a2) { |
| 91 | int length1 = ( ( null == a1 ) ? 0 : Array.getLength( a1 ) ); |
| 92 | int length2 = ( ( null == a2 ) ? 0 : Array.getLength( a2 ) ); |
| 93 | Object mergedArray = null; |
| 94 | if ( 0 == length1 ) { |
| 95 | mergedArray = a2; |
| 96 | } |
| 97 | else if ( 0 == length2 ) { |
| 98 | mergedArray = a1; |
| 99 | } |
| 100 | else { |
| 101 | mergedArray = Array.newInstance( c, length1 + length2 ); |
| 102 | for ( int i = 0; i < length1; i++ ) { |
| 103 | Array.set( mergedArray, i, Array.get( a1, i ) ); |
| 104 | } |
| 105 | for ( int i = 0; i < length2; i++ ) { |
| 106 | Array.set( mergedArray, length1+i, Array.get( a2, i ) ); |
| 107 | } |
| 108 | } |
| 109 | return mergedArray; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * merge two MBeanInfo objects. |
| 114 | * <em>Does NOT check for duplication of attributes, constructors, |
| 115 | * operations, or notifications.</em> |
| 116 | * @param className MBean class name |
| 117 | * @param description MBean description |
| 118 | * @param mbi1 first MBeanInfo |
| 119 | * @param mbi2 second MBeanInfo |
| 120 | * @return merged MBeanInfo |
| 121 | */ |
| 122 | public static MBeanInfo mergeMBeanInfo( String className, String description, |
| 123 | MBeanInfo mbi1, MBeanInfo mbi2 ) { |
| 124 | MBeanInfo mbi; |
| 125 | if ( ( null == mbi1 ) && ( null == mbi2 ) ) { |
| 126 | mbi = new MBeanInfo( className, description, |
| 127 | null, null, null, null ); |
| 128 | } |
| 129 | else if ( ( null == mbi1 ) && ( null != mbi2 ) ) { |
| 130 | mbi = new MBeanInfo( className, description, |
| 131 | mbi2.getAttributes(), |
| 132 | mbi2.getConstructors(), |
| 133 | mbi2.getOperations(), |
| 134 | mbi2.getNotifications() ); |
| 135 | } |
| 136 | else if ( ( null == mbi2 ) && ( null != mbi1 ) ) { |
| 137 | mbi = new MBeanInfo( className, description, |
| 138 | mbi1.getAttributes(), |
| 139 | mbi1.getConstructors(), |
| 140 | mbi1.getOperations(), |
| 141 | mbi1.getNotifications() ); |
| 142 | } |
| 143 | else { |
| 144 | MBeanAttributeInfo aI[] = |
| 145 | (MBeanAttributeInfo[])mergeArrays( |
| 146 | MBeanAttributeInfo.class, |
| 147 | mbi1.getAttributes(), mbi2.getAttributes() ); |
| 148 | MBeanConstructorInfo cI[] = |
| 149 | (MBeanConstructorInfo[])mergeArrays( |
| 150 | MBeanConstructorInfo.class, |
| 151 | mbi1.getConstructors(), mbi2.getConstructors() ); |
| 152 | MBeanOperationInfo oI[] = |
| 153 | (MBeanOperationInfo[])mergeArrays( |
| 154 | MBeanOperationInfo.class, |
| 155 | mbi1.getOperations(), mbi2.getOperations() ); |
| 156 | MBeanNotificationInfo nI[] = |
| 157 | (MBeanNotificationInfo[])mergeArrays( |
| 158 | MBeanNotificationInfo.class, |
| 159 | mbi1.getNotifications(), mbi2.getNotifications() ); |
| 160 | mbi = new MBeanInfo( className, description, aI, cI, oI, nI ); |
| 161 | } |
| 162 | return mbi; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * see if given attribute in given MBeanInfo |
| 167 | * @param mbi MBeanInfo to search for given attribute |
| 168 | * @param attrName attribute name to look for |
| 169 | */ |
| 170 | public static boolean mBeanHasAttr( MBeanInfo mbi, String attrName ) { |
| 171 | boolean attrIn = false; |
| 172 | if ( null != mbi ) { |
| 173 | MBeanAttributeInfo aI[] = mbi.getAttributes(); |
| 174 | int aCount = aI.length; |
| 175 | for ( int i = 0; i < aCount; i++ ) { |
| 176 | if ( aI[i].getName().equals( attrName) ) { |
| 177 | attrIn = true; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | return attrIn; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * see if given operation in given MBeanInfo |
| 187 | * @param mbi MBeanInfo to search for given operation |
| 188 | * @param operName attribute name to look for |
| 189 | * @param signature operation signature |
| 190 | */ |
| 191 | public static boolean mBeanHasOper( |
| 192 | MBeanInfo mbi, String operName, String signature[] ) { |
| 193 | boolean operIn = false; |
| 194 | if ( null != mbi ) { |
| 195 | MBeanOperationInfo oI[] = mbi.getOperations(); |
| 196 | int oCount = oI.length; |
| 197 | for ( int i = 0; i < oCount; i++ ) { |
| 198 | MBeanOperationInfo opInfo = oI[i]; |
| 199 | String opName = opInfo.getName(); |
| 200 | MBeanParameterInfo opSig[] = opInfo.getSignature(); |
| 201 | if ( opName.equals( operName ) && |
| 202 | ( signature.length == opSig.length ) ) { |
| 203 | int pCount = signature.length; |
| 204 | operIn = true; |
| 205 | for ( int j = 0; j < pCount; j++ ) { |
| 206 | if ( ! signature[j].equals(opSig[j].getType()) ) { |
| 207 | operIn = false; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | return operIn; |
| 215 | } |
| 216 | |
| 217 | } |