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