KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > CoverageInfoImpl


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.support;
24
25 import java.io.Serializable JavaDoc;
26
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Collections JavaDoc;
33
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.management.MBeanAttributeInfo JavaDoc;
36 import javax.management.MBeanOperationInfo JavaDoc;
37
38 import com.sun.appserv.management.util.misc.StringUtil;
39 import com.sun.appserv.management.util.misc.CollectionUtil;
40 import com.sun.appserv.management.util.misc.ExceptionUtil;
41 import com.sun.appserv.management.util.misc.GSetUtil;
42 import com.sun.appserv.management.util.jmx.JMXUtil;
43 import com.sun.appserv.management.util.jmx.ReadWriteAttributeFilter;
44
45
46 /**
47     Used to record access to AMX.
48     @see AMXDebugStuff
49  */

50 public final class CoverageInfoImpl implements Serializable JavaDoc, CoverageInfo
51 {
52     public static final long serialVersionUID = 0xABCDEF;
53     
54     private final Set JavaDoc<String JavaDoc> mAttributesRead;
55     private final Set JavaDoc<String JavaDoc> mAttributesWritten;
56     private final Set JavaDoc<String JavaDoc> mOperationsInvoked;
57     
58     private final Map JavaDoc<String JavaDoc,Integer JavaDoc> mAttributeGetFailures;
59     private final Map JavaDoc<String JavaDoc,Integer JavaDoc> mAttributeSetFailures;
60     private final Map JavaDoc<String JavaDoc,Integer JavaDoc> mUnknownAttributes;
61     private final Map JavaDoc<String JavaDoc,Integer JavaDoc> mUnknownOperations;
62     private final Map JavaDoc<String JavaDoc,Integer JavaDoc> mInvocationFailures;
63     
64     private Set JavaDoc<String JavaDoc> mLegalReadableAttributes;
65     private Set JavaDoc<String JavaDoc> mLegalWriteableAttributes;
66     private Set JavaDoc<String JavaDoc> mLegalOperations;
67     
68     private MBeanInfo JavaDoc mMBeanInfo;
69     
70         public
71     CoverageInfoImpl( final MBeanInfo JavaDoc mbeanInfo )
72     {
73         mLegalReadableAttributes = null;
74         mLegalWriteableAttributes = null;
75         mLegalOperations = null;
76         mMBeanInfo = mbeanInfo;
77         setMBeanInfo( mMBeanInfo );
78         
79         mAttributesRead = new HashSet JavaDoc<String JavaDoc>();
80         mAttributesWritten = new HashSet JavaDoc<String JavaDoc>();
81         mOperationsInvoked = new HashSet JavaDoc<String JavaDoc>();
82         
83         mAttributeGetFailures = new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>();
84         mAttributeSetFailures = new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>();
85         mUnknownAttributes = new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>();
86         mUnknownOperations = new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>();
87         mInvocationFailures = new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>();
88         
89     }
90     
91         public void
92     clear()
93     {
94         mAttributesRead.clear();
95         mAttributesWritten.clear();
96         mOperationsInvoked.clear();
97         
98         mAttributeGetFailures.clear();
99         mAttributeSetFailures.clear();
100         mUnknownAttributes.clear();
101         mUnknownOperations.clear();
102         mInvocationFailures.clear();
103     }
104     
105         public MBeanInfo JavaDoc
106     getMBeanInfo()
107     {
108         return mMBeanInfo;
109     }
110
111         public void
112     setMBeanInfo( final MBeanInfo JavaDoc mbeanInfo )
113     {
114         mLegalOperations = new HashSet JavaDoc<String JavaDoc>();
115         mLegalReadableAttributes = new HashSet JavaDoc<String JavaDoc>();
116         mLegalWriteableAttributes = new HashSet JavaDoc<String JavaDoc>();
117         
118         if ( mbeanInfo != null ) try
119         {
120             final MBeanOperationInfo JavaDoc[] ops = mbeanInfo.getOperations();
121             for( final MBeanOperationInfo JavaDoc opInfo : ops )
122             {
123                 final String JavaDoc[] sig = JMXUtil.getSignature( opInfo.getSignature() );
124                 final String JavaDoc fullName =
125                     getFullOperationName( opInfo.getName(), sig );
126                 mLegalOperations.add( fullName );
127             }
128             mLegalOperations = Collections.unmodifiableSet( mLegalOperations );
129             
130             final MBeanAttributeInfo JavaDoc[] allAttrInfos = getMBeanInfo().getAttributes();
131                 
132             final MBeanAttributeInfo JavaDoc[] readables =
133                 JMXUtil.filterAttributeInfos( allAttrInfos,
134                     ReadWriteAttributeFilter.READABLE_FILTER );
135                     
136             final MBeanAttributeInfo JavaDoc[] writeables =
137                 JMXUtil.filterAttributeInfos( allAttrInfos,
138                     ReadWriteAttributeFilter.WRITEABLE_FILTER );
139             
140             mLegalReadableAttributes =
141                 GSetUtil.newUnmodifiableStringSet( JMXUtil.getAttributeNames( readables ) );
142                 
143             mLegalWriteableAttributes =
144                 GSetUtil.newUnmodifiableStringSet( JMXUtil.getAttributeNames( writeables ) );
145         }
146         catch( Exception JavaDoc e )
147         {
148             System.out.println( ExceptionUtil.toString( e ) );
149             throw new RuntimeException JavaDoc( e );
150         }
151     }
152     
153     
154         private void
155     mergeCounts(
156         final Map JavaDoc<String JavaDoc,Integer JavaDoc> src,
157         final Map JavaDoc<String JavaDoc,Integer JavaDoc> dest )
158     {
159         for( final String JavaDoc key : src.keySet() )
160         {
161             final Integer JavaDoc srcValue = src.get( key );
162             final Integer JavaDoc destValue = dest.get( key );
163             
164             final int sum = srcValue.intValue() +
165                     (destValue == null ? 0 : destValue.intValue());
166             
167             dest.put( key, new Integer JavaDoc( sum ) );
168         }
169     }
170     
171         public void
172     merge( final CoverageInfo info )
173     {
174         mAttributesRead.addAll( info.getAttributesRead() );
175         mAttributesWritten.addAll( info.getAttributesWritten() );
176         mOperationsInvoked.addAll( info.getOperationsInvoked() );
177         
178         mergeCounts( info.getAttributeGetFailures(), mAttributeGetFailures );
179         mergeCounts( info.getAttributeSetFailures(), mAttributeSetFailures );
180         mergeCounts( info.getUnknownAttributes(), mUnknownAttributes );
181         mergeCounts( info.getUnknownOperations(), mUnknownOperations );
182         mergeCounts( info.getInvocationFailures(), mInvocationFailures );
183     }
184     
185         public Set JavaDoc<String JavaDoc>
186     getAttributesRead()
187     {
188         return new HashSet JavaDoc<String JavaDoc>( mAttributesRead );
189     }
190     
191         public Set JavaDoc<String JavaDoc>
192     getAttributesNotRead()
193     {
194         checkHaveMBeanInfo();
195         
196         // remove all Attributes which were written from the legal set
197
final Set JavaDoc<String JavaDoc> notRead = new HashSet JavaDoc<String JavaDoc>( mLegalReadableAttributes );
198         notRead.removeAll( mAttributesRead );
199
200         return notRead;
201     }
202     
203         public Set JavaDoc<String JavaDoc>
204     getAttributesWritten()
205     {
206         return new HashSet JavaDoc<String JavaDoc>( mAttributesWritten );
207     }
208     
209         public Set JavaDoc<String JavaDoc>
210     getAttributesNotWritten()
211     {
212         checkHaveMBeanInfo();
213         
214         // remove all Attributes which were read from the legal set
215
final Set JavaDoc<String JavaDoc> notWritten = new HashSet JavaDoc<String JavaDoc>( mLegalWriteableAttributes );
216         notWritten.removeAll( mAttributesWritten );
217
218         return notWritten;
219     }
220     
221         public Set JavaDoc<String JavaDoc>
222     getOperationsInvoked()
223     {
224         return new HashSet JavaDoc<String JavaDoc>( mOperationsInvoked );
225     }
226     
227         public Set JavaDoc<String JavaDoc>
228     getOperationsNotInvoked()
229     {
230         checkHaveMBeanInfo();
231         
232         // remove all Attributes which were read from the legal set
233
final Set JavaDoc<String JavaDoc> notInvoked = new HashSet JavaDoc<String JavaDoc>( mLegalOperations );
234         notInvoked.removeAll( getOperationsInvoked() );
235
236         return notInvoked;
237     }
238     
239         private void
240     checkHaveMBeanInfo()
241     {
242         if ( getMBeanInfo() == null )
243         {
244             throw new IllegalArgumentException JavaDoc(
245                 "MBeanInfo must be set using setMBeanInfo() prior to call" );
246         }
247     }
248     
249         public Map JavaDoc<String JavaDoc,Integer JavaDoc>
250     getAttributeGetFailures()
251     {
252         return new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>( mAttributeGetFailures );
253     }
254     
255         public Map JavaDoc<String JavaDoc,Integer JavaDoc>
256     getAttributeSetFailures()
257     {
258         return new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>( mAttributeSetFailures );
259     }
260     
261         public Map JavaDoc<String JavaDoc,Integer JavaDoc>
262     getUnknownAttributes()
263     {
264         return new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>( mUnknownAttributes );
265     }
266
267     
268         public void
269     ignoreUnknownAttribute( final String JavaDoc name )
270     {
271         mUnknownAttributes.remove( name );
272         mAttributeGetFailures.remove( name );
273         mAttributeSetFailures.remove( name );
274     }
275     
276     
277         public Map JavaDoc<String JavaDoc,Integer JavaDoc>
278     getUnknownOperations()
279     {
280         return new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>( mUnknownOperations );
281     }
282     
283         public Map JavaDoc<String JavaDoc,Integer JavaDoc>
284     getInvocationFailures()
285     {
286         return new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>( mInvocationFailures );
287     }
288     
289         private void
290     unknownAttribute( final String JavaDoc name )
291     {
292         Integer JavaDoc count = mUnknownAttributes.get( name );
293         count = new Integer JavaDoc( count == null ? 1 : 1 + count.intValue() );
294         
295         mUnknownAttributes.put( name, count );
296     }
297     
298         public void
299     attributeWasRead( final String JavaDoc name )
300     {
301         if ( mLegalReadableAttributes.contains( name ) )
302         {
303             mAttributesRead.add( name );
304         }
305         else
306         {
307             unknownAttribute( name );
308         }
309     }
310     
311         public void
312     attributesWereRead( final String JavaDoc[] names )
313     {
314         for( final String JavaDoc name : names )
315         {
316             attributeWasRead( name );
317         }
318     }
319     
320         public void
321     attributeSetFailure( final String JavaDoc name )
322     {
323         Integer JavaDoc count = mAttributeSetFailures.get( name );
324         count = new Integer JavaDoc( count == null ? 1 : 1 + count.intValue() );
325         
326         mAttributeSetFailures.put( name, count );
327     }
328     
329     
330         public void
331     attributeGetFailure( final String JavaDoc name )
332     {
333         Integer JavaDoc count = mAttributeGetFailures.get( name );
334         count = new Integer JavaDoc( count == null ? 1 : 1 + count.intValue() );
335         
336         mAttributeGetFailures.put( name, count );
337     }
338     
339         public void
340     attributeWasWritten( final String JavaDoc name )
341     {
342         if ( mLegalWriteableAttributes.contains( name ) )
343         {
344             mAttributesWritten.add( name );
345         }
346         else
347         {
348             unknownAttribute( name );
349         }
350     }
351     
352         private String JavaDoc
353     getFullOperationName(
354         final String JavaDoc name,
355         final String JavaDoc[] sig)
356     {
357         final String JavaDoc sigString = StringUtil.toString( ",", sig == null ? EMPTY_SIG : sig );
358         
359         final String JavaDoc s = name + "(" + sigString + ")";
360         
361         return s;
362     }
363     
364     private static final String JavaDoc[] EMPTY_SIG = new String JavaDoc[0];
365     
366     private void sdebug( final Object JavaDoc o )
367     {
368         System.out.println( "" + o );
369     }
370         public void
371     operationWasInvoked(
372         final String JavaDoc name,
373         final String JavaDoc[] sig)
374     {
375         final String JavaDoc fullName = getFullOperationName( name, sig );
376
377         if ( mLegalOperations.contains( fullName ) )
378         {
379             mOperationsInvoked.add( fullName );
380         }
381         else
382         {
383             unknownOperation( fullName, sig );
384             assert( ! mOperationsInvoked.contains( fullName ) );
385         }
386     }
387     
388         private void
389     unknownOperation(
390         final String JavaDoc name,
391         final String JavaDoc[] sig)
392     {
393         final String JavaDoc fullName =
394             getFullOperationName( name, sig );
395         
396         Integer JavaDoc count = mUnknownOperations.get( fullName );
397         count = new Integer JavaDoc( count == null ? 1 : 1 + count.intValue() );
398         
399         mUnknownOperations.put( fullName, count );
400     }
401     
402         public void
403     markAsInvoked(final String JavaDoc fullName )
404     {
405         if ( (! fullName.endsWith( ")" )) || fullName.indexOf( "(" ) < 0 )
406         {
407             throw new IllegalArgumentException JavaDoc( fullName );
408         }
409         
410         mOperationsInvoked.add( fullName );
411     }
412     
413         public void
414     operationFailed(
415         final String JavaDoc name,
416         final String JavaDoc[] sig)
417     {
418         final String JavaDoc fullName = getFullOperationName( name, sig );
419         if ( ! getOperations().contains( fullName ) )
420         {
421             throw new IllegalArgumentException JavaDoc( fullName );
422         }
423         
424         Integer JavaDoc count = mInvocationFailures.get( name );
425         count = new Integer JavaDoc( count == null ? 1 : 1 + count.intValue() );
426         
427         mInvocationFailures.put( fullName, count );
428     }
429     
430         
431         public String JavaDoc
432     toString()
433     {
434         return toString( true );
435     }
436     
437         private String JavaDoc
438     toString( final Collection JavaDoc c)
439     {
440         return CollectionUtil.toString( c );
441     }
442     
443         private String JavaDoc
444     toString( final Collection JavaDoc c, final String JavaDoc sep )
445     {
446         return CollectionUtil.toString( c, sep );
447     }
448
449         public int
450     getNumReadableAttributes()
451     {
452         checkHaveMBeanInfo();
453         
454         return mLegalReadableAttributes.size();
455     }
456     
457         public Set JavaDoc<String JavaDoc>
458     getReadableAttributes()
459     {
460         return mLegalReadableAttributes;
461     }
462     
463         public Set JavaDoc<String JavaDoc>
464     getWriteableAttributes()
465     {
466         return mLegalWriteableAttributes;
467     }
468     
469     
470         public Set JavaDoc<String JavaDoc>
471     getOperations()
472     {
473         return mLegalOperations;
474     }
475     
476         public int
477     getNumWriteableAttributes()
478     {
479         checkHaveMBeanInfo();
480         
481         return mLegalWriteableAttributes.size();
482     }
483     
484         public int
485     getNumOperations()
486     {
487         return mLegalOperations.size();
488     }
489     
490         public int
491     getAttributeReadCoverage()
492     {
493         return percent( mAttributesRead.size(), mLegalReadableAttributes.size() );
494     }
495     
496         public int
497     getAttributeWriteCoverage()
498     {
499         return percent( mAttributesWritten.size(), getNumWriteableAttributes() );
500     }
501     
502     /**
503         @return percent of legal operations which were invoked
504      */

505         public int
506     getOperationCoverage()
507     {
508         checkHaveMBeanInfo();
509         
510         final int numOperations = mLegalOperations.size();
511         
512         // do it this way; some illegal operations get invoked, too
513
final Set JavaDoc<String JavaDoc> remaining = new HashSet JavaDoc<String JavaDoc>( mLegalOperations );
514         for( final String JavaDoc invoked : mOperationsInvoked )
515         {
516             remaining.remove( invoked );
517         }
518
519         final int numInvoked = numOperations - remaining.size();
520         
521         return percent( numInvoked, numOperations );
522     }
523     
524         public boolean
525     getFullCoverage()
526     {
527         return getAttributeReadCoverage() == 100 &&
528                 getAttributeWriteCoverage() == 100 &&
529                 getOperationCoverage() == 100;
530     }
531     
532         private int
533     percent( final int numerator, final int denominator )
534     {
535         return (int)((((float)numerator / (float)denominator))*100.0);
536     }
537     
538         public String JavaDoc
539     toString( final boolean verbose )
540     {
541         final String JavaDoc NEWLINE = System.getProperty( "line.separator" );
542         final String JavaDoc INDENT = " ";
543         final String JavaDoc ITEM_SEP = NEWLINE + INDENT;
544         
545         final StringBuilder JavaDoc b = new StringBuilder JavaDoc();
546         
547         b.append( "Attribute read coverage " +
548             getAttributesRead().size() + "/" + getNumReadableAttributes() +
549             " = " + getAttributeReadCoverage() + "%" + NEWLINE );
550             
551         b.append( "Attribute write coverage " +
552             getAttributesWritten().size() + "/" + getNumWriteableAttributes() +
553             " = " + getAttributeWriteCoverage() + "%" + NEWLINE );
554             
555         b.append( "Operation invocation coverage " +
556                     getOperationsInvoked().size() + "/" + getNumOperations() +
557                     " = " + getOperationCoverage() + "%" + NEWLINE );
558         
559         if ( verbose )
560         {
561             b.append( mAttributesRead.size() +
562                 " Attributes read: " + ITEM_SEP +
563                     toString( getAttributesRead(), ITEM_SEP) + NEWLINE );
564                 
565             b.append( mAttributesWritten.size() +
566                 " Attributes written: " + ITEM_SEP +
567                     toString( getAttributesWritten(), ITEM_SEP) + NEWLINE );
568                 
569             b.append( mOperationsInvoked.size() +
570                 " operations invoked: " + ITEM_SEP +
571                     toString( getOperationsInvoked(), ITEM_SEP) + NEWLINE );
572         }
573         
574         if ( getMBeanInfo() != null )
575         {
576             Set JavaDoc<String JavaDoc> not = null;
577             
578             not = getAttributesNotRead();
579             if ( not.size() != 0 || verbose )
580             {
581                 b.append( not.size() +
582                     " Attributes NOT read:" + ITEM_SEP + toString( not, ITEM_SEP ) + NEWLINE );
583             }
584                 
585             not = getAttributesNotWritten();
586             if ( not.size() != 0 || verbose )
587             {
588                 b.append( not.size() +
589                     " Attributes NOT written:" + ITEM_SEP + toString( not, ITEM_SEP) + NEWLINE );
590             }
591             
592             not = getOperationsNotInvoked();
593             if ( not.size() != 0 || verbose )
594             {
595                 b.append( not.size() +
596                     " operations NOT invoked:" + ITEM_SEP +
597                         CollectionUtil.toString( not, ITEM_SEP ) + NEWLINE );
598             }
599         }
600         else
601         {
602             b.append( "WARNING: MBeanInfo not supplied, " +
603                 "can't emit Attributes/operations not read/written/invoked" + NEWLINE );
604         }
605         
606         Map JavaDoc<String JavaDoc,Integer JavaDoc> failures = null;
607         
608         failures = getAttributeGetFailures();
609         if ( failures.size() != 0 || verbose )
610         {
611             b.append( failures.keySet().size() +
612                 " getAttribute failures: " + ITEM_SEP +
613                     toString( failures.keySet(), ITEM_SEP) + NEWLINE );
614         }
615             
616         failures = getAttributeSetFailures();
617         if ( failures.size() != 0 || verbose )
618         {
619             b.append( failures.keySet().size() +
620                 " setAttribute failures: " + ITEM_SEP +
621                     toString( failures.keySet(), ITEM_SEP) + NEWLINE );
622         }
623             
624         failures = getUnknownAttributes();
625         if ( failures.size() != 0 || verbose )
626         {
627             b.append( failures.keySet().size() +
628                 " unknown Attributes: " + ITEM_SEP +
629                     toString( failures.keySet(), ITEM_SEP) + NEWLINE );
630         }
631         
632         failures = getUnknownOperations();
633         if ( failures.size() != 0 || verbose )
634         {
635             b.append( failures.keySet().size() +
636                 " unknown operations: " + ITEM_SEP +
637                     toString( failures.keySet(), ITEM_SEP) + NEWLINE );
638         }
639             
640         failures = getInvocationFailures();
641         if ( failures.size() != 0 || verbose )
642         {
643             b.append( failures.keySet().size() +
644                 " invoke() failures: " + ITEM_SEP +
645                 CollectionUtil.toString( failures.keySet(), ITEM_SEP) + NEWLINE );
646         }
647         
648         return b.toString();
649     }
650 }
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
Popular Tags