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 */ 017 package org.apache.camel.management; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.api.management.PerformanceCounter; 021 022 /** 023 * Delegates to another {@link org.apache.camel.api.management.PerformanceCounter}. 024 * <p/> 025 * This is used to allow Camel to pre initialize these delegate performance counters 026 * when Camel creates the actual route from the model. Then later as the various 027 * processors, routes etc. is created and registered in the {@link org.apache.camel.spi.LifecycleStrategy} 028 * then we link this to the real {@link org.apache.camel.management.mbean.ManagedPerformanceCounter} mbean 029 * so the mbean can gather statistics. 030 * <p/> 031 * This delegation is needed as how Camel is designed to register services in the 032 * {@link org.apache.camel.spi.LifecycleStrategy} in various stages. 033 * 034 * @version 035 */ 036 public class DelegatePerformanceCounter implements PerformanceCounter { 037 038 private PerformanceCounter counter; 039 private boolean statisticsEnabled; 040 041 public DelegatePerformanceCounter() { 042 } 043 044 public void setCounter(PerformanceCounter counter) { 045 this.counter = counter; 046 // init statistics based on the real counter based on how we got initialized 047 this.counter.setStatisticsEnabled(statisticsEnabled); 048 } 049 050 public void completedExchange(Exchange exchange, long time) { 051 counter.completedExchange(exchange, time); 052 } 053 054 public void failedExchange(Exchange exchange) { 055 counter.failedExchange(exchange); 056 } 057 058 public boolean isStatisticsEnabled() { 059 // statistics is only considered enabled if we have a counter to delegate to 060 // otherwise we do not want to gather statistics (we are just a delegate with none to delegate to) 061 return counter != null && counter.isStatisticsEnabled(); 062 } 063 064 public void setStatisticsEnabled(boolean statisticsEnabled) { 065 if (counter != null) { 066 counter.setStatisticsEnabled(statisticsEnabled); 067 } else { 068 this.statisticsEnabled = statisticsEnabled; 069 } 070 } 071 072 @Override 073 public String toString() { 074 return counter != null ? counter.toString() : super.toString(); 075 } 076 }