001: /*
002: * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.wso2.esb.jmx.mbean;
017:
018: import org.wso2.esb.persistence.dataobject.StatisticsDO;
019: import org.wso2.esb.services.ServiceAdmin;
020:
021: /**
022: *
023: */
024:
025: public class AbstractStatisticsView {
026:
027: protected static final String NODATA = "No data available";
028: private final static int TOTAL = 1;
029: private final static int FAULT = 2;
030: private final static int MAX = 3;
031: private final static int AVG = 4;
032: private final static int MIN = 5;
033: private final static String IN = "IN";
034: private final static String OUT = "OUT";
035: protected ServiceAdmin serviceAdmin;
036:
037: public AbstractStatisticsView() {
038: this .serviceAdmin = new ServiceAdmin();
039: }
040:
041: private double getStatistics(StatisticsDO[] resultSet, int type,
042: String flow) {
043: if (resultSet != null && (resultSet.length != 0)) {
044: for (int i = 0; i < resultSet.length; i++) {
045: StatisticsDO statisticsDO = resultSet[i];
046: if (statisticsDO != null) {
047: long requiredDirection = -1;
048: if (IN.equalsIgnoreCase(flow)) {
049: requiredDirection = 0;
050: }
051: if (OUT.equalsIgnoreCase(flow)) {
052: requiredDirection = 1;
053: }
054: long direction = statisticsDO.getDirection();
055: if (direction == requiredDirection) {
056: switch (type) {
057: case TOTAL: {
058: return statisticsDO.getTotalCount();
059: }
060: case FAULT: {
061: return statisticsDO.getFaultCount();
062: }
063: case MAX: {
064: return statisticsDO.getMaxTime();
065: }
066: case MIN: {
067: return statisticsDO.getMinTime();
068: }
069: case AVG: {
070: return statisticsDO.getAvgTime();
071: }
072: }
073: }
074: }
075: }
076: }
077: return -1;
078: }
079:
080: protected int getTotalCount(StatisticsDO[] resultSet,
081: String direction) {
082: return (int) getStatistics(resultSet, TOTAL, direction);
083: }
084:
085: protected int getFaultCount(StatisticsDO[] resultSet,
086: String direction) {
087: return (int) getStatistics(resultSet, FAULT, direction);
088: }
089:
090: protected double getMaxTime(StatisticsDO[] resultSet,
091: String direction) {
092: return getStatistics(resultSet, MAX, direction);
093: }
094:
095: protected double getMinTime(StatisticsDO[] resultSet,
096: String direction) {
097: return getStatistics(resultSet, MIN, direction);
098: }
099:
100: protected double getAvgTime(StatisticsDO[] resultSet,
101: String direction) {
102: return getStatistics(resultSet, AVG, direction);
103: }
104: }
|