KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > base > AMXTest


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.base;
24
25 import java.io.Serializable JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.AttributeList JavaDoc;
39 import javax.management.MBeanInfo JavaDoc;
40 import javax.management.MBeanOperationInfo JavaDoc;
41 import javax.management.MBeanParameterInfo JavaDoc;
42 import javax.management.MBeanAttributeInfo JavaDoc;
43 import javax.management.MBeanFeatureInfo JavaDoc;
44 import javax.management.Attribute JavaDoc;
45 import javax.management.JMException JavaDoc;
46 import javax.management.MBeanServerConnection JavaDoc;
47 import javax.management.AttributeNotFoundException JavaDoc;
48 import javax.management.NotificationListener JavaDoc;
49 import javax.management.Notification JavaDoc;
50
51
52 import com.sun.appserv.management.DomainRoot;
53 import com.sun.appserv.management.base.AMX;
54 import com.sun.appserv.management.base.Container;
55 import com.sun.appserv.management.base.AMXAttributes;
56 import com.sun.appserv.management.base.QueryMgr;
57 import com.sun.appserv.management.base.Container;
58 import com.sun.appserv.management.base.Container;
59 import com.sun.appserv.management.base.Util;
60 import com.sun.appserv.management.base.XTypes;
61 import com.sun.appserv.management.base.Extra;
62
63 import com.sun.appserv.management.client.ProxyFactory;
64
65 import com.sun.appserv.management.monitor.Monitoring;
66 import com.sun.appserv.management.monitor.JMXMonitorMgr;
67 import com.sun.appserv.management.monitor.AMXStringMonitor;
68 import com.sun.appserv.management.monitor.AMXCounterMonitor;
69 import com.sun.appserv.management.monitor.AMXGaugeMonitor;
70 import com.sun.appserv.management.monitor.EJBModuleMonitor;
71 import com.sun.appserv.management.monitor.HTTPServiceMonitor;
72 import com.sun.appserv.management.monitor.ApplicationMonitor;
73
74 import com.sun.appserv.management.j2ee.J2EETypes;
75
76 import com.sun.appserv.management.config.AMXConfig;
77 import com.sun.appserv.management.config.NamedConfigElement;
78 import com.sun.appserv.management.config.SecurityMapConfig;
79
80 import com.sun.appserv.management.util.jmx.MBeanServerConnectionConnectionSource;
81 import com.sun.appserv.management.util.jmx.JMXUtil;
82 import com.sun.appserv.management.util.misc.ExceptionUtil;
83
84 import com.sun.appserv.management.util.misc.GSetUtil;
85 import com.sun.appserv.management.util.misc.ClassUtil;
86 import com.sun.appserv.management.util.misc.StringUtil;
87 import com.sun.appserv.management.util.misc.CollectionUtil;
88
89 import com.sun.appserv.management.ext.wsmgmt.WebServiceEndpointInfo;
90 import com.sun.appserv.management.ext.wsmgmt.MessageTrace;
91 import com.sun.appserv.management.ext.logging.LogQueryResult;
92 import com.sun.appserv.management.ext.logging.Logging;
93
94 import com.sun.enterprise.management.support.AMXNonConfigImplBase;
95 import com.sun.enterprise.management.support.QueryMgrImpl;
96 import com.sun.enterprise.management.support.TypeInfos;
97 import com.sun.enterprise.management.support.TypeInfo;
98 import com.sun.enterprise.management.support.CoverageInfo;
99 import com.sun.enterprise.management.support.AMXDebugStuff;
100
101
102
103 import com.sun.enterprise.management.AMXTestBase;
104 import com.sun.enterprise.management.Capabilities;
105 import com.sun.enterprise.management.Capabilities;
106
107
108
109 /**
110  */

