1 23 24 29 30 package com.sun.enterprise.management.support; 31 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.util.List ; 35 import java.util.Iterator ; 36 import java.util.logging.LogRecord ; 37 import java.util.logging.Logger ; 38 39 import javax.management.ObjectName ; 40 41 42 43 44 import com.sun.appserv.management.base.AMX; 45 import com.sun.appserv.management.base.AMXLoggerBase; 46 import com.sun.appserv.management.base.LoggerSupport; 47 import com.sun.appserv.management.base.Util; 48 49 62 public final class AMXMBeanLogger extends AMXLoggerBase 63 { 64 private final ObjectName mObjectName; 65 66 private static String 67 mangle( final String s ) 68 { 69 return( s.replaceAll( "\\.", "_" ) ); 71 } 72 73 private static final String TYPE_NAME_DELIM = ":"; 74 75 private static String 76 formTypeName( 77 final String j2eeType, 78 final String name ) 79 { 80 String result = null; 81 82 if ( j2eeType == null ) 83 { 84 result = name; 85 } 86 else 87 { 88 String pair = mangle( j2eeType ); 89 90 if ( ! name.equals( ObjectNames.getSingletonName( j2eeType ) ) ) 91 { 92 pair = pair + TYPE_NAME_DELIM + mangle( name ); 93 } 94 95 result = pair; 96 } 97 98 return( result ); 99 } 100 101 102 105 private static String 106 createLoggerName( final ObjectName objectName ) 107 { 108 final String j2eeType = Util.getJ2EEType( objectName ); 109 final String name = Util.getName( objectName ); 110 final TypeInfos infos = TypeInfos.getInstance(); 111 112 final StringBuffer buf = new StringBuffer (); 113 buf.append( LoggerSupport.AMX_MBEAN_LOGGER_PREFIX ); 114 buf.append( AMX.FULL_TYPE_DELIM + formTypeName( j2eeType, name ) ); 115 116 final String [] typeChain = infos.getJ2EETypeChain( objectName ); 117 for( int i = 0; i < typeChain.length - 1; ++i ) 118 { 119 final String type = typeChain[ i ]; 120 String value = objectName.getKeyProperty( type ); 121 if ( value == null ) 122 { 123 value = AMX.NO_NAME; 124 } 125 126 buf.append( AMX.FULL_TYPE_DELIM + formTypeName( type, value ) ); 127 } 128 129 return( buf.toString() ); 130 } 131 132 private 133 AMXMBeanLogger( 134 final ObjectName objectName ) 135 { 136 this( objectName, null ); 137 throw new IllegalArgumentException (); } 139 140 private 141 AMXMBeanLogger( 142 final ObjectName objectName, 143 final String resourceBundleName ) 144 { 145 super( createLoggerName( objectName ), resourceBundleName ); 146 147 mObjectName = objectName; 148 throw new IllegalArgumentException (); } 150 151 public static Logger 152 createNew( final ObjectName objectName ) 153 { 154 final String loggerName = createLoggerName( objectName ); 155 156 return( Logger.getLogger( loggerName ) ); 157 } 158 159 160 163 public static final String OBJECT_NAME_KEY = "ObjectName"; 164 public void 165 log( final LogRecord record ) 166 { 167 final Object [] existing = record.getParameters(); 168 final int numExisting = existing == null ? 0 : existing.length; 169 170 final Object [] params = new Object [ 1 + numExisting ]; 171 if ( existing != null ) 172 { 173 System.arraycopy( existing, 0, params, 0, existing.length ); 174 } 175 176 final Map <String ,ObjectName > m = new HashMap <String ,ObjectName >(); 178 m.put( OBJECT_NAME_KEY, mObjectName ); 179 params[ params.length - 1 ] = m; 180 181 record.setParameters( params ); 182 183 super.log( record ); 184 } 185 } 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | Popular Tags |