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;
020:
021: import org.apache.synapse.SynapseConstants;
022: import org.apache.synapse.MessageContext;
023: import org.apache.synapse.endpoints.Endpoint;
024: import org.apache.synapse.core.SynapseEnvironment;
025: import org.apache.synapse.statistics.impl.SequenceStatisticsStack;
026: import org.apache.synapse.statistics.impl.EndPointStatisticsStack;
027: import org.apache.synapse.statistics.impl.ProxyServiceStatisticsStack;
028:
029: /**
030: * A utils to process statistics
031: *
032: */
033:
034: public class StatisticsUtils {
035:
036: /**
037: * To process statistics related to the proxy services
038: *
039: * @param synCtx
040: */
041: public static void processProxyServiceStatistics(
042: MessageContext synCtx) {
043:
044: StatisticsCollector statisticsCollector = getStatisticsCollector(synCtx);
045: boolean isFault = synCtx.getEnvelope().getBody().hasFault();
046: ProxyServiceStatisticsStack proxyServiceStatisticsStack = (ProxyServiceStatisticsStack) synCtx
047: .getProperty(SynapseConstants.PROXY_STATS);
048: if (proxyServiceStatisticsStack != null) {
049: proxyServiceStatisticsStack.reportToStatisticsCollector(
050: statisticsCollector, isFault);
051: }
052: ProxyServiceStatisticsStack synapseServiceStatisticsStack = (ProxyServiceStatisticsStack) synCtx
053: .getProperty(SynapseConstants.SERVICE_STATS);
054: if (synapseServiceStatisticsStack != null) {
055: synapseServiceStatisticsStack.reportToStatisticsCollector(
056: statisticsCollector, isFault);
057: }
058: }
059:
060: /**
061: * To process statistics related to the End Points
062: *
063: * @param synCtx
064: */
065: public static void processEndPointStatistics(MessageContext synCtx) {
066: StatisticsCollector statisticsCollector = getStatisticsCollector(synCtx);
067: boolean isFault = synCtx.getEnvelope().getBody().hasFault();
068: EndPointStatisticsStack endPointStatisticsStack = (EndPointStatisticsStack) synCtx
069: .getProperty(SynapseConstants.ENDPOINT_STATS);
070: if (endPointStatisticsStack != null) {
071: Object endpointObj = synCtx
072: .getProperty(SynapseConstants.PROCESSED_ENDPOINT);
073: if (endpointObj instanceof Endpoint) {
074: Endpoint endpoint = (Endpoint) endpointObj;
075: String name = endpoint.getName();
076: if (name == null) {
077: endPointStatisticsStack
078: .reportToStatisticsCollector(
079: statisticsCollector, isFault);
080: } else {
081: endPointStatisticsStack
082: .reportToStatisticsCollector(
083: statisticsCollector, isFault, name);
084: }
085: endPointStatisticsStack.reportAllToStatisticsCollector(
086: statisticsCollector, true);
087: }
088: }
089: }
090:
091: /**
092: * To process statistics related to the sequence
093: *
094: * @param synCtx
095: */
096: public static void processSequenceStatistics(MessageContext synCtx) {
097: StatisticsCollector statisticsCollector = getStatisticsCollector(synCtx);
098: boolean isFault = synCtx.getEnvelope().getBody().hasFault();
099: SequenceStatisticsStack sequenceStatisticsStack = (SequenceStatisticsStack) synCtx
100: .getProperty(SynapseConstants.SEQUENCE_STATS);
101: if (sequenceStatisticsStack != null) {
102: sequenceStatisticsStack.reportToStatisticsCollector(
103: statisticsCollector, isFault);
104: }
105: }
106:
107: /**
108: * To process all statistics related to the sequence
109: *
110: * @param synCtx
111: */
112: public static void processAllSequenceStatistics(
113: MessageContext synCtx) {
114: StatisticsCollector statisticsCollector = getStatisticsCollector(synCtx);
115: boolean isFault = synCtx.getEnvelope().getBody().hasFault();
116: SequenceStatisticsStack sequenceStatisticsStack = (SequenceStatisticsStack) synCtx
117: .getProperty(SynapseConstants.SEQUENCE_STATS);
118: if (sequenceStatisticsStack != null) {
119: sequenceStatisticsStack.reportAllToStatisticsCollector(
120: statisticsCollector, isFault);
121: }
122: }
123:
124: /**
125: * A helper method to get StatisticsCollector from the Synapse Message Context
126: *
127: * @param synCtx
128: * @return StatisticsCollector
129: */
130: private static StatisticsCollector getStatisticsCollector(
131: MessageContext synCtx) {
132: SynapseEnvironment synEnv = synCtx.getEnvironment();
133: StatisticsCollector statisticsCollector = null;
134: if (synEnv != null) {
135: statisticsCollector = synEnv.getStatisticsCollector();
136: if (statisticsCollector == null) {
137: statisticsCollector = new StatisticsCollector();
138: synEnv.setStatisticsCollector(statisticsCollector);
139: }
140: }
141: return statisticsCollector;
142: }
143: }
|