KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > j2ee > J2EEUtil


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.appserv.management.util.j2ee;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collections JavaDoc;
32
33 import java.lang.reflect.Method JavaDoc;
34
35 import javax.management.openmbean.CompositeType JavaDoc;
36 import javax.management.openmbean.CompositeData JavaDoc;
37 import javax.management.openmbean.CompositeDataSupport JavaDoc;
38 import javax.management.openmbean.OpenType JavaDoc;
39 import javax.management.openmbean.OpenDataException JavaDoc;
40
41 import javax.management.j2ee.statistics.*;
42
43 import com.sun.appserv.management.util.jmx.OpenMBeanUtil;
44 import com.sun.appserv.management.util.misc.GSetUtil;
45 import com.sun.appserv.management.util.misc.CollectionUtil;
46 import com.sun.appserv.management.util.jmx.JMXUtil;
47
48 import com.sun.appserv.management.j2ee.statistics.MapStatistic;
49 import com.sun.appserv.management.j2ee.statistics.StringStatistic;
50 import com.sun.appserv.management.j2ee.statistics.NumberStatistic;
51
52 /**
53     J2EE JSR 77 utilities
54  */

55 public class J2EEUtil
56 {
57     private final static String JavaDoc GET = "get";
58     
59     private J2EEUtil() {}
60     
61     
62     private static final Set JavaDoc<String JavaDoc> IGNORE =
63         GSetUtil.newUnmodifiableStringSet( "getClass" );
64     
65     /**
66         Convert the Statistic into a Map containing all the name/value pairs
67         obtained by finding method names that match a getter pattern
68         getXXX(). Any Statistic can thus be converted into a Map which preserves
69         all its members.
70         
71         @param statistic any Statistic or subclass
72      */

73         public static Map JavaDoc<String JavaDoc,Object JavaDoc>
74     statisticToMap( Statistic statistic )
75     {
76         final Map JavaDoc<String JavaDoc,Object JavaDoc> result = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
77         
78         if ( statistic instanceof MapStatistic )
79         {
80             result.putAll( ((MapStatistic)statistic).asMap() );
81         }
82         else
83         {
84             // get all public methods
85
final Method JavaDoc[] methods = statistic.getClass().getMethods();
86
87             for( int i = 0; i < methods.length; ++i )
88             {
89                 final Method JavaDoc method = methods[ i ];
90                 final String JavaDoc methodName = method.getName();
91                 
92                 if ( methodName.startsWith( GET ) &&
93                         ! IGNORE.contains( methodName ) &&
94                         method.getParameterTypes().length == 0 &&
95                         method.getExceptionTypes().length == 0 &&
96                         method.getReturnType() != Void JavaDoc.class )
97                 {
98                     try
99                     {
100                         final Object JavaDoc value = method.invoke( statistic, (Object JavaDoc[])null );
101                         
102                         final String JavaDoc name = methodName.substring( GET.length(), methodName.length() );
103                         result.put( name, value );
104                     }
105                     catch( Exception JavaDoc e )
106                     {
107                         // ignore
108
}
109                 }
110             }
111         }
112
113         return( result );
114     }
115     
116     
117     /**
118         Get the type name for a Statistic.
119      */

120         public static String JavaDoc
121     getStatisticType( Statistic statistic )
122     {
123         String JavaDoc type = getType( STATISTIC_CLASSES, statistic );
124         
125         if ( type == null )
126         {
127             type = statistic.getClass().getName();
128         }
129
130         return( type );
131     }
132     
133     
134     /**
135         Get the type matching the object.
136      */

137         private static String JavaDoc
138     getType( Class JavaDoc[] classes, Object JavaDoc o )
139     {
140         String JavaDoc type = null;
141         
142         for( int i = 0; i < classes.length; ++i )
143         {
144             final Class JavaDoc theClass = classes[ i ];
145             
146             if ( theClass.isInstance( o ) )
147             {
148                 type = theClass.getName();
149                 break;
150             }
151         }
152         return( type );
153     }
154     
155     private static final Class JavaDoc[] STATISTIC_CLASSES =
156     {
157         BoundaryStatistic.class,
158         BoundedRangeStatistic.class,
159         TimeStatistic.class,
160         RangeStatistic.class,
161         CountStatistic.class,
162         StringStatistic.class,
163         NumberStatistic.class,
164         MapStatistic.class,
165     };
166     
167     
168     
169         private static CompositeType JavaDoc
170     statisticMapToCompositeType( final String JavaDoc statisticType, final Map JavaDoc<String JavaDoc,?> map )
171         throws OpenDataException JavaDoc
172     {
173         final String JavaDoc description = "J2EE management statistic " + statisticType;
174         
175         return( OpenMBeanUtil.mapToCompositeType( statisticType, description, map, null) );
176     }
177     
178     
179     /**
180         Get the JMX OpenMBean CompositeType corresponding to the Statistic.
181      */

182         public static CompositeType JavaDoc
183     statisticToCompositeType( Statistic statistic )
184         throws OpenDataException JavaDoc
185     {
186         final String JavaDoc statisticType = getStatisticType( statistic );
187         final Map JavaDoc<String JavaDoc,Object JavaDoc> map = statisticToMap( statistic );
188         
189         return( statisticMapToCompositeType( statisticType, map ) );
190     }
191     
192     /**
193         Convert a Statistic into a JMX CompositeDataSupport.
194         
195         @param statistic any Statistic or subclass
196      */

197         public static CompositeDataSupport JavaDoc
198     statisticToCompositeData( Statistic statistic )
199         throws OpenDataException JavaDoc
200     {
201         final String JavaDoc statisticType = getStatisticType( statistic );
202         
203         final Map JavaDoc<String JavaDoc,Object JavaDoc> map = statisticToMap( statistic );
204     
205         final CompositeType JavaDoc type = statisticMapToCompositeType( statisticType, map );
206         
207         return( new CompositeDataSupport JavaDoc( type, map ) );
208     }
209     
210     
211     private static final Class JavaDoc[] STATS_CLASSES =
212     {
213         EJBStats.class,
214         MessageDrivenBeanStats.class,
215         SessionBeanStats.class,
216         StatefulSessionBeanStats.class,
217         StatelessSessionBeanStats.class,
218         EntityBeanStats.class,
219         JavaMailStats.class,
220         JCAConnectionStats.class,
221         JCAConnectionPoolStats.class,
222         JDBCConnectionPoolStats.class,
223         JCAStats.class,
224         JDBCConnectionPoolStats.class,
225         JDBCStats.class,
226         JMSConnectionStats.class,
227         JMSConsumerStats.class,
228         JMSEndpointStats.class,
229         JMSProducerStats.class,
230         JMSSessionStats.class,
231         JMSStats.class,
232         JVMStats.class,
233         ServletStats.class,
234         URLStats.class
235     };
236     
237     /**
238         Get the type for use in a {@link CompositeType} type.
239      */

240         public static String JavaDoc
241     getStatsType( Stats stats )
242     {
243         String JavaDoc type = getType( STATS_CLASSES, stats );
244         
245         if ( type == null )
246         {
247             type = stats.getClass().getName();
248         }
249         
250         return( type );
251     }
252     
253     
254     /**
255         Convert a Stats object into a CompositeDataSupport, which is a standard
256         type in JMX which can be serialized.
257         <p>
258         The CompositeData entries will keyed by the name of the Statistic. The value
259         of each Statistic will also be a CompositeData, keyed by the value names
260         found in that statistic.
261         <p>
262         For example, the JVMStats object would be converted into a CompositeData with the entries:
263         <p>
264         <ul>
265         <li>HeapSize</li>
266             <ul>
267             <li>Description</li>
268             <li>LastSampleTime</li>
269             <li>Name</li>
270             <li>StartTime</li>
271             <li>Unit</li>
272             <li>LowerBound</li>
273             <li>UpperBound</li>
274             <li>HighWaterMark</li>
275             <li>LowWaterMark</li>
276             <li>Current</li>
277             </ul>
278         <li>UpTime</li>
279             <ul>
280             <li>Description</li>
281             <li>LastSampleTime</li>
282             <li>Name</li>
283             <li>StartTime</li>
284             <li>Unit</li>
285             <li>Count</li>
286             </ul>
287         </ul>
288         
289         @param stats any JSR 77 Stats object
290      */

291         public static CompositeDataSupport JavaDoc
292     statsToCompositeData( final Stats stats )
293         throws OpenDataException JavaDoc
294     {
295         final String JavaDoc statsType = getStatsType( stats );
296         
297         final Statistic[] statistics = stats.getStatistics();
298         final String JavaDoc[] names = new String JavaDoc[ statistics.length ];
299         final CompositeData JavaDoc[] datas = new CompositeData JavaDoc[ names.length ];
300         final String JavaDoc[] itemDescriptions = new String JavaDoc[ names.length ];
301         final CompositeType JavaDoc[] itemTypes = new CompositeType JavaDoc[ names.length ];
302         
303         for( int i = 0; i < names.length; ++i )
304         {
305             names[ i ] = statistics[ i ].getName();
306             datas[ i ] = statisticToCompositeData( statistics[ i ] );
307             itemTypes[ i ] = datas[ i ].getCompositeType();
308             itemDescriptions[ i ] = statistics[ i ].getName();
309         }
310         
311         final CompositeType JavaDoc type = new CompositeType JavaDoc(
312                 statsType,
313                 "CompositeData for " + statsType,
314                 names,
315                 itemDescriptions,
316                 itemTypes );
317         
318         return( new CompositeDataSupport JavaDoc( type, names, datas ) );
319     }
320     
321     
322     /**
323         Turn a Stats object into a Map containing all its Statistics, keyed by Statistic.getName()
324         and value of the Statistic.
325      */

326         public static Map JavaDoc<String JavaDoc,Statistic>
327     statsToMap( final Stats stats )
328     {
329         final Statistic[] statistics = stats.getStatistics();
330         
331         return( statisticsToMap( statistics ) );
332     }
333
334
335     /**
336         Convert a Statistic[] into a Map keyed by the name of the Statistic, with
337         value being the Statistic.
338         
339         @return Map keyed by name of the Statistic
340      */

341         public static Map JavaDoc<String JavaDoc,Statistic>
342     statisticsToMap( final Statistic[] statistics )
343     {
344         final Map JavaDoc<String JavaDoc,Statistic> m = new HashMap JavaDoc<String JavaDoc,Statistic>();
345         
346         for( int i = 0; i < statistics.length; ++i )
347         {
348             final String JavaDoc name = statistics[ i ].getName();
349             m.put( name, statistics[ i ] );
350         }
351         
352         return( m );
353     }
354     
355     
356         public static Method JavaDoc[]
357     getStatisticGetterMethodsUsingNames( final Stats stats)
358         throws NoSuchMethodException JavaDoc
359     {
360         final String JavaDoc[] statisticNames = stats.getStatisticNames();
361         
362         final Class JavaDoc statsClass = stats.getClass();
363         final Method JavaDoc[] methods = new Method JavaDoc[ statisticNames.length ];
364         
365         final Set JavaDoc<String JavaDoc> missing = new HashSet JavaDoc<String JavaDoc>();
366         
367         for( int i = 0; i < statisticNames.length; ++i )
368         {
369             //System.out.print( statisticNames[i ] + ", " );
370
final String JavaDoc methodName = JMXUtil.GET + statisticNames[ i ];
371             try
372             {
373             methods[ i ] = statsClass.getMethod( methodName, (Class JavaDoc[])null );
374             }
375             catch( final NoSuchMethodException JavaDoc e )
376             {
377                 missing.add( methodName );
378             }
379         }
380         
381         if ( missing.size() != 0 )
382         {
383             throw new NoSuchMethodException JavaDoc(
384                 "Missing methods: in object of class " + stats.getClass().getName() +
385                 ": " + CollectionUtil.toString( missing, ", ") );
386         }
387         
388         return( methods );
389     }
390     
391         public static Method JavaDoc[]
392     getStatisticGetterMethodsUsingIntrospection( final Stats stats)
393     {
394         final Method JavaDoc[] candidates = stats.getClass().getMethods();
395         
396         final List JavaDoc<Method JavaDoc> results = new ArrayList JavaDoc<Method JavaDoc>();
397         for( int methodIdx = 0; methodIdx < candidates.length; ++methodIdx )
398         {
399             final Method JavaDoc method = candidates[ methodIdx ];
400             final String JavaDoc methodName = method.getName();
401             
402             final Class JavaDoc returnType = method.getReturnType();
403             
404             if ( JMXUtil.isGetter( method ) &&
405                 Statistic.class.isAssignableFrom( returnType ) &&
406                 method.getParameterTypes().length == 0 )
407             {
408                 results.add( method );
409             }
410         }
411     
412         return (Method JavaDoc[])results.toArray( new Method JavaDoc[ results.size() ] );
413     }
414
415
416 }
417
418
419
420
421
422
Popular Tags