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.Collection;
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.Endpoint;
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.ManagedEndpointRegistryMBean;
033import org.apache.camel.spi.EndpointRegistry;
034import org.apache.camel.spi.ManagementStrategy;
035import org.apache.camel.util.URISupport;
036
037@ManagedResource(description = "Managed EndpointRegistry")
038public class ManagedEndpointRegistry extends ManagedService implements ManagedEndpointRegistryMBean {
039    private final EndpointRegistry endpointRegistry;
040    private boolean sanitize;
041
042    public ManagedEndpointRegistry(CamelContext context, EndpointRegistry endpointRegistry) {
043        super(context, endpointRegistry);
044        this.endpointRegistry = endpointRegistry;
045    }
046
047    @Override
048    public void init(ManagementStrategy strategy) {
049        super.init(strategy);
050        sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : false;
051    }
052
053    public EndpointRegistry getEndpointRegistry() {
054        return endpointRegistry;
055    }
056
057    @Override
058    public String getSource() {
059        return endpointRegistry.toString();
060    }
061
062    @Override
063    public Integer getDynamicSize() {
064        return endpointRegistry.dynamicSize();
065    }
066
067    @Override
068    public Integer getStaticSize() {
069        return endpointRegistry.staticSize();
070    }
071
072    @Override
073    public Integer getSize() {
074        return endpointRegistry.size();
075    }
076
077    @Override
078    public Integer getMaximumCacheSize() {
079        return endpointRegistry.getMaximumCacheSize();
080    }
081
082    @Override
083    public void purge() {
084        endpointRegistry.purge();
085    }
086
087    @Override
088    @SuppressWarnings("unchecked")
089    public TabularData listEndpoints() {
090        try {
091            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listEndpointsTabularType());
092            Collection<Endpoint> endpoints = endpointRegistry.values();
093            for (Endpoint endpoint : endpoints) {
094                CompositeType ct = CamelOpenMBeanTypes.listEndpointsCompositeType();
095                String url = endpoint.getEndpointUri();
096                if (sanitize) {
097                    url = URISupport.sanitizeUri(url);
098                }
099
100                boolean fromStatic = endpointRegistry.isStatic(url);
101                boolean fromDynamic = endpointRegistry.isDynamic(url);
102
103                CompositeData data = new CompositeDataSupport(
104                        ct, new String[] { "url", "static", "dynamic" }, new Object[] { url, fromStatic, fromDynamic });
105                answer.put(data);
106            }
107            return answer;
108        } catch (Exception e) {
109            throw RuntimeCamelException.wrapRuntimeCamelException(e);
110        }
111    }
112
113}