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