1 23 package com.sun.appserv.management.util.jmx; 24 25 import javax.management.*; 26 import java.io.IOException ; 27 import java.io.PrintStream ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.Collections ; 31 32 import com.sun.appserv.management.util.misc.Output; 33 34 35 36 46 public class MBeanServerConnection_Perf 47 extends MBeanServerConnection_Hook 48 { 49 private final PerfHook mHook; 50 private final Output mOutput; 51 private boolean mPerfEnabled; 52 53 public 54 MBeanServerConnection_Perf( 55 MBeanServerConnection impl, 56 Output output ) 57 { 58 super( impl ); 59 60 mOutput = output; 61 mHook = new PerfHook(); 62 mPerfEnabled = true; 63 } 64 65 public final boolean 66 getPerf() 67 { 68 return( mPerfEnabled ); 69 } 70 71 public final void 72 setPerf( final boolean perfEnabled) 73 { 74 mPerfEnabled = perfEnabled; 75 } 76 77 protected final Hook 78 getHook() 79 { 80 return( mHook ); 81 } 82 83 private final static Object [] EMPTY_ARRAY = new Object [ 0 ]; 84 85 private final class PerfHook extends MBeanServerConnection_Hook.HookImpl 86 { 87 private final Map <Long ,Long > mTimers; 88 89 public 90 PerfHook() 91 { 92 mTimers = Collections.synchronizedMap( new HashMap <Long ,Long >() ); 93 } 94 95 private final void 96 print( Object o ) 97 { 98 if ( getPerf() ) 99 { 100 mOutput.printDebug( o ); 101 } 102 } 103 104 public long 105 preHook( String methodName ) 106 { 107 return( preHook( methodName, EMPTY_ARRAY ) ); 108 } 109 110 public long 111 preHook( String methodName, Object [] args ) 112 { 113 final long id = super.preHook( methodName, args); 114 115 final Long start = new Long ( System.currentTimeMillis() ); 116 mTimers.put( new Long ( id ), start ); 117 118 return( id ); 119 } 120 121 private void 122 printTime( final long id, final String methodName, final Object [] args ) 123 { 124 final long curTime = System.currentTimeMillis(); 125 final Long start = (Long )mTimers.remove( new Long ( id ) ); 126 if ( start != null ) 127 { 128 mOutput.println( getInvocationString( methodName, args ) + ": " + 129 (curTime - start.longValue()) ); 130 } 131 } 132 133 public void 134 postHook( long id, String methodName ) 135 { 136 printTime( id, methodName, null); 137 } 138 139 public void 140 postHook( long id, String methodName, Object [] args ) 141 { 142 printTime( id, methodName, args); 143 } 144 145 public void 146 postHook( long id, String methodName, Object [] args, Object result ) 147 { 148 printTime( id, methodName, args); 149 } 150 } 151 }; 152 153 | Popular Tags |