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 */ 017 package org.apache.camel.spring.spi; 018 019 import javax.management.JMException; 020 import javax.management.MBeanServer; 021 import javax.management.ObjectName; 022 import javax.management.modelmbean.InvalidTargetObjectTypeException; 023 import javax.management.modelmbean.ModelMBean; 024 import javax.management.modelmbean.ModelMBeanInfo; 025 import javax.management.modelmbean.RequiredModelMBean; 026 027 import org.apache.camel.api.management.ManagedInstance; 028 import org.apache.camel.management.DefaultManagementMBeanAssembler; 029 import org.apache.camel.util.ObjectHelper; 030 import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource; 031 import org.springframework.jmx.export.annotation.ManagedResource; 032 import org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler; 033 034 /** 035 * An assembler to assemble a {@link javax.management.modelmbean.ModelMBean} which can be used 036 * to register the object in JMX. The assembler is capable of using the Spring JMX annotations to 037 * gather the list of JMX operations and attributes. 038 * 039 */ 040 public class SpringManagementMBeanAssembler extends DefaultManagementMBeanAssembler { 041 042 private final MetadataMBeanInfoAssembler assembler; 043 044 public SpringManagementMBeanAssembler() { 045 this.assembler = new MetadataMBeanInfoAssembler(); 046 this.assembler.setAttributeSource(new AnnotationJmxAttributeSource()); 047 } 048 049 public ModelMBean assemble(MBeanServer mBeanServer, Object obj, ObjectName name) throws JMException { 050 ModelMBeanInfo mbi = null; 051 052 // prefer to use the managed instance if it has been annotated with Spring JMX annotations 053 if (obj instanceof ManagedInstance) { 054 Object custom = ((ManagedInstance) obj).getInstance(); 055 if (custom != null && ObjectHelper.hasAnnotation(custom.getClass().getAnnotations(), ManagedResource.class)) { 056 log.trace("Assembling MBeanInfo for: {} from custom @ManagedResource object: {}", name, custom); 057 // get the mbean info from the custom managed object 058 mbi = assembler.getMBeanInfo(custom, name.toString()); 059 // and let the custom object be registered in JMX 060 obj = custom; 061 } 062 } 063 064 if (mbi == null) { 065 if (ObjectHelper.hasAnnotation(obj.getClass().getAnnotations(), ManagedResource.class)) { 066 // the object has a Spring ManagedResource annotations so assemble the MBeanInfo 067 log.trace("Assembling MBeanInfo for: {} from @ManagedResource object: {}", name, obj); 068 mbi = assembler.getMBeanInfo(obj, name.toString()); 069 } else { 070 // fallback and let the default mbean assembler handle this instead 071 return super.assemble(mBeanServer, obj, name); 072 } 073 } 074 075 log.trace("Assembled MBeanInfo {}", mbi); 076 077 ModelMBean mbean = (RequiredModelMBean) mBeanServer.instantiate(RequiredModelMBean.class.getName()); 078 mbean.setModelMBeanInfo(mbi); 079 080 try { 081 mbean.setManagedResource(obj, "ObjectReference"); 082 } catch (InvalidTargetObjectTypeException e) { 083 throw new JMException(e.getMessage()); 084 } 085 086 return mbean; 087 } 088 089 }