001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.synapse.statistics.impl;
020:
021: import org.apache.synapse.statistics.StatisticsStack;
022: import org.apache.synapse.statistics.StatisticsCollector;
023: import java.util.Iterator;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: /**
028: * The data structure to hold statistics related to the endpoints
029: *
030: */
031:
032: public class EndPointStatisticsStack implements StatisticsStack {
033:
034: /** list of endpoint statistics */
035: private List endpointStatistics;
036: /** To decide whether the reporting of the in flow statistics have been completed*/
037: private boolean isCompleteInFlowStatisicsReport = false;
038:
039: /**
040: * To put statistics
041: * @param key - The name of the End Point
042: * @param initTime
043: * @param isInFlow
044: * @param isStatisticsEnable
045: * @param isFault
046: */
047: public void put(String key, long initTime, boolean isInFlow,
048: boolean isStatisticsEnable, boolean isFault) {
049: if (endpointStatistics == null) {
050: endpointStatistics = new ArrayList();
051: }
052: endpointStatistics.add(new EndPointStatistics(key, initTime,
053: isInFlow, isStatisticsEnable, isFault));
054: }
055:
056: /**
057: * This method used to report the latest statistics to the StatisticsCollector
058: * @param statisticsCollector
059: * @param isFault
060: */
061:
062: public void reportToStatisticsCollector(
063: StatisticsCollector statisticsCollector, boolean isFault) {
064: if (endpointStatistics != null && !endpointStatistics.isEmpty()) {
065: EndPointStatistics statistics = (EndPointStatistics) endpointStatistics
066: .get(endpointStatistics.size() - 1);
067: if (statistics != null && statistics.isStatisticsEnable
068: && statistics.endPointName != null) {
069: if (statistics.inTimeForInFlow != -1) {
070: long initTimeForOutFlow = System
071: .currentTimeMillis();
072: statisticsCollector.reportForEndPoint(
073: statistics.endPointName, false,
074: statistics.inTimeForInFlow,
075: initTimeForOutFlow, isFault);
076: statistics.inTimeForInFlow = -1;
077: statistics.inTimeForOutFlow = initTimeForOutFlow;
078: } else if (statistics.inTimeForOutFlow != -1
079: && isCompleteInFlowStatisicsReport) {
080: statisticsCollector.reportForEndPoint(
081: statistics.endPointName, true,
082: statistics.inTimeForOutFlow, System
083: .currentTimeMillis(), isFault);
084: endpointStatistics.remove(statistics);
085: }
086: }
087: }
088: }
089:
090: /**
091: * Report a particular statistics to the StatisticsCollector
092: * @param statisticsCollector
093: * @param isFault
094: * @param name
095: */
096: public void reportToStatisticsCollector(
097: StatisticsCollector statisticsCollector, boolean isFault,
098: String name) {
099: if (endpointStatistics != null && !endpointStatistics.isEmpty()) {
100: List tobeRemoved = new ArrayList();
101: for (Iterator epIterator = endpointStatistics.iterator(); epIterator
102: .hasNext();) {
103: Object statisticsObj = epIterator.next();
104: if (statisticsObj instanceof EndPointStatistics) {
105: EndPointStatistics statistics = (EndPointStatistics) statisticsObj;
106: if (statistics.isStatisticsEnable
107: && statistics.endPointName != null
108: && statistics.endPointName.equals(name)) {
109: if (statistics.inTimeForInFlow != -1) {
110: long initTimeForOutFlow = System
111: .currentTimeMillis();
112: statisticsCollector.reportForEndPoint(
113: statistics.endPointName, false,
114: statistics.inTimeForInFlow,
115: initTimeForOutFlow, isFault);
116: statistics.inTimeForInFlow = -1;
117: statistics.inTimeForOutFlow = initTimeForOutFlow;
118: } else if (statistics.inTimeForOutFlow != -1
119: && isCompleteInFlowStatisicsReport) {
120: statisticsCollector.reportForEndPoint(
121: statistics.endPointName, true,
122: statistics.inTimeForOutFlow, System
123: .currentTimeMillis(),
124: isFault);
125: tobeRemoved.add(statistics);
126: }
127: }
128: }
129: }
130: endpointStatistics.removeAll(tobeRemoved);
131: }
132: }
133:
134: /**
135: * This method used to unreported all statistics to the StatisticsCollector
136: * @param statisticsCollector
137: */
138: public void reportAllToStatisticsCollector(
139: StatisticsCollector statisticsCollector, boolean isFault) {
140: if (endpointStatistics != null && !endpointStatistics.isEmpty()) {
141: List tobeRemoved = new ArrayList();
142: for (Iterator epIterator = endpointStatistics.iterator(); epIterator
143: .hasNext();) {
144: Object statisticsObj = epIterator.next();
145: if (statisticsObj instanceof EndPointStatistics) {
146: EndPointStatistics statistics = (EndPointStatistics) statisticsObj;
147: if (statistics.isStatisticsEnable
148: && statistics.endPointName != null) {
149: if (statistics.inTimeForInFlow != -1) {
150: long initTimeForOutFlow = System
151: .currentTimeMillis();
152: statisticsCollector.reportForEndPoint(
153: statistics.endPointName, false,
154: statistics.inTimeForInFlow,
155: initTimeForOutFlow, isFault);
156: statistics.inTimeForInFlow = -1;
157: statistics.inTimeForOutFlow = initTimeForOutFlow;
158: } else if (statistics.inTimeForOutFlow != -1
159: && isCompleteInFlowStatisicsReport) {
160: statisticsCollector.reportForEndPoint(
161: statistics.endPointName, true,
162: statistics.inTimeForOutFlow, System
163: .currentTimeMillis(),
164: isFault);
165: tobeRemoved.add(statistics);
166: }
167: }
168: }
169: }
170: endpointStatistics.removeAll(tobeRemoved);
171: }
172: isCompleteInFlowStatisicsReport = true;
173: }
174:
175: class EndPointStatistics {
176:
177: /** The name of the endpoint */
178: private String endPointName;
179: /** To check whether IN message flow or not */
180: private boolean isStatisticsEnable;
181: /** To indicate whether this is fault or not */
182: private boolean isFault;
183: /** The time which starts to collect statistics for IN flow */
184: private long inTimeForInFlow = -1;
185: /** The time which starts to collect statistics for OUT flow */
186: private long inTimeForOutFlow = -1;
187:
188: public EndPointStatistics(String endPointName, long initTime,
189: boolean inFlow, boolean statisticsEnable, boolean fault) {
190: if (inFlow) {
191: this .endPointName = endPointName;
192: this .inTimeForInFlow = initTime;
193: isStatisticsEnable = statisticsEnable;
194: isFault = fault;
195: }
196: }
197:
198: public boolean equals(Object o) {
199: if (this == o)
200: return true;
201: if (o == null || getClass() != o.getClass())
202: return false;
203:
204: EndPointStatistics that = (EndPointStatistics) o;
205:
206: if (endPointName != null ? !endPointName
207: .equals(that.endPointName)
208: : that.endPointName != null)
209: return false;
210:
211: return true;
212: }
213:
214: public int hashCode() {
215: return (endPointName != null ? endPointName.hashCode() : 0);
216: }
217: }
218: }
|