KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
27
28 import java.io.Serializable JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.openmbean.OpenType JavaDoc;
32 import javax.management.openmbean.ArrayType JavaDoc;
33 import javax.management.openmbean.CompositeData JavaDoc;
34 import javax.management.openmbean.CompositeDataSupport JavaDoc;
35 import javax.management.openmbean.CompositeType JavaDoc;
36 import javax.management.openmbean.TabularType JavaDoc;
37 import javax.management.openmbean.TabularData JavaDoc;
38 import javax.management.openmbean.SimpleType JavaDoc;
39 import javax.management.openmbean.OpenDataException JavaDoc;
40 import javax.management.openmbean.InvalidOpenTypeException JavaDoc;
41
42 import com.sun.appserv.management.util.jmx.OpenMBeanUtil;
43 import com.sun.appserv.management.util.misc.TypeCast;
44
45 public class CompositeDataHelper
46 {
47         public
48     CompositeDataHelper()
49     {
50     }
51     
52         protected <T extends Serializable JavaDoc> CompositeType JavaDoc
53     mapToCompositeType(
54         final String JavaDoc typeName,
55         final String JavaDoc description,
56         final Map JavaDoc<String JavaDoc,T> map)
57         throws OpenDataException JavaDoc
58     {
59         return mapToCompositeType( typeName, description, map, null );
60     }
61     
62     /**
63         Create a CompositeType from a Map. Each key in the map must be a String,
64         and each value must be a type consistent with OpenTypes.
65         
66         @param typeName the arbitrary name of the OpenType to be used
67         @param description the arbitrary description of the OpenType to be used
68         @param map a Map keyed by String, whose values may not be null
69      */

70         protected <T extends Serializable JavaDoc> CompositeType JavaDoc
71     mapToCompositeType(
72         final String JavaDoc typeName,
73         final String JavaDoc description,
74         final Map JavaDoc<String JavaDoc,T> map,
75         CompositeTypeFromNameCallback callback)
76         throws OpenDataException JavaDoc
77     {
78         return OpenMBeanUtil.mapToCompositeType( typeName, description, map, callback );
79     }
80     
81     /**
82         Create a CompositeData from a Map. Each key in the map must be a String,
83         and each value must be a type consistent with OpenTypes.
84         
85         @param typeName the arbitrary name of the OpenType to be used
86         @param description the arbitrary description of the OpenType to be used
87         @param map a Map keyed by String, whose values may not be null
88      */

89         protected <T extends Serializable JavaDoc> CompositeData JavaDoc
90     mapToCompositeData(
91         final String JavaDoc typeName,
92         final String JavaDoc description,
93         final Map JavaDoc<String JavaDoc,T> map )
94         throws OpenDataException JavaDoc
95     {
96         final CompositeType JavaDoc type = mapToCompositeType( typeName, description, map );
97         
98         return( new CompositeDataSupport JavaDoc( type, map ) );
99     }
100     
101         public Serializable JavaDoc
102     asData( final Serializable JavaDoc o )
103         throws OpenDataException JavaDoc
104     {
105         Object JavaDoc result = null;
106         
107         if ( o instanceof StackTraceElement JavaDoc )
108         {
109             result = stackTraceElementCompositeData( (StackTraceElement JavaDoc)o );
110         }
111         else if ( o instanceof Throwable JavaDoc )
112         {
113             result = throwableToCompositeData( (Throwable JavaDoc)o );
114         }
115         else if ( o instanceof Map JavaDoc )
116         {
117             final Map JavaDoc<String JavaDoc,Serializable JavaDoc> m = TypeCast.asSerializableMap( o );
118             
119             result = mapToCompositeData( Map JavaDoc.class.getName(), "", m );
120         }
121         else
122         {
123             final OpenType JavaDoc type = OpenMBeanUtil.getOpenType( o );
124             
125             if ( type instanceof SimpleType JavaDoc )
126             {
127                 result = o;
128             }
129             else if ( type instanceof ArrayType JavaDoc )
130             {
131                 result = o;
132             }
133             else
134             {
135                 throw new IllegalArgumentException JavaDoc( "" + o );
136             }
137         }
138         
139         return( Serializable JavaDoc.class.cast( result ) );
140     }
141     
142         
143     /**
144         Get a CompositeType describing a CompositeData which has no elements.
145      */

146         public CompositeType JavaDoc
147     getStackTraceElementCompositeType()
148         throws OpenDataException JavaDoc
149     {
150         final String JavaDoc[] itemNames = new String JavaDoc[]
151         {
152             "ClassName",
153             "FileName",
154             "LineNumber",
155             "isNativeMethod",
156         };
157         
158         final String JavaDoc[] descriptions = new String JavaDoc[ ]
159         {
160             "ClassName",
161             "FileName",
162             "LineNumber",
163             "IsNativeMethod",
164         };
165         
166         final OpenType JavaDoc[] openTypes = new OpenType JavaDoc[ itemNames.length ];
167         openTypes[ 0 ] = SimpleType.STRING;
168         openTypes[ 1 ] = SimpleType.STRING;
169         openTypes[ 2 ] = SimpleType.INTEGER;
170         openTypes[ 3 ] = SimpleType.BOOLEAN;
171         
172         final CompositeType JavaDoc type = new CompositeType JavaDoc(
173             StackTraceElement JavaDoc.class.getName(),
174             "StackTraceElement composite type",
175             itemNames,
176             descriptions,
177             openTypes
178             );
179         return( type );
180     }
181     
182     
183     /**
184         Get a CompositeType describing a CompositeData which has no elements.
185      */

186         public CompositeData JavaDoc
187     stackTraceElementCompositeData( StackTraceElement JavaDoc elem )
188         throws OpenDataException JavaDoc
189     {
190         final Map JavaDoc<String JavaDoc,Serializable JavaDoc> m = new HashMap JavaDoc<String JavaDoc,Serializable JavaDoc>();
191         m.put( "ClassName", elem.getClassName() );
192         m.put( "FileName", elem.getFileName() );
193         m.put( "LineNumber", new Integer JavaDoc( elem.getLineNumber() ) );
194         m.put( "isNativeMethod", Boolean.valueOf( elem.isNativeMethod() ) );
195         
196         return( new CompositeDataSupport JavaDoc( getStackTraceElementCompositeType(), m ) );
197     }
198     
199     
200
201     /**
202         Get a CompositeType describing a CompositeData which has no elements.
203      */

204         public CompositeData JavaDoc
205     throwableToCompositeData( final Throwable JavaDoc t)
206         throws OpenDataException JavaDoc
207     {
208         final Throwable JavaDoc cause = t.getCause();
209         
210         final String JavaDoc[] itemNames = new String JavaDoc[]
211         {
212             "Message",
213             "Cause",
214             "StackTrace",
215         };
216         
217         final String JavaDoc[] descriptions = new String JavaDoc[ ]
218         {
219             "The message from the Throwable",
220             "The cause (if any) from the Throwable",
221             "The stack trace from the Throwable",
222         };
223         
224         final OpenType JavaDoc[] openTypes = new OpenType JavaDoc[ itemNames.length ];
225         
226         openTypes[ 0 ] = SimpleType.STRING;
227         openTypes[ 1 ] = cause == null ?
228             getEmptyCompositeType() : throwableToCompositeData( cause ).getCompositeType();
229         openTypes[ 2 ] = new ArrayType JavaDoc( t.getStackTrace().length,
230                             getStackTraceElementCompositeType() );
231         
232         
233         final CompositeType JavaDoc type = new CompositeType JavaDoc(
234             t.getClass().getName(),
235             "Throwable composite type",
236             itemNames,
237             descriptions,
238             openTypes
239             );
240         
241         
242         final Map JavaDoc<String JavaDoc,Object JavaDoc> m = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
243         m.put( "Message", t.getMessage() );
244         m.put( "Cause", cause == null ? null : throwableToCompositeData( cause ) );
245         m.put( "StackTrace", t.getStackTrace() );
246         
247         return( new CompositeDataSupport JavaDoc( type, m ) );
248     }
249     
250     private final static String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
251     private final static OpenType JavaDoc[] EMPTY_OPENTYPES = new OpenType JavaDoc[0];
252     
253     /**
254         Get a CompositeType describing a CompositeData which has no elements.
255      */

256         public static CompositeType JavaDoc
257     getEmptyCompositeType()
258         throws OpenDataException JavaDoc
259     {
260         return( new CompositeType JavaDoc(
261             CompositeType JavaDoc.class.getName() + ".Empty",
262             "Empty composite type",
263             EMPTY_STRING_ARRAY,
264             EMPTY_STRING_ARRAY,
265             EMPTY_OPENTYPES
266             ) );
267     }
268 }
269
270
271
272
273
274
275
Popular Tags