KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > j2ee > J2EETest


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.j2ee;
24
25 import java.io.IOException JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Collections JavaDoc;
32
33 import javax.management.ObjectName JavaDoc;
34
35 import com.sun.appserv.management.base.QueryMgr;
36 import com.sun.appserv.management.base.Util;
37 import com.sun.appserv.management.base.AMX;
38 import com.sun.appserv.management.base.XTypes;
39
40 import com.sun.appserv.management.config.AMXConfig;
41 import com.sun.appserv.management.config.ServerConfig;
42 import com.sun.appserv.management.config.ClusterConfig;
43
44 import com.sun.appserv.management.j2ee.J2EETypes;
45 import com.sun.appserv.management.j2ee.JVM;
46 import com.sun.appserv.management.j2ee.J2EEManagedObject;
47 import com.sun.appserv.management.j2ee.J2EEServer;
48 import com.sun.appserv.management.j2ee.J2EECluster;
49 import com.sun.appserv.management.j2ee.StateManageable;
50 import com.sun.appserv.management.j2ee.EventProvider;
51
52 import com.sun.appserv.management.monitor.Monitoring;
53
54 import com.sun.appserv.management.util.misc.GSetUtil;
55 import com.sun.appserv.management.util.misc.MapUtil;
56 import com.sun.appserv.management.util.misc.CollectionUtil;
57 import com.sun.appserv.management.util.misc.ExceptionUtil;
58
59
60 import com.sun.enterprise.management.AMXTestBase;
61 import com.sun.enterprise.management.Capabilities;
62
63
64 /**
65  */