111 public final class AMXTest extends AMXTestBase
112 {
113         public
114     AMXTest( )
115     {
116     }
117     
118         public static Capabilities
119     getCapabilities()
120     {
121         return getOfflineCapableCapabilities( true );
122     }
123     
124     /**
125         Verify that the ObjectName returned from the ATTR_CONTAINER_OBJECT_NAME is the same
126         as the ObjectName obtained from the getContainer() proxy.
127      */

128         public void
129     checkContainerObjectName( final ObjectName JavaDoc objectName )
130         throws Exception JavaDoc
131     {
132         final ObjectName JavaDoc containerObjectName = (ObjectName JavaDoc)
133             getConnection().getAttribute( objectName, AMXAttributes.ATTR_CONTAINER_OBJECT_NAME );
134             
135         if ( Util.getJ2EEType( objectName ).equals( XTypes.DOMAIN_ROOT ) )
136         {
137             assert( containerObjectName == null );
138         }
139         else
140         {
141             assert( containerObjectName != null );
142             final AMX proxy = getProxyFactory().getProxy( objectName, AMX.class );
143             assert( Util.getObjectName( proxy.getContainer() ).equals( containerObjectName ) );
144         }
145         
146     }
147     
148         public void
149     testContainerObjectName()
150         throws Exception JavaDoc
151     {
152         testAll( "checkContainerObjectName" );
153     }
154     
155     
156     /**
157         Look for Attributes that probably should be String and not int/long
158         due to our template facility ${...}
159      */

160         public void
161     checkTemplateAttributes( final ObjectName JavaDoc objectName )
162     {
163         final AMX proxy = getProxyFactory().getProxy( objectName, AMX.class);
164         
165         if ( proxy instanceof AMXConfig )
166         {
167             final AMXConfig config = (AMXConfig)proxy;
168             
169             final Set JavaDoc<String JavaDoc> s = new HashSet JavaDoc<String JavaDoc>();
170             
171             final MBeanInfo JavaDoc mbeanInfo = Util.getExtra( config ).getMBeanInfo();
172             final MBeanAttributeInfo JavaDoc[] attrInfos = mbeanInfo.getAttributes();
173             for( int i = 0; i < attrInfos.length; ++i )
174             {
175                 final MBeanAttributeInfo JavaDoc info = attrInfos[ i ];
176                 
177                 final String JavaDoc type = info.getType();
178                 if ( type.equals( "int" ) || type.equals( "long" ) )
179                 {
180                     s.add( info.getName() );
181                 }
182             }
183             
184             if ( s.size() != 0 )
185             {
186                 trace( "\n" + objectName +
187                     " contains the following int/long Attributes which perhaps ought to be String" +
188                     " due to the templatizing of config: " + toString( s ) + "\n" );
189             }
190         }
191     }
192     
193         public void
194     testTemplateAttributes()
195         throws Exception JavaDoc
196     {
197         testAll( "checkTemplateAttributes" );
198     }
199     
200     
201     
202     
203     /**
204         Verify that the ObjectName returned from the MBean is in fact itself.
205      */

206         public void
207     checkSelfObjectName( final ObjectName JavaDoc obj )
208         throws Exception JavaDoc
209     {
210         final ObjectName JavaDoc selfName = (ObjectName JavaDoc)
211             getConnection().getAttribute( obj, AMXAttributes.ATTR_OBJECT_NAME );
212             
213         assert( selfName.equals( obj ) );
214     }
215     
216         public void
217     testSelfObjectName()
218         throws Exception JavaDoc
219     {
220         testAll( "checkSelfObjectName" );
221     }
222     
223     
224     /**
225         Verify that the MBean has an ATTR_INTERFACE_NAME Attribute
226      */

227         public void
228     checkInterface( final ObjectName JavaDoc src )
229         throws Exception JavaDoc
230     {
231         final String JavaDoc interfaceName = (String JavaDoc)
232             getConnection().getAttribute( src, AMXAttributes.ATTR_INTERFACE_NAME );
233         
234         assert( interfaceName != null );
235     }
236     
237     
238         public void
239     testInterface()
240         throws Exception JavaDoc
241     {
242         testAll( "checkInterface" );
243     }
244     
245     /**
246         Verify that the MBean has j2eeType and name.
247      */

248         public void
249     checkJ2EETypeAndName( final ObjectName JavaDoc src )
250         throws Exception JavaDoc
251     {
252         assert( src.getKeyProperty( AMX.J2EE_TYPE_KEY ) != null );
253         assert( src.getKeyProperty( AMX.NAME_KEY ) != null );
254     }
255     
256     
257         public void
258     testJ2EETypeAndName()
259         throws Exception JavaDoc
260     {
261         testAll( "checkJ2EETypeAndName" );
262     }
263     
264     
265     
266     /**
267         Verify that all j2eeTypes have a proper Container that does actually hold them.
268      */

269         public void
270     testContainerChild()
271     {
272         final TypeInfos infos = TypeInfos.getInstance();
273         final Set JavaDoc<String JavaDoc> j2eeTypesSet = infos.getJ2EETypes();
274
275         for( final String JavaDoc j2eeType : j2eeTypesSet )
276         {
277             checkContainerChild( j2eeType );
278         }
279     }
280     
281     /**
282         Verify that each child's Container actually claims the child as a child.
283      */

284         public void
285     checkContainerChild( final String JavaDoc childJ2EEType )
286     {
287         final QueryMgr queryMgr = getQueryMgr();
288         final Set JavaDoc children = queryMgr.queryJ2EETypeSet( childJ2EEType );
289         
290         final Iterator JavaDoc iter = children.iterator();
291         while ( iter.hasNext() )
292         {
293             final AMX containee = Util.asAMX(iter.next());
294             Container container = null;
295             
296             final ObjectName JavaDoc objectName = Util.getObjectName( containee );
297             if ( ! shouldTest( objectName ) )
298             {
299                 continue;
300             }
301             
302             try
303             {
304                 container = (Container)containee.getContainer();
305             }
306             catch( Exception JavaDoc e )
307             {
308                 trace( "Can't get container for: " + objectName );
309             }
310             
311             if ( container == null )
312             {
313                 assert( containee.getJ2EEType().equals( XTypes.DOMAIN_ROOT ) ) :
314                     "container is null for: " + objectName;
315                 continue;
316             }
317             
318             final Set JavaDoc<AMX> containeeSet = container.getContaineeSet( childJ2EEType );
319             final Set JavaDoc<ObjectName JavaDoc> containeeObjectNameSet = Util.toObjectNames( containeeSet );
320             
321             assert( containeeObjectNameSet.contains( Util.getExtra( containee ).getObjectName() ) );
322         }
323     }
324     
325     
326     /**
327         Statically verify that the interface for each proxy has a J2EE_TYPE field.
328      */

329         public void
330     testHaveJ2EE_TYPE()
331     {
332         final TypeInfos infos = TypeInfos.getInstance();
333         final Set JavaDoc j2eeTypes = infos.getJ2EETypes();
334         
335         boolean success = true;
336         final Iterator JavaDoc iter = j2eeTypes.iterator();
337         while ( iter.hasNext() )
338         {
339             final String JavaDoc j2eeType = (String JavaDoc)iter.next();
340             final TypeInfo info = infos.getInfo( j2eeType );
341             
342             final Class JavaDoc theInterface = info.getInterface();
343             try
344             {
345                 final String JavaDoc value =
346                     (String JavaDoc)ClassUtil.getFieldValue( theInterface, "J2EE_TYPE" );
347                 assert( value.equals( j2eeType ) ) :
348                     "info and J2EE_TYPE don't match: " + j2eeType + " != " + value;
349             }
350             catch( Exception JavaDoc e )
351             {
352                 trace( "no J2EE_TYPE field found for proxy of type: " + theInterface.getName() );
353                 success = false;
354             }
355         }
356         assert( success );
357     }
358     
359     
360     /**
361         Verify that getName() is the same as the 'name' property in the ObjectName.
362      */

363         public void
364     checkNameMatchesJ2EEName( final ObjectName JavaDoc childObjectName )
365         throws Exception JavaDoc
366     {
367         final AMX childProxy = getProxyFactory().getProxy( childObjectName, AMX.class);
368         if ( childProxy instanceof NamedConfigElement )
369         {
370             final String JavaDoc j2eeName = childProxy.getName();
371             
372             assertEquals( j2eeName, childProxy.getName() );
373         }
374     }
375     
376         public void
377     testNameMatchesJ2EEName()
378         throws Exception JavaDoc
379     {
380         testAll( "checkNameMatchesJ2EEName" );
381     }
382     
383     
384     private static final String JavaDoc MAP_SUFFIX = "Map";
385     private static final String JavaDoc OBJECTNAME_MAP_SUFFIX = "ObjectName" + MAP_SUFFIX;
386     
387         private static boolean
388     isMapGetterName( final String JavaDoc methodName )
389     {
390         return( methodName.startsWith( JMXUtil.GET ) &&
391                 methodName.endsWith( MAP_SUFFIX ) );
392     }
393     
394         private static boolean
395     isMapGetter( final Method JavaDoc method)
396     {
397         return( Map JavaDoc.class.isAssignableFrom( method.getReturnType() ) &&
398             isMapGetterName( method.getName() ) );
399     }
400     
401     /**
402         Verify that a proxy getAbcMap(...) Attribute or operation has an appropriate
403         MBean getAbcObjectNameMap() method.
404      */

405         public void
406     checkMaps( final ObjectName JavaDoc objectName )
407         throws Exception JavaDoc
408     {
409         final AMX proxy = getProxyFactory().getProxy( objectName, AMX.class );
410         if ( proxy instanceof Container )
411         {
412             final Method JavaDoc[] methods = getInterfaceClass( proxy ).getMethods();
413             final MBeanInfo JavaDoc mbeanInfo = Util.getExtra( proxy ).getMBeanInfo();
414                         
415             for( int methodIdx = 0; methodIdx < methods.length; ++methodIdx )
416             {
417                 final Method JavaDoc method = methods[ methodIdx ];
418                 final String JavaDoc methodName = method.getName();
419                 
420                 if ( isMapGetter( method ) )
421                 {
422                     if ( methodName.endsWith( OBJECTNAME_MAP_SUFFIX ) )
423                     {
424                         warning( "method should exist in MBeanInfo, not interface: " + methodName );
425                         continue;
426                     }
427                     
428                     // verify that a corresponding peer method exists and
429
// has the right return type and same number and type of parameters
430
final String JavaDoc peerMethodName =
431                         StringUtil.replaceSuffix( methodName, MAP_SUFFIX, OBJECTNAME_MAP_SUFFIX);
432                     
433                     checkCompatibleOperationExists( Util.getObjectName( proxy ),
434                         method,
435                         peerMethodName,
436                         mbeanInfo );
437                 }
438                 else if ( isMapGetterName( methodName ) )
439                 {
440                     warning( "operation " + methodName + " does not return a Map!" );
441                 }
442             }
443         }
444     }
445     
446     /**
447         Verify that the proxy method has a compatible Attribute or operation.
448         <ul>
449         <li>a proxy getter must have a corresponding Attribute returning an ObjectName</li>
450         <li>a proxy operation must have a corresponding operation with matching signature</li>
451         <li>a proxy operation must have a corresponding operation with compatible return type</li>
452         </u
453      */

454         private void
455     checkCompatibleOperationExists(
456         final ObjectName JavaDoc objectName,
457         final Method JavaDoc proxyMethod,
458         final String JavaDoc mbeanMethodName,
459         final MBeanInfo JavaDoc mbeanInfo )
460     {
461         final Class JavaDoc proxyReturnType = proxyMethod.getReturnType();
462         final String JavaDoc proxyMethodName = proxyMethod.getName();
463         
464         String JavaDoc mbeanReturnType = null;
465         
466         final Class JavaDoc[] parameterTypes = proxyMethod.getParameterTypes();
467         if ( JMXUtil.isGetter( proxyMethod ) )
468         {
469             // it's getter
470
final Map JavaDoc<String JavaDoc,MBeanAttributeInfo JavaDoc> m = JMXUtil.attributeInfosToMap( mbeanInfo.getAttributes() );
471             
472             final String JavaDoc attrName = StringUtil.stripPrefix( mbeanMethodName, JMXUtil.GET );
473             final MBeanAttributeInfo JavaDoc attrInfo = (MBeanAttributeInfo JavaDoc)m.get( attrName );
474             if ( attrInfo != null )
475             {
476                 mbeanReturnType = attrInfo.getType();
477             }
478         }
479         else
480         {
481             // look for an operation that matches
482
final MBeanOperationInfo JavaDoc[] operations = mbeanInfo.getOperations();
483             
484             final String JavaDoc[] stringSig = ClassUtil.classnamesFromSignature( parameterTypes );
485             final MBeanOperationInfo JavaDoc opInfo = JMXUtil.findOperation( operations, mbeanMethodName, stringSig );
486             if ( opInfo != null )
487             {
488                 mbeanReturnType = opInfo.getReturnType();
489             }
490         }
491         
492         boolean hasPeer = mbeanReturnType != null;
493         if ( hasPeer )
494         {
495             // a proxy return type of AMX should have an Attribute type of ObjectName
496
if ( AMX.class.isAssignableFrom( proxyReturnType ) )
497             {
498                 assert( mbeanReturnType.equals( ObjectName JavaDoc.class.getName() ) );
499             }
500             else // return types must match
501
{
502                 assert( mbeanReturnType.equals( proxyReturnType.getName() ) );
503             }
504             hasPeer = true;
505         }
506         
507         
508         if( ! hasPeer )
509         {
510             trace( "MBean " + objectName + " has operation " + proxyMethodName +
511                 " without corresponding peer Attribute/operation " + mbeanMethodName );
512         }
513     }
514                             
515         public void
516     testMaps()
517         throws Exception JavaDoc
518     {
519         testAll( "checkMaps" );
520     }
521     
522     private static final Set JavaDoc SUITABLE_TYPES = GSetUtil.newUnmodifiableStringSet(
523         Void JavaDoc.class.getName(),
524         Object JavaDoc.class.getName(),
525         
526         // these are quick checks--other classes may be OK too
527
"boolean", "byte", "char", "short", "int", "long", "void",
528         
529         boolean[].class.getName(),
530         char[].class.getName(),
531         byte[].class.getName(),
532         short[].class.getName(),
533         int[].class.getName(),
534         long[].class.getName(),
535         Object JavaDoc[].class.getName(),
536         
537         Boolean JavaDoc.class.getName(),
538         Character JavaDoc.class.getName(),
539         Byte JavaDoc.class.getName(),
540         Short JavaDoc.class.getName(),
541         Integer JavaDoc.class.getName(),
542         Long JavaDoc.class.getName(),
543         
544         String JavaDoc.class.getName(),
545         String JavaDoc[].class.getName(),
546         
547         Date JavaDoc.class.getName(),
548         
549         ObjectName JavaDoc.class.getName(),
550         ObjectName JavaDoc[].class.getName(),
551         
552         Set JavaDoc.class.getName(),
553         List JavaDoc.class.getName(),
554         Map JavaDoc.class.getName(),
555         
556         java.util.logging.Level JavaDoc.class.getName(),
557         java.io.File JavaDoc.class.getName(),
558         
559         // these are passed as Maps, but declared as their proper types
560
// in the interface
561
WebServiceEndpointInfo.class.getName(),
562         LogQueryResult.class.getName(),
563         MessageTrace.class.getName()
564         );
565     
566
567     /**
568         Verify that the type is suitable for the API. It must meet the following constraints
569         <ul>
570         <li>that it is an OpenType or a standard Java type or a JMX type</li>
571         <li>that it is Serializable or an interface</li>
572         <li>or that it is an array whose elements meet the above constraints</li>
573         <li>or that it is one of our specific Stats types</li>
574         </ul>
575      */

576         private boolean
577     isSuitableReturnTypeForAPI( final String JavaDoc type )
578     {
579         boolean isSuitable = SUITABLE_TYPES.contains( type );
580         
581         if ( ! isSuitable )
582         {
583             final boolean isArray = ClassUtil.classnameIsArray( type );
584             
585             if ( isArray ||
586                 type.startsWith( "java." ) || type.startsWith( "javax.management." ) )
587             {
588                 Class JavaDoc c = null;
589                 try
590                 {
591                     c = ClassUtil.getClassFromName( type );
592                     isSuitable = c.isInterface() || Serializable JavaDoc.class.isAssignableFrom( c ) ||
593                         c == Object JavaDoc.class;
594                 }
595                 catch( ClassNotFoundException JavaDoc e )
596                 {
597                     trace( "WARNING: can't find class for type: " + type );
598                     isSuitable = false;
599                 }
600                 
601                 if ( isArray )
602                 {
603                     final Class JavaDoc elementClass = ClassUtil.getArrayElementClass( c );
604                     isSuitable = isSuitableReturnTypeForAPI( elementClass.getName() );
605                 }
606                 else if ( isSuitable &&
607                     (! type.startsWith( "javax." )) &&
608                     ! c.isInterface() )
609                 {
610                     // insist on an interface except for those types explicit in SUITABLE_TYPES
611
isSuitable = false;
612                 }
613             }
614             else if ( type.endsWith( "Stats" ) )
615             {
616                 isSuitable = type.startsWith( "com.sun.appserv.management.monitor.statistics" ) ||
617                         type.startsWith( "javax.management.j2ee.statistics" );
618             }
619         }
620         
621         return( isSuitable );
622     }
623     
624         
625     /**
626         Verify:
627         <ul>
628         <li>that all return types are suitable for the API</li>
629         </ul>
630      */

631         public void
632     checkReturnTypes( final ObjectName JavaDoc objectName )
633         throws Exception JavaDoc
634     {
635         final AMX proxy = getProxyFactory().getProxy( objectName, AMX.class);
636         final MBeanInfo JavaDoc info = Util.getExtra( proxy ).getMBeanInfo();
637         final MBeanOperationInfo JavaDoc[] operations = info.getOperations();
638         
639         boolean emittedName = false;
640         
641         for( int i = 0; i < operations.length; ++i )
642         {
643             final MBeanOperationInfo JavaDoc opInfo = operations[ i ];
644             
645             final String JavaDoc returnType = opInfo.getReturnType();
646             if ( ! isSuitableReturnTypeForAPI( returnType ) )
647             {
648                 if ( ! emittedName )
649                 {
650                     emittedName = true;
651                     trace( "\n" + objectName );
652                 }
653                 
654                 trace( "WARNING: unsuitable return type in API: " +
655                     returnType + " " + opInfo.getName() + "(...)" );
656             }
657         }
658     }
659     
660                             
661         public void
662     testReturnTypes()
663         throws Exception JavaDoc
664     {
665         testAll( "checkReturnTypes" );
666     }
667     
668     
669     
670     /**
671         Verify:
672         <ul>
673         <li>that all Attributes are of standard types and Serializable</li>
674         </ul>
675      */

676         public void
677     checkAttributeTypes( final ObjectName JavaDoc objectName )
678         throws Exception JavaDoc
679     {
680         final AMX proxy = getProxyFactory().getProxy( objectName, AMX.class);
681         final MBeanInfo JavaDoc info = Util.getExtra( proxy ).getMBeanInfo();
682         final MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
683         
684         boolean emittedName = false;
685         
686         for( int i = 0; i < attributes.length; ++i )
687         {
688             final MBeanAttributeInfo JavaDoc attrInfo = attributes[ i ];
689             
690             final String JavaDoc type = attrInfo.getType();
691             if ( ! isSuitableReturnTypeForAPI( type ) )
692             {
693                 if ( ! emittedName )
694                 {
695                     emittedName = true;
696                 }
697
698                 if ( ! type.equals( CoverageInfo.class.getName() ) )
699                 {
700                     trace( "WARNING: unsuitable Attribute type in API: " +
701                         type + " " + attrInfo.getName() + " in " + objectName );
702                 }
703             }
704         }
705     }
706                             
707         public void
708     testAttributeTypes()
709         throws Exception JavaDoc
710     {
711         testAll( "checkAttributeTypes" );
712     }
713     
714     /**
715         Verify:
716         <ul>
717         <li>each create() or createAbc() method ends in "Config" if it returns an AMXConfig subclass</li>
718         <li>each remove() or removeAbc() method ends in "Config"</li>
719         </ul>
720      */

721         public void
722     checkCreateRemoveGet( final ObjectName JavaDoc objectName )
723         throws Exception JavaDoc
724     {
725         final AMX proxy = getProxyFactory().getProxy( objectName, AMX.class );
726         if ( proxy instanceof Container )
727         {
728             final Method JavaDoc[] methods = getInterfaceClass( proxy ).getMethods();
729             final MBeanInfo JavaDoc mbeanInfo = Util.getExtra( proxy ).getMBeanInfo();
730             final MBeanOperationInfo JavaDoc[] operations = mbeanInfo.getOperations();
731             
732             for( int methodIdx = 0; methodIdx < methods.length; ++methodIdx )
733             {
734                 final Method JavaDoc method = methods[ methodIdx ];
735                 final String JavaDoc methodName = method.getName();
736                 
737                 if ( methodName.startsWith( "create" ) && ! methodName.endsWith( "Config" ) )
738                 {
739                     if ( AMXConfig.class.isAssignableFrom( method.getReturnType() ) &&
740                         (! (proxy instanceof SecurityMapConfig)) )
741                     {
742                         trace( "WARNING: method " + methodName + " does not end in 'Config': " + objectName );
743                     }
744                 }
745                 else if ( methodName.startsWith( "remove" ) &&
746                     ! methodName.endsWith( "Config" ) &&
747                     proxy instanceof AMXConfig )
748                 {
749                     if ( //method.getReturnType() == Void.class &&
750
method.getParameterTypes().length == 1 &&
751                             method.getParameterTypes()[ 0 ] == String JavaDoc.class &&
752                             ! method.getName().equals( "removeProperty" ) &&
753                             ! method.getName().equals( "removeSystemProperty" ) &&
754                             (! (proxy instanceof SecurityMapConfig)) )
755                     {
756                         trace( "WARNING: method " + methodName + " does not end in 'Config': " + methodName );
757                     }
758                 }
759             }
760         }
761     }
762     
763             
764         public void
765     testCreateRemoveGet()
766         throws Exception JavaDoc
767     {
768         testAll( "checkCreateRemoveGet" );
769     }
770     
771     /**
772         Verify:
773         <ul>
774         <li>if the interface name ends in "Config" or "ConfigMgr", then is is an AMXConfig</li>
775         </ul>
776      */

777         public void
778     checkImplementsAMXConfig( final ObjectName JavaDoc objectName )
779         throws Exception JavaDoc
780     {
781         final AMX proxy = getProxyFactory().getProxy( objectName, AMX.class );
782         final String JavaDoc interfaceName = Util.getExtra( proxy ).getInterfaceName();
783         if ( interfaceName.endsWith( "Config" ) || interfaceName.endsWith( "ConfigMgr" ) )
784         {
785             if ( !( proxy instanceof AMXConfig) )
786             {
787                 trace( "WARNING: " + ClassUtil.stripPackageName( interfaceName ) + " does not implement AMXConfig" );
788             }
789         }
790     }
791     
792     
793     /**
794         A few items supply Map of things, but have no corresponding create/remove routines.
795      */

796         private boolean
797     ignoreCreateRemove(
798         final String JavaDoc j2eeType,
799         final String JavaDoc suggestedMethod )
800     {
801         boolean ignore = false;
802         
803         if ( j2eeType.equals( XTypes.DOMAIN_CONFIG ) )
804         {
805             if ( suggestedMethod.equals( "createServerConfig" ) ||
806                 suggestedMethod.equals( "createWebModuleConfig" ) ||
807                 suggestedMethod.equals( "createEJBModuleConfig" ) ||
808                 suggestedMethod.equals( "createJ2EEApplicationConfig" ) ||
809                 suggestedMethod.equals( "createRARModuleConfig" ) ||
810                 suggestedMethod.equals( "createAppClientModuleConfig" ) ||
811                 suggestedMethod.equals( "createNodeAgentConfig" ) ||
812                 false
813                  )
814             {
815                 ignore = true;
816             }
817         }
818         else if ( j2eeType.equals( XTypes.CLUSTERED_SERVER_CONFIG ) )
819         {
820             if ( suggestedMethod.equals( "createDeployedItemRefConfig" ) ||
821                 suggestedMethod.equals( "createResourceRefConfig" )
822                  )
823             {
824                 ignore = true;
825             }
826         }
827         
828         return( ignore );
829     }
830     
831     /**
832         Verify that all getAbcConfigMgr() calls return a non-null result.
833      */

834         public void
835     checkMapsHaveCreateRemove( final ObjectName JavaDoc objectName )
836         throws Exception JavaDoc
837     {
838         final AMX proxy = getProxyFactory().getProxy( objectName, AMX.class );
839         
840         if ( proxy instanceof Container && proxy.getGroup().equals( AMX.GROUP_CONFIGURATION ) )
841         {
842             final Extra extra = Util.getExtra( proxy );
843             final String JavaDoc[] attrNames = extra.getAttributeNames();
844             
845             for( int i = 0; i < attrNames.length; ++i )
846             {
847                 final String JavaDoc name = attrNames[ i ];
848                 
849                 final String JavaDoc SUFFIX = "ObjectNameMap";
850                 final String JavaDoc PREFIX = JMXUtil.GET;
851                 if ( name.endsWith( SUFFIX ) )
852                 {
853                     final String JavaDoc base = StringUtil.stripPrefixAndSuffix( name, PREFIX, SUFFIX );
854                     final String JavaDoc createName = "create" + base;
855                     final String JavaDoc removeName = "remove" + base;
856                     
857                     final String JavaDoc j2eeType = proxy.getJ2EEType();
858                     if ( ignoreCreateRemove( proxy.getJ2EEType(), createName ) )
859                     {
860                         continue;
861                     }
862                     
863                     final MBeanOperationInfo JavaDoc[] creates =
864                         JMXUtil.findOperations( extra.getMBeanInfo().getOperations(), createName );
865                     boolean haveCreate = false;
866                     for( int op = 0; op < creates.length; ++op )
867                     {
868                         final MBeanOperationInfo JavaDoc info = creates[ op ];
869                         if ( info.getReturnType().equals( ObjectName JavaDoc.class.getName() ) )
870                         {
871                             haveCreate = true;
872                             break;
873                         }
874                     }
875                     assert( haveCreate ) :
876                         "Missing operation " + createName + "() for " + objectName;
877                     
878                     final MBeanOperationInfo JavaDoc[] removes =
879                         JMXUtil.findOperations( extra.getMBeanInfo().getOperations(), removeName );
880                     boolean haveRemove = false;
881                     for( int op = 0; op < removes.length; ++op )
882                     {
883                         final MBeanOperationInfo JavaDoc info = removes[ op ];
884                         if ( info.getReturnType().equals( "void" ) &&
885                             info.getSignature().length <= 2)
886                         {
887                             haveRemove = true;
888                             break;
889                         }
890                     }
891                     assert( haveRemove ) :
892                         "Missing operation " + removeName + "() for " + objectName;
893                 }
894             }
895         }
896     }
897     
898         public void
899     testMapsHaveCreateRemove()
900         throws Exception JavaDoc
901     {
902         testAll( "checkMapsHaveCreateRemove" );
903     }
904     
905     
906         public void
907     testImplementsAMXConfig()
908         throws Exception JavaDoc
909     {
910         testAll( "checkImplementsAMXConfig" );
911     }
912     
913     
914     private static Set JavaDoc<Class JavaDoc> MON_IGNORE = GSetUtil.newUnmodifiableSet( new Class JavaDoc[]
915     {
916         JMXMonitorMgr.class,
917         AMXStringMonitor.class,
918         AMXCounterMonitor.class,
919         AMXGaugeMonitor.class,
920         
921         EJBModuleMonitor.class,
922         HTTPServiceMonitor.class,
923         ApplicationMonitor.class,
924     } );
925     
926     /**
927         Verify:
928         <ul>
929         <li>verify that if the interface name ends in "Monitor", then it is an AMX, Monitoring</li>
930         <li>verify that if the interface name ends in "MonitorMgr", then it is an Container</li>
931         </ul>
932      */

933         public void
934     testImplementsAMXMonitoring()
935         throws Exception JavaDoc
936     {
937         final TypeInfos infos = TypeInfos.getInstance();
938         
939         final Iterator JavaDoc iter = infos.getJ2EETypes().iterator();
940         while ( iter.hasNext() )
941         {
942             final TypeInfo info = infos.getInfo( (String JavaDoc)iter.next() );
943             final Class JavaDoc theInterface = info.getInterface();
944             final String JavaDoc interfaceName = theInterface.getName();
945             if ( ! MON_IGNORE.contains( theInterface ) )
946             {
947                 if ( interfaceName.endsWith( "Monitor" ) )
948                 {
949                     if ( ! Monitoring.class.isAssignableFrom( theInterface ) )
950                     {
951                         warning( ClassUtil.stripPackageName( interfaceName ) + " does not implement Monitoring" );
952                     }
953                 }
954                 else if ( interfaceName.endsWith( "MonitorMgr" ) )
955                 {
956                     if ( ! Container.class.isAssignableFrom( theInterface ) )
957                     {
958                         warning( ClassUtil.stripPackageName( interfaceName ) + " does not implement Container" );
959                     }
960                 }
961             }
962         }
963     }
964     
965         public void
966     testGetInterfaceName()
967         throws IOException JavaDoc, JMException JavaDoc
968     {
969         final Set JavaDoc<ObjectName JavaDoc> all = getQueryMgr().queryAllObjectNameSet();
970         
971         final MBeanServerConnection JavaDoc conn =
972             Util.getExtra( getDomainRoot() ).getConnectionSource().getExistingMBeanServerConnection();
973         
974         final Set JavaDoc<ObjectName JavaDoc> failedSet = new HashSet JavaDoc<ObjectName JavaDoc>();
975
976         for( final ObjectName JavaDoc objectName : all )
977         {
978             try
979             {
980                 final String JavaDoc value = (String JavaDoc)
981                     conn.getAttribute( objectName, AMXAttributes.ATTR_INTERFACE_NAME );
982                 assert( value != null );
983                 value.toString();
984             }
985             catch( AttributeNotFoundException JavaDoc e )
986             {
987                 warning( "Can't get InterfaceName for: " + objectName );
988                 failedSet.add( objectName );
989             }
990         }
991         
992         if ( failedSet.size() != 0 )
993         {
994             warning( "The following MBeans did not return the Attribute InterfaceName:\n" +
995                 CollectionUtil.toString( failedSet, "\n") );
996             assert( false );
997             throw new Error JavaDoc();
998         }
999     }
1000    
1001
1002        public void
1003    testInterfaceAgainstDelegate()
1004        throws Exception JavaDoc
1005    {
1006        final long start = now();
1007        final Set JavaDoc<AMX> all = getAllAMX();
1008        
1009        final MBeanServerConnection JavaDoc conn = getMBeanServerConnection();
1010        for( final AMX amx : all )
1011        {
1012            
1013            final String JavaDoc result = (String JavaDoc)
1014                conn.invoke( Util.getObjectName( amx ),
1015                    "checkInterfaceAgainstDelegate", null, null );
1016        }
1017        
1018        printElapsed( "testInterfaceAgainstDelegate", all.size(), start );
1019    }
1020    
1021        public void
1022    testMisc()
1023    {
1024        final long start = now();
1025        final Set JavaDoc<AMX> all = getAllAMX();
1026        
1027        for( final AMX amx : all )
1028        {
1029            amx.setMBeanLogLevel( amx.getMBeanLogLevel() );
1030            
1031            final ObjectName JavaDoc objectName = Util.getObjectName( amx );
1032            assert( objectName.getKeyProperty( AMX.NAME_KEY ) != null );
1033            assert( objectName.getKeyProperty( AMX.J2EE_TYPE_KEY ) != null );
1034        }
1035        
1036        printElapsed( "testMisc", all.size(), start );
1037    }
1038    
1039    
1040    
1041        public void
1042    testNoGoofyNames(
1043        final ObjectName JavaDoc objectName,
1044        final MBeanFeatureInfo JavaDoc[] featureInfos )
1045    {
1046        final Set JavaDoc<String JavaDoc> goofy = new HashSet JavaDoc<String JavaDoc>();
1047        
1048        for( final MBeanFeatureInfo JavaDoc info : featureInfos )
1049        {
1050            final String JavaDoc name = info.getName();
1051            
1052            if ( name.indexOf( "ObjectNameObjectName" ) >= 0 )
1053            {
1054                goofy.add( name );
1055            }
1056        }
1057        
1058        if ( goofy.size() != 0 )
1059        {
1060            assert( false ) : NEWLINE +
1061                "MBean " + objectName + " has the following goofy Attributes:" + NEWLINE +
1062                CollectionUtil.toString( goofy, NEWLINE );
1063        }
1064    }
1065    
1066        public void
1067    testNoGoofyNames()
1068    {
1069        final long start = now();
1070        final Set JavaDoc<AMX> all = getAllAMX();
1071        
1072        for( final AMX amx : all )
1073        {
1074            final ObjectName JavaDoc objectName = Util.getObjectName( amx );
1075            final MBeanInfo JavaDoc mbeanInfo = Util.getExtra( amx ).getMBeanInfo();
1076            
1077            testNoGoofyNames( objectName, mbeanInfo.getAttributes() );
1078            testNoGoofyNames( objectName, mbeanInfo.getOperations() );
1079        }
1080        
1081        printElapsed( "testNoGoofyNames", all.size(), start );
1082    }
1083    
1084    
1085        public void
1086    testToString()
1087    {
1088        final long start = now();
1089        final Set JavaDoc<AMX> all = getAllAMX();
1090        
1091        for( final AMX amx : all )
1092        {
1093            final AMXDebugStuff debug = getTestUtil().asAMXDebugStuff( amx );
1094            
1095            if ( debug != null )
1096            {
1097                final String JavaDoc s = debug.getImplString( true );
1098                assert( s.length() != 0 );
1099            }
1100        }
1101        
1102        printElapsed( "testToString", all.size(), start );
1103    }
1104}
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
Popular Tags