KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > monitor > stats > LoadBalancerContextRootStatsImpl


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 package com.sun.enterprise.management.monitor.stats;
25
26 import java.util.Date JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.management.j2ee.statistics.Statistic JavaDoc;
29 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
30 import javax.management.j2ee.statistics.TimeStatistic JavaDoc;
31 import com.sun.appserv.management.j2ee.statistics.CountStatisticImpl;
32 import com.sun.appserv.management.j2ee.statistics.TimeStatisticImpl;
33 import com.sun.appserv.management.monitor.statistics.LoadBalancerServerStats;
34 import com.sun.enterprise.admin.monitor.stats.lb.ClusterStats;
35 import com.sun.enterprise.admin.monitor.stats.lb.InstanceStats;
36 import com.sun.enterprise.admin.monitor.stats.lb.LoadBalancerStatsInterface;
37 import com.sun.enterprise.admin.monitor.stats.lb.InstanceStatsInterface;
38 import com.sun.enterprise.server.ApplicationServer;
39 import com.sun.enterprise.server.pluggable.LBFeatureFactory;
40
41
42 public final class LoadBalancerContextRootStatsImpl implements CustomStatsImpl {
43
44     /** Returns the statistics for a load-balanced server instance
45      * @return an array of {@link Statistic}
46      */

47     public Statistic JavaDoc[] getStatistics() {
48         LoadBalancerStatsInterface lbstats =
49                 lbff.getLoadBalancerMonitoringStats(lbConfigName,lbName);
50         InstanceStatsInterface instance = getInstanceStats(lbstats);
51         return createStats(instance);
52     }
53
54     /** Returns average response time in milliseconds
55      * @return an instance of {@link TimeStatistic}
56      */

57     public TimeStatistic JavaDoc getResponseTime() {
58         LoadBalancerStatsInterface lbstats =
59                 lbff.getLoadBalancerMonitoringStats(lbConfigName,lbName);
60         InstanceStatsInterface istat = getInstanceStats(lbstats);
61         if(istat!=null){
62             long avgRespTime = Long.parseLong(
63                     istat.getApplicationStatsAverageResponseTime());
64             long maxRespTime = Long.parseLong(
65                     istat.getApplicationStatsMaxResponseTime());
66             long minRespTime = Long.parseLong(
67                     istat.getApplicationStatsMinResponseTime());
68             long totalRequests = Long.parseLong(
69                     istat.getApplicationStatsNumTotalRequests());
70             long sampleTime = System.currentTimeMillis();
71             return createResponseTimeStat(maxRespTime,
72             totalRequests, sampleTime,
73             minRespTime, avgRespTime);
74         }
75         return null;
76     }
77
78     /** Returns average response time in milliseconds
79      * @return an instance of {@link TimeStatistic}
80      */

81     public CountStatistic JavaDoc getAverageResponseTime() {
82         LoadBalancerStatsInterface lbstats =
83                 lbff.getLoadBalancerMonitoringStats(lbConfigName,lbName);
84         InstanceStatsInterface istat = getInstanceStats(lbstats);
85         if(istat!=null){
86             long avgRespTime = Long.parseLong(
87                     istat.getApplicationStatsAverageResponseTime());
88             long sampleTime = System.currentTimeMillis();
89             return createAverageRespTimeStat(sampleTime,avgRespTime);
90         }
91         return null;
92     }
93     
94     /** Returns the number of failover requests
95      * @return an instance of {@link CountStatistic}
96      */

97     public CountStatistic JavaDoc getFailoverReqCount() {
98         LoadBalancerStatsInterface lbstats =
99                 lbff.getLoadBalancerMonitoringStats(lbConfigName,lbName);
100         InstanceStatsInterface istat = getInstanceStats(lbstats);
101         if(istat!=null){
102             long time = Long.parseLong(
103                     istat.getApplicationStatsNumFailoverRequests());
104             long sampleTime = System.currentTimeMillis();
105             return createFailoverReqCountStat(sampleTime,time);
106         }
107         return null;
108     }
109     
110     /** Returns the number of error requests
111      * @return an instance of {@link CountStatistic}
112      */

113     public CountStatistic JavaDoc getErrorRequestCount() {
114         LoadBalancerStatsInterface lbstats =
115                 lbff.getLoadBalancerMonitoringStats(lbConfigName,lbName);
116         InstanceStatsInterface istat = getInstanceStats(lbstats);
117         if(istat!=null){
118             long time = Long.parseLong(
119                     istat.getApplicationStatsNumErrorRequests());
120             long sampleTime = System.currentTimeMillis();
121             return createErrorRequestCountStat(sampleTime,time);
122         }
123         return null;
124     }
125     
126     /** Returns the number of active requests
127      * @return an instance of {@link CountStatistic}
128      */

129     public CountStatistic JavaDoc getActiveRequestCount() {
130         LoadBalancerStatsInterface lbstats =
131                 lbff.getLoadBalancerMonitoringStats(lbConfigName,lbName);
132         InstanceStatsInterface istat = getInstanceStats(lbstats);
133         if(istat!=null){
134             long time = Long.parseLong(
135                     istat.getApplicationStatsNumActiveRequests());
136             long sampleTime = System.currentTimeMillis();
137             return createActiveRequestCountStat(sampleTime,time);
138         }
139         return null;
140     }
141
142     /** Returns the number of total requests
143      * @return an instance of {@link CountStatistic}
144      */

145     public CountStatistic JavaDoc getTotalRequestCount() {
146         LoadBalancerStatsInterface lbstats =
147                 lbff.getLoadBalancerMonitoringStats(lbConfigName,lbName);
148         InstanceStatsInterface istat = getInstanceStats(lbstats);
149         if(istat!=null){
150             long time = Long.parseLong(
151                     istat.getApplicationStatsNumTotalRequests());
152             long sampleTime = System.currentTimeMillis();
153             return createTotalRequestCountStat(sampleTime,time);
154         }
155         return null;
156     }
157
158     /** Returns the number of times all idempotent
159      * urls in this application are accessed
160      * @return an instance of {@link CountStatistic}
161      */

162     public CountStatistic JavaDoc getIdempotentUrlAccessCount() {
163         LoadBalancerStatsInterface lbstats =
164                 lbff.getLoadBalancerMonitoringStats(lbConfigName,lbName);
165         InstanceStatsInterface istat = getInstanceStats(lbstats);
166         if(istat!=null){
167             long time = Long.parseLong(
168                     istat.getApplicationStatsNumIdempotentUrlRequests());
169             long sampleTime = System.currentTimeMillis();
170             return createIdempotentUrlAccessCountStat(sampleTime,time);
171         }
172         return null;
173     }
174     
175
176     public LoadBalancerContextRootStatsImpl(String JavaDoc lbName,String JavaDoc lbConfigName,
177             String JavaDoc clusterName,String JavaDoc instanceName,String JavaDoc appName,
178             String JavaDoc contextRoot) {
179         this.lbName=lbName;
180         this.clusterName=clusterName;
181         this.instanceName=instanceName;
182         this.appName=appName;
183         this.contextRoot=contextRoot;
184         this.lbConfigName=lbConfigName;
185         this.lbff = ApplicationServer.getServerContext().
186                 getPluggableFeatureFactory().getLBFeatureFactory();
187         this.startTime=System.currentTimeMillis();
188     }
189
190     public LoadBalancerContextRootStatsImpl() {
191     }
192
193     private String JavaDoc contextRoot = null;
194     private String JavaDoc lbName = null;
195     private String JavaDoc lbConfigName = null;
196     private String JavaDoc clusterName = null;
197     private String JavaDoc instanceName = null;
198     private String JavaDoc appName = null;
199     private LBFeatureFactory lbff = null;
200     private long startTime = 0;
201     
202     private InstanceStatsInterface getInstanceStats(
203             final LoadBalancerStatsInterface lbstats) {
204         if(lbstats == null)
205             return null;
206         ClusterStats [] cstats = lbstats.getClusterStats();
207         for(ClusterStats cstat :cstats){
208             for(InstanceStats istat : cstat.getInstanceStats()){
209                 if(!istat.getId().equals(instanceName))
210                     continue;
211                 String JavaDoc appid = istat.getApplicationStatsId();
212                     if(!appid.equals(contextRoot))
213                         continue;
214                 return istat;
215
216             }
217         }
218         return null;
219     }
220     
221     private Statistic JavaDoc[] createStats(InstanceStatsInterface istat) {
222         Statistic JavaDoc[] emptyStats = createEmptyStats();
223         if(istat==null)
224             return emptyStats;
225         long avgRespTime = Long.parseLong(
226                 istat.getApplicationStatsAverageResponseTime());
227         long maxRespTime = Long.parseLong(
228                 istat.getApplicationStatsMaxResponseTime());
229         long minRespTime = Long.parseLong(
230                 istat.getApplicationStatsMinResponseTime());
231         long noActiveReq = Long.parseLong(
232                 istat.getApplicationStatsNumActiveRequests());
233         long noErrorReq = Long.parseLong(
234                 istat.getApplicationStatsNumErrorRequests());
235         long noFailoverReq = Long.parseLong(
236                 istat.getApplicationStatsNumFailoverRequests());
237         long noIdempotentUrlReq = Long.parseLong(
238                 istat.getApplicationStatsNumIdempotentUrlRequests());
239         long totalRequests = Long.parseLong(
240                 istat.getApplicationStatsNumTotalRequests());
241         long sampleTime = System.currentTimeMillis();
242         Statistic JavaDoc[] statArr = new Statistic JavaDoc[7];
243         statArr[0] = createResponseTimeStat(maxRespTime, totalRequests,
244                 sampleTime, minRespTime, avgRespTime);
245         statArr[1] = createAverageRespTimeStat(sampleTime, avgRespTime);
246         statArr[2] = createFailoverReqCountStat(sampleTime, noFailoverReq);
247         statArr[3] = createErrorRequestCountStat(sampleTime, noErrorReq);
248         statArr[4] = createActiveRequestCountStat(sampleTime, noActiveReq);
249         statArr[5] = createTotalRequestCountStat(sampleTime, totalRequests);
250         statArr[6] = createIdempotentUrlAccessCountStat(sampleTime,
251                 noIdempotentUrlReq);
252
253         return statArr;
254     }
255     
256     private CountStatistic JavaDoc createAverageRespTimeStat(final long sampleTime,
257             final long count) {
258         return new CountStatisticImpl("AverageResponseTime",
259                 "Average Response Time", "MILLISECOND", startTime, sampleTime,count);
260     }
261     private CountStatistic JavaDoc createFailoverReqCountStat(final long sampleTime,
262             final long count) {
263         return new CountStatisticImpl("FailoverReqCount",
264                 "Failover Request Count", "NONE", startTime, sampleTime,count);
265     }
266     private CountStatistic JavaDoc createErrorRequestCountStat(final long sampleTime,
267             final long count) {
268         return new CountStatisticImpl("ErrorRequestCount",
269                 "Error Request Count", "NONE", startTime, sampleTime,count);
270     }
271     private CountStatistic JavaDoc createActiveRequestCountStat(final long sampleTime,
272             final long count) {
273         return new CountStatisticImpl("ActiveRequestCount",
274                 "Active Request Count", "NONE", startTime, sampleTime,count);
275     }
276     private CountStatistic JavaDoc createTotalRequestCountStat(final long sampleTime,
277             final long count) {
278         return new CountStatisticImpl("TotalRequestCount",
279                 "Total Request Count", "NONE", startTime, sampleTime,count);
280     }
281     private CountStatistic JavaDoc createIdempotentUrlAccessCountStat(final long sampleTime,
282             final long count) {
283         return new CountStatisticImpl("IdempotentUrlAccessCount",
284                 "Idempotent Url Access Count", "NONE", startTime, sampleTime,count);
285     }
286
287     private TimeStatistic JavaDoc createResponseTimeStat(final long maxRespTime,
288             final long totalRequests, final long sampleTime,
289             final long minRespTime, final long avgRespTime) {
290         return new TimeStatisticImpl("ResponseTime", "Response Time",
291                 "MILLISECOND", startTime, sampleTime,totalRequests,maxRespTime,
292                 minRespTime, totalRequests*avgRespTime);
293     }
294
295     private Statistic JavaDoc[] createEmptyStats() {
296         Statistic JavaDoc[] statArr1 = new Statistic JavaDoc[7];
297         statArr1[0] = createResponseTimeStat(-1, -1, -1, -1, -1);
298         statArr1[1] = createAverageRespTimeStat(-1, -1);
299         statArr1[2] = createFailoverReqCountStat(-1, -1);
300         statArr1[3] = createErrorRequestCountStat(-1, -1);
301         statArr1[4] = createActiveRequestCountStat(-1, -1);
302         statArr1[5] = createTotalRequestCountStat(-1, -1);
303         statArr1[6] = createIdempotentUrlAccessCountStat( -1, -1);
304         return statArr1;
305     }
306         
307     
308 }
309
Popular Tags