001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.management;
018
019import org.apache.camel.Exchange;
020
021/**
022 * Delegates to another {@link PerformanceCounter}.
023 * <p/>
024 * This is used to allow Camel to pre initialize these delegate performance counters when Camel creates the actual route
025 * from the model. Then later as the various processors, routes etc. is created and registered in the
026 * {@link org.apache.camel.spi.LifecycleStrategy} then we link this to the real
027 * {@link org.apache.camel.management.mbean.ManagedPerformanceCounter} mbean so the mbean can gather statistics.
028 * <p/>
029 * This delegation is needed as how Camel is designed to register services in the
030 * {@link org.apache.camel.spi.LifecycleStrategy} in various stages.
031 */
032public class DelegatePerformanceCounter implements PerformanceCounter {
033
034    private PerformanceCounter counter;
035    private boolean statisticsEnabled;
036
037    public DelegatePerformanceCounter() {
038    }
039
040    public void setCounter(PerformanceCounter counter) {
041        this.counter = counter;
042        // init statistics based on the real counter based on how we got initialized
043        this.counter.setStatisticsEnabled(statisticsEnabled);
044    }
045
046    @Override
047    public void processExchange(Exchange exchange) {
048        if (counter != null) {
049            counter.processExchange(exchange);
050        }
051    }
052
053    @Override
054    public void completedExchange(Exchange exchange, long time) {
055        if (counter != null) {
056            counter.completedExchange(exchange, time);
057        }
058    }
059
060    @Override
061    public void failedExchange(Exchange exchange) {
062        counter.failedExchange(exchange);
063    }
064
065    @Override
066    public boolean isStatisticsEnabled() {
067        // statistics is only considered enabled if we have a counter to delegate to
068        // otherwise we do not want to gather statistics (we are just a delegate with none to delegate to)
069        return counter != null && counter.isStatisticsEnabled();
070    }
071
072    @Override
073    public void setStatisticsEnabled(boolean statisticsEnabled) {
074        if (counter != null) {
075            counter.setStatisticsEnabled(statisticsEnabled);
076        } else {
077            this.statisticsEnabled = statisticsEnabled;
078        }
079    }
080
081    @Override
082    public String toString() {
083        return counter != null ? counter.toString() : super.toString();
084    }
085}