KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.io.Serializable JavaDoc;
31
32 import java.lang.reflect.Method JavaDoc;
33
34 import javax.management.openmbean.CompositeData JavaDoc;
35 import javax.management.openmbean.CompositeType JavaDoc;
36 import javax.management.j2ee.statistics.Stats JavaDoc;
37 import javax.management.j2ee.statistics.Statistic JavaDoc;
38 import com.sun.appserv.management.util.j2ee.J2EEUtil;
39
40 import com.sun.appserv.management.util.misc.GSetUtil;
41
42
43 /**
44     Generic implementation of Stats based on either a Map or a {@link CompositeData}.
45     There are two ways to implement a specific type of Stats object:
46     <ul>
47         <li>
48         The subclass extends this base class, using getValue() to fetch
49         the requested value from the Map maintained by this class.
50         </li>
51         <li>
52         Create a proxy implementing the desired Stats subclass interface and use
53         an instance of this class as the {@link java.lang.reflect.InvocationHandler}.
54         </li>
55     </ul>
56     In addition to the standard JSR 77 Stats interfaces,
57     the following specific Stats interfaces are available:
58     <ul>
59 <li>{@link com.sun.appserv.management.monitor.statistics.AltJDBCConnectionPoolStats}</li>
60 <li>{@link com.sun.appserv.management.monitor.statistics.AltServletStats}</li>
61 <li>{@link com.sun.appserv.management.monitor.statistics.ConnectionManagerStats}</li>
62 <li>{@link com.sun.appserv.management.monitor.statistics.ConnectionPoolStats}</li>
63 <li>{@link com.sun.appserv.management.monitor.statistics.ConnectorConnectionPoolStats}</li>
64 <li>{@link com.sun.appserv.management.monitor.statistics.EJBCacheStats}</li>
65 <li>{@link com.sun.appserv.management.monitor.statistics.EJBMethodStats}</li>
66 <li>{@link com.sun.appserv.management.monitor.statistics.EJBPoolStats}</li>
67 <li>{@link com.sun.appserv.management.monitor.statistics.HTTPListenerStats}</li>
68 <li>{@link com.sun.appserv.management.monitor.statistics.HTTPServiceVirtualServerStats}</li>
69 <li>{@link com.sun.appserv.management.monitor.statistics.KeepAliveStats}</li>
70 <li>{@link com.sun.appserv.management.monitor.statistics.ThreadPoolStats}</li>
71 <li>{@link com.sun.appserv.management.monitor.statistics.TransactionServiceStats}</li>
72 <li>{@link com.sun.appserv.management.monitor.statistics.WebModuleVirtualServerStats}</li>
73     </ul>
74  */

