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