KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > CoverageInfoAnalyzer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.management;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Arrays JavaDoc;
30
31 import javax.management.MBeanServerConnection JavaDoc;
32 import javax.management.MBeanInfo JavaDoc;
33 import javax.management.MBeanAttributeInfo JavaDoc;
34 import javax.management.MBeanOperationInfo JavaDoc;
35 import javax.management.ObjectName JavaDoc;
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 /**
57     Analyze the CoverageInfo for AMX MBeans. Use only after tests
58     have been run.
59  */

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 JavaDoc 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 JavaDoc( "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 JavaDoc( "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 JavaDoc<String JavaDoc> 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 JavaDoc<String JavaDoc> IGNORE_UNKNOWN =
105         GSetUtil.newUnmodifiableStringSet(
106             "ContaineeJ2EETypes",
107             "eventProvider"
108         );
109     /**
110         Certain methods will never be called via remote access due to
111         the JMX implementation. removeNotificationListener() in particular
112         is only invoked in one way by the MBeanServer in response to
113         remote clients registering listeners. Omit these cases, since
114         they will never be invoked.
115      */

116         private void
117     handleSpecialCases( final CoverageInfo coverageInfo )
118     {
119         final Set JavaDoc<String JavaDoc> notInvoked = coverageInfo.getOperationsNotInvoked();
120         
121         for( final String JavaDoc op : notInvoked )
122         {
123             for( final String JavaDoc prefix : IGNORE_METHODS )
124             {
125                 if ( op.startsWith( prefix ) )
126                 {
127                     coverageInfo.markAsInvoked( op );
128                 }
129             }
130         }
131         
132         // make a copy, we'll be modifying it
133
final Set JavaDoc<String JavaDoc> unknown =coverageInfo.getUnknownAttributes().keySet();
134         for( final String JavaDoc 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 JavaDoc<String JavaDoc,CoverageInfo>
146     getCoverage(final Set JavaDoc<AMX> candidates )
147     {
148         final Map JavaDoc<String JavaDoc,CoverageInfo> coverageMap = new HashMap JavaDoc<String JavaDoc,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 JavaDoc 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 JavaDoc
173     getCoverageSummary()
174     {
175         final Set JavaDoc<AMX> amx = mTestUtil.getAllAMX();
176         final Map JavaDoc<String JavaDoc,CoverageInfo> coverage = getCoverage( amx );
177         
178         final String JavaDoc[] j2eeTypes = GSetUtil.toStringArray( coverage.keySet() );
179         Arrays.sort( j2eeTypes );
180         
181         final String JavaDoc LINE_SEP = System.getProperty( "line.separator" );
182         
183         final StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
184         for( final String JavaDoc j2eeType : j2eeTypes )
185         {
186             final CoverageInfo info = coverage.get( j2eeType );
187             
188             final String JavaDoc 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 JavaDoc 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     /**
208         @return Set of j2eeTypes for which no MBeans exist
209      */

210         protected Set JavaDoc<String JavaDoc>
211     findMissingJ2EETypes()
212     {
213         final Set JavaDoc<String JavaDoc> missing = new HashSet JavaDoc<String JavaDoc>();
214         missing.addAll( XTypesMapper.getInstance().getJ2EETypes() );
215         missing.addAll( J2EETypesMapper.getInstance().getJ2EETypes() );
216         
217         missing.removeAll( mTestUtil.findRegisteredJ2EETypes() );
218         
219         final Set JavaDoc<ObjectName JavaDoc> registered =
220             Observer.getInstance().getRegistrationListener().getRegistered();
221         for( final ObjectName JavaDoc objectName : registered )
222         {
223             final String JavaDoc 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 JavaDoc<String JavaDoc> allMissing,
236         final Set JavaDoc<String JavaDoc> missingConfigs,
237         final Set JavaDoc<String JavaDoc> missingMonitors,
238         final Set JavaDoc<String JavaDoc> missingOthers
239         )
240     {
241         for( final String JavaDoc 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 JavaDoc
259     setToSortedString( final Set JavaDoc<String JavaDoc> s, final String JavaDoc delim )
260     {
261         final String JavaDoc[] a = GSetUtil.toStringArray( s );
262         Arrays.sort( a );
263        
264         return StringUtil.toString( NEWLINE, (Object JavaDoc[])a );
265     }
266     
267     
268         protected String JavaDoc
269     createMissingString( final String JavaDoc msg)
270     {
271         String JavaDoc result = "";
272         
273         final Set JavaDoc<String JavaDoc> missing = findMissingJ2EETypes();
274         if ( missing.size() != 0 )
275         {
276             final Set JavaDoc<String JavaDoc> missingConfig = new HashSet JavaDoc<String JavaDoc>();
277             final Set JavaDoc<String JavaDoc> missingMonitors = new HashSet JavaDoc<String JavaDoc>();
278             final Set JavaDoc<String JavaDoc> missingOthers = new HashSet JavaDoc<String JavaDoc>();
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