KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > monitor > ComSunAppservMonitorTest


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.monitor;
24
25 import javax.management.ObjectName JavaDoc;
26
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Arrays JavaDoc;
34
35 import java.io.IOException JavaDoc;
36
37 import javax.management.MBeanInfo JavaDoc;
38 import javax.management.MBeanOperationInfo JavaDoc;
39 import javax.management.j2ee.statistics.Statistic JavaDoc;
40 import javax.management.j2ee.statistics.Stats JavaDoc;
41 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
42 import javax.management.j2ee.statistics.BoundaryStatistic JavaDoc;
43 import javax.management.j2ee.statistics.RangeStatistic JavaDoc;
44 import javax.management.j2ee.statistics.BoundedRangeStatistic JavaDoc;
45 import javax.management.j2ee.statistics.TimeStatistic JavaDoc;
46
47 import com.sun.appserv.management.util.j2ee.J2EEUtil;
48 import com.sun.appserv.management.util.misc.CollectionUtil;
49 import com.sun.appserv.management.util.misc.ExceptionUtil;
50 import com.sun.appserv.management.util.misc.GSetUtil;
51 import com.sun.appserv.management.util.misc.StringUtil;
52 import com.sun.appserv.management.util.stringifier.ArrayStringifier;
53 import com.sun.appserv.management.util.jmx.JMXUtil;
54
55 import com.sun.appserv.management.j2ee.statistics.*;
56 import com.sun.appserv.management.base.Util;
57
58 import com.sun.enterprise.management.support.ComSunAppservTest;
59 import com.sun.enterprise.management.support.OldMonitorTypes;
60
61 /**
62     Unit test for the com.sun.appserv monitoring MBeans relied
63     upon by AMX.
64  */

