KickJava   Java API By Example, From Geeks To Geeks.

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


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.j2ee.statistics;
24
25 import java.util.Map JavaDoc;
26
27 import javax.management.openmbean.CompositeData JavaDoc;
28 import javax.management.openmbean.CompositeType JavaDoc;
29
30 import javax.management.j2ee.statistics.Statistic JavaDoc;
31 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
32 import javax.management.j2ee.statistics.RangeStatistic JavaDoc;
33 import javax.management.j2ee.statistics.BoundaryStatistic JavaDoc;
34 import javax.management.j2ee.statistics.BoundedRangeStatistic JavaDoc;
35 import javax.management.j2ee.statistics.TimeStatistic JavaDoc;
36
37 import com.sun.appserv.management.util.jmx.OpenMBeanUtil;
38 import com.sun.appserv.management.util.misc.ClassUtil;
39 import com.sun.appserv.management.util.misc.TypeCast;
40
41 /**
42     Creates Statistic implementations based on CompositeData or Map.
43  */

44 public final class StatisticFactory
45 {
46     private StatisticFactory() {}
47     
48     
49     /**
50         Create a new Statistic using the specified CompositeData
51         
52         @param theInterface interface which the Statistic should implement, must extend Statistic
53      */

54         public static Statistic JavaDoc
55     create( Class JavaDoc<? extends Statistic JavaDoc> theInterface, final CompositeData JavaDoc data )
56     {
57         return( create( theInterface, OpenMBeanUtil.compositeDataToMap( data ) ) );
58     }
59     
60     private static final String JavaDoc COUNT_STATISTIC = CountStatistic JavaDoc.class.getName();
61     private static final String JavaDoc TIME_STATISTIC = TimeStatistic JavaDoc.class.getName();
62     private static final String JavaDoc RANGE_STATISTIC = RangeStatistic JavaDoc.class.getName();
63     private static final String JavaDoc BOUNDARY_STATISTIC = BoundaryStatistic JavaDoc.class.getName();
64     private static final String JavaDoc BOUNDED_RANGE_STATISTIC = BoundedRangeStatistic JavaDoc.class.getName();
65     private static final String JavaDoc STRING_STATISTIC = StringStatistic.class.getName();
66     private static final String JavaDoc MAP_STATISTIC = MapStatistic.class.getName();
67     private static final String JavaDoc NUMBER_STATISTIC = NumberStatistic.class.getName();
68     
69     
70     
71     /**
72         Create a new Statistic using the specified CompositeData.
73         The CompositeType of data must be an appropriate Statistic class.
74      */

75         public static Statistic JavaDoc
76     create( final CompositeData JavaDoc data )
77     {
78         final String JavaDoc typeName = data.getCompositeType().getTypeName();
79         Class JavaDoc<? extends Statistic JavaDoc> theInterface = null;
80         if ( typeName.equals( COUNT_STATISTIC ) )
81         {
82             theInterface = CountStatistic JavaDoc.class;
83         }
84         else if ( typeName.equals( TIME_STATISTIC ) )
85         {
86             theInterface = TimeStatistic JavaDoc.class;
87         }
88         else if ( typeName.equals( RANGE_STATISTIC ) )
89         {
90             theInterface = RangeStatistic JavaDoc.class;
91         }
92         else if ( typeName.equals( BOUNDARY_STATISTIC ) )
93         {
94             theInterface = BoundaryStatistic JavaDoc.class;
95         }
96         else if ( typeName.equals( BOUNDED_RANGE_STATISTIC ) )
97         {
98             theInterface = BoundedRangeStatistic JavaDoc.class;
99         }
100         else if ( typeName.equals( STRING_STATISTIC ) )
101         {
102             theInterface = StringStatistic.class;
103         }
104         else if ( typeName.equals( NUMBER_STATISTIC ) )
105         {
106             theInterface = NumberStatistic.class;
107         }
108         else if ( typeName.equals( MAP_STATISTIC ) )
109         {
110             theInterface = MapStatistic.class;
111         }
112         else
113         {
114             try
115             {
116                 theInterface = TypeCast.asClass( ClassUtil.classForName( typeName ) );
117             }
118             catch( Exception JavaDoc e )
119             {
120                 theInterface = Statistic JavaDoc.class;
121             }
122         }
123         
124         return( create( theInterface, data ) );
125     }
126     
127     
128     /**
129         Create a new Statistic using the specified map. The standard JSR 77
130         types are handled appropriately. Custom (non-standard) Statistics
131         may also be used; in this case a proxy is returned which implements
132         the interface specified by theClass.
133         
134         @param theInterface the interface which the resulting statistic implements
135         @param mappings a Map containing keys of type String and their Object values
136      */

137         public static Statistic JavaDoc
138     create( Class JavaDoc<? extends Statistic JavaDoc> theInterface, final Map JavaDoc<String JavaDoc,?> mappings )
139     {
140         Statistic JavaDoc result = null;
141         
142         // hopefully specific classes are faster than a proxy...
143
if ( theInterface == CountStatistic JavaDoc.class )
144         {
145             result = new CountStatisticImpl( mappings );
146         }
147         else if ( theInterface == RangeStatistic JavaDoc.class )
148         {
149             result = new RangeStatisticImpl( mappings );
150         }
151         else if ( theInterface == BoundaryStatistic JavaDoc.class )
152         {
153             result = new BoundaryStatisticImpl( mappings );
154         }
155         else if ( theInterface == BoundedRangeStatistic JavaDoc.class )
156         {
157             result = new BoundedRangeStatisticImpl( mappings );
158         }
159         else if ( theInterface == TimeStatistic JavaDoc.class )
160         {
161             result = new TimeStatisticImpl( mappings );
162         }
163         else if ( theInterface == StringStatistic.class )
164         {
165             result = new StringStatisticImpl( mappings );
166         }
167         else if ( theInterface == NumberStatistic.class )
168         {
169             result = new NumberStatisticImpl( mappings );
170         }
171         else if ( theInterface == MapStatistic.class )
172         {
173             result = new MapStatisticImpl( mappings );
174         }
175         else
176         {
177             throw new IllegalArgumentException JavaDoc(
178                 "Unsupported Statistic interface: " + theInterface.getName() );
179         }
180         
181         return( result );
182     }
183     
184
185     
186     private static final Class JavaDoc<? extends Statistic JavaDoc>[] KNOWN_STATISTICS = TypeCast.asArray( new Class JavaDoc[]
187     {
188         CountStatistic JavaDoc.class,
189         TimeStatistic JavaDoc.class,
190         BoundedRangeStatistic JavaDoc.class, // must come before RangeStatistic,BoundaryStatistic
191
RangeStatistic JavaDoc.class,
192         BoundaryStatistic JavaDoc.class,
193         StringStatistic.class,
194         NumberStatistic.class,
195     } );
196     
197     private static final String JavaDoc INTERNAL_STRING_STATISTIC_CLASSNAME =
198         "com.sun.enterprise.admin.monitor.stats.StringStatisticImpl";
199         
200         public static Class JavaDoc<? extends Statistic JavaDoc>
201     getInterface( final Statistic JavaDoc s )
202     {
203         final Class JavaDoc<? extends Statistic JavaDoc> implClass = s.getClass();
204         
205         Class JavaDoc<? extends Statistic JavaDoc> theInterface = MapStatistic.class;
206         
207         for( int i = 0; i < KNOWN_STATISTICS.length; ++i )
208         {
209             final Class JavaDoc<? extends Statistic JavaDoc> candidateInterface = KNOWN_STATISTICS[ i ];
210             
211             if ( candidateInterface.isAssignableFrom( implClass ) )
212             {
213                 theInterface = candidateInterface;
214                 break;
215             }
216         }
217         
218         if ( theInterface == MapStatistic.class && ! (s instanceof MapStatisticImpl) )
219         {
220             if ( s.getClass().getName().equals( INTERNAL_STRING_STATISTIC_CLASSNAME ) )
221             {
222                 theInterface = StringStatistic.class;
223             }
224             else
225             {
226                 throw new IllegalArgumentException JavaDoc( "Unknown statistic class: " + s.getClass().getName() );
227             }
228         }
229         
230         return( theInterface );
231     }
232     
233         
234
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
Popular Tags