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.List;
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.ManagedRuntimeEndpointRegistryMBean;
032import org.apache.camel.spi.EndpointRegistry;
033import org.apache.camel.spi.ManagementStrategy;
034import org.apache.camel.spi.RuntimeEndpointRegistry;
035import org.apache.camel.util.URISupport;
036
037@ManagedResource(description = "Managed RuntimeEndpointRegistry")
038public class ManagedRuntimeEndpointRegistry extends ManagedService implements ManagedRuntimeEndpointRegistryMBean {
039
040    private final RuntimeEndpointRegistry registry;
041    private boolean sanitize;
042
043    public ManagedRuntimeEndpointRegistry(CamelContext context, RuntimeEndpointRegistry registry) {
044        super(context, registry);
045        this.registry = registry;
046    }
047
048    @Override
049    public void init(ManagementStrategy strategy) {
050        super.init(strategy);
051        sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : false;
052    }
053
054    @Override
055    public void clear() {
056        registry.clear();
057    }
058
059    @Override
060    public void reset() {
061        registry.reset();
062    }
063
064    @Override
065    public boolean isEnabled() {
066        return registry.isEnabled();
067    }
068
069    @Override
070    public void setEnabled(boolean enabled) {
071        registry.setEnabled(enabled);
072    }
073
074    @Override
075    public int getLimit() {
076        return registry.getLimit();
077    }
078
079    @Override
080    public int getSize() {
081        return registry.size();
082    }
083
084    @Override
085    public List<String> getAllEndpoints(boolean includeInputs) {
086        return registry.getAllEndpoints(includeInputs);
087    }
088
089    @Override
090    public List<String> getEndpointsPerRoute(String routeId, boolean includeInputs) {
091        return registry.getEndpointsPerRoute(routeId, includeInputs);
092    }
093
094    @Override
095    public TabularData endpointStatistics() {
096        try {
097            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listRuntimeEndpointsTabularType());
098
099            EndpointRegistry staticRegistry = getContext().getEndpointRegistry();
100            int index = 0;
101
102            for (RuntimeEndpointRegistry.Statistic stat : registry.getEndpointStatistics()) {
103                CompositeType ct = CamelOpenMBeanTypes.listRuntimeEndpointsCompositeType();
104
105                String url = stat.getUri();
106                boolean isStatic = staticRegistry.isStatic(url);
107                boolean isDynamic = staticRegistry.isDynamic(url);
108                if (sanitize) {
109                    url = URISupport.sanitizeUri(url);
110                }
111                String routeId = stat.getRouteId();
112                String direction = stat.getDirection();
113                long hits = stat.getHits();
114
115                CompositeData data = new CompositeDataSupport(
116                        ct, new String[] { "index", "url", "routeId", "direction", "static", "dynamic", "hits" },
117                        new Object[] { index, url, routeId, direction, isStatic, isDynamic, hits });
118                answer.put(data);
119
120                // use a counter as the single index in the TabularData as we do not want a multi-value index
121                index++;
122            }
123            return answer;
124        } catch (Exception e) {
125            throw RuntimeCamelException.wrapRuntimeCamelException(e);
126        }
127    }
128}