65 public final class ComSunAppservMonitorTest extends ComSunAppservTest
66 {
67         public
68     ComSunAppservMonitorTest()
69     {
70     }
71     
72      
73     private interface MonitorIntf
74     {
75         public String JavaDoc[] getStatisticNames() throws Exception JavaDoc;
76         public Statistic JavaDoc[] getStatistics() throws Exception JavaDoc;
77     };
78     
79     private final class MonitorImpl implements MonitorIntf
80     {
81         final ObjectName JavaDoc mObjectName;
82         
83             public
84         MonitorImpl(
85             final ObjectName JavaDoc objectName )
86         {
87             mObjectName = objectName;
88         }
89         
90             public String JavaDoc[]
91         getStatisticNames()
92             throws Exception JavaDoc
93         {
94             final String JavaDoc[] statisticNames =
95                 (String JavaDoc[])getMBeanServerConnection().invoke( mObjectName, "getStatisticNames", null, null );
96             
97             return statisticNames;
98         }
99         
100             public Statistic JavaDoc[]
101         getStatistics()
102             throws Exception JavaDoc
103         {
104             final Statistic JavaDoc[] statistics =
105                 (Statistic JavaDoc[])getMBeanServerConnection().invoke( mObjectName, "getStatistics", null, null );
106             
107             return statistics;
108         }
109     };
110     
111         private MonitorIntf
112     getMonitorIntf( final ObjectName JavaDoc objectName )
113     {
114         final MonitorIntf intf = new MonitorImpl( objectName );
115         boolean basicsOK = true;
116         
117         if ( ! hasStatisticSupport( objectName ) )
118         {
119             warning( "MBean " + StringUtil.quote( objectName ) +
120                 " doesn't have getStatisticNames() and getStatistics() methods." );
121             return null;
122         }
123         
124         try
125         {
126             final String JavaDoc[] statisticNames = intf.getStatisticNames();
127             if ( statisticNames == null )
128             {
129                 warning( "MBean " + StringUtil.quote( objectName ) +
130                     " returned null from getStatisticNames()" );
131                 basicsOK = false;
132             }
133             if ( statisticNames.length == 0 )
134             {
135                 warning( "MBean " + StringUtil.quote( objectName ) +
136                     " returned an empty String[] from getStatisticNames()" );
137                 basicsOK = false;
138             }
139         }
140         catch( Exception JavaDoc e )
141         {
142             final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
143             
144             warning( "MBean " + StringUtil.quote( objectName ) +
145                 " threw an exception from getStatisticNames(): " + rootCause );
146             basicsOK = false;
147         }
148         
149         if ( basicsOK )
150         {
151             try
152             {
153                 final Statistic JavaDoc[] statistics = intf.getStatistics();
154                 if ( statistics == null )
155                 {
156                     warning( "MBean " + StringUtil.quote( objectName ) +
157                         " returned null from getStatistics()" );
158                     basicsOK = false;
159                 }
160                 if ( statistics != null && statistics.length == 0 )
161                 {
162                     warning( "MBean " + StringUtil.quote( objectName ) +
163                         " returned an empty Statistic[] from getStatistics()" );
164                     basicsOK = false;
165                 }
166             }
167             catch( Exception JavaDoc e )
168             {
169                 final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
170             
171                 warning( "MBean " + StringUtil.quote( objectName ) +
172                     " threw an exception from getStatistics(): " + rootCause );
173                 basicsOK = false;
174             }
175         }
176         
177         return basicsOK ? intf : null;
178     }
179
180
181         private boolean
182     checkMonitor(final ObjectName JavaDoc objectName )
183         throws Exception JavaDoc
184     {
185         boolean worksOK = true;
186         
187         final MonitorIntf intf = getMonitorIntf( objectName );
188         if ( intf != null )
189         {
190             final String JavaDoc[] statisticNames = intf.getStatisticNames();
191             final Set JavaDoc<String JavaDoc> statisticNamesSet = GSetUtil.newStringSet( statisticNames );
192             
193             // get all names as defined by Statistics
194
final Statistic JavaDoc[] statistics = intf.getStatistics();
195             final Set JavaDoc<String JavaDoc> statisticNamesFromStatisticsSet = new HashSet JavaDoc<String JavaDoc>();
196             for( final Statistic JavaDoc s : statistics )
197             {
198                 statisticNamesFromStatisticsSet.add( s.getName() );
199             }
200             
201             if ( ! statisticNamesSet.equals( statisticNamesFromStatisticsSet ) )
202             {
203                 final String JavaDoc[] statisticNamesFromStatistics =
204                     GSetUtil.toStringArray( statisticNamesFromStatisticsSet );
205                 
206                 Arrays.sort( statisticNames );
207                 Arrays.sort( statisticNamesFromStatistics );
208                 
209                 printVerbose( "WARNING: MBean " + StringUtil.quote( objectName ) +
210                     " returns Statistic names from getStatisticNames() " +
211                     "that disagree with the names actually " +
212                     "found in Statistics from getStatistics(): " +
213                     "getStatisticNames() = {" + ArrayStringifier.stringify( statisticNames, "," ) +
214                     "}, getStatistics() = {" +
215                         ArrayStringifier.stringify( statisticNamesFromStatistics, "," ) + "}" );
216                 worksOK = false;
217             }
218         }
219         
220         return( worksOK );
221     }
222     
223     
224     /**
225         private boolean
226    shouldBeTested( final ObjectName objectName )
227         throws Exception
228    {
229         final MBeanInfo mbeanInfo = getMBeanServerConnection().getMBeanInfo( objectName );
230         boolean shouldTest = false;
231         
232         final MBeanOperationInfo[] candidates = mbeanInfo.getOperations();
233         if ( JMXUtil.findOperations( candidates, "getStatisticNames" ).length != 0 &&
234              JMXUtil.findOperations( candidates, "getStatistics" ).length != 0 )
235         {
236             shouldTest = true;
237         }
238         
239         return shouldTest;
240    }
241    */

242    
243    /**
244      types to be tested as in "category=monitor,type=xxx". We can't just test all
245      category=monitor, or even check that getStatistics() and getStatisticNames()
246      are present, as many such MBeans don't work at all.
247      
248      Types taken from com.sun.enterprise.management.support.initMap().
249      */

250     private static final Set JavaDoc<String JavaDoc> COM_SUN_APPSERV_MONITOR_TYPES =
251         GSetUtil.newUnmodifiableStringSet(
252             "jvm",
253             "ejb",
254             "standalone-ejb-module",
255             "bean-pool",
256             "bean-cache",
257             "bean-method",
258             "servlet",
259             "virtual-server",
260             "webmodule-virtual-server",
261             "http-listener",
262             "transaction-service",
263             "thread-pool",
264             "connection-manager",
265             "jdbc-connection-pool",
266             "connector-connection-pool",
267             "file-cache",
268             "keep-alive",
269             "dns",
270             "connection-queue",
271             "webservice-endpoint" );
272         
273         
274         private boolean
275    shouldBeTested( final ObjectName JavaDoc objectName )
276         throws Exception JavaDoc
277    {
278         final String JavaDoc type = objectName.getKeyProperty( "type" );
279         
280         return COM_SUN_APPSERV_MONITOR_TYPES.contains( type );
281    }
282    
283           private boolean
284     hasStatisticSupport( final ObjectName JavaDoc objectName )
285     {
286         boolean hasSupport = false;
287         
288         try
289         {
290             final MBeanInfo JavaDoc mbeanInfo = getMBeanServerConnection().getMBeanInfo( objectName );
291             boolean shouldTest = false;
292             
293             final MBeanOperationInfo JavaDoc[] candidates = mbeanInfo.getOperations();
294             if ( JMXUtil.findOperations( candidates, "getStatisticNames" ).length != 0 &&
295                  JMXUtil.findOperations( candidates, "getStatistics" ).length != 0 )
296             {
297                 hasSupport = true;
298             }
299         }
300         catch( Exception JavaDoc e )
301         {
302             final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
303             
304             warning( "hasStatisticSupport: got exception: " + rootCause );
305         }
306         
307         return hasSupport;
308     }
309     
310         public void
311     testAllMonitor()
312         throws Exception JavaDoc
313     {
314         try
315         {
316             Class.forName( "com.sun.enterprise.admin.monitor.stats.CountStatisticImpl" );
317         }
318         catch( ClassNotFoundException JavaDoc e )
319         {
320             failure( "ComSunAppservMonitorTest.testAllMonitor: " +
321             "CLASSPATH is missing Statistic classes, skipping tests. " +
322             "Use 'maven run-tests' instead of 'ant run-tests'" );
323             return;
324         }
325         
326         final Map JavaDoc<String JavaDoc,ObjectName JavaDoc> m = getAllComSunAppservMonitor();
327         
328         final Collection JavaDoc<ObjectName JavaDoc> objectNames = m.values();
329         final Set JavaDoc<ObjectName JavaDoc> defective = new HashSet JavaDoc<ObjectName JavaDoc>();
330         int testedCount = 0;
331         for( final ObjectName JavaDoc objectName : objectNames )
332         {
333             if ( ! shouldBeTested( objectName ) )
334             {
335                 continue;
336             }
337             
338             ++testedCount;
339             
340             if ( ! checkMonitor( objectName ) )
341             {
342                 defective.add( objectName );
343             }
344         }
345         
346         printVerbose( "ComSunAppservMonitorTest.testAllMonitor: checked " +
347             testedCount + " com.sun.appserv:category=monitor MBeans for basic functionality, " +
348             defective.size() + " failures." );
349             
350         if ( defective.size() != 0 )
351         {
352             // slim down the ObjectName for better readability
353
final String JavaDoc[] names = new String JavaDoc[ defective.size() ];
354             int i = 0;
355             for( final ObjectName JavaDoc objectName : defective )
356             {
357                 names[ i ] = objectName.getCanonicalKeyPropertyListString();
358                 ++i;
359             }
360             
361             Arrays.sort( names );
362             
363             final boolean verbose = getVerbose();
364             
365             warning( "The following " + defective.size() +
366                 " com.sun.appserv MBeans don't work correctly, so " +
367                 "subsequent tests (eg J2EETest) may fail:\n" +
368                 ArrayStringifier.stringify( names, "\n") +
369                 (verbose ? "" : "\n(set amxtest.verbose=true for details)") );
370         }
371     }
372     
373 }
374
375
376
377
378
379
380
Popular Tags