KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > jmx > OpenMBeanUtil


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.jmx;
24
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.io.Serializable JavaDoc;
32
33 import java.math.BigInteger JavaDoc;
34 import java.math.BigDecimal JavaDoc;
35
36 import java.lang.reflect.Array JavaDoc;
37
38 import javax.management.ObjectName JavaDoc;
39 import javax.management.openmbean.OpenType JavaDoc;
40 import javax.management.openmbean.ArrayType JavaDoc;
41 import javax.management.openmbean.CompositeData JavaDoc;
42 import javax.management.openmbean.CompositeDataSupport JavaDoc;
43 import javax.management.openmbean.CompositeType JavaDoc;
44 import javax.management.openmbean.TabularType JavaDoc;
45 import javax.management.openmbean.TabularData JavaDoc;
46 import javax.management.openmbean.SimpleType JavaDoc;
47 import javax.management.openmbean.OpenDataException JavaDoc;
48 import javax.management.openmbean.InvalidOpenTypeException JavaDoc;
49
50 import com.sun.appserv.management.util.misc.ArrayConversion;
51 import com.sun.appserv.management.util.misc.IteratorUtil;
52 import com.sun.appserv.management.util.misc.TypeCast;
53
54 /**
55     Utilities dealing with OpenMBeans
56  */

