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.mbean;
018
019import javax.management.openmbean.CompositeData;
020import javax.management.openmbean.CompositeDataSupport;
021import javax.management.openmbean.CompositeType;
022import javax.management.openmbean.TabularData;
023import javax.management.openmbean.TabularDataSupport;
024
025import org.apache.camel.CamelContext;
026import org.apache.camel.RuntimeCamelException;
027import org.apache.camel.api.management.ManagedResource;
028import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
029import org.apache.camel.api.management.mbean.ManagedSendProcessorMBean;
030import org.apache.camel.model.ProcessorDefinition;
031import org.apache.camel.processor.SendProcessor;
032import org.apache.camel.spi.ManagementStrategy;
033import org.apache.camel.util.URISupport;
034
035@ManagedResource(description = "Managed SendProcessor")
036public class ManagedSendProcessor extends ManagedProcessor implements ManagedSendProcessorMBean {
037    private final SendProcessor processor;
038    private String destination;
039
040    public ManagedSendProcessor(CamelContext context, SendProcessor processor, ProcessorDefinition<?> definition) {
041        super(context, processor, definition);
042        this.processor = processor;
043    }
044
045    @Override
046    public void init(ManagementStrategy strategy) {
047        super.init(strategy);
048        boolean sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : false;
049        if (sanitize) {
050            destination = URISupport.sanitizeUri(processor.getDestination().getEndpointUri());
051        } else {
052            destination = processor.getDestination().getEndpointUri();
053        }
054    }
055
056    @Override
057    public Boolean getSupportExtendedInformation() {
058        return true;
059    }
060
061    @Override
062    public void reset() {
063        super.reset();
064        processor.reset();
065    }
066
067    @Override
068    public SendProcessor getProcessor() {
069        return processor;
070    }
071
072    @Override
073    public String getDestination() {
074        return destination;
075    }
076
077    @Override
078    public String getMessageExchangePattern() {
079        if (processor.getPattern() != null) {
080            return processor.getPattern().name();
081        } else {
082            return null;
083        }
084    }
085
086    @Override
087    public TabularData extendedInformation() {
088        try {
089            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.endpointsUtilizationTabularType());
090
091            // we only have 1 endpoint
092            CompositeType ct = CamelOpenMBeanTypes.endpointsUtilizationCompositeType();
093            String url = getDestination();
094            long hits = processor.getCounter();
095
096            CompositeData data = new CompositeDataSupport(ct, new String[] { "url", "hits" }, new Object[] { url, hits });
097            answer.put(data);
098            return answer;
099        } catch (Exception e) {
100            throw RuntimeCamelException.wrapRuntimeCamelException(e);
101        }
102    }
103}