75 public class StatsImpl
76     extends MapGetterInvocationHandler<Statistic JavaDoc>
77     implements Stats JavaDoc, Serializable JavaDoc
78 {
79     static final long serialVersionUID = 6228973710059979557L;
80     
81     /**
82         Create a Stats from a CompositeData, whose keys are the Statistic names
83         and whose values are CompositeData for the Statistic.
84      */

85         public
86     StatsImpl( final CompositeData JavaDoc compositeData )
87     {
88         this( createStatisticsMap( compositeData ) );
89     }
90     
91         private static Map JavaDoc<String JavaDoc,Statistic JavaDoc>
92     requireSerializableMap( final Map JavaDoc<String JavaDoc, Statistic JavaDoc> m )
93     {
94         // required every entry to be a Statistic
95
for( final Statistic JavaDoc s : m.values() )
96         {
97             if ( ! (s instanceof Statistic JavaDoc ) )
98             {
99                 throw new IllegalArgumentException JavaDoc();
100             }
101         }
102         
103         Map JavaDoc<String JavaDoc,Statistic JavaDoc> sMap = null;
104         
105         if ( m instanceof Serializable JavaDoc )
106         {
107             sMap = m;
108         }
109         else
110         {
111             sMap = new HashMap JavaDoc<String JavaDoc, Statistic JavaDoc>( m );
112         }
113         
114         return( sMap );
115     }
116     
117     /**
118         Ensure that every Statistic is an instance of one of our acceptable
119         implementations. If you are adding a custom statistic, add that
120         statistic here in the if clause.
121      */

122         private static Statistic JavaDoc
123     requireStatisticImpl( final Statistic JavaDoc statisticIn )
124     {
125         Statistic JavaDoc statisticOut = null;
126         
127         if ( statisticIn instanceof CountStatisticImpl ||
128             statisticIn instanceof RangeStatisticImpl ||
129             statisticIn instanceof BoundedRangeStatisticImpl ||
130             statisticIn instanceof BoundaryStatisticImpl ||
131             statisticIn instanceof TimeStatisticImpl ||
132             statisticIn instanceof NumberStatisticImpl ||
133             statisticIn instanceof StringStatisticImpl
134             )
135         {
136             statisticOut = statisticIn;
137         }
138         else if ( statisticIn instanceof MapStatistic )
139         {
140             final Class JavaDoc<? extends Statistic JavaDoc> theInterface =
141                 StatisticFactory.getInterface( statisticIn );
142             
143             if ( theInterface != MapStatistic.class )
144             {
145                 statisticOut = StatisticFactory.create( theInterface,
146                                 J2EEUtil.statisticToMap( statisticIn ) );
147             }
148             else if ( ! ( statisticIn instanceof MapStatisticImpl ) )
149             {
150                 // it's a MapStatistic, but not our implementation.
151
statisticOut = new MapStatisticImpl( statisticIn );
152             }
153         }
154         else
155         {
156             // some weird kind, convert it to generic Statistic
157
assert( false ) :
158                 "requireStatisticImpl: unsupported Statistic type of class " + statisticIn.getClass().getName();
159             statisticOut = new MapStatisticImpl( statisticIn );
160         }
161         
162         return( statisticOut );
163     }
164     
165     /**
166         Ensure that every Statistic is an instance of StatisticsImpl.
167      */

168         private static Statistic JavaDoc[]
169     requireStatisticImpl( final Statistic JavaDoc[] statisticsIn )
170     {
171         boolean convert = false;
172         
173         // avoid conversion if already in desired form
174
for( int i = 0; i < statisticsIn.length; ++i )
175         {
176             if ( ! (statisticsIn[ i ] instanceof MapStatistic ) )
177             {
178                 convert = true;
179                 break;
180             }
181         }
182         
183         Statistic JavaDoc[] statisticsOut = null;
184         if ( convert )
185         {
186             statisticsOut = new Statistic JavaDoc[ statisticsIn.length ];
187             
188             for( int i = 0; i <
189              statisticsIn.length; ++i )
190             {
191                 statisticsOut[ i ] = requireStatisticImpl( statisticsIn[ i ] );
192             }
193         }
194         else
195         {
196             statisticsOut = statisticsIn;
197         }
198         
199         return( statisticsOut );
200     }
201
202     /**
203         Create a Stats from a Map, whose keys are the Statistic names
204         and whose values are the Statistics.
205      */

206         public
207     StatsImpl( final Map JavaDoc<String JavaDoc, Statistic JavaDoc> statisticsIn )
208     {
209         super( requireSerializableMap( statisticsIn ) );
210     }
211     
212     
213     /**
214      */

215         public
216     StatsImpl( final Statistic JavaDoc[] statistics )
217     {
218         this( createStatisticsMap( statistics ) );
219     }
220     
221     
222         private static Map JavaDoc<String JavaDoc,Statistic JavaDoc>
223     createStatisticsMap( final CompositeData JavaDoc compositeData )
224     {
225         final CompositeType JavaDoc compositeType = compositeData.getCompositeType();
226         final Set JavaDoc keySet = compositeType.keySet();
227         final Iterator JavaDoc iter = keySet.iterator();
228         
229         final Map JavaDoc<String JavaDoc,Statistic JavaDoc> map = new HashMap JavaDoc<String JavaDoc,Statistic JavaDoc>();
230         
231         while ( iter.hasNext() )
232         {
233             final String JavaDoc name = (String JavaDoc)iter.next();
234             final CompositeData JavaDoc data = (CompositeData JavaDoc)compositeData.get( name );
235             
236             map.put( name, StatisticFactory.create( data ) );
237         }
238         
239         return( map );
240     }
241     
242         private static Map JavaDoc<String JavaDoc,Statistic JavaDoc>
243     createStatisticsMap( final Statistic JavaDoc[] statistics )
244     {
245         final Map JavaDoc<String JavaDoc,Statistic JavaDoc> m = new HashMap JavaDoc<String JavaDoc,Statistic JavaDoc>();
246         
247         for( int i = 0; i < statistics.length; ++i )
248         {
249             final Statistic JavaDoc statistic = requireStatisticImpl( statistics[ i ] );
250             
251             if ( statistic != null )
252             {
253                 m.put( statistic.getName(), statistic );
254             }
255         }
256         
257         return( m );
258     }
259     
260     
261         public Statistic JavaDoc
262     getStatistic( String JavaDoc statisticName )
263     {
264         try
265         {
266             return( (Statistic JavaDoc)getValue( statisticName ) );
267         }
268         catch( Exception JavaDoc e )
269         {
270             final String JavaDoc msg = "NOT a Statistic: " + statisticName +
271                 " of class " + getValue( statisticName ).getClass();
272             throw new RuntimeException JavaDoc( msg, e );
273         }
274     }
275     
276         public String JavaDoc[]
277     getStatisticNames()
278     {
279         final Set JavaDoc<String JavaDoc> names = getMap().keySet();
280         
281         return( GSetUtil.toStringArray( names ) );
282     }
283     
284         public Statistic JavaDoc[]
285     getStatistics()
286     {
287         final Collection JavaDoc<Statistic JavaDoc> values = getMap().values();
288         final Statistic JavaDoc[] statistics = new Statistic JavaDoc[ values.size() ];
289         
290         return( (Statistic JavaDoc[])values.toArray( statistics ) );
291     }
292     
293         public String JavaDoc
294     toString()
295     {
296         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
297         
298         final Statistic JavaDoc[] statistics = getStatistics();
299         buf.append( statistics.length + " Statistics:\n" );
300         for( int i = 0; i < statistics.length; ++i )
301         {
302             buf.append( statistics[ i ].toString() + "\n");
303         }
304         
305         return( buf.toString() );
306     }
307     
308     
309         public boolean
310     equals( final Object JavaDoc rhs )
311     {
312         boolean equals = false;
313         
314         if ( rhs != null && rhs instanceof Stats JavaDoc )
315         {
316             final Stats JavaDoc stats = (Stats JavaDoc)rhs;
317             
318             final String JavaDoc[] myNames = getStatisticNames();
319             final String JavaDoc[] rhsNames = stats.getStatisticNames();
320             if ( myNames.length == rhsNames.length )
321             {
322                 equals = true;
323                 
324                 for( int i = 0; i < myNames.length; ++i )
325                 {
326                     final String JavaDoc statisticName = myNames[ i ];
327                     final Statistic JavaDoc myStatistic = getStatistic( statisticName );
328                     final Statistic JavaDoc rhsStatistic = stats.getStatistic( statisticName );
329                     
330                     if ( ! myStatistic.equals( rhsStatistic ) )
331                     {
332                         equals = false;
333                         break;
334                     }
335                 }
336             }
337         }
338         return( equals );
339     }
340     
341         public Object JavaDoc
342     invoke(
343         Object JavaDoc myProxy,
344         Method JavaDoc method,
345         Object JavaDoc[] args )
346         throws java.lang.Throwable JavaDoc
347     {
348         Object JavaDoc result = null;
349         final String JavaDoc methodName = method.getName();
350         final int numArgs = args == null ? 0 : args.length;
351         
352         if ( numArgs == 0 && methodName.equals( "getStatisticNames" ) )
353         {
354             result = getStatisticNames();
355         }
356         else if ( numArgs == 0 && methodName.equals( "getStatistics" ) )
357         {
358             result = getStatistics();
359         }
360         else if ( numArgs == 1 && methodName.equals( "getStatistic" ) &&
361             method.getReturnType() == Statistic JavaDoc.class &&
362             method.getParameterTypes()[ 0 ] == String JavaDoc.class )
363         {
364             result = getStatistic( (String JavaDoc)args[ 0 ] );
365         }
366         else
367         {
368             result = super.invoke( myProxy, method, args );
369         }
370
371         return( result );
372     }
373 }
374
375
376
377
378
379
380
Popular Tags