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