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.ManagedRecipientListMBean;
032import org.apache.camel.model.RecipientListDefinition;
033import org.apache.camel.processor.RecipientList;
034import org.apache.camel.spi.EndpointUtilizationStatistics;
035import org.apache.camel.spi.ManagementStrategy;
036import org.apache.camel.util.URISupport;
037
038@ManagedResource(description = "Managed RecipientList")
039public class ManagedRecipientList extends ManagedProcessor implements ManagedRecipientListMBean {
040    private final RecipientList processor;
041    private String uri;
042    private boolean sanitize;
043
044    public ManagedRecipientList(CamelContext context, RecipientList processor, RecipientListDefinition definition) {
045        super(context, processor, definition);
046        this.processor = processor;
047    }
048
049    @Override
050    public void init(ManagementStrategy strategy) {
051        super.init(strategy);
052        sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : false;
053        uri = getDefinition().getExpression().getExpression();
054        if (sanitize) {
055            uri = URISupport.sanitizeUri(uri);
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    @Override
073    public RecipientListDefinition getDefinition() {
074        return (RecipientListDefinition) super.getDefinition();
075    }
076
077    @Override
078    public String getExpressionLanguage() {
079        return getDefinition().getExpression().getLanguage();
080    }
081
082    @Override
083    public String getExpression() {
084        return uri;
085    }
086
087    @Override
088    public String getUriDelimiter() {
089        return processor.getDelimiter();
090    }
091
092    @Override
093    public Integer getCacheSize() {
094        return processor.getCacheSize();
095    }
096
097    @Override
098    public Boolean isParallelAggregate() {
099        return processor.isParallelAggregate();
100    }
101
102    @Override
103    public Boolean isParallelProcessing() {
104        return processor.isParallelProcessing();
105    }
106
107    @Override
108    public Boolean isStreaming() {
109        return processor.isStreaming();
110    }
111
112    @Override
113    public Boolean isStopOnException() {
114        return processor.isStopOnException();
115    }
116
117    @Override
118    public Boolean isShareUnitOfWork() {
119        return processor.isShareUnitOfWork();
120    }
121
122    @Override
123    public Long getTimeout() {
124        return processor.getTimeout();
125    }
126
127    @Override
128    public TabularData extendedInformation() {
129        try {
130            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.endpointsUtilizationTabularType());
131
132            EndpointUtilizationStatistics stats = processor.getEndpointUtilizationStatistics();
133            if (stats != null) {
134                for (Map.Entry<String, Long> entry : stats.getStatistics().entrySet()) {
135                    CompositeType ct = CamelOpenMBeanTypes.endpointsUtilizationCompositeType();
136                    String url = entry.getKey();
137                    if (sanitize) {
138                        url = URISupport.sanitizeUri(url);
139                    }
140
141                    Long hits = entry.getValue();
142                    if (hits == null) {
143                        hits = 0L;
144                    }
145
146                    CompositeData data
147                            = new CompositeDataSupport(ct, new String[] { "url", "hits" }, new Object[] { url, hits });
148                    answer.put(data);
149                }
150            }
151            return answer;
152        } catch (Exception e) {
153            throw RuntimeCamelException.wrapRuntimeCamelException(e);
154        }
155    }
156
157}