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