KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > stats > GenericStatsImpl


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  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 /*
30  * $Id: GenericStatsImpl.java,v 1.2 2005/12/25 03:52:14 tcfujii Exp $
31  * $Date: 2005/12/25 03:52:14 $
32  * $Revision: 1.2 $
33  */

34
35 package com.sun.enterprise.admin.monitor.stats;
36
37 import java.lang.reflect.Method JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import javax.management.j2ee.statistics.Stats JavaDoc;
43 import javax.management.j2ee.statistics.Statistic JavaDoc;
44
45 /** Provides for generic implementation of any Stats interface. This class facilitates
46  * composition over inheritance for all the classes that implement their
47  * specific Stats interfaces. None of them has to implement the methods defined
48  * by the {@link javax.management.j2ee.statistics.Stats} interface. This class
49  * implements the same interface and does that job. All that implementing classes
50  * have to do is implement the specific accessing methods in their Stats interfaces
51  * and delegate the rest to this class. <b> This class invokes all these methods in
52  * implementing class through introspection. </b>
53  * @author Kedar Mhaswade
54  * @since S1AS8.0
55  * @version $Version$
56  */

57 public class GenericStatsImpl implements Stats JavaDoc {
58     
59     private final Class JavaDoc statsInterface;
60     private final Object JavaDoc statsProvider;
61     /** A map with binding of a String XXX to a method with name <b>get</b>XXX */
62     private final Map JavaDoc getters;
63     
64     /**
65      */

66     public GenericStatsImpl(String JavaDoc statsInterfaceName, Object JavaDoc statsProvider)
67     throws ClassNotFoundException JavaDoc {
68         this(statsInterfaceName, GenericStatsImpl.class.getClassLoader(), statsProvider);
69     }
70     
71     /**
72      */

73     public GenericStatsImpl(String JavaDoc statsInterfaceName, ClassLoader JavaDoc loader,
74     Object JavaDoc statsProvider) throws ClassNotFoundException JavaDoc {
75         this(Class.forName(statsInterfaceName, true, loader), statsProvider);
76     }
77     
78     /** Constructs a new instance of this class for a given interface and its implementation.
79      * It is mandatory that following contract is satisfied to call this satisfactorily:
80      * <ul>
81      * <li> None of the parameters are null. </li>
82      * <li> Given statsProvider implements the given statsInterface. </li>
83      * <li> Given statsInterface has to extend the @{link Stats} interface. </li>
84      * </ul>
85      * Note that it is expected (though not mandatory) to have a getXXX method that
86      * does not return an instance of {@link Statistic} interface.
87      * @throws NullPointerException if any of the given parameters are null
88      * @throws IllegalArgumentException if the contract is not satisfied by given parameters
89      */

90     public GenericStatsImpl(Class JavaDoc statsInterface, Object JavaDoc statsProvider) {
91         if (! implementsInterface(statsInterface, statsProvider) ||
92             ! extendsStatsInterface(statsInterface)) {
93             throw new IllegalArgumentException JavaDoc("Contract violation: invalid interface-implementation pair");
94         }
95         this.statsProvider = statsProvider;
96         this.statsInterface = statsInterface;
97         this.getters = new HashMap JavaDoc();
98         populateGetterMap();
99     }
100     
101     public Statistic JavaDoc getStatistic(String JavaDoc statisticName) {
102         final Method JavaDoc getter = (Method JavaDoc) getters.get(statisticName);
103         assert (getter != null) : ("Getter not initialized properly: " + statisticName);
104         Object JavaDoc result = null;
105         try {
106             result = getter.invoke(statsProvider);
107         }
108         catch(Exception JavaDoc e) {
109             final RuntimeException JavaDoc oe = new IllegalStateException JavaDoc();
110             oe.initCause(e);
111             throw oe;
112         }
113         return ( (Statistic JavaDoc)result );
114     }
115     
116     public String JavaDoc[] getStatisticNames() {
117         /* The return array is fixed at the construction time */
118         final String JavaDoc[] names = new String JavaDoc[getters.size()];
119         return ( (String JavaDoc[])getters.keySet().toArray(names) ); //TODOOOOOOO
120
}
121     
122     public Statistic JavaDoc[] getStatistics() {
123         return ( getStatisticsOneByOne() ); //invokes sequentially
124
}
125
126     private Statistic JavaDoc[] getStatisticsOneByOne() {
127         final Iterator JavaDoc iter = getters.keySet().iterator();
128         final Statistic JavaDoc[] stats = new Statistic JavaDoc[getters.keySet().size()];
129         int i = 0;
130         while (iter.hasNext()) {
131             final String JavaDoc sn = (String JavaDoc) iter.next();
132             stats[i++] = this.getStatistic(sn);
133         }
134         assert (stats.length == i);
135         return ( stats );
136     }
137     
138     private boolean implementsInterface(Class JavaDoc c, Object JavaDoc o) {
139         boolean impls = false;
140         final Class JavaDoc[] interfaces = o.getClass().getInterfaces();
141         for (int i = 0 ; i < interfaces.length ; i++) {
142             if (interfaces[i].equals(c)){
143                 impls = true;
144                 break;
145             }
146         }
147         return ( impls );
148     }
149     
150     private boolean extendsStatsInterface(Class JavaDoc i) {
151         final Class JavaDoc statsInterface = javax.management.j2ee.statistics.Stats JavaDoc.class;
152         return ( statsInterface.isAssignableFrom(i) );
153     }
154     
155     private void populateGetterMap() {
156         // Fix for Bugs 5045435, 6172088
157
//final Method[] apis = statsInterface.getDeclaredMethods(); //all of these should be PUBLIC.
158
final Method JavaDoc[] m = statsInterface.getMethods();
159         // exclude methods that belong to the javax.management.j2ee.Stats
160
final Method JavaDoc[] apis = filterStatsMethods(m);
161         final Method JavaDoc[] methods = getGetters(apis);
162         final String JavaDoc[] names = methods2Statistics(methods);
163         assert (names.length == methods.length) : ("Statistic names array is not having same length as that of array of getters");
164         int i;
165         for (i = 0 ; i < names.length ; i++) {
166             getters.put(names[i], methods[i]);
167         }
168         assert (getters.size() == i) : ("Getters map is incorrect, names.length = " + names.length + " methods.length = " + methods.length);
169     }
170     
171     private Method JavaDoc[] getGetters(Method JavaDoc[] all) {
172         final ArrayList JavaDoc l = new ArrayList JavaDoc();
173         for (int i = 0 ; i < all.length ; i++) {
174             final Method JavaDoc am = all[i];
175             if (isValidGetter(am)) {
176                 l.add(am);
177             }
178         }
179         final Method JavaDoc[] m = new Method JavaDoc[l.size()];
180         return ( (Method JavaDoc[])l.toArray(m) );
181     }
182     
183     private boolean isValidGetter(Method JavaDoc m) {
184         final boolean startsWithGet = m.getName().startsWith("get");
185         final boolean hasNoParams = m.getParameterTypes().length == 0;
186         final boolean returnsStatistic = Statistic JavaDoc.class.isAssignableFrom(m.getReturnType());
187         
188         return ( startsWithGet && hasNoParams && returnsStatistic );
189     }
190     
191     private String JavaDoc[] methods2Statistics(Method JavaDoc[] methods) {
192         final String JavaDoc[] names = new String JavaDoc[methods.length];
193         for (int i = 0 ; i < methods.length ; i++) {
194             final String JavaDoc m = methods[i].getName();
195             final int s = "get".length();
196             names[i] = m.substring(s);
197         }
198         return ( names );
199     }
200     
201     private boolean isStatsInterfaceMethod(String JavaDoc name) {
202         final Method JavaDoc[] methods = javax.management.j2ee.statistics.Stats JavaDoc.class.getMethods();
203         boolean isInterfaceMethod = false;
204         for (int i = 0 ; i < methods.length ; i++) {
205             if (methods[i].getName().equals(name)) {
206                 isInterfaceMethod = true;
207                 break;
208             }
209         }
210         return ( isInterfaceMethod );
211     }
212     
213     private Method JavaDoc[] filterStatsMethods(Method JavaDoc[] m) {
214         ArrayList JavaDoc methodList = new ArrayList JavaDoc();
215         for(int i = 0; i < m.length; i++) {
216             if(! isStatsInterfaceMethod(m[i].getName()))
217                 methodList.add(m[i]);
218         }
219         final Method JavaDoc[] methods = new Method JavaDoc[methodList.size()];
220         return (Method JavaDoc[])methodList.toArray(methods);
221     }
222 }
223
Popular Tags