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.RuntimeCamelException;
029import org.apache.camel.api.management.ManagedResource;
030import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
031import org.apache.camel.api.management.mbean.ManagedTypeConverterRegistryMBean;
032import org.apache.camel.spi.TypeConverterRegistry;
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 getMissCounter() {
064        return registry.getStatistics().getMissCounter();
065    }
066
067    public long getFailedCounter() {
068        return registry.getStatistics().getFailedCounter();
069    }
070
071    public void resetTypeConversionCounters() {
072        registry.getStatistics().reset();
073    }
074
075    public boolean isStatisticsEnabled() {
076        return registry.getStatistics().isStatisticsEnabled();
077    }
078
079    public void setStatisticsEnabled(boolean statisticsEnabled) {
080        registry.getStatistics().setStatisticsEnabled(statisticsEnabled);
081    }
082
083    public int getNumberOfTypeConverters() {
084        return registry.size();
085    }
086
087    public String getTypeConverterExistsLoggingLevel() {
088        return registry.getTypeConverterExistsLoggingLevel().name();
089    }
090
091    public String getTypeConverterExists() {
092        return registry.getTypeConverterExists().name();
093    }
094
095    public boolean hasTypeConverter(String fromType, String toType) {
096        try {
097            Class<?> from = getContext().getClassResolver().resolveMandatoryClass(fromType);
098            Class<?> to = getContext().getClassResolver().resolveMandatoryClass(toType);
099            return registry.lookup(to, from) != null;
100        } catch (ClassNotFoundException e) {
101            throw RuntimeCamelException.wrapRuntimeCamelException(e);
102        }
103    }
104
105    public TabularData listTypeConverters() {
106        try {
107            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listTypeConvertersTabularType());
108            List<Class<?>[]> converters = registry.listAllTypeConvertersFromTo();
109            for (Class<?>[] entry : converters) {
110                CompositeType ct = CamelOpenMBeanTypes.listTypeConvertersCompositeType();
111                String from = entry[0].getCanonicalName();
112                String to = entry[1].getCanonicalName();
113                CompositeData data = new CompositeDataSupport(ct, new String[]{"from", "to"}, new Object[]{from, to});
114                if (!answer.containsValue(data)) {
115                    answer.put(data);
116                }
117            }
118            return answer;
119        } catch (Exception e) {
120            throw RuntimeCamelException.wrapRuntimeCamelException(e);
121        }
122    }
123}