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;
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.ManagedResource;
030import org.apache.camel.api.management.NotificationSenderAware;
031import org.apache.camel.spi.ManagementMBeanAssembler;
032import org.apache.camel.support.service.ServiceHelper;
033import org.apache.camel.support.service.ServiceSupport;
034import org.apache.camel.util.ObjectHelper;
035
036/**
037 * An assembler to assemble a {@link javax.management.modelmbean.ModelMBean} which can be used
038 * to register the object in JMX. The assembler is capable of using the Camel JMX annotations to
039 * gather the list of JMX operations and attributes.
040 */
041public class DefaultManagementMBeanAssembler extends ServiceSupport implements ManagementMBeanAssembler {
042
043    protected final MBeanInfoAssembler assembler;
044    protected final CamelContext camelContext;
045
046    public DefaultManagementMBeanAssembler(CamelContext camelContext) {
047        this.camelContext = camelContext;
048        this.assembler = new MBeanInfoAssembler();
049    }
050
051    public ModelMBean assemble(MBeanServer mBeanServer, Object obj, ObjectName name) throws JMException {
052        ModelMBeanInfo mbi = null;
053        ModelMBeanInfo standardMbi = null;
054        Object custom = null;
055
056        // prefer to use the managed instance if it has been annotated with JMX annotations
057        if (obj instanceof ManagedInstance) {
058            // there may be a custom embedded instance which have additional methods
059            custom = ((ManagedInstance) obj).getInstance();
060            if (custom != null && ObjectHelper.hasAnnotation(custom.getClass().getAnnotations(), ManagedResource.class)) {
061                log.trace("Assembling MBeanInfo for: {} from custom @ManagedResource object: {}", name, custom);
062                // get the mbean info into different groups (mbi = both, standard = standard out of the box mbi)
063                mbi = assembler.getMBeanInfo(obj, custom, name.toString());
064                standardMbi = assembler.getMBeanInfo(obj, null, name.toString());
065            }
066        }
067
068        if (mbi == null) {
069            // use the default provided mbean which has been annotated with JMX annotations
070            log.trace("Assembling MBeanInfo for: {} from @ManagedResource object: {}", name, obj);
071            mbi = assembler.getMBeanInfo(obj, null, name.toString());
072        }
073
074        if (mbi == null) {
075            return null;
076        }
077
078        RequiredModelMBean mbean;
079        RequiredModelMBean mixinMBean = null;
080
081        boolean sanitize = camelContext.getManagementStrategy().getManagementAgent().getMask() != null && camelContext.getManagementStrategy().getManagementAgent().getMask();
082
083        // if we have a custom mbean then create a mixin mbean for the standard mbean which we would
084        // otherwise have created that contains the out of the box attributes and operations
085        // as we want a combined mbean that has both the custom and the standard
086        if (standardMbi != null) {
087            mixinMBean = (RequiredModelMBean) mBeanServer.instantiate(RequiredModelMBean.class.getName());
088            mixinMBean.setModelMBeanInfo(standardMbi);
089            try {
090                mixinMBean.setManagedResource(obj, "ObjectReference");
091            } catch (InvalidTargetObjectTypeException e) {
092                throw new JMException(e.getMessage());
093            }
094            // use custom as the object to call
095            obj = custom;
096        }
097
098        // use a mixin mbean model to combine the custom and standard (custom is optional)
099        mbean = new MixinRequiredModelMBean(mbi, sanitize, standardMbi, mixinMBean);
100
101        try {
102            mbean.setManagedResource(obj, "ObjectReference");
103        } catch (InvalidTargetObjectTypeException e) {
104            throw new JMException(e.getMessage());
105        }
106
107        // Allows the managed object to send notifications
108        if (obj instanceof NotificationSenderAware) {
109            ((NotificationSenderAware)obj).setNotificationSender(new NotificationSenderAdapter(mbean));
110        }
111
112        return mbean;
113    }
114
115    @Override
116    protected void doStart() throws Exception {
117        ServiceHelper.startService(assembler);
118    }
119
120    @Override
121    protected void doStop() throws Exception {
122        ServiceHelper.stopService(assembler);
123    }
124}