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