1 23 24 28 29 44 45 package com.sun.enterprise.admin.util.jmx; 46 47 import java.util.Iterator ; 48 import java.util.Map ; 49 import java.util.HashMap ; 50 import java.util.ArrayList ; 51 import javax.management.AttributeList ; 52 import javax.management.Attribute ; 53 import java.util.StringTokenizer ; 54 55 64 public class AttributeListUtils { 65 66 private AttributeListUtils() { 67 } 69 70 78 public static final boolean containsNamedAttribute(final AttributeList al, 79 final String name) { 80 if (al == null || name == null) 81 throw new IllegalArgumentException ("null arg"); 82 boolean contains = false; 83 final Iterator it = al.iterator(); 84 while (it.hasNext()) { 85 final Attribute at = (Attribute ) it.next(); 86 if (name.equals(at.getName())) { contains = true; 88 break; 89 } 90 } 91 return ( contains ); 92 } 93 94 102 public static final boolean containsNamedAttribute(final AttributeList al, 103 final Attribute a) { 104 if (al == null || a == null) 105 throw new IllegalArgumentException ("null arg"); 106 return ( containsNamedAttribute(al, a.getName()) ); 107 } 108 109 116 public static final Map asNameMap(final AttributeList al) { 117 if (al == null) { 118 throw new IllegalArgumentException ("null arg"); 119 } 120 final Map m = new HashMap (); 121 final Iterator it = al.iterator(); 122 while (it.hasNext()) { 123 final Attribute a = (Attribute ) it.next(); 124 m.put(a.getName(), a); 125 } 126 return ( m ); 127 } 128 129 140 public static final String toJmx12Attribute(final String name) { 141 if (name == null || name.length() == 0) 142 throw new IllegalArgumentException ("invalid arg"); 143 final char rc = '_'; 144 assert (Character.isJavaIdentifierStart(rc) && Character.isJavaIdentifierPart(rc)); 145 final char[] chars = new char[name.length()]; 146 name.getChars(0, name.length(), chars, 0); 147 if (! Character.isJavaIdentifierStart(chars[0])) { 148 chars[0] = rc; 149 } 150 for (int i = 1 ; i < name.length() ; i++) { if (! Character.isJavaIdentifierPart(chars[i])) { 152 chars[i] = rc; 153 } 154 } 155 return ( new String (chars) ); 156 } 157 158 166 public static final String toString(final AttributeList al) { 167 final StringBuffer sb = new StringBuffer (); 168 final char SEP = ','; 169 final char NL = '\n'; 170 if (al != null) { 171 final Iterator it = al.iterator(); 172 while (it.hasNext()) { 173 final Attribute a = (Attribute ) it.next(); 174 sb.append(a.getName()).append(SEP).append(a.getValue().toString()).append(NL); 175 } 176 } 177 return (sb.toString()); 178 } 179 180 public static String dash2CamelCase(String dashed) { 181 187 if (dashed == null) 188 throw new IllegalArgumentException ("Null Arg"); 189 dashed = dashed.toLowerCase(); 190 final ArrayList list = new ArrayList (); 191 final StringTokenizer tz = new StringTokenizer (dashed, "-"); 192 while (tz.hasMoreTokens()) { 193 list.add(tz.nextToken()); 194 } 195 final String [] tmp = new String [list.size()]; 196 final String [] strings = getCamelCaseArray((String [])list.toArray(tmp)); 197 return ( strings2String(strings) ); 198 } 199 200 private static String [] getCamelCaseArray(final String [] from) { 201 final String [] humps = new String [from.length]; 202 for (int i = 0 ; i < from.length ; i++) { 203 final StringBuffer sb = new StringBuffer (from[i]); 204 sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); 205 humps[i] = sb.toString(); 206 } 207 return ( humps ); 208 } 209 private static String strings2String(final String [] a) { 210 final StringBuffer sb = new StringBuffer (); 211 for (int i = 0 ; i < a.length ; i++) { 212 sb.append(a[i]); 213 } 214 return ( sb.toString() ); 215 } 216 } | Popular Tags |