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.NamedNode;
022import org.apache.camel.Processor;
023import org.apache.camel.management.mbean.ManagedPerformanceCounter;
024import org.apache.camel.spi.ManagementInterceptStrategy;
025import org.apache.camel.util.KeyValueHolder;
026
027/**
028 * This strategy class wraps targeted processors with a {@link InstrumentationProcessor}. Each InstrumentationProcessor
029 * has an embedded {@link ManagedPerformanceCounter} for monitoring performance metrics.
030 * <p/>
031 * This class looks up a map to determine which PerformanceCounter should go into the InstrumentationProcessor for any
032 * particular target processor.
033 */
034public class InstrumentationInterceptStrategy implements ManagementInterceptStrategy {
035
036    private Map<NamedNode, PerformanceCounter> registeredCounters;
037    private final Map<Processor, KeyValueHolder<NamedNode, InstrumentationProcessor>> wrappedProcessors;
038
039    public InstrumentationInterceptStrategy(Map<NamedNode, PerformanceCounter> registeredCounters,
040                                            Map<Processor, KeyValueHolder<NamedNode, InstrumentationProcessor>> wrappedProcessors) {
041        this.registeredCounters = registeredCounters;
042        this.wrappedProcessors = wrappedProcessors;
043    }
044
045    @Override
046    public InstrumentationProcessor<?> createProcessor(String type) {
047        return new DefaultInstrumentationProcessor(type);
048    }
049
050    @Override
051    public InstrumentationProcessor<?> createProcessor(NamedNode definition, Processor target) {
052        InstrumentationProcessor instrumentationProcessor
053                = new DefaultInstrumentationProcessor(definition.getShortName(), target);
054        PerformanceCounter counter = registeredCounters.get(definition);
055        if (counter != null) {
056            // add it to the mapping of wrappers so we can later change it to a
057            // decorated counter when we register the processor
058            KeyValueHolder<NamedNode, InstrumentationProcessor> holder
059                    = new KeyValueHolder<>(definition, instrumentationProcessor);
060            wrappedProcessors.put(target, holder);
061        }
062        return instrumentationProcessor;
063    }
064
065}