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.RuntimeCamelException; 029import org.apache.camel.api.management.ManagedResource; 030import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes; 031import org.apache.camel.api.management.mbean.ManagedTransformerRegistryMBean; 032import org.apache.camel.spi.DataType; 033import org.apache.camel.spi.ManagementStrategy; 034import org.apache.camel.spi.Transformer; 035import org.apache.camel.spi.TransformerRegistry; 036 037@ManagedResource(description = "Managed TransformerRegistry") 038public class ManagedTransformerRegistry extends ManagedService implements ManagedTransformerRegistryMBean { 039 private final TransformerRegistry transformerRegistry; 040 041 public ManagedTransformerRegistry(CamelContext context, TransformerRegistry transformerRegistry) { 042 super(context, transformerRegistry); 043 this.transformerRegistry = transformerRegistry; 044 } 045 046 public void init(ManagementStrategy strategy) { 047 super.init(strategy); 048 } 049 050 public TransformerRegistry getTransformerRegistry() { 051 return transformerRegistry; 052 } 053 054 public String getSource() { 055 return transformerRegistry.toString(); 056 } 057 058 public Integer getDynamicSize() { 059 return transformerRegistry.dynamicSize(); 060 } 061 062 public Integer getStaticSize() { 063 return transformerRegistry.staticSize(); 064 } 065 066 public Integer getSize() { 067 return transformerRegistry.size(); 068 } 069 070 public Integer getMaximumCacheSize() { 071 return transformerRegistry.getMaximumCacheSize(); 072 } 073 074 public void purge() { 075 transformerRegistry.purge(); 076 } 077 078 @SuppressWarnings("unchecked") 079 public TabularData listTransformers() { 080 try { 081 TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listTransformersTabularType()); 082 Collection<Transformer> transformers = transformerRegistry.values(); 083 for (Transformer transformer : transformers) { 084 CompositeType ct = CamelOpenMBeanTypes.listTransformersCompositeType(); 085 String scheme = transformer.getModel(); 086 DataType from = transformer.getFrom(); 087 DataType to = transformer.getTo(); 088 String desc = transformer.toString(); 089 boolean fromStatic = scheme != null ? transformerRegistry.isStatic(scheme) : transformerRegistry.isStatic(from, to); 090 boolean fromDynamic = scheme != null ? transformerRegistry.isDynamic(scheme) : transformerRegistry.isDynamic(from, to); 091 092 CompositeData data = new CompositeDataSupport(ct, new String[]{"scheme", "from", "to", "static", "dynamic", "description"}, 093 new Object[]{scheme, from.toString(), to.toString(), fromStatic, fromDynamic, desc}); 094 answer.put(data); 095 } 096 return answer; 097 } catch (Exception e) { 098 throw RuntimeCamelException.wrapRuntimeCamelException(e); 099 } 100 } 101 102}