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 javax.management.openmbean.CompositeData; 021import javax.management.openmbean.CompositeDataSupport; 022import javax.management.openmbean.CompositeType; 023import javax.management.openmbean.TabularData; 024import javax.management.openmbean.TabularDataSupport; 025 026import org.apache.camel.CamelContext; 027import org.apache.camel.api.management.ManagedResource; 028import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes; 029import org.apache.camel.api.management.mbean.ManagedTypeConverterRegistryMBean; 030import org.apache.camel.spi.TypeConverterRegistry; 031import org.apache.camel.util.ObjectHelper; 032 033/** 034 * 035 */ 036@ManagedResource(description = "Managed TypeConverterRegistry") 037public class ManagedTypeConverterRegistry extends ManagedService implements ManagedTypeConverterRegistryMBean { 038 039 private final TypeConverterRegistry registry; 040 041 public ManagedTypeConverterRegistry(CamelContext context, TypeConverterRegistry registry) { 042 super(context, registry); 043 this.registry = registry; 044 } 045 046 public TypeConverterRegistry getRegistry() { 047 return registry; 048 } 049 050 public long getAttemptCounter() { 051 return registry.getStatistics().getAttemptCounter(); 052 } 053 054 public long getHitCounter() { 055 return registry.getStatistics().getHitCounter(); 056 } 057 058 public long getMissCounter() { 059 return registry.getStatistics().getMissCounter(); 060 } 061 062 public long getFailedCounter() { 063 return registry.getStatistics().getFailedCounter(); 064 } 065 066 public void resetTypeConversionCounters() { 067 registry.getStatistics().reset(); 068 } 069 070 public boolean isStatisticsEnabled() { 071 return registry.getStatistics().isStatisticsEnabled(); 072 } 073 074 public void setStatisticsEnabled(boolean statisticsEnabled) { 075 registry.getStatistics().setStatisticsEnabled(statisticsEnabled); 076 } 077 078 public int getNumberOfTypeConverters() { 079 return registry.size(); 080 } 081 082 public boolean hasTypeConverter(String fromType, String toType) { 083 try { 084 Class<?> from = getContext().getClassResolver().resolveMandatoryClass(fromType); 085 Class<?> to = getContext().getClassResolver().resolveMandatoryClass(toType); 086 return registry.lookup(to, from) != null; 087 } catch (ClassNotFoundException e) { 088 throw ObjectHelper.wrapRuntimeCamelException(e); 089 } 090 } 091 092 public TabularData listTypeConverters() { 093 try { 094 TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listTypeConvertersTabularType()); 095 List<Class<?>[]> converters = registry.listAllTypeConvertersFromTo(); 096 for (Class<?>[] entry : converters) { 097 CompositeType ct = CamelOpenMBeanTypes.listTypeConvertersCompositeType(); 098 String from = entry[0].getCanonicalName(); 099 String to = entry[1].getCanonicalName(); 100 CompositeData data = new CompositeDataSupport(ct, new String[]{"from", "to"}, new Object[]{from, to}); 101 answer.put(data); 102 } 103 return answer; 104 } catch (Exception e) { 105 throw ObjectHelper.wrapRuntimeCamelException(e); 106 } 107 } 108}