KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Proxy JavaDoc;
27
28 import javax.management.openmbean.CompositeData JavaDoc;
29 import javax.management.openmbean.CompositeType JavaDoc;
30
31 import javax.management.j2ee.statistics.*;
32
33 import com.sun.appserv.management.util.jmx.OpenMBeanUtil;
34 import com.sun.appserv.management.util.j2ee.J2EEUtil;
35 import com.sun.appserv.management.util.misc.TypeCast;
36 import com.sun.appserv.management.util.misc.ClassUtil;
37
38 /**
39     Factory to create Stats subclasses of any kind, based on supplied interface type
40     and a Map or CompositeData containing the Statistics.
41  */

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

52         public static Stats
53     create( Class JavaDoc<? extends Stats> theInterface, final CompositeData JavaDoc data )
54     {
55         final Map JavaDoc<String JavaDoc,Statistic> statistics = TypeCast.asMap( compositeDataToMap( data ) );
56         
57         return( createStats( theInterface, statistics ) );
58     }
59     
60         public static Map JavaDoc<String JavaDoc,Statistic>
61     compositeDataToMap( final CompositeData JavaDoc data )
62     {
63         return( TypeCast.asMap( OpenMBeanUtil.compositeDataToMap( data ) ) );
64     }
65     
66     
67     /**
68         Create a Stats using Stats class found as data.getCompositeType().getTypeName().
69         If this interface is not available, a generic Stats interface will be used.
70      */

71         public static Stats
72     createStats( final CompositeData JavaDoc data )
73     {
74         final String JavaDoc typeName = data.getCompositeType().getTypeName();
75         Class JavaDoc<? extends Stats> theInterface = null;
76         
77         try
78         {
79             theInterface = TypeCast.asClass( ClassUtil.classForName( typeName ) );
80         }
81         catch( Exception JavaDoc e )
82         {
83             theInterface = Stats.class;
84         }
85         
86         return( create( theInterface, data ) );
87     }
88     
89     
90     /**
91         Create a new Stats using the specified Map. The standard JSR 77
92         Statistic types are handled appropriately. Custom (non-standard) Stats
93         may also be used; in this case a proxy is returned which implements
94         the interface specified by theClass.
95         
96         @param theInterface the Stats sub-interface which the resulting should implement
97         @param statistics a Map containing keys of type String and their Statistic values
98      */

99         public static <T extends Stats> T
100     createStats( Class JavaDoc<T> theInterface, final Map JavaDoc<String JavaDoc,Statistic> statistics )
101     {
102         if ( ! Stats.class.isAssignableFrom( theInterface ) )
103         {
104             throw new IllegalArgumentException JavaDoc( theInterface.getName() );
105         }
106
107         // generate a proxy
108
final MapGetterInvocationHandler handler =
109             new MapGetterInvocationHandler<Statistic>( statistics );
110         final ClassLoader JavaDoc classLoader = theInterface.getClassLoader();
111         
112         final Class JavaDoc<T>[] interfaces = TypeCast.asArray( new Class JavaDoc[] { theInterface } );
113         
114         final Object JavaDoc proxy = Proxy.newProxyInstance( classLoader, interfaces, handler);
115         
116         return theInterface.cast( proxy );
117     }
118     
119     private void test()
120     {
121         createStats( EJBStats.class, new java.util.HashMap JavaDoc<String JavaDoc,Statistic>() );
122     }
123     
124     /**
125         Calls createStats( theInterface, J2EEUtil.statisticsToMap( statistics ) )
126      */

