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 java.util.Map;
020
021import org.apache.camel.CamelContext;
022import org.apache.camel.Processor;
023import org.apache.camel.api.management.PerformanceCounter;
024import org.apache.camel.management.mbean.ManagedPerformanceCounter;
025import org.apache.camel.model.ProcessorDefinition;
026import org.apache.camel.spi.InterceptStrategy;
027import 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 */
039public 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 PerformanceCounter prepareProcessor(ProcessorDefinition<?> definition, Processor target, InstrumentationProcessor advice) {
051        PerformanceCounter counter = registeredCounters.get(definition);
052        if (counter != null) {
053            // add it to the mapping of wrappers so we can later change it to a
054            // decorated counter when we register the processor
055            KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor> holder = new KeyValueHolder<>(definition, advice);
056            wrappedProcessors.put(target, holder);
057        }
058        return counter;
059    }
060
061    public Processor wrapProcessorInInterceptors(CamelContext context, ProcessorDefinition<?> definition,
062                                                 Processor target, Processor nextTarget) throws Exception {
063        // no longer in use as we have optimised to avoid wrapping unless needed
064        return target;
065    }
066
067}