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