127         public static <T extends Stats> T
128     createStats( Class JavaDoc<T> theInterface, final Statistic[] statistics )
129     {
130         final Map JavaDoc<String JavaDoc,Statistic> statisticsMap = J2EEUtil.statisticsToMap( statistics );
131         
132         return( createStats( theInterface, statisticsMap ) );
133     }
134     
135         public static EJBStats
136     createEJBStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
137     {
138         return( createStats( EJBStats.class, statistics ) );
139     }
140     
141         public static URLStats
142     createURLStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
143     {
144         return( createStats( URLStats.class, statistics ) );
145     }
146     
147         public static EntityBeanStats
148     createEntityBeanStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
149     {
150         return( createStats( EntityBeanStats.class, statistics ) );
151     }
152     
153         public static JCAConnectionPoolStats
154     createJCAConnectionPoolStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
155     {
156         return( createStats( JCAConnectionPoolStats.class, statistics ) );
157     }
158     
159         public static JCAConnectionStats
160     createJCAConnectionStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
161     {
162         return( createStats( JCAConnectionStats.class, statistics ) );
163     }
164     
165         public static JCAStats
166     createJCAStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
167     {
168         return( createStats( JCAStats.class, statistics ) );
169     }
170     
171         public static JDBCConnectionPoolStats
172     createJDBCConnectionPoolStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
173     {
174         return( createStats( JDBCConnectionPoolStats.class, statistics ) );
175     }
176     
177         public static JDBCConnectionStats
178     createJDBCConnectionStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
179     {
180         return( createStats( JDBCConnectionStats.class, statistics ) );
181     }
182     
183         public static JDBCStats
184     createJDBCStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
185     {
186         return( createStats( JDBCStats.class, statistics ) );
187     }
188     
189         public static JMSConnectionStats
190     createJMSConnectionStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
191     {
192         return( createStats( JMSConnectionStats.class, statistics ) );
193     }
194     
195         public static JMSConsumerStats
196     createJMSConsumerStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
197     {
198         return( createStats( JMSConsumerStats.class, statistics ) );
199     }
200     
201         public static JMSEndpointStats
202     createJMSEndpointStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
203     {
204         return( createStats( JMSEndpointStats.class, statistics ) );
205     }
206     
207         public static JMSProducerStats
208     createJMSProducerStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
209     {
210         return( createStats( JMSProducerStats.class, statistics ) );
211     }
212     
213         public static JMSSessionStats
214     createJMSSessionStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
215     {
216         return( createStats( JMSSessionStats.class, statistics ) );
217     }
218     
219         public static JMSStats
220     createJMSStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
221     {
222         return( createStats( JMSStats.class, statistics ) );
223     }
224     
225         public static JTAStats
226     createJTAStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
227     {
228         return( createStats( JTAStats.class, statistics ) );
229     }
230     
231         public static JVMStats
232     createJVMStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
233     {
234         return( createStats( JVMStats.class, statistics ) );
235     }
236     
237         public static MessageDrivenBeanStats
238     createMessageDrivenBeanStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
239     {
240         return( createStats( MessageDrivenBeanStats.class, statistics ) );
241     }
242     
243         public static ServletStats
244     createServletStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
245     {
246         return( createStats( ServletStats.class, statistics ) );
247     }
248     
249         public static SessionBeanStats
250     createSessionBeanStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
251     {
252         return( createStats( SessionBeanStats.class, statistics ) );
253     }
254     
255         public static StatefulSessionBeanStats
256     createStatefulSessionBeanStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
257     {
258         return( createStats( StatefulSessionBeanStats.class, statistics ) );
259     }
260     
261         public static StatelessSessionBeanStats
262     createStatelessSessionBeanStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
263     {
264         return( createStats( StatelessSessionBeanStats.class, statistics ) );
265     }
266     
267         public static JavaMailStats
268     createJavaMailStats( final Map JavaDoc<String JavaDoc,Statistic> statistics )
269     {
270         // bizarre compiler error in JDK 1.5.0_06 forces us to inline the code here
271
final Class JavaDoc<JavaMailStats> theInterface = JavaMailStats.class;
272         JavaMailStats result = null;
273         
274         // generate a proxy
275
final MapGetterInvocationHandler handler =
276             new MapGetterInvocationHandler<Statistic>( statistics );
277         final ClassLoader JavaDoc classLoader = theInterface.getClassLoader();
278         
279         final Class JavaDoc<JavaMailStats>[] interfaces = TypeCast.asArray( new Class JavaDoc[] { theInterface } );
280         
281         result = (JavaMailStats)Proxy.newProxyInstance( classLoader, interfaces, handler);
282         
283         return result;
284     }
285
286 }
287
288
289
290
291
292
Popular Tags