KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
26
27 import java.util.Set JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.lang.reflect.Field JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Arrays JavaDoc;
35
36
37 import javax.management.MBeanServerConnection JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.management.Attribute JavaDoc;
40 import javax.management.JMException JavaDoc;
41 import javax.management.Notification JavaDoc;
42 import javax.management.NotificationListener JavaDoc;
43
44 import com.sun.appserv.management.base.AMX;
45 import com.sun.appserv.management.base.XTypes;
46 import com.sun.appserv.management.base.QueryMgr;
47 import com.sun.appserv.management.base.SystemInfo;
48 import com.sun.appserv.management.base.BulkAccess;
49 import com.sun.appserv.management.base.NotificationServiceMgr;
50 import com.sun.appserv.management.base.Util;
51 import com.sun.appserv.management.base.XTypesMapper;
52 import com.sun.appserv.management.j2ee.J2EETypesMapper;
53
54
55 import com.sun.appserv.management.DomainRoot;
56
57 import com.sun.appserv.management.config.AMXConfig;
58 import com.sun.appserv.management.config.DomainConfig;
59 import com.sun.appserv.management.config.ConfigConfig;
60 import com.sun.appserv.management.config.ModuleMonitoringLevelsConfig;
61 import com.sun.appserv.management.config.ModuleMonitoringLevelValues;
62 import com.sun.appserv.management.config.NodeAgentConfig;
63
64 import com.sun.appserv.management.client.ProxyFactory;
65 import com.sun.appserv.management.client.ConnectionSource;
66 import com.sun.appserv.management.client.AppserverConnectionSource;
67
68 import com.sun.appserv.management.j2ee.J2EEDomain;
69
70 import com.sun.appserv.management.util.jmx.MBeanServerConnectionConnectionSource;
71 import com.sun.appserv.management.util.jmx.JMXUtil;
72
73 import com.sun.appserv.management.util.stringifier.SmartStringifier;
74 import com.sun.appserv.management.util.jmx.stringifier.AttributeStringifier;
75
76 import com.sun.appserv.management.util.misc.StringUtil;
77 import com.sun.appserv.management.util.misc.ClassUtil;
78 import com.sun.appserv.management.util.misc.GSetUtil;
79 import com.sun.appserv.management.util.misc.CollectionUtil;
80 import com.sun.appserv.management.util.misc.ExceptionUtil;
81 import com.sun.appserv.management.util.misc.TypeCast;
82
83 import com.sun.enterprise.management.PropertyKeys;
84 import com.sun.enterprise.management.util.jmx.JMXTestBase;
85 import com.sun.enterprise.management.support.AMXDebugStuff;
86 import com.sun.enterprise.management.support.AMXDebugSupportMBean;
87
88 /**
89 Base class for testing the AMX API
90 */

