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;
020import java.util.StringJoiner;
021
022import javax.management.openmbean.CompositeData;
023import javax.management.openmbean.CompositeDataSupport;
024import javax.management.openmbean.CompositeType;
025import javax.management.openmbean.TabularData;
026import javax.management.openmbean.TabularDataSupport;
027
028import org.apache.camel.CamelContext;
029import org.apache.camel.RuntimeCamelException;
030import org.apache.camel.api.management.ManagedResource;
031import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
032import org.apache.camel.api.management.mbean.ManagedEndpointServiceRegistryMBean;
033import org.apache.camel.spi.EndpointServiceRegistry;
034import org.apache.camel.spi.ManagementStrategy;
035import org.apache.camel.util.URISupport;
036
037/**
038 *
039 */
040@ManagedResource(description = "Managed EndpointServiceRegistry")
041public class ManagedEndpointServiceRegistry extends ManagedService implements ManagedEndpointServiceRegistryMBean {
042
043    private final EndpointServiceRegistry registry;
044    private boolean sanitize;
045
046    public ManagedEndpointServiceRegistry(CamelContext context, EndpointServiceRegistry registry) {
047        super(context, registry);
048        this.registry = registry;
049    }
050
051    @Override
052    public void init(ManagementStrategy strategy) {
053        super.init(strategy);
054        sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : true;
055    }
056
057    public EndpointServiceRegistry getRegistry() {
058        return registry;
059    }
060
061    @Override
062    public int getNumberOfEndpointServices() {
063        return registry.size();
064    }
065
066    @Override
067    public TabularData listEndpointServices() {
068        try {
069            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listEndpointServicesTabularType());
070            List<EndpointServiceRegistry.EndpointService> services = registry.listAllEndpointServices();
071            for (EndpointServiceRegistry.EndpointService entry : services) {
072                CompositeType ct = CamelOpenMBeanTypes.listEndpointServicesCompositeType();
073                String component = entry.getComponent();
074                String dir = entry.getDirection();
075                String protocol = entry.getServiceProtocol();
076                String serviceUrl = entry.getServiceUrl();
077                String metadata = null;
078                String endpointUri = entry.getEndpointUri();
079                if (sanitize) {
080                    endpointUri = URISupport.sanitizeUri(endpointUri);
081                }
082
083                long hits = entry.getHits();
084                String routeId = entry.getRouteId();
085                var m = entry.getServiceMetadata();
086                if (m != null) {
087                    StringJoiner sj = new StringJoiner(" ");
088                    m.forEach((k, v) -> sj.add(k + "=" + v));
089                    metadata = sj.toString();
090                }
091
092                CompositeData data = new CompositeDataSupport(
093                        ct,
094                        new String[] {
095                                "component", "dir", "protocol", "serviceUrl", "metadata", "endpointUri", "routeId", "hits" },
096                        new Object[] {
097                                component, dir, protocol, serviceUrl, metadata, endpointUri, routeId, hits });
098                answer.put(data);
099            }
100            return answer;
101        } catch (Exception e) {
102            throw RuntimeCamelException.wrapRuntimeCamelException(e);
103        }
104    }
105
106}