57 public final class OpenMBeanUtil
58 {
59     private OpenMBeanUtil() {}
60     
61     private static Map JavaDoc<Class JavaDoc,SimpleType JavaDoc> SIMPLETYPES_MAP = null;
62         private static Map JavaDoc<Class JavaDoc,SimpleType JavaDoc>
63     getSimpleTypesMap()
64     {
65         if ( SIMPLETYPES_MAP == null )
66         {
67             final Map JavaDoc<Class JavaDoc,SimpleType JavaDoc> m = new HashMap JavaDoc<Class JavaDoc,SimpleType JavaDoc>();
68             
69             m.put( Byte JavaDoc.class, SimpleType.BYTE );
70             m.put( Short JavaDoc.class, SimpleType.SHORT );
71             m.put( Integer JavaDoc.class, SimpleType.INTEGER );
72             m.put( Long JavaDoc.class, SimpleType.LONG );
73             m.put( BigInteger JavaDoc.class, SimpleType.BIGINTEGER );
74             m.put( BigDecimal JavaDoc.class, SimpleType.BIGDECIMAL );
75             m.put( Float JavaDoc.class, SimpleType.FLOAT );
76             m.put( Double JavaDoc.class, SimpleType.DOUBLE );
77             
78             m.put( Character JavaDoc.class, SimpleType.CHARACTER );
79             m.put( Boolean JavaDoc.class, SimpleType.BOOLEAN );
80             m.put( String JavaDoc.class, SimpleType.STRING );
81             m.put( Date JavaDoc.class, SimpleType.DATE );
82             m.put( Void JavaDoc.class, SimpleType.VOID );
83             
84             m.put( ObjectName JavaDoc.class, SimpleType.OBJECTNAME );
85             
86             SIMPLETYPES_MAP = m;
87         }
88         
89         return( SIMPLETYPES_MAP );
90     }
91     
92     /**
93         Get the SimpleType for a class which can be so-represented.
94      */

95         static public SimpleType JavaDoc
96     getSimpleType( final Class JavaDoc c )
97     {
98         final SimpleType JavaDoc type = (SimpleType JavaDoc)(getSimpleTypesMap().get( c ));
99         
100         return( type );
101
102     }
103     
104     private static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[ 0 ];
105     private static final OpenType JavaDoc[] EMPTY_OPENTYPES = new OpenType JavaDoc[ 0 ];
106     
107
108     
109     /**
110         Get any non-null array element from the array.
111      */

112         private static Object JavaDoc
113     getAnyArrayElement( Object JavaDoc o )
114     {
115         Object JavaDoc result = null;
116     
117         final int length = Array.getLength( o );
118         if ( length != 0 )
119         {
120             for( int i = 0; i < length; ++i )
121             {
122                 final Object JavaDoc element = Array.get( o, 0 );
123                 
124                 if ( element != null )
125                 {
126                     if ( element.getClass().isArray() )
127                     {
128                         result = getAnyArrayElement( element );
129                         if ( result != null )
130                             break;
131                     }
132                     else
133                     {
134                         result = element;
135                         break;
136                     }
137                 }
138             }
139         }
140         
141         return( result );
142     }
143     
144     
145         private static int
146     getArrayDimensions( final Class JavaDoc theClass )
147     {
148         final String JavaDoc classname = theClass.getName();
149         
150         int dim = 0;
151         while ( classname.charAt( dim ) == '[' )
152         {
153             ++dim;
154         }
155         
156         return( dim );
157     }
158     
159     
160     /**
161         Get the OpenType of an Object, which must conform to OpenType requirements.
162      */

163         static public OpenType JavaDoc
164     getOpenType( final Object JavaDoc o )
165         throws InvalidOpenTypeException JavaDoc, OpenDataException JavaDoc
166     {
167         if ( o == null )
168         {
169             // no OpenType for a null
170
throw new IllegalArgumentException JavaDoc();
171         }
172         
173         OpenType JavaDoc type = getSimpleType( o.getClass() );
174         
175         if ( type == null )
176         {
177             final Class JavaDoc theClass = o.getClass();
178             
179             if ( theClass.isArray() )
180             {
181                 final int length = Array.getLength( o );
182                 final int dimensions = getArrayDimensions( theClass );
183                 final Class JavaDoc elementClass = theClass.getComponentType();
184                 
185                 final SimpleType JavaDoc simpleType = getSimpleType( elementClass );
186                 if ( simpleType != null )
187                 {
188                     type = new ArrayType JavaDoc( dimensions, simpleType );
189                 }
190                 else
191                 {
192                     final Object JavaDoc element = getAnyArrayElement( o );
193                     
194                     if ( CompositeData JavaDoc.class.isAssignableFrom( elementClass ) )
195                     {
196                         if ( element == null )
197                         {
198                             type = SimpleType.VOID;
199                         }
200                         else
201                         {
202                             type = new ArrayType JavaDoc( dimensions, ((CompositeData JavaDoc)element).getCompositeType() );
203                         }
204                     }
205                     else if ( TabularData JavaDoc.class.isAssignableFrom( elementClass ) )
206                     {
207                         if ( element == null )
208                         {
209                             type = SimpleType.VOID;
210                         }
211                         else
212                         {
213                             type = new ArrayType JavaDoc( dimensions, ((TabularData JavaDoc)element).getTabularType() );
214                         }
215                     }
216                 }
217                 
218             }
219             else if ( o instanceof CompositeData JavaDoc )
220             {
221                 type = ((CompositeData JavaDoc)o).getCompositeType();
222             }
223             else if ( o instanceof TabularData JavaDoc )
224             {
225                 type = ((TabularData JavaDoc)o).getTabularType();
226             }
227         }
228         
229         if ( type == null )
230         {
231             throw new IllegalArgumentException JavaDoc( o.getClass().getName() );
232         }
233         
234         return( type );
235     }
236     
237     /**
238         Convert certain types and return a new Map:
239         <ul>
240         <li>Collection converts to an array</li>
241         </ul>
242         Nulls are not eliminated; use
243         {@link com.sun.appserv.management.util.misc.MapUtil#newMapNoNullValues} to
244         do that before or after.
245      */

246         public static Map JavaDoc<String JavaDoc,Serializable JavaDoc>
247     convertTypes( final Map JavaDoc<String JavaDoc,Serializable JavaDoc> orig )
248     {
249         final Map JavaDoc<String JavaDoc,Serializable JavaDoc>
250             result = new HashMap JavaDoc<String JavaDoc,Serializable JavaDoc>();
251         
252         for( final String JavaDoc key : orig.keySet() )
253         {
254             final Serializable JavaDoc value = (Serializable JavaDoc)orig.get( key );
255             
256             if ( value instanceof Collection JavaDoc )
257             {
258                 Object JavaDoc[] newValue =
259                     IteratorUtil.toArray( ((Collection JavaDoc<Serializable JavaDoc>)value).iterator() );
260                 newValue = ArrayConversion.specializeArray( newValue );
261                 
262                 result.put( key, newValue );
263             }
264             else
265             {
266                 result.put( key, value );
267             }
268         }
269         
270         
271         return( result );
272     }
273     
274     /**
275         Create a CompositeType from a Map. Each key in the map must be a String,
276         and each value must be a type consistent with OpenTypes.
277         
278         @param typeName the arbitrary name of the OpenType to be used
279         @param description the arbitrary description of the OpenType to be used
280         @param map a Map keyed by String, whose values may not be null
281      */

282         public static CompositeType JavaDoc
283     mapToCompositeType(
284         final String JavaDoc typeName,
285         final String JavaDoc description,
286         final Map JavaDoc<String JavaDoc,?> map,
287         CompositeTypeFromNameCallback callback)
288         throws OpenDataException JavaDoc
289     {
290         final String JavaDoc[] itemNames = new String JavaDoc[ map.keySet().size()];
291         map.keySet().toArray( itemNames );
292         
293         final String JavaDoc[] itemDescriptions = new String JavaDoc[ itemNames.length ];
294         final OpenType JavaDoc[] itemTypes = new OpenType JavaDoc[ itemNames.length ];
295         
296         for( int i = 0; i < itemNames.length; ++i )
297         {
298             final String JavaDoc name = itemNames[ i ];
299             final Object JavaDoc value = map.get( name );
300             
301             itemDescriptions[ i ] = "value " + name;
302             if ( value == null )
303             {
304                 // force nulls to type String
305
itemTypes[ i ] = callback.getOpenTypeFromName( name );
306             }
307             else
308             {
309                 itemTypes[ i ] = getOpenType( value );
310             }
311         }
312         
313         final CompositeType JavaDoc type = new CompositeType JavaDoc(
314                                 typeName,
315                                 description,
316                                 itemNames,
317                                 itemDescriptions,
318                                 itemTypes );
319     
320         return( type );
321     }
322     
323     /**
324         Create a CompositeData from a Map. Each key in the map must be a String,
325         and each value must be a type consistent with OpenTypes.
326         
327         @param typeName the arbitrary name of the OpenType to be used
328         @param description the arbitrary description of the OpenType to be used
329         @param map a Map keyed by String, whose values may not be null
330      */

331         public static CompositeData JavaDoc
332     mapToCompositeData(
333         final String JavaDoc typeName,
334         final String JavaDoc description,
335         final Map JavaDoc<String JavaDoc,Object JavaDoc> map )
336         throws OpenDataException JavaDoc
337     {
338         final CompositeType JavaDoc type = mapToCompositeType( typeName, description, map, null);
339         
340         return( new CompositeDataSupport JavaDoc( type, map ) );
341     }
342     
343     
344     /**
345         Convert a CompositeData to a Map.
346      */

347         public static Map JavaDoc<String JavaDoc,Serializable JavaDoc>
348     compositeDataToMap( final CompositeData JavaDoc data )
349     {
350         final Map JavaDoc<String JavaDoc,Serializable JavaDoc> map = new HashMap JavaDoc<String JavaDoc,Serializable JavaDoc>();
351         final CompositeType JavaDoc type = data.getCompositeType();
352         final Set JavaDoc<String JavaDoc> keySet = TypeCast.asSet( type.keySet() );
353         
354         for( String JavaDoc name : keySet )
355         {
356             map.put( name, (Serializable JavaDoc)data.get( name ) );
357         }
358         
359         return( map );
360     }
361     
362     
363         
364     /**
365         Get a CompositeType describing a CompositeData which has no elements.
366      */

367         public static OpenType JavaDoc
368     getStackTraceElementOpenType()
369         throws OpenDataException JavaDoc
370     {
371         final String JavaDoc[] itemNames = new String JavaDoc[]
372         {
373             "ClassName",
374             "FileName",
375             "LineNumber",
376             "IsNativeMethod",
377         };
378         
379         final String JavaDoc[] descriptions = new String JavaDoc[ ]
380         {
381             "ClassName",
382             "FileName",
383             "LineNumber",
384             "IsNativeMethod",
385         };
386         
387         final OpenType JavaDoc[] openTypes = new OpenType JavaDoc[ itemNames.length ];
388         openTypes[ 0 ] = SimpleType.STRING;
389         openTypes[ 1 ] = SimpleType.STRING;
390         openTypes[ 2 ] = SimpleType.INTEGER;
391         openTypes[ 3 ] = SimpleType.BOOLEAN;
392         
393         return( new CompositeType JavaDoc(
394             StackTraceElement JavaDoc.class.getName(),
395             "StackTraceElement composite type",
396             itemNames,
397             descriptions,
398             openTypes
399             ) );
400     }
401     
402     /**
403         Get a CompositeType describing a CompositeData which has no elements.
404      */

405         public static OpenType JavaDoc
406     getThrowableOpenType( final Throwable JavaDoc t)
407         throws OpenDataException JavaDoc
408     {
409         final String JavaDoc[] itemNames = new String JavaDoc[]
410         {
411             "Message",
412             "Cause",
413             "StackTrace",
414         };
415         
416         final String JavaDoc[] descriptions = new String JavaDoc[ ]
417         {
418             "The message from the Throwable",
419             "The cause (if any) from the Throwable",
420             "The stack trace from the Throwable",
421         };
422         
423         final OpenType JavaDoc[] openTypes = new OpenType JavaDoc[ itemNames.length ];
424         
425         openTypes[ 0 ] = SimpleType.STRING;
426         openTypes[ 1 ] = t.getCause() == null ? SimpleType.VOID : getThrowableOpenType( t.getCause() );
427         openTypes[ 2 ] = new ArrayType JavaDoc( t.getStackTrace().length,
428                             getStackTraceElementOpenType() );
429         
430         
431         return( new CompositeType JavaDoc(
432             t.getClass().getName(),
433             "Throwable composite type",
434             itemNames,
435             descriptions,
436             openTypes
437             ) );
438     }
439     
440 }
441
442
443
444
445
446
447
Popular Tags