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 public void init(ManagementStrategy strategy) { 048 super.init(strategy); 049 sanitize = strategy.getManagementAgent().getMask() != null ? strategy.getManagementAgent().getMask() : false; 050 } 051 052 public EndpointRegistry getEndpointRegistry() { 053 return endpointRegistry; 054 } 055 056 public String getSource() { 057 return endpointRegistry.toString(); 058 } 059 060 public Integer getDynamicSize() { 061 return endpointRegistry.dynamicSize(); 062 } 063 064 public Integer getStaticSize() { 065 return endpointRegistry.staticSize(); 066 } 067 068 public Integer getSize() { 069 return endpointRegistry.size(); 070 } 071 072 public Integer getMaximumCacheSize() { 073 return endpointRegistry.getMaximumCacheSize(); 074 } 075 076 public void purge() { 077 endpointRegistry.purge(); 078 } 079 080 @SuppressWarnings("unchecked") 081 public TabularData listEndpoints() { 082 try { 083 TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listEndpointsTabularType()); 084 Collection<Endpoint> endpoints = endpointRegistry.values(); 085 for (Endpoint endpoint : endpoints) { 086 CompositeType ct = CamelOpenMBeanTypes.listEndpointsCompositeType(); 087 String url = endpoint.getEndpointUri(); 088 if (sanitize) { 089 url = URISupport.sanitizeUri(url); 090 } 091 092 boolean fromStatic = endpointRegistry.isStatic(url); 093 boolean fromDynamic = endpointRegistry.isDynamic(url); 094 095 CompositeData data = new CompositeDataSupport(ct, new String[]{"url", "static", "dynamic"}, new Object[]{url, fromStatic, fromDynamic}); 096 answer.put(data); 097 } 098 return answer; 099 } catch (Exception e) { 100 throw RuntimeCamelException.wrapRuntimeCamelException(e); 101 } 102 } 103 104}