1 23 package com.sun.enterprise.management; 24 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.Arrays ; 30 31 import javax.management.MBeanServerConnection ; 32 import javax.management.MBeanInfo ; 33 import javax.management.MBeanAttributeInfo ; 34 import javax.management.MBeanOperationInfo ; 35 import javax.management.ObjectName ; 36 37 import com.sun.appserv.management.DomainRoot; 38 39 import com.sun.appserv.management.base.Util; 40 import com.sun.appserv.management.base.AMX; 41 import com.sun.appserv.management.base.Extra; 42 import com.sun.appserv.management.base.QueryMgr; 43 import com.sun.appserv.management.base.XTypesMapper; 44 45 import com.sun.appserv.management.j2ee.J2EETypesMapper; 46 47 import com.sun.appserv.management.util.misc.GSetUtil; 48 import com.sun.appserv.management.util.misc.GSetUtil; 49 import com.sun.appserv.management.util.misc.StringUtil; 50 51 import com.sun.enterprise.management.support.CoverageInfo; 52 import com.sun.enterprise.management.support.CoverageInfoDummy; 53 import com.sun.enterprise.management.support.AMXDebugStuff; 54 55 56 60 public final class CoverageInfoAnalyzer 61 { 62 private final DomainRoot mDomainRoot; 63 private final QueryMgr mQueryMgr; 64 private final TestUtil mTestUtil; 65 66 private final String NEWLINE; 67 68 public 69 CoverageInfoAnalyzer( final DomainRoot domainRoot ) 70 { 71 mDomainRoot = domainRoot; 72 mQueryMgr = domainRoot.getQueryMgr(); 73 mTestUtil = new TestUtil( domainRoot ); 74 75 final AMXDebugStuff debugRoot = mTestUtil.asAMXDebugStuff( mDomainRoot ); 76 if ( debugRoot == null ) 77 { 78 throw new RuntimeException ( "AMX-DEBUG/CoverageInfo is not enabled" ); 79 } 80 81 final CoverageInfo coverageInfo = debugRoot.getCoverageInfo(); 82 83 if ( coverageInfo instanceof CoverageInfoDummy ) 84 { 85 throw new IllegalArgumentException ( "Coverage disabled--add system property " + 86 "'-DAMX-DEBUG=true', then restart server" ); 87 } 88 89 NEWLINE = System.getProperty( "line.separator" ); 90 } 91 92 private static final Set <String > IGNORE_METHODS = 93 GSetUtil.newUnmodifiableStringSet( 94 "addNotificationListener(javax.management.NotificationListener", 95 "removeNotificationListener(javax.management.NotificationListener", 96 "getAMXDebug", 97 "setAMXDebug", 98 "enableAMXDebug", 99 "getImplString", 100 "enableCoverageInfo", 101 "clearCoverageInfo" 102 ); 103 104 private static final Set <String > IGNORE_UNKNOWN = 105 GSetUtil.newUnmodifiableStringSet( 106 "ContaineeJ2EETypes", 107 "eventProvider" 108 ); 109 116 private void 117 handleSpecialCases( final CoverageInfo coverageInfo ) 118 { 119 final Set <String > notInvoked = coverageInfo.getOperationsNotInvoked(); 120 121 for( final String op : notInvoked ) 122 { 123 for( final String prefix : IGNORE_METHODS ) 124 { 125 if ( op.startsWith( prefix ) ) 126 { 127 coverageInfo.markAsInvoked( op ); 128 } 129 } 130 } 131 132 final Set <String > unknown =coverageInfo.getUnknownAttributes().keySet(); 134 for( final String s : unknown ) 135 { 136 if ( s.startsWith( "bogus" ) || 137 IGNORE_UNKNOWN.contains( s ) ) 138 { 139 coverageInfo.ignoreUnknownAttribute( s ); 140 } 141 } 142 } 143 144 145 public Map <String ,CoverageInfo> 146 getCoverage(final Set <AMX> candidates ) 147 { 148 final Map <String ,CoverageInfo> coverageMap = new HashMap <String ,CoverageInfo>(); 149 150 for( final AMX amx: candidates ) 151 { 152 final AMXDebugStuff debug = mTestUtil.asAMXDebugStuff( amx ); 153 final CoverageInfo coverageInfo = debug.getCoverageInfo(); 154 assert( coverageInfo != null ); 155 handleSpecialCases( coverageInfo ); 156 157 final String j2eeType = amx.getJ2EEType(); 158 final CoverageInfo existing = coverageMap.get( j2eeType ); 159 if ( existing != null ) 160 { 161 existing.merge( coverageInfo ); 162 } 163 else 164 { 165 coverageMap.put( j2eeType, coverageInfo ); 166 } 167 } 168 169 return coverageMap; 170 } 171 172 public String 173 getCoverageSummary() 174 { 175 final Set <AMX> amx = mTestUtil.getAllAMX(); 176 final Map <String ,CoverageInfo> coverage = getCoverage( amx ); 177 178 final String [] j2eeTypes = GSetUtil.toStringArray( coverage.keySet() ); 179 Arrays.sort( j2eeTypes ); 180 181 final String LINE_SEP = System.getProperty( "line.separator" ); 182 183 final StringBuilder builder = new StringBuilder (); 184 for( final String j2eeType : j2eeTypes ) 185 { 186 final CoverageInfo info = coverage.get( j2eeType ); 187 188 final String infoString = 189 "Coverage for j2eeType = " + j2eeType + 190 ": " + (info.getFullCoverage() ? "100%" : "INCOMPLETE COVERAGE" ) + 191 LINE_SEP + 192 info.toString( false ) + LINE_SEP + LINE_SEP; 193 194 builder.append( infoString ); 195 } 196 197 final String msg = 198 "No AMX MBeans having the following types " + 199 "were ever present, and so were NEVER TESTED:" + NEWLINE; 200 builder.append( createMissingString( msg ) ); 201 202 return builder.toString(); 203 } 204 205 206 207 210 protected Set <String > 211 findMissingJ2EETypes() 212 { 213 final Set <String > missing = new HashSet <String >(); 214 missing.addAll( XTypesMapper.getInstance().getJ2EETypes() ); 215 missing.addAll( J2EETypesMapper.getInstance().getJ2EETypes() ); 216 217 missing.removeAll( mTestUtil.findRegisteredJ2EETypes() ); 218 219 final Set <ObjectName > registered = 220 Observer.getInstance().getRegistrationListener().getRegistered(); 221 for( final ObjectName objectName : registered ) 222 { 223 final String j2eeType = Util.getJ2EEType( objectName ); 224 if ( j2eeType != null ) 225 { 226 missing.remove( j2eeType ); 227 } 228 } 229 230 return missing; 231 } 232 233 protected void 234 groupMissingJ2EETypes( 235 final Set <String > allMissing, 236 final Set <String > missingConfigs, 237 final Set <String > missingMonitors, 238 final Set <String > missingOthers 239 ) 240 { 241 for( final String j2eeType : allMissing ) 242 { 243 if ( j2eeType.endsWith( "Config" ) ) 244 { 245 missingConfigs.add( j2eeType ); 246 } 247 else if ( j2eeType.endsWith( "Monitor" ) ) 248 { 249 missingMonitors.add( j2eeType ); 250 } 251 else 252 { 253 missingOthers.add( j2eeType ); 254 } 255 } 256 } 257 258 private String 259 setToSortedString( final Set <String > s, final String delim ) 260 { 261 final String [] a = GSetUtil.toStringArray( s ); 262 Arrays.sort( a ); 263 264 return StringUtil.toString( NEWLINE, (Object [])a ); 265 } 266 267 268 protected String 269 createMissingString( final String msg) 270 { 271 String result = ""; 272 273 final Set <String > missing = findMissingJ2EETypes(); 274 if ( missing.size() != 0 ) 275 { 276 final Set <String > missingConfig = new HashSet <String >(); 277 final Set <String > missingMonitors = new HashSet <String >(); 278 final Set <String > missingOthers = new HashSet <String >(); 279 280 groupMissingJ2EETypes( missing, missingConfig, missingMonitors, missingOthers ); 281 282 result = msg + NEWLINE + 283 "Config: " + NEWLINE + 284 setToSortedString( missingConfig, NEWLINE) + NEWLINE + NEWLINE + 285 "Monitor: " + NEWLINE + 286 setToSortedString( missingMonitors, NEWLINE) + NEWLINE + NEWLINE + 287 "J2EE/Other: " + NEWLINE + 288 setToSortedString( missingOthers, NEWLINE); 289 } 290 291 return result; 292 } 293 } 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | Popular Tags |