KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.*;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Collections JavaDoc;
31
32 import com.sun.appserv.management.util.misc.Output;
33
34
35
36 /**
37     This class allows use of MBeanServerConnection methods with ObjectName patterns
38     that resolve to a single MBean. This is useful to avoid hard-coupling to specific
39     ObjectNames; instead an ObjectName pattern may be used which resolves to a
40     single MBean.
41     
42     For example, if you know the 'name' property is unique (at least for your MBean),
43     you could use the ObjectName "*:name=myname,*" instead of a possibly much longer
44     and complicated name (which potentially could change each time the MBean is registered).
45  */

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 JavaDoc [] EMPTY_ARRAY = new Object JavaDoc [ 0 ];
84         
85     private final class PerfHook extends MBeanServerConnection_Hook.HookImpl
86     {
87         private final Map JavaDoc<Long JavaDoc,Long JavaDoc> mTimers;
88         
89             public
90         PerfHook()
91         {
92             mTimers = Collections.synchronizedMap( new HashMap JavaDoc<Long JavaDoc,Long JavaDoc>() );
93         }
94         
95             private final void
96         print( Object JavaDoc o )
97         {
98             if ( getPerf() )
99             {
100                 mOutput.printDebug( o );
101             }
102         }
103         
104             public long
105         preHook( String JavaDoc methodName )
106         {
107             return( preHook( methodName, EMPTY_ARRAY ) );
108         }
109         
110             public long
111         preHook( String JavaDoc methodName, Object JavaDoc [] args )
112         {
113             final long id = super.preHook( methodName, args);
114
115             final Long JavaDoc start = new Long JavaDoc( System.currentTimeMillis() );
116             mTimers.put( new Long JavaDoc( id ), start );
117             
118             return( id );
119         }
120         
121             private void
122         printTime( final long id, final String JavaDoc methodName, final Object JavaDoc[] args )
123         {
124             final long curTime = System.currentTimeMillis();
125             final Long JavaDoc start = (Long JavaDoc)mTimers.remove( new Long JavaDoc( 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 JavaDoc methodName )
135         {
136             printTime( id, methodName, null);
137         }
138         
139             public void
140         postHook( long id, String JavaDoc methodName, Object JavaDoc [] args )
141         {
142             printTime( id, methodName, args);
143         }
144         
145             public void
146         postHook( long id, String JavaDoc methodName, Object JavaDoc [] args, Object JavaDoc result )
147         {
148             printTime( id, methodName, args);
149         }
150     }
151 };
152
153
Popular Tags