91 public class AMXTestBase extends JMXTestBase
92 {
93     private ConnectionSource mConnectionSource;
94     private final DomainRoot mDomainRoot;
95     private final ProxyFactory mProxyFactory;
96     private final Set JavaDoc<ObjectName JavaDoc> mNotTested;
97     private final TestUtil mTestUtil;
98
99     /** config name for PE (non-EE) configuration */
100     protected static final String JavaDoc PE_CONFIG_NAME = "server-config";
101
102     private final static boolean WEB_MODULE_MONITOR_BROKEN = true;
103     
104     private static boolean MONITORING_ENABLED = false;
105     
106     protected static final String JavaDoc NEWLINE = System.getProperty( "line.separator" );
107     
108     
109         public
110     AMXTestBase( )
111     {
112         checkConnection( getConnection() );
113         
114         mNotTested = new HashSet JavaDoc<ObjectName JavaDoc>();
115         try
116         {
117             mConnectionSource =
118                     new MBeanServerConnectionConnectionSource( getConnection() );
119             
120             mProxyFactory = ProxyFactory.getInstance( mConnectionSource, true );
121             mDomainRoot = mProxyFactory.getDomainRoot();
122         }
123         catch( Exception JavaDoc e )
124         {
125             throw new RuntimeException JavaDoc( e.toString(), e );
126         }
127         
128         mTestUtil = new TestUtil( getDomainRoot() );
129     }
130     
131         protected Set JavaDoc<AMX>
132     getAllAMX()
133     {
134         return getTestUtil().getAllAMX();
135     }
136     
137         protected ModuleMonitoringLevelsConfig
138     getModuleMonitoringLevelsConfig( final String JavaDoc configName )
139     {
140         final ConfigConfig cc = configName == null ? getConfigConfig() : getConfigConfig( configName );
141         assert( cc != null );
142         
143         final ModuleMonitoringLevelsConfig mon =
144             cc.getMonitoringServiceConfig().getModuleMonitoringLevelsConfig();
145         
146         return( mon );
147     }
148     
149         protected ModuleMonitoringLevelsConfig
150     getModuleMonitoringLevelsConfig( )
151     {
152         return getModuleMonitoringLevelsConfig( null );
153     }
154     
155     /**
156         Ensure that monitoring is enabled so that unit tests don't miss anything
157      */

158         protected synchronized void
159     turnOnMonitoring()
160     {
161         if ( ! MONITORING_ENABLED )
162         {
163             synchronized( AMXTestBase.class )
164             {
165                 final String JavaDoc[] configNames = getConfigNames();
166                 for( int i = 0; i < configNames.length; ++i )
167                 {
168                     getModuleMonitoringLevelsConfig( configNames[ i ] ).changeAll( ModuleMonitoringLevelValues.HIGH );
169                 }
170                 
171                 MONITORING_ENABLED = true;
172             }
173         }
174     }
175     
176         protected synchronized void
177     turnOffMonitoring()
178     {
179         synchronized( AMXTestBase.class )
180         {
181             getModuleMonitoringLevelsConfig().changeAll( ModuleMonitoringLevelValues.OFF );
182             MONITORING_ENABLED = false;
183         }
184     }
185     
186     private static final Set JavaDoc<String JavaDoc> EXPECTED_REMOTE_INCOMPLETE_TYPES =
187         GSetUtil.newUnmodifiableStringSet( XTypes.CALL_FLOW_MONITOR, XTypes.LOGGING );
188         
189         protected boolean
190     isRemoteIncomplete( final ObjectName JavaDoc objectName )
191     {
192        final AMX amx = getProxyFactory().getProxy( objectName, AMX.class );
193        final String JavaDoc j2eeType = amx.getJ2EEType();
194        
195        final boolean isRemote = ! amx.isDAS();
196        
197        if ( isRemote &&
198          ! EXPECTED_REMOTE_INCOMPLETE_TYPES.contains( j2eeType ) )
199        {
200             warning( "isRemoteIncomplete: not expecting j2eeType=" + j2eeType +
201                 ", has the implementation changed?" );
202        }
203        
204        return isRemote;
205     }
206     
207     
208         protected boolean
209     shouldTest( final ObjectName JavaDoc objectName )
210     {
211         boolean shouldTest = ! isRemoteIncomplete( objectName );
212         
213         return( shouldTest );
214     }
215
216         protected void
217     waitUnregistered( final ObjectName JavaDoc objectName )
218         throws IOException JavaDoc
219     {
220         final MBeanServerConnection JavaDoc conn = getConnection();
221         if ( conn.isRegistered( objectName ) )
222         {
223             mySleep( 100 );
224         }
225
226         while ( conn.isRegistered( objectName ) )
227         {
228             trace( "waitUnregistered: " + objectName);
229             mySleep( 100 );
230         }
231     }
232
233
234     /**
235         Because proxies are cleaned up asynchronously, it's possible for one
236         to remain in the factory until the factory processes the Notification
237         that it's MBean has been unregistered.
238      */

239         protected void
240     waitProxyGone(
241         final ProxyFactory factory,
242         final ObjectName JavaDoc objectName )
243     {
244         long millis = 1;
245         
246         while ( factory.getProxy( objectName, AMX.class, false ) != null )
247         {
248             mySleep( millis );
249             millis *= 2;
250             trace( "waitProxyGone: waiting for proxy to disappear: " + objectName );
251         }
252     }
253
254         protected final void
255     notTested( final ObjectName JavaDoc objectName )
256     {
257         if ( isRemoteIncomplete( objectName ) )
258         {
259             trace( "remoteIncomplete (this is OK): " + objectName );
260         }
261         else
262         {
263             mNotTested.add( objectName );
264         }
265     }
266
267         protected final Set JavaDoc<ObjectName JavaDoc>
268     getNotTestedSet( )
269     {
270         return( mNotTested );
271     }
272
273         public static void
274     mySleep( final long millis )
275     {
276         try
277         {
278             Thread.sleep( millis );
279         }
280         catch( InterruptedException JavaDoc e )
281         {
282         }
283     }
284
285         protected final void
286     warnNotTested( )
287     {
288         final Set JavaDoc<ObjectName JavaDoc> notTested = getNotTestedSet();
289         
290         if ( notTested.size() != 0 )
291         {
292             final Set JavaDoc j2eeTypes =
293                 JMXUtil.getKeyPropertySet( AMX.J2EE_TYPE_KEY, notTested );
294             
295             trace( "WARNING: DID NOT TEST: " + notTested.size() + " MBeans of types {" +
296                 toString( j2eeTypes ) + "}" );
297         }
298     }
299     
300
301
302         protected void
303     checkConnection( final MBeanServerConnection JavaDoc conn )
304     {
305         assert( getConnection() != null );
306         
307         try
308         {
309             conn.isRegistered( JMXUtil.getMBeanServerDelegateObjectName() );
310         }
311         catch( Exception JavaDoc e )
312         {
313             fail( "Connection failed:\n" +
314                 ExceptionUtil.getStackTrace( getRootCause( e ) ) );
315         }
316     }
317     
318         protected void
319     checkConnection( )
320     {
321         checkConnection( getConnection( ) );
322     }
323     
324         protected TestUtil
325     getTestUtil()
326     {
327         return mTestUtil;
328     }
329
330
331         protected final AMX
332     getProxy( final ObjectName JavaDoc objectName )
333     {
334         final ProxyFactory factory = ProxyFactory.getInstance( getConnectionSource(), true);
335         
336         final AMX proxy = factory.getProxy( objectName, AMX.class );
337         
338         return( proxy );
339     }
340     
341     /**
342          We don't have T extend AMX because not all mixin interfaces extend AMX.
343      */

344         protected final <T> T
345     getProxy(
346         final ObjectName JavaDoc objectName,
347         final Class JavaDoc<T> theClass)
348     {
349         return( theClass.cast( getProxy(objectName) ) );
350     }
351
352
353         protected final DomainRoot
354     getDomainRoot()
355     {
356         assert( mDomainRoot != null ) : "mDomainRoot is null";
357         return( mDomainRoot );
358     }
359
360         protected final DomainConfig
361     getDomainConfig()
362     {
363         return( getDomainRoot().getDomainConfig() );
364     }
365     
366         protected final J2EEDomain
367     getJ2EEDomain()
368     {
369         return( getDomainRoot().getJ2EEDomain() );
370     }
371
372         protected String JavaDoc[]
373     getConfigNames()
374     {
375         final Map JavaDoc<String JavaDoc,ConfigConfig> configMap =
376             getDomainConfig().getConfigConfigMap();
377         
378         return( GSetUtil.toStringArray( configMap.keySet() ) );
379     }
380
381         protected ConfigConfig
382     getConfigConfig( final String JavaDoc name )
383     {
384         final Map JavaDoc<String JavaDoc,ConfigConfig> configs = getDomainConfig().getConfigConfigMap();
385
386         return configs.get( name == null ? PE_CONFIG_NAME : name );
387     }
388     
389         protected static ConfigConfig
390     getConfigConfig( final AMXConfig any )
391     {
392         final ObjectName JavaDoc objectName = Util.getObjectName( any );
393         final String JavaDoc configName = objectName.getKeyProperty( XTypes.CONFIG_CONFIG );
394     
395         return any.getDomainRoot().getDomainConfig().getConfigConfigMap().get( configName );
396     }
397     
398         protected ConfigConfig
399     getConfigConfig( )
400     {
401         return( getConfigConfig( PE_CONFIG_NAME ) );
402     }
403
404
405         protected QueryMgr
406     getQueryMgr()
407     {
408         assert( mDomainRoot != null );
409         final QueryMgr proxy = getDomainRoot().getQueryMgr();
410         assert( proxy != null );
411         return( proxy );
412     }
413
414
415         protected NotificationServiceMgr
416     getNotificationServiceMgr()
417     {
418         return( getDomainRoot().getNotificationServiceMgr() );
419     }
420
421         protected BulkAccess
422     getBulkAccess()
423     {
424         return( getDomainRoot().getBulkAccess() );
425     }
426
427         protected ConnectionSource
428     getConnectionSource()
429     {
430         assert( mConnectionSource != null );
431         return( mConnectionSource );
432     }
433
434         protected MBeanServerConnection JavaDoc
435     getConnection()
436     {
437         return( getGlobalConnection() );
438     }
439
440         protected ProxyFactory
441     getProxyFactory()
442     {
443         return( mProxyFactory );
444     }
445
446
447         protected Class JavaDoc
448     getInterfaceClass( AMX proxy )
449         throws ClassNotFoundException JavaDoc
450     {
451         final String JavaDoc name = Util.getExtra( proxy ).getInterfaceName();
452         return( ClassUtil.getClassFromName( name ) );
453     }
454         
455         protected <T extends AMX> boolean
456     testOnProxies(
457         final Collection JavaDoc<T> proxies,
458         final Method JavaDoc method )
459         throws Exception JavaDoc
460     {
461         final long start = now();
462         
463         boolean failed = false;
464         
465         int testCount = 0;
466         
467         final Object JavaDoc[] args = new Object JavaDoc[ 1 ];
468         for( final T proxy : proxies )
469         {
470             final ObjectName JavaDoc objectName = Util.getExtra( proxy ).getObjectName();
471             
472             if ( ! shouldTest( objectName ) )
473             {
474                 notTested( objectName );
475                 continue;
476             }
477
478             ++testCount;
479             try
480             {
481                 args[ 0 ] = proxy;
482                 method.invoke( this, args );
483             }
484             catch( Exception JavaDoc e )
485             {
486                 trace( method.getName() + " failed for proxy: " +
487                     quote( JMXUtil.toString( objectName ) ) );
488                 failed = true;
489                 trace( ExceptionUtil.toString( e ) );
490             }
491         }
492         
493         
494         final long elapsed = now() - start;
495         printVerbose( "Ran test method " + method.getName() + " on " + testCount +
496             " candidates in " + elapsed + "ms");
497         warnNotTested();
498         
499         warnNotTested();
500         
501         return( ! failed );
502     }
503
504
505         protected boolean
506     testOnObjectNames(
507         final Collection JavaDoc<ObjectName JavaDoc> objectNames,
508         final Method JavaDoc method )
509         throws Exception JavaDoc
510     {
511         boolean failed = false;
512         
513         final Object JavaDoc[] args = new Object JavaDoc[ 1 ];
514         
515         int testCount = 0;
516         final long start = now();
517         
518         for( final ObjectName JavaDoc objectName : objectNames )
519         {
520             if ( ! shouldTest( objectName ) )
521             {
522                 notTested( objectName );
523                 continue;
524             }
525
526             ++testCount;
527             try
528             {
529                 args[ 0 ] = objectName;
530                 method.invoke( this, args );
531             }
532             catch( Exception JavaDoc e )
533             {
534                 final Throwable JavaDoc rootCause = getRootCause( e );
535                 trace( method.getName() + " failed for: " +
536                     quote( JMXUtil.toString( objectName ) ) + " with Exception of type " +
537                         rootCause.getClass().getName() + ", msg = " + rootCause.getMessage() );
538                 failed = true;
539             }
540         }
541         
542         final long elapsed = now() - start;
543         printVerbose( "Ran test method " + method.getName() + " on " + testCount +
544             " candidates in " + elapsed + "ms");
545         warnNotTested();
546         
547         return( ! failed );
548     }
549
550
551         
552     protected final static Class JavaDoc[] OBJECTNAME_SIG = new Class JavaDoc[] { ObjectName JavaDoc.class };
553     protected final static Class JavaDoc[] PROXY_SIG = new Class JavaDoc[] { AMX.class };
554
555         protected void
556     testAll( final Collection JavaDoc<ObjectName JavaDoc> objectNames, final String JavaDoc methodName )
557         throws Exception JavaDoc
558     {
559         final boolean success = testOnObjectNames( objectNames,
560                     this.getClass().getMethod( methodName, OBJECTNAME_SIG ) );
561         
562         assert( success );
563     }
564
565         protected <T extends AMX> void
566     testAllProxies( final Collection JavaDoc<T> proxies, final String JavaDoc methodName )
567         throws Exception JavaDoc
568     {
569         final boolean success = testOnProxies( proxies,
570                     this.getClass().getMethod( methodName, PROXY_SIG ) );
571         
572         assert( success );
573     }
574
575         protected void
576     testAll( String JavaDoc methodName )
577         throws Exception JavaDoc
578     {
579         final Set JavaDoc<ObjectName JavaDoc> names = getTestUtil().getAllObjectNames();
580         
581         testAll( names, methodName );
582     }
583
584         public void
585     setUp() throws Exception JavaDoc
586     {
587         super.setUp();
588                     
589         turnOnMonitoring();
590     }
591     
592         public void
593     testAssertsOn()
594     {
595         checkAssertsOn();
596     }
597     
598     private static final String JavaDoc DEFAULT_INSTANCE_NAME = "test";
599     
600         protected static String JavaDoc
601     getDefaultInstanceName( final String JavaDoc qualifier )
602     {
603         String JavaDoc name = null;
604         
605         if ( qualifier == null )
606         {
607             name = DEFAULT_INSTANCE_NAME;
608         }
609         else
610         {
611             name = qualifier + "." + DEFAULT_INSTANCE_NAME;
612         }
613         return name;
614     }
615     
616         protected Throwable JavaDoc
617     getRootCause( Throwable JavaDoc t )
618     {
619         return ExceptionUtil.getRootCause( t );
620     }
621     
622         protected String JavaDoc
623     getStackTrace( Throwable JavaDoc t )
624     {
625         return ExceptionUtil.getStackTrace( t );
626     }
627     
628         protected String JavaDoc
629     getRootCauseStackTrace( Throwable JavaDoc t )
630     {
631         return getStackTrace( getRootCause( t ) );
632     }
633     
634         protected Map JavaDoc<String JavaDoc,AppserverConnectionSource>
635     getNodeAgents()
636     {
637        final Map JavaDoc<?,?> m = Map JavaDoc.class.cast( getEnvValue( PropertyKeys.NODE_AGENTS_KEY ) );
638        return TypeCast.checkedMap( m,
639                 String JavaDoc.class, AppserverConnectionSource.class);
640     }
641     
642     
643         protected String JavaDoc
644     getDASNodeAgentName()
645     {
646         return getEnvString( PropertyKeys.DAS_NODE_AGENT_NAME, null);
647     }
648     
649         protected NodeAgentConfig
650     getDASNodeAgentConfig()
651     {
652        final String JavaDoc name = getDASNodeAgentName();
653        
654        NodeAgentConfig config = null;
655        if ( name != null )
656        {
657             config = getDomainConfig().getNodeAgentConfigMap().get( name );
658        }
659        
660        return config;
661     }
662     
663         protected boolean
664     getTestOffline()
665     {
666         return getEnvBoolean( PropertyKeys.TEST_OFFLINE_KEY, false );
667     }
668     
669     /**
670         Check if we're testing in Offline mode, which means that
671         Config MBeans are loaded in-process. If so, issue a warning.
672         
673         @return true if test should be run, false if in offline mode
674      */

675         protected boolean
676     checkNotOffline( final String JavaDoc testName )
677     {
678         boolean offline = getTestOffline();
679         
680         if ( offline )
681         {
682             //warning( "amxtest.testOffline=true, skipping test " + testName + "()" );
683
}
684         
685         return ! offline;
686     }
687     
688     
689         public static Capabilities
690     getDefaultCapabilities()
691     {
692         return getOfflineCapableCapabilities( true );
693     }
694     
695         protected static Capabilities
696     getOfflineCapableCapabilities( boolean offlineCapable )
697     {
698         final Capabilities c = new Capabilities( );
699         c.setOfflineCapable( offlineCapable );
700         
701         return c;
702     }
703     
704         AMXDebugSupportMBean
705     getAMXDebugSupportMBean()
706     {
707         final ObjectName JavaDoc objectName = Util.newObjectName( AMXDebugSupportMBean.OBJECT_NAME );
708         
709         try
710         {
711         return (AMXDebugSupportMBean)newProxy( objectName, AMXDebugSupportMBean.class );
712         }
713         catch( Exception JavaDoc e )
714         {
715             assert false : "Can't get proxy to " + objectName;
716         }
717         return null;
718     }
719     
720         protected boolean
721     supportsMultipleServers()
722     {
723         return supportsMultipleServers( getDomainRoot() );
724     }
725     
726         public static boolean
727     supportsMultipleServers( final DomainRoot domainRoot )
728     {
729         return domainRoot.getSystemInfo().supportsFeature( SystemInfo.MULTIPLE_SERVERS_FEATURE );
730     }
731
732     
733         protected boolean
734     supportsClusters()
735     {
736         return getDomainRoot().getSystemInfo().supportsFeature( SystemInfo.CLUSTERS_FEATURE );
737     }
738 }
739
740
741
742
743
744
745
746
747
748
749
Popular Tags