KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > base > Util


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.base;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.io.Serializable JavaDoc;
32
33 import java.lang.reflect.Proxy JavaDoc;
34
35 import javax.management.ObjectName JavaDoc;
36 import javax.management.Notification JavaDoc;
37
38 import com.sun.appserv.management.base.AMX;
39 import com.sun.appserv.management.util.jmx.JMXUtil;
40 import com.sun.appserv.management.util.misc.MapUtil;
41 import com.sun.appserv.management.util.misc.GSetUtil;
42 import com.sun.appserv.management.util.misc.TypeCast;
43
44 /**
45     Utility routines pertinent to the MBean API.
46  */

47 public final class Util
48 {
49     private Util() {}
50     
51     /**
52         Create a new ObjectName, caller is guaranteeing that the name is
53         well-formed (a RuntimeException will be thrown if not). This avoids
54         having to catch all sorts of JMX exceptions.<br>
55         <b>NOTE:</b> Do not call this method if there is not certainty of a well-formed name.
56         
57         @param name
58      */

59         public static ObjectName JavaDoc
60     newObjectName( String JavaDoc name )
61     {
62         return( JMXUtil.newObjectName( name ) );
63     }
64     
65     
66     /**
67         Build an ObjectName. Calls newObjectName( domain + ":" + props )
68         
69         @param domain the JMX domain
70         @param props properties of the ObjectName
71      */

72         public static ObjectName JavaDoc
73     newObjectName( String JavaDoc domain, String JavaDoc props )
74     {
75         return( newObjectName( domain + ":" + props ) );
76     }
77     
78     /**
79         Build an ObjectName pattern.
80         
81         @param domain the JMX domain
82         @param props properties of the ObjectName
83      */

84         public static ObjectName JavaDoc
85     newObjectNamePattern(
86         final String JavaDoc domain,
87         final String JavaDoc props )
88     {
89         return( JMXUtil.newObjectNamePattern( domain, props ) );
90     }
91     
92     /**
93         Build an ObjectName pattern.
94         
95         @param objectName
96      */

97         public static ObjectName JavaDoc
98     newObjectNamePattern( ObjectName JavaDoc objectName )
99     {
100         final String JavaDoc props = objectName.getKeyPropertyListString();
101         
102         return( newObjectNamePattern( objectName.getDomain(), props) );
103     }
104     
105     
106     /**
107         Make an ObjectName property of the form <i>name</i>=<i>value</i>.
108         
109         @param name
110         @param value
111      */

112         public static String JavaDoc
113     makeProp(
114         final String JavaDoc name,
115         final String JavaDoc value )
116     {
117         return( JMXUtil.makeProp( name, value ) );
118     }
119     
120     /**
121         Make an ObjectName property of the form j2eeType=<i>value</i>.
122         
123         @param value
124      */

125         public static String JavaDoc
126     makeJ2EETypeProp( final String JavaDoc value )
127     {
128         return( makeProp( AMX.J2EE_TYPE_KEY, value ) );
129     }
130     
131     /**
132         Make an ObjectName property of the form name=<i>value</i>.
133         
134         @param value
135      */

136         public static String JavaDoc
137     makeNameProp( final String JavaDoc value )
138     {
139         return( makeProp( AMX.NAME_KEY, value ) );
140     }
141
142     /**
143         @param j2eeType
144         @param name
145      */

146         public static String JavaDoc
147     makeRequiredProps(
148         final String JavaDoc j2eeType,
149         final String JavaDoc name )
150     {
151         final String JavaDoc j2eeTypeProp = Util.makeJ2EETypeProp( j2eeType );
152         final String JavaDoc nameProp = Util.makeNameProp( name );
153         
154         final String JavaDoc props = Util.concatenateProps( j2eeTypeProp, nameProp );
155         
156         return( props );
157     }
158     
159     /**
160         Extract the j2eeType and name properties and return it as a single property
161         <i>j2eeType</i>=<i>name</i>
162         
163         @param objectName
164      */

165         public static String JavaDoc
166     getSelfProp( final ObjectName JavaDoc objectName )
167     {
168         final String JavaDoc j2eeType = objectName.getKeyProperty( AMX.J2EE_TYPE_KEY );
169         final String JavaDoc name = objectName.getKeyProperty( AMX.NAME_KEY );
170         
171         return( Util.makeProp( j2eeType, name ) );
172     }
173     
174     
175     /**
176         Get properties corresponding to the FullType of this ObjectName.
177         Its j2eeType/name are included as <i>j2eeType</i>=<i>name</i>, <b>not</b>
178         as j2eeType=<i>j2eeType</i>,name=<i>name</i>.
179         
180         @param objectName
181         @param fullType
182         @return String of relevant ObjectName properties
183      */

184         public static String JavaDoc
185     getFullTypeProps(
186         final ObjectName JavaDoc objectName,
187         final String JavaDoc fullType )
188     {
189         final String JavaDoc selfProp = Util.getSelfProp( objectName );
190             
191         // now add properties for ancestors; skip the last type; it's
192
// present in the j2eeType/name properties (selfProp)
193
String JavaDoc ancestorProps = "";
194         final String JavaDoc[] types = Util.getTypeArray( fullType );
195         for( int i = 0; i < types.length - 1; ++i )
196         {
197             final String JavaDoc key = types[ i ];
198             final String JavaDoc value = objectName.getKeyProperty( key );
199             final String JavaDoc prop = Util.makeProp( key, value );
200             
201             ancestorProps = Util.concatenateProps( ancestorProps, prop );
202         }
203         
204         final String JavaDoc props = Util.concatenateProps( selfProp, ancestorProps );
205         
206         return( props );
207     }
208     
209     /**
210         Get the value of the AMX.NAME_KEY property within the ObjectName
211         or AMX.NO_NAME if not present.
212         
213         @return the name
214      */

215         public static String JavaDoc
216     getName( final ObjectName JavaDoc objectName )
217     {
218         String JavaDoc name = objectName.getKeyProperty( AMX.NAME_KEY );
219         
220         if ( name == null )
221         {
222             name = AMX.NO_NAME;
223         }
224
225         return( name );
226     }
227     
228         public static String JavaDoc
229     getJ2EEType( final ObjectName JavaDoc objectName )
230     {
231         return( objectName.getKeyProperty( AMX.J2EE_TYPE_KEY ) );
232     }
233     
234     /**
235         Get the FullType as a String[], last element being the j2eeType.
236         
237         @param fullType as returned from {@link AMX#getFullType}
238      */

239         public static String JavaDoc[]
240     getTypeArray( final String JavaDoc fullType )
241     {
242         if ( fullType == null )
243         {
244             throw new IllegalArgumentException JavaDoc();
245         }
246         
247         assert( AMX.FULL_TYPE_DELIM.equals( "." ) );
248         return( fullType.split( "\\." ) );
249     }
250     
251     
252     /**
253         Minimal ObjectName properties required for an ObjectName pattern
254         to uniquely identify an MBean. See {@link #getObjectNamePattern}.
255      */

256     private static final Set JavaDoc<String JavaDoc> PATTERN_PROPS =
257         GSetUtil.newUnmodifiableStringSet(
258         AMX.J2EE_TYPE_KEY,
259         AMX.NAME_KEY );
260
261
262     /**
263         Get all keys required for an ObjectName pattern which uniquely
264         identifies the MBean.
265      */

266         public static Set JavaDoc<String JavaDoc>
267     getPatternKeys(
268         final String JavaDoc fullType )
269     {
270         final Set JavaDoc<String JavaDoc> requiredKeys = GSetUtil.copySet( PATTERN_PROPS );
271         
272         // omit the last one, it is the simple type of this MBean, which we've
273
// already included
274
final String JavaDoc[] types = Util.getTypeArray( fullType );
275         for( int i = 0; i < types.length - 1; ++i )
276         {
277             requiredKeys.add( types[ i ] );
278         }
279
280         return TypeCast.checkedStringSet( requiredKeys );
281     }
282     
283         public static String JavaDoc
284     concatenateProps(
285         final String JavaDoc props1,
286         final String JavaDoc props2 )
287     {
288         return( JMXUtil.concatenateProps( props1, props2 ) );
289     }
290     
291         public static String JavaDoc
292     concatenateProps(
293         final String JavaDoc props1,
294         final String JavaDoc props2,
295         final String JavaDoc props3 )
296     {
297         return( concatenateProps( concatenateProps( props1, props2), props3) );
298     }
299     
300     /**
301         @return a Set of ObjectNames from a Set of AMX.
302      */

303         public static Set JavaDoc<ObjectName JavaDoc>
304     toObjectNames( final Set JavaDoc<? extends AMX> amxs )
305     {
306         final Set JavaDoc<ObjectName JavaDoc> objectNames = new HashSet JavaDoc<ObjectName JavaDoc>();
307         for( final AMX next : amxs )
308         {
309             objectNames.add( getObjectName( next ) );
310         }
311         return( Collections.checkedSet(objectNames, ObjectName JavaDoc.class) );
312     }
313     
314     /**
315         @return a Map of ObjectNames from a Map whose values are AMX.
316      */

317         public static Map JavaDoc<String JavaDoc,ObjectName JavaDoc>
318     toObjectNames( final Map JavaDoc<String JavaDoc,? extends AMX> amxMap )
319     {
320         final Map JavaDoc<String JavaDoc,ObjectName JavaDoc> m = new HashMap JavaDoc<String JavaDoc,ObjectName JavaDoc>();
321         
322         for( final String JavaDoc key : amxMap.keySet() )
323         {
324             final AMX value = amxMap.get( key );
325             m.put( key, getExtra( value ).getObjectName() );
326         }
327         return( Collections.checkedMap( m, String JavaDoc.class, ObjectName JavaDoc.class) );
328     }
329     
330     /**
331         @return an ObjectName[] from an AMX[]
332      */

333         public static ObjectName JavaDoc[]
334     toObjectNames( final AMX[] amx )
335     {
336         final ObjectName JavaDoc[] objectNames = new ObjectName JavaDoc[ amx.length ];
337         for( int i = 0; i < objectNames.length; ++i )
338         {
339             objectNames[ i ] = amx[ i ] == null ? null : getExtra( amx[ i ] ).getObjectName();
340         }
341         
342         return( objectNames );
343     }
344     
345     /**
346         Extract the names from all ObjectNames. The name is the value of the
347         property NAME_KEY (See {@link AMX}). Note that if two or more ObjectNames
348         share the same name, the resulting Set will be of smaller size() than
349         the original.
350         
351         @return Set
352      */

353         public static Set JavaDoc<String JavaDoc>
354     getNames( final Set JavaDoc<? extends AMX> amxs )
355     {
356         return getNamesSet( Util.toObjectNames( amxs ) );
357     }
358     
359     /**
360         Extract the names from all ObjectNames. The name is the value of the
361         property NAME_KEY (See {@link AMX}). Note that if two or more ObjectNames
362         share the same name, the resulting Set will be of smaller size() than
363         the original.
364         
365         @return Set
366      */

367         public static Set JavaDoc<String JavaDoc>
368     getNamesSet( final Set JavaDoc<ObjectName JavaDoc> objectNames )
369     {
370         return TypeCast.checkedStringSet(
371                 JMXUtil.getKeyPropertySet( AMX.NAME_KEY, objectNames ) );
372     }
373     
374     /**
375         Extract the names from all ObjectNames.
376         
377         @return String[] of names from the ObjectNames
378      */

379         public static String JavaDoc[]
380     getNamesArray( final Set JavaDoc<ObjectName JavaDoc> objectNames )
381     {
382         return( JMXUtil.getKeyProperty( AMX.NAME_KEY, objectNames ) );
383     }
384     
385     /**
386         Create a Map keyed by the value of the AMX.NAME_KEY with
387         value the ObjectName. Note that if two or more ObjectNames
388         share the same name, the resulting Map will contain only
389         one of the original ObjectNames.
390         
391         @param objectNames Set of ObjectName
392      */

393         public static final Map JavaDoc<String JavaDoc,ObjectName JavaDoc>
394     createObjectNameMap( final Set JavaDoc<ObjectName JavaDoc> objectNames )
395     {
396         final Map JavaDoc<String JavaDoc,ObjectName JavaDoc> m = new HashMap JavaDoc<String JavaDoc,ObjectName JavaDoc>();
397         
398         for( final ObjectName JavaDoc objectName : objectNames )
399         {
400             final String JavaDoc name = getName( objectName );
401             
402             assert( ! m.containsKey( name ) ) :
403                 "createObjectNameMap: key already present: " + name + " in " + objectName;
404             m.put( name, objectName );
405         }
406         
407         assert( m.keySet().size() == objectNames.size() );
408         
409         return( Collections.checkedMap(m, String JavaDoc.class, ObjectName JavaDoc.class) );
410     }
411     
412     
413     /**
414         Create a Map keyed by the value of the AMX.NAME_KEY with
415         value the AMX item.
416         
417         @param amxs Set of AMX
418      */

419         public static <T extends AMX> Map JavaDoc<String JavaDoc,T>
420     createNameMap( final Set JavaDoc<T> amxs )
421     {
422         final Map JavaDoc<String JavaDoc,T> m = new HashMap JavaDoc<String JavaDoc,T>();
423         
424         for( final T amx : amxs )
425         {
426             final String JavaDoc name = amx.getName();
427             m.put( name, amx );
428         }
429         
430         return( m );
431     }
432     
433     /**
434         Get extra information about this {@link AMX}.
435         'Extra' is not an MBean Attribute; it exists only in the {@link AMX}.
436         
437         @param proxy an AMX
438      */

439         public static Extra
440     getExtra( final AMX proxy )
441     {
442         return( (Extra)Proxy.getInvocationHandler( proxy ) );
443     }
444     
445     
446     /**
447         Get the ObjectName targeted by this {@link AMX}.
448         
449         @param proxy an AMX
450      */

451         public static <T extends AMX> ObjectName JavaDoc
452     getObjectName( final T proxy )
453     {
454         return( getExtra( proxy ).getObjectName() );
455     }
456     
457     
458     /**
459         Get an ObjectName from a Map of ObjectName where the
460         values are keyed by name.
461         
462         @param candidates
463         @param name
464         @return ObjectName
465         @throws IllegalArgumentException if not found
466      */

467         public static ObjectName JavaDoc
468     getObjectName(
469         final Map JavaDoc<String JavaDoc,ObjectName JavaDoc> candidates,
470         final String JavaDoc name )
471     {
472         final Object JavaDoc item = candidates.get( name );
473         if ( item == null )
474         {
475             throw new IllegalArgumentException JavaDoc( "Not found: " + name );
476         }
477         
478         return (ObjectName JavaDoc)item;
479     }
480     
481     /**
482         All Notifications emitted by AMX MBeans which are not
483         standard types defined by JMX place a Map
484         into the userData field of the Notification. This call
485         retrieves that Map, which may be null if no additional
486         data is included.
487      */

488         public static Map JavaDoc<String JavaDoc,Serializable JavaDoc>
489     getAMXNotificationData( final Notification JavaDoc notif )
490     {
491         return Collections.unmodifiableMap(
492                 JMXUtil.getUserDataMapString_Serializable( notif ) );
493     }
494     
495     /**
496         Use of generic type form taking Class<T> is preferred.
497      */

498         public static Serializable JavaDoc
499     getAMXNotificationValue( final Notification JavaDoc notif, final String JavaDoc key )
500     {
501         final Map JavaDoc<String JavaDoc,Serializable JavaDoc> data =
502             getAMXNotificationData( notif );
503         
504         if ( data == null )
505         {
506             throw new IllegalArgumentException JavaDoc( notif.toString() );
507         }
508         
509         if ( ! data.containsKey( key ) )
510         {
511             throw new IllegalArgumentException JavaDoc( "Value not found for " + key +
512                 " in " + notif );
513         }
514         
515         return data.get( key );
516     }
517     
518     /**
519         Retrieve a particular value associated with the specified
520         key from an AMX Notification.
521         @see #getAMXNotificationData
522      */

523         public static <T extends Serializable JavaDoc> T
524     getAMXNotificationValue(
525         final Notification JavaDoc notif,
526         final String JavaDoc key,
527         final Class JavaDoc<T> theClass)
528     {
529         final Serializable JavaDoc value = getAMXNotificationValue( notif, key );
530         
531         return theClass.cast( value );
532     }
533     
534         public static void
535     sleep( final long millis )
536     {
537         try
538         {
539             Thread.sleep( millis );
540         }
541         catch( InterruptedException JavaDoc e )
542         {
543         }
544     }
545     
546     
547     /**
548         A safe way to cast to AMX.
549      */

550         public static AMX
551     asAMX( final Object JavaDoc o)
552     {
553         return AMX.class.cast( o );
554     }
555 }
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
Popular Tags