1 23 package com.sun.enterprise.server.logging; 24 25 import java.util.Set ; 26 27 import java.util.logging.LogRecord ; 28 import java.util.logging.Level ; 29 import java.util.logging.Formatter ; 30 31 import javax.management.ObjectName ; 32 import javax.management.MBeanServer ; 33 34 import java.lang.reflect.Method ; 35 import java.lang.reflect.Constructor ; 36 37 42 final class AMXLoggingHook { 44 private ObjectName mLoggingObjectName; 45 private MBeanServer mMBeanServer; 46 private boolean mLoggingAvailable; 47 private LoggingImplHook mLoggingImplHook; 48 49 55 private Level mMinimumLogLevel; 56 57 61 64 private static final boolean DEBUG = false; 65 private final Output mOut; 66 private final String mServerName; 67 68 private void 69 dumpSystemProps( final Output output ) 70 { 71 final java.util.Properties props = System.getProperties(); 72 73 final String [] keys = (String [])props.keySet().toArray( new String [0] ); 74 java.util.Arrays.sort( keys ); 75 for( final String key : keys ) 76 { 77 debug( key + "=" + props.getProperty( key ) ); 78 } 79 80 } 81 82 AMXLoggingHook() 83 { 84 mServerName = System.getProperty( 85 com.sun.enterprise.util.SystemPropertyConstants.SERVER_NAME); 86 final String instanceRoot = System.getProperty( 87 com.sun.enterprise.util.SystemPropertyConstants.INSTANCE_ROOT_PROPERTY ); 88 89 mOut = createOutput( DEBUG, instanceRoot + "/AMXLoggingHook-" + mServerName + ".debug" ); 90 94 96 mLoggingObjectName = null; 97 mMBeanServer = getMBeanServer(); 99 mMinimumLogLevel = Level.FINEST; 100 mLoggingImplHook = null; 101 102 } 103 104 private static Output 105 createOutput( final boolean debug, final String fileName ) 106 { 107 try 108 { 109 final java.io.File f = new java.io.File ( fileName ); 110 111 return debug ? new FileOutput( f ) : new NullOutput(); 112 } 113 catch( Throwable t ) 114 { 115 } 117 118 return new NullOutput(); 119 } 120 121 private final void debug( final Object o ) { mOut.println( o.toString() ); } 122 123 private MBeanServer 124 getMBeanServer() 125 { 126 if ( mMBeanServer == null ) 127 { 128 mMBeanServer = 129 com.sun.enterprise.admin.common.MBeanServerFactory.getMBeanServer(); 130 if ( mMBeanServer != null ) 132 { 133 debug( "MBeanServer NOW EXISTS, creating LoggingImpl" ); 134 initLogging( mMBeanServer ); 135 } 136 } 137 138 return mMBeanServer; 139 } 140 141 142 private static final String LOGGING_IMPL_CLASSNAME = 143 "com.sun.enterprise.management.ext.logging.LoggingImpl"; 144 147 private void 148 initLogging( final MBeanServer mbeanServer ) 149 { 150 try 151 { 152 final Class loggingClass = Class.forName( LOGGING_IMPL_CLASSNAME ); 153 154 final Constructor constructor = loggingClass.getConstructor( String .class ); 155 mLoggingImplHook = (LoggingImplHook)constructor.newInstance( mServerName ); 156 157 final Method getObjectNameMethod = 158 loggingClass.getMethod( "getObjectName", String .class ); 159 mLoggingObjectName = (ObjectName )getObjectNameMethod.invoke( mLoggingImplHook, mServerName ); 160 debug( "registering Logging as: " + mLoggingObjectName ); 161 mbeanServer.registerMBean( mLoggingImplHook, mLoggingObjectName ); 162 } 163 catch ( Exception e ) 164 { 165 final String msg = "Can't load " + LOGGING_IMPL_CLASSNAME + ", caught: " + e; 166 debug( msg ); 167 throw new Error ( msg ); 168 } 169 } 170 171 public Level 172 getMinimumLogLevel( ) 173 { 174 return mMinimumLogLevel; 175 } 176 177 public void 178 setMinimumLogLevel( final Level level ) 179 { 180 mMinimumLogLevel = level; 181 } 182 183 void 185 publish( 186 final LogRecord record, 187 final Formatter theFormatter ) 188 { 189 if ( record.getLevel().intValue() < mMinimumLogLevel.intValue() ) 190 { 191 return; 192 } 193 194 debug( "publish: " + theFormatter.format( record ) ); 195 196 final MBeanServer server = getMBeanServer(); 197 if ( server != null ) 198 { 199 if ( ! mLoggingAvailable ) 200 { 201 mLoggingAvailable = server.isRegistered( mLoggingObjectName ); 202 if ( mLoggingAvailable ) 203 { 204 debug( "Logging MBean is now available: " + mLoggingObjectName ); 205 } 206 } 207 208 if ( mLoggingAvailable ) 209 { 210 try 211 { 212 mLoggingImplHook.privateLoggingHook( record, theFormatter ); 213 } 216 catch( Throwable t ) 217 { 218 debug( 219 "AMXLoggingHook.publish: Exception calling privateLoggingHook: " + t ); 220 } 222 } 223 else 224 { 225 debug( "publish: no Logging yet." ); 226 } 227 } 228 else 229 { 230 debug( "publish: no MBeanServer yet." ); 231 } 232 } 233 234 235 236 252 253 private interface Output 254 { 255 public void println( Object o ); 256 } 257 258 262 private static final class FileOutput implements Output 263 { 264 private java.io.PrintStream mOut; 265 266 public 268 FileOutput( final java.io.File f) 269 throws java.io.IOException 270 { 271 mOut = new java.io.PrintStream ( new java.io.FileOutputStream ( f ) ); 272 } 273 274 public void 275 println( Object o ) 276 { 277 mOut.println( o.toString() ); 278 } 279 280 public void 281 close( ) 282 { 283 if ( mOut != null ) 284 try 285 { 286 mOut.close(); 287 } 288 finally 289 { 290 mOut = null; 291 } 292 } 293 }; 294 295 private static final class NullOutput implements Output 297 { 298 NullOutput() {} 299 public void println( Object o ) {} 300 } 301 } 302 303 304 305 306 | Popular Tags |