1 23 package com.sun.appserv.management.util.jmx; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 28 import java.io.Serializable ; 29 30 import javax.management.ObjectName ; 31 import javax.management.openmbean.OpenType ; 32 import javax.management.openmbean.ArrayType ; 33 import javax.management.openmbean.CompositeData ; 34 import javax.management.openmbean.CompositeDataSupport ; 35 import javax.management.openmbean.CompositeType ; 36 import javax.management.openmbean.TabularType ; 37 import javax.management.openmbean.TabularData ; 38 import javax.management.openmbean.SimpleType ; 39 import javax.management.openmbean.OpenDataException ; 40 import javax.management.openmbean.InvalidOpenTypeException ; 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 > CompositeType 53 mapToCompositeType( 54 final String typeName, 55 final String description, 56 final Map <String ,T> map) 57 throws OpenDataException 58 { 59 return mapToCompositeType( typeName, description, map, null ); 60 } 61 62 70 protected <T extends Serializable > CompositeType 71 mapToCompositeType( 72 final String typeName, 73 final String description, 74 final Map <String ,T> map, 75 CompositeTypeFromNameCallback callback) 76 throws OpenDataException 77 { 78 return OpenMBeanUtil.mapToCompositeType( typeName, description, map, callback ); 79 } 80 81 89 protected <T extends Serializable > CompositeData 90 mapToCompositeData( 91 final String typeName, 92 final String description, 93 final Map <String ,T> map ) 94 throws OpenDataException 95 { 96 final CompositeType type = mapToCompositeType( typeName, description, map ); 97 98 return( new CompositeDataSupport ( type, map ) ); 99 } 100 101 public Serializable 102 asData( final Serializable o ) 103 throws OpenDataException 104 { 105 Object result = null; 106 107 if ( o instanceof StackTraceElement ) 108 { 109 result = stackTraceElementCompositeData( (StackTraceElement )o ); 110 } 111 else if ( o instanceof Throwable ) 112 { 113 result = throwableToCompositeData( (Throwable )o ); 114 } 115 else if ( o instanceof Map ) 116 { 117 final Map <String ,Serializable > m = TypeCast.asSerializableMap( o ); 118 119 result = mapToCompositeData( Map .class.getName(), "", m ); 120 } 121 else 122 { 123 final OpenType type = OpenMBeanUtil.getOpenType( o ); 124 125 if ( type instanceof SimpleType ) 126 { 127 result = o; 128 } 129 else if ( type instanceof ArrayType ) 130 { 131 result = o; 132 } 133 else 134 { 135 throw new IllegalArgumentException ( "" + o ); 136 } 137 } 138 139 return( Serializable .class.cast( result ) ); 140 } 141 142 143 146 public CompositeType 147 getStackTraceElementCompositeType() 148 throws OpenDataException 149 { 150 final String [] itemNames = new String [] 151 { 152 "ClassName", 153 "FileName", 154 "LineNumber", 155 "isNativeMethod", 156 }; 157 158 final String [] descriptions = new String [ ] 159 { 160 "ClassName", 161 "FileName", 162 "LineNumber", 163 "IsNativeMethod", 164 }; 165 166 final OpenType [] openTypes = new OpenType [ 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 type = new CompositeType ( 173 StackTraceElement .class.getName(), 174 "StackTraceElement composite type", 175 itemNames, 176 descriptions, 177 openTypes 178 ); 179 return( type ); 180 } 181 182 183 186 public CompositeData 187 stackTraceElementCompositeData( StackTraceElement elem ) 188 throws OpenDataException 189 { 190 final Map <String ,Serializable > m = new HashMap <String ,Serializable >(); 191 m.put( "ClassName", elem.getClassName() ); 192 m.put( "FileName", elem.getFileName() ); 193 m.put( "LineNumber", new Integer ( elem.getLineNumber() ) ); 194 m.put( "isNativeMethod", Boolean.valueOf( elem.isNativeMethod() ) ); 195 196 return( new CompositeDataSupport ( getStackTraceElementCompositeType(), m ) ); 197 } 198 199 200 201 204 public CompositeData 205 throwableToCompositeData( final Throwable t) 206 throws OpenDataException 207 { 208 final Throwable cause = t.getCause(); 209 210 final String [] itemNames = new String [] 211 { 212 "Message", 213 "Cause", 214 "StackTrace", 215 }; 216 217 final String [] descriptions = new String [ ] 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 [] openTypes = new OpenType [ itemNames.length ]; 225 226 openTypes[ 0 ] = SimpleType.STRING; 227 openTypes[ 1 ] = cause == null ? 228 getEmptyCompositeType() : throwableToCompositeData( cause ).getCompositeType(); 229 openTypes[ 2 ] = new ArrayType ( t.getStackTrace().length, 230 getStackTraceElementCompositeType() ); 231 232 233 final CompositeType type = new CompositeType ( 234 t.getClass().getName(), 235 "Throwable composite type", 236 itemNames, 237 descriptions, 238 openTypes 239 ); 240 241 242 final Map <String ,Object > m = new HashMap <String ,Object >(); 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 ( type, m ) ); 248 } 249 250 private final static String [] EMPTY_STRING_ARRAY = new String [0]; 251 private final static OpenType [] EMPTY_OPENTYPES = new OpenType [0]; 252 253 256 public static CompositeType 257 getEmptyCompositeType() 258 throws OpenDataException 259 { 260 return( new CompositeType ( 261 CompositeType .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 |