KickJava   Java API By Example, From Geeks To Geeks.

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


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

29 package com.sun.enterprise.jmx.kstat;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import com.sun.cli.util.ClassUtil;
35 import com.sun.cli.util.stringifier.SmartStringifier;
36
37 public final class kstat
38 {
39     final String JavaDoc mModuleName;
40     final int mInstanceNumber;
41     final String JavaDoc mkstatName;
42     final HashMap JavaDoc mAttributes;
43     
44     public static class kstatAttribute
45     {
46         public final String JavaDoc mName;
47         public final Object JavaDoc mValue;
48         
49         
50             private static Object JavaDoc
51         tryType( Class JavaDoc theClass, String JavaDoc input )
52         {
53             Object JavaDoc value = null;
54             
55             try
56             {
57                 value = ClassUtil.InstantiateFromString( theClass, input );
58             }
59             catch( Exception JavaDoc e )
60             {
61                 // ignore
62
}
63             return( value );
64         }
65         
66             private static Object JavaDoc
67         createValue( String JavaDoc input )
68         {
69             Object JavaDoc value = tryType( Long JavaDoc.class, input );
70             if ( value == null )
71             {
72                 value = tryType( Double JavaDoc.class, input );
73             }
74             if ( value == null )
75             {
76                 value = input;
77             }
78             
79             return( value );
80         }
81         
82             public
83         kstatAttribute( String JavaDoc name, String JavaDoc value )
84         {
85             mName = name;
86             mValue = createValue( value );
87         }
88             public String JavaDoc
89         toString()
90         {
91             return( mName + "=" + mValue );
92         }
93     };
94     
95         public
96     kstat(
97         String JavaDoc moduleName,
98         int instanceNumber,
99         String JavaDoc kstatName )
100     {
101         mModuleName = moduleName;
102         mkstatName = kstatName;
103         mInstanceNumber = instanceNumber;
104         mAttributes = new HashMap JavaDoc();
105     }
106         public void
107     addAttribute( kstatAttribute attr )
108     {
109         mAttributes.put( attr.mName, attr );
110     }
111     
112         String JavaDoc
113     getModuleName()
114     {
115         return( mModuleName );
116     }
117     
118         int
119     getInstanceNumber()
120     {
121         return( mInstanceNumber );
122     }
123     
124         String JavaDoc
125     getName()
126     {
127         return( mkstatName );
128     }
129     
130         Class JavaDoc
131     getAttributeType( String JavaDoc name )
132     {
133         final Object JavaDoc value = getValue( name );
134         return( value.getClass() );
135     }
136     
137         String JavaDoc
138     getScopedName( char delim )
139     {
140         return( getScopedName( mModuleName, mInstanceNumber, mkstatName, delim ) );
141     }
142     
143         String JavaDoc
144     getScopedName( )
145     {
146         return( getScopedName( ':' ) );
147     }
148     
149         public static String JavaDoc
150     getScopedName( String JavaDoc moduleName, int instanceNumber, String JavaDoc name, char delim)
151     {
152         return( moduleName + delim + instanceNumber + delim + name );
153     }
154     
155         public static String JavaDoc
156     getScopedName( String JavaDoc moduleName, int instanceNumber, String JavaDoc name )
157     {
158         return( getScopedName( moduleName, instanceNumber, name, ':' ) );
159     }
160     
161         Set JavaDoc
162     getAttributeNames()
163     {
164         return( mAttributes.keySet() );
165     }
166
167         public Object JavaDoc
168     getValue( String JavaDoc attributeName )
169     {
170         final kstatAttribute attr = (kstatAttribute)mAttributes.get( attributeName );
171         
172         Object JavaDoc value = null;
173         if ( attr != null )
174         {
175             value = attr.mValue;
176         }
177         
178         return( value );
179     }
180     
181         public String JavaDoc
182     toString()
183     {
184         return( getScopedName( ':' ) + "\n" + SmartStringifier.toString( mAttributes ) );
185     }
186 };
187
188
189
Popular Tags