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 java.util.Map;
020
021import javax.management.openmbean.CompositeData;
022import javax.management.openmbean.CompositeDataSupport;
023import javax.management.openmbean.CompositeType;
024import javax.management.openmbean.TabularData;
025import javax.management.openmbean.TabularDataSupport;
026
027import org.apache.camel.CamelContext;
028import org.apache.camel.api.management.ManagedResource;
029import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
030import org.apache.camel.api.management.mbean.ManagedWireTapMBean;
031import org.apache.camel.model.ProcessorDefinition;
032import org.apache.camel.processor.WireTapProcessor;
033import org.apache.camel.spi.EndpointUtilizationStatistics;
034import org.apache.camel.spi.ManagementStrategy;
035import org.apache.camel.util.ObjectHelper;
036import org.apache.camel.util.URISupport;
037
038/**
039 * @version 
040 */
041@ManagedResource(description = "Managed WireTapProcessor")
042public class ManagedWireTapProcessor extends ManagedProcessor implements ManagedWireTapMBean {
043    private final WireTapProcessor processor;
044    private String uri;
045    private boolean sanitize;
046
047    public ManagedWireTapProcessor(CamelContext context, WireTapProcessor processor, ProcessorDefinition<?> definition) {
048        super(context, processor, definition);
049        this.processor = processor;
050    }
051
052    public void init(ManagementStrategy strategy) {
053        super.init(strategy);
054        sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : false;
055        if (sanitize) {
056            uri = URISupport.sanitizeUri(processor.getUri());
057        } else {
058            uri = processor.getUri();
059        }
060    }
061
062    @Override
063    public void reset() {
064        super.reset();
065        if (processor.getEndpointUtilizationStatistics() != null) {
066            processor.getEndpointUtilizationStatistics().clear();
067        }
068    }
069
070    @Override
071    public Boolean getSupportExtendedInformation() {
072        return true;
073    }
074
075    public WireTapProcessor getProcessor() {
076        return processor;
077    }
078
079    public String getUri() {
080        return uri;
081    }
082
083    public Integer getCacheSize() {
084        return processor.getCacheSize();
085    }
086
087    public Boolean isIgnoreInvalidEndpoint() {
088        return processor.isIgnoreInvalidEndpoint();
089    }
090
091    public Boolean isCopy() {
092        return processor.isCopy();
093    }
094
095    public Boolean isDynamicUri() {
096        return processor.isDynamicUri();
097    }
098
099    public Integer getTaskSize() {
100        return processor.getPendingExchangesSize();
101    }
102
103    @Override
104    public TabularData extendedInformation() {
105        try {
106            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.endpointsUtilizationTabularType());
107
108            EndpointUtilizationStatistics stats = processor.getEndpointUtilizationStatistics();
109            if (stats != null) {
110                for (Map.Entry<String, Long> entry : stats.getStatistics().entrySet()) {
111                    CompositeType ct = CamelOpenMBeanTypes.endpointsUtilizationCompositeType();
112                    String url = entry.getKey();
113                    if (sanitize) {
114                        url = URISupport.sanitizeUri(url);
115                    }
116
117                    Long hits = entry.getValue();
118                    if (hits == null) {
119                        hits = 0L;
120                    }
121
122                    CompositeData data = new CompositeDataSupport(ct, new String[]{"url", "hits"}, new Object[]{url, hits});
123                    answer.put(data);
124                }
125            }
126            return answer;
127        } catch (Exception e) {
128            throw ObjectHelper.wrapRuntimeCamelException(e);
129        }
130    }
131
132}