KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > j2ee > stringifier > StatisticStringifier


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.j2ee.stringifier;
24
25 import java.util.Arrays JavaDoc;
26 import java.util.SortedMap JavaDoc;
27 import java.util.TreeMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.management.j2ee.statistics.Statistic JavaDoc;
31
32 import com.sun.appserv.management.util.stringifier.Stringifier;
33 import com.sun.appserv.management.util.stringifier.SmartStringifier;
34 import com.sun.appserv.management.util.misc.StringUtil;
35
36 import com.sun.appserv.management.util.j2ee.J2EEUtil;
37
38 /**
39     Stringifier for javax.management.j2ee.Statistic
40  */

41 public class StatisticStringifier implements Stringifier
42 {
43     public static final StatisticStringifier DEFAULT = new StatisticStringifier();
44     
45         public
46     StatisticStringifier( )
47     {
48     }
49     
50     private final static String JavaDoc DELIM = ", ";
51     private final static String JavaDoc GET = "get";
52     
53     
54     private static final String JavaDoc[] ORDERED_VALUES =
55     new String JavaDoc[]
56     {
57         "Name",
58         "Description",
59         "Unit",
60         "StartTime",
61         "LastSampleTime",
62         "Count",
63         "Low", "Current", "High",
64         "LowerBound", "UpperBound",
65         "Current",
66     };
67     
68         private static String JavaDoc
69     createNameValuePair( final String JavaDoc name, final Object JavaDoc value )
70     {
71         final String JavaDoc valueString = value instanceof String JavaDoc ?
72             StringUtil.quote( (String JavaDoc)value ) : SmartStringifier.toString( value );
73             
74         return name + "=" + valueString;
75     }
76     
77     /**
78         Stringify by accessing all public getXXX() methods that return a value
79         and don't throw exceptions.
80      */

81         public String JavaDoc
82     stringify( Object JavaDoc o )
83     {
84         final Statistic JavaDoc statistic = (Statistic JavaDoc)o;
85         
86         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
87         
88         buf.append( statistic.getName() + ": " );
89         
90         final SortedMap JavaDoc<String JavaDoc,Object JavaDoc> pairs =
91             new TreeMap JavaDoc<String JavaDoc,Object JavaDoc>( J2EEUtil.statisticToMap( statistic ) );
92         
93         // first emit the standard value names in a proscribed order
94
for( int i = 0; i < ORDERED_VALUES.length; ++i )
95         {
96             final String JavaDoc name = ORDERED_VALUES[ i ];
97             if ( pairs.containsKey( name ) )
98             {
99                 final Object JavaDoc value = pairs.get( name );
100                 buf.append( createNameValuePair( name, value ) );
101                 buf.append( DELIM );
102                 pairs.remove( name );
103             }
104         }
105         
106         
107         final Iterator JavaDoc iter = pairs.keySet().iterator();
108         while ( iter.hasNext() )
109         {
110             final String JavaDoc name = (String JavaDoc)iter.next();
111             final Object JavaDoc value = pairs.get( name );
112             
113             buf.append( createNameValuePair( name, value ) );
114             buf.append( DELIM );
115             
116         }
117         
118         String JavaDoc result = buf.toString();
119         if ( result.endsWith( DELIM ) )
120         {
121             result = result.substring( 0, result.length() - DELIM.length() );
122         }
123         
124         
125         return( result );
126     }
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Popular Tags