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