KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > jmx > kstat > kstatMBean


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/enterprise/jmx/kstat/kstatMBean.java,v 1.3 2005/12/25 03:46:10 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:46:10 $
28  */

29 package com.sun.enterprise.jmx.kstat;
30
31 import javax.management.*;
32
33 import java.lang.reflect.Method JavaDoc;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.Iterator JavaDoc;
38
39
40 /*
41     Solaris specific MBean to support kstat
42  */

43 public class kstatMBean implements DynamicMBean
44 {
45     private final kstat mSource;
46     private final kstatMgrMBean mMgr;
47     private MBeanInfo mMBeanInfo;
48     
49         public
50     kstatMBean( kstatMgrMBean mgr, final kstat source )
51     {
52         mSource = source;
53         mMgr = mgr;
54     }
55
56         public AttributeList
57     getAttributes( String JavaDoc [] names )
58     {
59         final AttributeList list = new AttributeList();
60         
61         for( int i = 0; i < names.length; ++i )
62         {
63             final Object JavaDoc value = _getAttribute( names[ i ] );
64             if ( value != null )
65             {
66                 list.add( new Attribute( names[ i ], value ) );
67             }
68         }
69
70         return( list );
71     }
72     
73         private Object JavaDoc
74     _getAttribute( String JavaDoc name )
75     {
76         final Object JavaDoc value = mSource.getValue( name );
77         
78         return( value );
79     }
80     
81         public Object JavaDoc
82     getAttribute( String JavaDoc name )
83     {
84         return( _getAttribute( name ) );
85     }
86     
87         public AttributeList
88     setAttributes( AttributeList names )
89     {
90         throw new java.lang.UnsupportedOperationException JavaDoc( "can't set kstat value" );
91     }
92     
93         public void
94     setAttribute( Attribute attr )
95     {
96         throw new java.lang.UnsupportedOperationException JavaDoc( "can't set kstat value" );
97     }
98     
99         private Object JavaDoc
100     internalInvoke( String JavaDoc actionName, Object JavaDoc[] params, String JavaDoc[] signature )
101         throws Exception JavaDoc
102     {
103         Object JavaDoc result = null;
104         
105         if ( actionName.equals( "refresh" ) )
106         {
107             refresh();
108         }
109         else
110         {
111             throw new OperationsException( "No such method: " + actionName );
112         }
113         
114         return( result );
115     }
116     
117     
118         public Object JavaDoc
119     invoke( String JavaDoc actionName, Object JavaDoc[] params, String JavaDoc[] signature )
120         throws MBeanException, ReflectionException
121     {
122         Object JavaDoc result = null;
123         
124         try
125         {
126             result = internalInvoke( actionName, params, signature );
127         }
128         catch( MBeanException e )
129         {
130             throw e;
131         }
132         catch( ReflectionException e )
133         {
134             throw e;
135         }
136         catch( Exception JavaDoc e )
137         {
138             throw new MBeanException( e );
139         }
140         
141         return( result );
142     }
143     
144         private MBeanAttributeInfo
145     createAttrInfo( String JavaDoc attrName )
146     {
147         final MBeanAttributeInfo info = new MBeanAttributeInfo(
148             attrName,
149             mSource.getAttributeType( attrName ).getName(),
150             "",
151             true, false, false );
152         
153         return( info );
154     }
155     
156         private MBeanAttributeInfo []
157     createAttrInfos()
158     {
159         final ArrayList JavaDoc list = new ArrayList JavaDoc();
160         
161         final Set JavaDoc attrNames = mSource.getAttributeNames();
162         final Iterator JavaDoc iter = attrNames.iterator();
163         while ( iter.hasNext() )
164         {
165             final String JavaDoc attrName = (String JavaDoc)iter.next();
166             try
167             {
168                 final MBeanAttributeInfo info = createAttrInfo( attrName );
169                 
170                 list.add( info );
171             }
172             catch( IllegalArgumentException JavaDoc e )
173             {
174                 // don't add it, but continue
175
//System.out.println( "createAttrInfos: illegal Attribute name: " + attrNames[ i ]);
176
}
177         }
178         
179         final MBeanAttributeInfo [] infos = new MBeanAttributeInfo [ list.size() ];
180         list.toArray( infos );
181         
182         return( infos );
183     }
184     
185         public void
186     refresh()
187         throws Exception JavaDoc
188     {
189         mMgr.refresh( mSource.getScopedName() );
190     }
191     
192     
193         private MBeanOperationInfo []
194     createOperationInfos()
195     {
196         final MBeanOperationInfo [] infos = new MBeanOperationInfo[ 1 ];
197         
198         try
199         {
200             final Method JavaDoc m = this.getClass().getDeclaredMethod( "refresh", null);
201             infos[ 0 ] = new MBeanOperationInfo(
202                 "refresh all attributes for " + mSource.getScopedName(),
203                 m );
204         }
205         catch( NoSuchMethodException JavaDoc e )
206         {
207             assert( false );
208         }
209         
210         return( infos );
211     }
212     
213         public synchronized MBeanInfo
214     getMBeanInfo( )
215     {
216         if ( mMBeanInfo == null )
217         {
218             final MBeanAttributeInfo [] attrInfo = createAttrInfos();
219             final MBeanOperationInfo [] operationInfo = createOperationInfos();
220             final MBeanConstructorInfo [] constructorInfo = new MBeanConstructorInfo[0];
221             final MBeanNotificationInfo [] notificationInfo = new MBeanNotificationInfo[0];
222             
223             final MBeanInfo info = new MBeanInfo(
224                 this.getClass().getName(),
225                 mSource.getScopedName(),
226                 attrInfo,
227                 constructorInfo,
228                 operationInfo,
229                 notificationInfo );
230             
231             mMBeanInfo = info;
232         }
233         
234         return( mMBeanInfo );
235     }
236 };
237
238
239
Popular Tags