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.ManagedEnricherMBean;
031import org.apache.camel.model.EnrichDefinition;
032import org.apache.camel.processor.Enricher;
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 Enricher")
042public class ManagedEnricher extends ManagedProcessor implements ManagedEnricherMBean {
043    private final Enricher processor;
044    private String uri;
045    private boolean sanitize;
046
047    public ManagedEnricher(CamelContext context, Enricher processor, EnrichDefinition 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        uri = getDefinition().getExpression().getExpression();
056        if (sanitize) {
057            uri = URISupport.sanitizeUri(uri);
058        }
059    }
060
061    @Override
062    public void reset() {
063        super.reset();
064        if (processor.getEndpointUtilizationStatistics() != null) {
065            processor.getEndpointUtilizationStatistics().clear();
066        }
067    }
068
069    @Override
070    public Boolean getSupportExtendedInformation() {
071        return true;
072    }
073
074    @Override
075    public EnrichDefinition getDefinition() {
076        return (EnrichDefinition) super.getDefinition();
077    }
078
079    @Override
080    public Enricher getProcessor() {
081        return processor;
082    }
083
084    @Override
085    public String getExpressionLanguage() {
086        return getDefinition().getExpression().getLanguage();
087    }
088
089    @Override
090    public String getExpression() {
091        return uri;
092    }
093
094    @Override
095    public Integer getCacheSize() {
096        return processor.getCacheSize();
097    }
098
099    @Override
100    public Boolean isIgnoreInvalidEndpoint() {
101        return processor.isIgnoreInvalidEndpoint();
102    }
103
104    @Override
105    public Boolean isShareUnitOfWork() {
106        return processor.isShareUnitOfWork();
107    }
108
109    @Override
110    public Boolean isAggregateOnException() {
111        return processor.isAggregateOnException();
112    }
113
114    @Override
115    public TabularData extendedInformation() {
116        try {
117            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.endpointsUtilizationTabularType());
118
119            EndpointUtilizationStatistics stats = processor.getEndpointUtilizationStatistics();
120            if (stats != null) {
121                for (Map.Entry<String, Long> entry : stats.getStatistics().entrySet()) {
122                    CompositeType ct = CamelOpenMBeanTypes.endpointsUtilizationCompositeType();
123                    String url = entry.getKey();
124                    if (sanitize) {
125                        url = URISupport.sanitizeUri(url);
126                    }
127
128                    Long hits = entry.getValue();
129                    if (hits == null) {
130                        hits = 0L;
131                    }
132
133                    CompositeData data = new CompositeDataSupport(ct, new String[]{"url", "hits"}, new Object[]{url, hits});
134                    answer.put(data);
135                }
136            }
137            return answer;
138        } catch (Exception e) {
139            throw ObjectHelper.wrapRuntimeCamelException(e);
140        }
141    }
142
143}