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 java.util.Map; 020 021 import org.apache.camel.CamelContext; 022 import org.apache.camel.Processor; 023 import org.apache.camel.api.management.PerformanceCounter; 024 import org.apache.camel.management.mbean.ManagedPerformanceCounter; 025 import org.apache.camel.model.ProcessorDefinition; 026 import org.apache.camel.spi.InterceptStrategy; 027 import org.apache.camel.util.KeyValueHolder; 028 029 /** 030 * This strategy class wraps targeted processors with a 031 * {@link InstrumentationProcessor}. Each InstrumentationProcessor has an 032 * embedded {@link ManagedPerformanceCounter} for monitoring performance metrics. 033 * <p/> 034 * This class looks up a map to determine which PerformanceCounter should go into the 035 * InstrumentationProcessor for any particular target processor. 036 * 037 * @version 038 */ 039 public class InstrumentationInterceptStrategy implements InterceptStrategy { 040 041 private Map<ProcessorDefinition<?>, PerformanceCounter> registeredCounters; 042 private final Map<Processor, KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor>> wrappedProcessors; 043 044 public InstrumentationInterceptStrategy(Map<ProcessorDefinition<?>, PerformanceCounter> registeredCounters, 045 Map<Processor, KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor>> wrappedProcessors) { 046 this.registeredCounters = registeredCounters; 047 this.wrappedProcessors = wrappedProcessors; 048 } 049 050 public Processor wrapProcessorInInterceptors(CamelContext context, ProcessorDefinition<?> definition, 051 Processor target, Processor nextTarget) throws Exception { 052 // do not double wrap it 053 if (target instanceof InstrumentationProcessor) { 054 return target; 055 } 056 057 // only wrap a performance counter if we have it registered in JMX by the jmx agent 058 PerformanceCounter counter = registeredCounters.get(definition); 059 if (counter != null) { 060 InstrumentationProcessor wrapper = new InstrumentationProcessor(counter); 061 wrapper.setProcessor(target); 062 wrapper.setType(definition.getShortName()); 063 064 // add it to the mapping of wrappers so we can later change it to a decorated counter 065 // that when we register the processor 066 KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor> holder = 067 new KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor>(definition, wrapper); 068 wrappedProcessors.put(target, holder); 069 return wrapper; 070 } 071 072 return target; 073 } 074 075 @Override 076 public String toString() { 077 return "InstrumentProcessor"; 078 } 079 080 }