66 public final class J2EETest extends AMXTestBase
67 {
68         public
69     J2EETest( )
70         throws IOException JavaDoc
71     {
72         turnOnMonitoring();
73     }
74     
75     private static boolean FAILURES_WARNED = false;
76     
77         public static Capabilities
78     getCapabilities()
79     {
80         return getOfflineCapableCapabilities( false );
81     }
82     
83     /**
84         Verify that there is one J2EEServer for each ServerConfig (standalone or not)
85      */

86         public void
87     testJ2EEServerMatchesServerConfig()
88     {
89         final Map JavaDoc<String JavaDoc,ServerConfig> serverConfigMap =
90             getDomainConfig().getServerConfigMap();
91             
92         final Map JavaDoc<String JavaDoc,J2EEServer> j2eeServerMap =
93             getDomainRoot().getJ2EEDomain().getJ2EEServerMap();
94         
95         assert( serverConfigMap.keySet().equals( j2eeServerMap.keySet() ) ) :
96             "ServerConfig names do not match J2EEServer names, ServerConfig names = " +
97             toString( serverConfigMap.keySet() ) + ", J2EEServer names = " +
98             toString( j2eeServerMap.keySet() );
99         
100     }
101     
102     
103     /**
104         Verify that there is one J2EEServer for each ServerConfig (standalone or not)
105      */

106         public void
107     testJ2EEClusterMatchesClusterConfig()
108     {
109         final Map JavaDoc<String JavaDoc,ClusterConfig> clusterConfigMap = getDomainConfig().getClusterConfigMap();
110         final Map JavaDoc<String JavaDoc,J2EECluster> j2eeClusterMap = getJ2EEDomain().getJ2EEClusterMap();
111         
112         assert( clusterConfigMap.keySet().equals( j2eeClusterMap.keySet() ) ) :
113             "ClusterConfig names do not match J2EECluster names, ClusterConfig names = " +
114             toString( clusterConfigMap.keySet() ) + ", J2EECluster names = " +
115             toString( j2eeClusterMap.keySet() );
116     }
117     
118         public void
119     testJVMs()
120     {
121         final QueryMgr queryMgr = getQueryMgr();
122         
123         final Set JavaDoc jvms = queryMgr.queryJ2EETypeSet( J2EETypes.JVM );
124         final Iterator JavaDoc iter = jvms.iterator();
125         
126         String JavaDoc lastVendor = null;
127         String JavaDoc lastVersion = null;
128         while ( iter.hasNext() )
129         {
130             final JVM jvm = (JVM)iter.next();
131             
132             // the ObjectName of the Node must match the String version for "node"
133
assert( jvm.getnode() != null );
134             
135             // the JVMs should all have the same vendor (presumably)
136
assert( jvm.getjavaVendor() != null );
137             if ( lastVendor == null )
138             {
139                 lastVendor = jvm.getjavaVendor();
140             }
141             else
142             {
143                 assert( lastVendor.equals( jvm.getjavaVendor() ) );
144             }
145             
146             // the JVMs should all have the same version (presumably)
147
assert( jvm.getjavaVersion() != null );
148             if ( lastVersion == null )
149             {
150                 lastVersion = jvm.getjavaVersion();
151             }
152             else
153             {
154                 assert( lastVersion.equals( jvm.getjavaVersion() ) );
155             }
156         }
157     }
158     
159         private boolean
160     appearsToBeDefaultWebModule( final String JavaDoc webModuleName )
161     {
162         return webModuleName.startsWith( "//" ) && webModuleName.endsWith( "/" );
163     }
164     
165     /**
166         Map from JSR77 j2eeType to our config j2eeType.
167      */

168     private static final Map JavaDoc<String JavaDoc,String JavaDoc> ToConfigMap = MapUtil.newMap( new String JavaDoc[]
169     {
170         J2EETypes.J2EE_DOMAIN, XTypes.DOMAIN_CONFIG,
171         J2EETypes.J2EE_CLUSTER, XTypes.CLUSTER_CONFIG,
172         J2EETypes.J2EE_SERVER, XTypes.STANDALONE_SERVER_CONFIG,
173         J2EETypes.JVM, XTypes.JAVA_CONFIG,
174         
175         J2EETypes.J2EE_APPLICATION, XTypes.J2EE_APPLICATION_CONFIG,
176         J2EETypes.EJB_MODULE, XTypes.EJB_MODULE_CONFIG,
177         J2EETypes.WEB_MODULE, XTypes.WEB_MODULE_CONFIG,
178         J2EETypes.APP_CLIENT_MODULE, XTypes.APP_CLIENT_MODULE_CONFIG,
179         
180         J2EETypes.JAVA_MAIL_RESOURCE, XTypes.MAIL_RESOURCE_CONFIG,
181         J2EETypes.JDBC_RESOURCE, XTypes.JDBC_RESOURCE_CONFIG,
182         J2EETypes.JNDI_RESOURCE, XTypes.JNDI_RESOURCE_CONFIG,
183         J2EETypes.WEB_SERVICE_ENDPOINT, XTypes.WEB_SERVICE_ENDPOINT_CONFIG,
184     }
185     );
186     
187         private static String JavaDoc
188     getConfigPeerJ2EEType( final String JavaDoc j2eeType )
189     {
190         return ToConfigMap.get( j2eeType );
191     }
192     
193         /**
194         Maps a j2eeType to its peer monitoring j2eeType
195      */

196     private static final Map JavaDoc<String JavaDoc,String JavaDoc> ToMonitorMap =
197         Collections.unmodifiableMap( MapUtil.newMap( new String JavaDoc[]
198     {
199         J2EETypes.J2EE_SERVER, XTypes.SERVER_ROOT_MONITOR,
200         J2EETypes.J2EE_APPLICATION, XTypes.APPLICATION_MONITOR,
201         
202         J2EETypes.WEB_MODULE, XTypes.WEB_MODULE_VIRTUAL_SERVER_MONITOR,
203         J2EETypes.SERVLET, XTypes.SERVLET_MONITOR,
204         
205         J2EETypes.EJB_MODULE, XTypes.EJB_MODULE_MONITOR,
206         J2EETypes.STATELESS_SESSION_BEAN, XTypes.STATELESS_SESSION_BEAN_MONITOR,
207         J2EETypes.STATEFUL_SESSION_BEAN, XTypes.STATEFUL_SESSION_BEAN_MONITOR,
208         J2EETypes.ENTITY_BEAN, XTypes.ENTITY_BEAN_MONITOR,
209         J2EETypes.MESSAGE_DRIVEN_BEAN, XTypes.MESSAGE_DRIVEN_BEAN_MONITOR,
210     }));
211     
212     // has a monitoring peer, but no Stats
213
private static final Set JavaDoc<String JavaDoc> HasNoStats =
214         GSetUtil.newUnmodifiableStringSet( J2EETypes.J2EE_SERVER );
215         
216         
217         protected String JavaDoc
218     getMonitoringPeerJ2EEType( final String JavaDoc j2eeType )
219     {
220         return ToMonitorMap.get( j2eeType );
221     }
222     
223         protected String JavaDoc
224     getMonitoringPeerProps( final J2EEManagedObject item )
225     {
226         final String JavaDoc j2eeType = item.getJ2EEType();
227         final String JavaDoc monitoringPeerJ2EEType = getMonitoringPeerJ2EEType( j2eeType );
228         final ObjectName JavaDoc objectName = Util.getObjectName( item );
229         
230         String JavaDoc props = null;
231         if ( monitoringPeerJ2EEType != null )
232         {
233             props = Util.makeRequiredProps( monitoringPeerJ2EEType, item.getName() );
234
235             for( final String JavaDoc propKey : ToMonitorMap.keySet() )
236             {
237                 final String JavaDoc value = objectName.getKeyProperty( propKey );
238                 if ( value != null )
239                 {
240                     final String JavaDoc prop =
241                         Util.makeProp( getMonitoringPeerJ2EEType( propKey ), value );
242                     props = Util.concatenateProps( props, prop );
243                 }
244             }
245         }
246         
247         return props;
248     }
249     
250         public void
251     testJ2EE()
252         throws ClassNotFoundException JavaDoc
253     {
254         final QueryMgr queryMgr = getQueryMgr();
255         
256         final Set JavaDoc<J2EEManagedObject> j2eeAll =
257             queryMgr.queryInterfaceSet( J2EEManagedObject.class.getName(), null);
258         
259         final Set JavaDoc<ObjectName JavaDoc> failedSet = new HashSet JavaDoc<ObjectName JavaDoc>();
260         final Set JavaDoc<ObjectName JavaDoc> noPeerSet = new HashSet JavaDoc<ObjectName JavaDoc>();
261         
262         for( final J2EEManagedObject item : j2eeAll )
263         {
264             final ObjectName JavaDoc objectName = Util.getObjectName( item );
265             assert( objectName.equals( Util.newObjectName( item.getobjectName() ) ) );
266             
267             final String JavaDoc j2eeType = item.getJ2EEType();
268             
269             if ( item.isstateManageable() )
270             {
271                 assert( item instanceof StateManageable );
272                 
273                 final StateManageable sm = (StateManageable)item;
274                 
275                 final int state = sm.getstate();
276                 assert( state == StateManageable.STATE_STARTING ||
277                     state == StateManageable.STATE_RUNNING ||
278                     state == StateManageable.STATE_STOPPING ||
279                     state == StateManageable.STATE_STOPPED ||
280                     state == StateManageable.STATE_FAILED );
281                 
282                 if ( state == StateManageable.STATE_RUNNING )
283                 {
284                     try
285                     {
286                         final long startTime = sm.getstartTime();
287                     
288                         // assume it was started less than 30 days ago
289
final long MILLIS_PER_DAY = 24L * 60L * 60L * 1000L;
290                         final long days30 = 30L * MILLIS_PER_DAY;
291                         if ( startTime < now() - days30 )
292                         {
293                             warning( "MBean " + quote( objectName ) +
294                             " claims a start time of " + new Date JavaDoc( startTime ) + ", which is more than 30 days prior to now = " +
295                                 new Date JavaDoc( now() ) );
296                             failedSet.add( objectName );
297                         }
298                     }
299                     catch( Exception JavaDoc e )
300                     {
301                         final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
302                         warning( "MBean " + quote( objectName ) +
303                         " is 'stateManageable' and in 'STATE_RUNNING', but could not supply Attribute 'startTime', " +
304                         "threw an exception of class " +
305                             rootCause.getClass().getName() );
306                             failedSet.add( objectName );
307                     }
308                 }
309             }
310             
311             if ( item.iseventProvider() )
312             {
313                 assert( item instanceof EventProvider );
314                 
315                 final EventProvider ep = (EventProvider)item;
316                 final String JavaDoc[] types = ep.gettypes();
317                 assert types != null :
318                     "Item claims to be EventProvider, but provides null 'types': " +
319                         toString( objectName );
320             }
321             
322             /*
323                  monitoring was enabled so monitoring peers should exist
324                  Can't just call isStatisticProvider(), since it will be false
325                  if the monitoring peer is null (correctly or incorrectly).
326              */

327             final String JavaDoc monitoringPeerJ2EEType = getMonitoringPeerJ2EEType( j2eeType );
328             final Monitoring monitoringPeer = item.getMonitoringPeer();
329             if ( monitoringPeerJ2EEType != null )
330             {
331                 // See if there actually is a monitoring peer, but null is being returned.
332
if ( monitoringPeer == null )
333                 {
334                     final String JavaDoc props = getMonitoringPeerProps( item );
335                     final Set JavaDoc<Monitoring> monitors = getQueryMgr().queryPropsSet( props );
336                     if ( monitors.size() != 0 )
337                     {
338                         warning( "MBean " + quote( objectName ) +
339                         " returned null for its monitoring peer, but found the following:" +
340                             NEWLINE +
341                             CollectionUtil.toString( Util.toObjectNames( monitors ), NEWLINE ) );
342                             
343                         failedSet.add( objectName );
344                     }
345                 }
346                 else
347                 {
348                     // we have a monitoring peer, verify that it states that it has
349
// statistics
350
if ( ! HasNoStats.contains( j2eeType ) )
351                     {
352                         assert item.isstatisticProvider() && item.isstatisticsProvider();
353                     }
354                 }
355             }
356             else
357             {
358                 // it has a monitoring peer
359
if ( item.isstatisticProvider() || item.isstatisticsProvider() )
360                 {
361                     warning( "MBean " + quote( objectName ) +
362                         " should not have its statisticProvider set to true" );
363                     failedSet.add( objectName );
364                 }
365             }
366             
367             
368             if ( item.isConfigProvider() )
369             {
370                 final AMXConfig config = item.getConfigPeer();
371                 
372                 if ( config == null )
373                 {
374                     // Some auto-generated items do not have config. See if it's there
375
final String JavaDoc props = Util.makeRequiredProps(
376                         getConfigPeerJ2EEType( j2eeType ), item.getName() );
377                     
378                     if ( getQueryMgr().queryPropsSet( props ).size() != 0 )
379                     {
380                         warning( "MBean " + quote( objectName ) +
381                         " has existing config peer, but returned null" );
382                         failedSet.add( objectName );
383                     }
384                 }
385             }
386         }
387         
388         if ( noPeerSet.size() != 0 )
389         {
390             warning( "The following MBeans do not have a Monitoring peer:" +
391                 NEWLINE + toString( noPeerSet ) );
392             
393         }
394                         
395         if( failedSet.size() != 0 )
396         {
397             failure( "Failures in the following " + failedSet.size() + " MBeans:\n" +
398                 toString( failedSet ) );
399         }
400     }
401 }
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
Popular Tags