KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > j2ee > statistics > GetterInvocationHandler


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-core/mbeanapi/src/java/com/sun/appserv/management/j2ee/statistics/GetterInvocationHandler.java,v 1.4 2006/03/09 20:30:29 llc Exp $
26  * $Revision: 1.4 $
27  * $Date: 2006/03/09 20:30:29 $
28  */

29
30 package com.sun.appserv.management.j2ee.statistics;
31
32 import java.io.Serializable JavaDoc;
33
34 import java.lang.reflect.InvocationHandler JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36
37 import com.sun.appserv.management.util.jmx.JMXUtil;
38 import com.sun.appserv.management.util.misc.StringUtil;
39
40 /**
41     Abstract base {@link InvocationHandler} for any getXXX() method.
42     <br><b>Internal use only</b>
43  */

44 public abstract class GetterInvocationHandler<T> implements InvocationHandler JavaDoc,Serializable JavaDoc
45 {
46     static final long serialVersionUID = 7293181901362984709L;
47
48     /**
49      */

50         public
51     GetterInvocationHandler()
52     {
53     }
54     
55     protected abstract T getValue( String JavaDoc name );
56     protected abstract boolean containsValue( String JavaDoc name );
57     
58     /**
59     */

60         public Object JavaDoc
61     invoke(
62         Object JavaDoc myProxy,
63         Method JavaDoc method,
64         Object JavaDoc[] args )
65         throws java.lang.Throwable JavaDoc
66     {
67         Object JavaDoc result = null;
68         final String JavaDoc methodName = method.getName();
69         final int numArgs = args == null ? 0 : args.length;
70         
71         if ( numArgs == 0 && JMXUtil.isGetter( method ) )
72         {
73             final String JavaDoc name = StringUtil.stripPrefix( methodName, JMXUtil.GET );
74             
75             result = getValue( name );
76             if ( result == null && ! containsValue( name ) )
77             {
78                 throw new NoSuchMethodException JavaDoc( methodName );
79             }
80         }
81         else if ( method.getName().equals( "equals" ) &&
82             numArgs == 1 )
83         {
84             result = new Boolean JavaDoc( equals( args[ 0 ] ) );
85         }
86         else if ( numArgs == 0 && method.getName().equals( "toString" ) &&
87             method.getReturnType() == String JavaDoc.class )
88         {
89             result = this.toString();
90         }
91         else if ( numArgs == 0 && method.getName().equals( "hashCode" ) &&
92             method.getReturnType() == int.class )
93         {
94             result = new Integer JavaDoc( this.hashCode() );
95         }
96         else
97         {
98             throw new IllegalArgumentException JavaDoc( methodName );
99         }
100
101         return( result );
102     }
103    
104 }
105
106
107
108
109
110
Popular Tags