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