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    @Override
053    public ModelMBean assemble(MBeanServer mBeanServer, Object obj, ObjectName name) throws JMException {
054        ModelMBeanInfo mbi = null;
055
056        // prefer to use the managed instance if it has been annotated with Spring JMX annotations
057        if (obj instanceof ManagedInstance) {
058            Object custom = ((ManagedInstance) obj).getInstance();
059            if (custom != null && ObjectHelper.hasAnnotation(custom.getClass().getAnnotations(), ManagedResource.class)) {
060                log.trace("Assembling MBeanInfo for: {} from custom @ManagedResource object: {}", name, custom);
061                // get the mbean info from the custom managed object
062                mbi = springAssembler.getMBeanInfo(custom, name.toString());
063                // and let the custom object be registered in JMX
064                obj = custom;
065            }
066        }
067
068        if (mbi == null) {
069            if (ObjectHelper.hasAnnotation(obj.getClass().getAnnotations(), ManagedResource.class)) {
070                // the object has a Spring ManagedResource annotations so assemble the MBeanInfo
071                log.trace("Assembling MBeanInfo for: {} from @ManagedResource object: {}", name, obj);
072                mbi = springAssembler.getMBeanInfo(obj, name.toString());
073            } else {
074                // fallback and let the default mbean assembler handle this instead
075                return super.assemble(mBeanServer, obj, name);
076            }
077        }
078
079        log.trace("Assembled MBeanInfo {}", mbi);
080
081        RequiredModelMBean mbean = (RequiredModelMBean) mBeanServer.instantiate(RequiredModelMBean.class.getName());
082        mbean.setModelMBeanInfo(mbi);
083
084        try {
085            mbean.setManagedResource(obj, "ObjectReference");
086        } catch (InvalidTargetObjectTypeException e) {
087            throw new JMException(e.getMessage());
088        }
089
090        // Allows the managed object to send notifications
091        if (obj instanceof NotificationSenderAware) {
092            ((NotificationSenderAware)obj).setNotificationSender(new NotificationSenderAdapter(mbean));
093        }
094
095        return mbean;
096    }
097
098}