KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > util > MBeanServerConnection_Debug


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  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/util/MBeanServerConnection_Debug.java,v 1.3 2005/12/25 03:45:55 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:55 $
28  */

29 package com.sun.cli.jmx.util;
30
31 import javax.management.*;
32 import java.io.IOException JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import com.sun.cli.util.DebugState;
36
37
38
39 /*
40     This class allows use of MBeanServerConnection methods with ObjectName patterns
41     that resolve to a single MBean. This is useful to avoid hard-coupling to specific
42     ObjectNames; instead an ObjectName pattern may be used which resolves to a
43     single MBean.
44     
45     For example, if you know the 'name' property is unique (at least for your MBean),
46     you could use the ObjectName "*:name=myname,*" instead of a possibly much longer
47     and complicated name (which potentially could change each time the MBean is registered).
48  */

49 public class MBeanServerConnection_Debug
50     extends MBeanServerConnection_Hook
51 {
52     final MBeanServerConnection_Hook.Hook mHook;
53     final DebugState mDebugState;
54     
55         public
56     MBeanServerConnection_Debug( MBeanServerConnection impl, DebugState debugState )
57     {
58         super( impl );
59         
60         mDebugState = debugState;
61         mHook = new DebugHook();
62     }
63     
64         Hook
65     getHook()
66     {
67         return( mHook );
68     }
69     
70         final DebugState
71     getDebugState()
72     {
73         return( mDebugState );
74     }
75     
76     /*
77         Hook which resolves a name pattern to a single name
78     */

79     private final static Object JavaDoc [] EMPTY_ARRAY = new Object JavaDoc [ 0 ];
80         
81     class DebugHook extends MBeanServerConnection_Hook.HookImpl
82     {
83             public
84         DebugHook( )
85         {
86             printDebug( "MBeanServerConnection_Debug: instantiated" );
87         }
88         
89             final void
90         printDebug( Object JavaDoc o )
91         {
92             if ( getDebugState().getDebug() )
93             {
94                 getDebugState().printDebug( o );
95             }
96         }
97         
98             
99             String JavaDoc
100         getInvocationString( long id, String JavaDoc methodName, Object JavaDoc [] args )
101         {
102             String JavaDoc msg = id + ": " + methodName + "(";
103             
104             for( int i = 0; i < args.length; ++i )
105             {
106                 msg = msg + args[ i ].getClass().getName();
107                 if ( i != args.length - 1 )
108                 {
109                     msg = msg + ",";
110                 }
111             }
112             msg = msg + ")";
113             
114             return( msg );
115         }
116         
117             public long
118         preHook( String JavaDoc methodName )
119         {
120             return( preHook( methodName, EMPTY_ARRAY ) );
121         }
122         
123             public long
124         preHook( String JavaDoc methodName, Object JavaDoc [] args )
125         {
126             final long id = getNewID();
127             
128             printDebug( "pre: " + getInvocationString( id, methodName, args ) );
129             
130             return( id );
131         }
132         
133             public void
134         postHook( long id, String JavaDoc methodName )
135         {
136             printDebug( "post: " + getInvocationString( id, methodName, null ) );
137         }
138         
139             public void
140         postHook( long id, String JavaDoc methodName, Object JavaDoc [] args )
141         {
142             printDebug( "post: " + getInvocationString( id, methodName, args ) );
143         }
144         
145             public void
146         postHook( long id, String JavaDoc methodName, Object JavaDoc [] args, Object JavaDoc result )
147         {
148             printDebug( "post: " +
149                 getInvocationString( id, methodName, args ) +
150                 result.getClass().getName() + " => " + result.toString()
151                 );
152         }
153     }
154 };
155
156
Popular Tags