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