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.spring.spi;
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.CamelContext;
028 import org.apache.camel.api.management.ManagedInstance;
029 import org.apache.camel.api.management.NotificationSenderAware;
030 import org.apache.camel.management.DefaultManagementMBeanAssembler;
031 import org.apache.camel.management.NotificationSenderAdapter;
032 import org.apache.camel.util.ObjectHelper;
033 import org.slf4j.Logger;
034 import org.slf4j.LoggerFactory;
035 import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource;
036 import org.springframework.jmx.export.annotation.ManagedResource;
037 import org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler;
038
039 /**
040 * An springAssembler to assemble a {@link javax.management.modelmbean.ModelMBean} which can be used
041 * to register the object in JMX. The springAssembler is capable of using the Spring JMX annotations to
042 * gather the list of JMX operations and attributes.
043 */
044 public class SpringManagementMBeanAssembler extends DefaultManagementMBeanAssembler {
045
046 private static final Logger LOG = LoggerFactory.getLogger(SpringManagementMBeanAssembler.class);
047 private final MetadataMBeanInfoAssembler springAssembler;
048
049 public SpringManagementMBeanAssembler(CamelContext camelContext) {
050 super(camelContext);
051 this.springAssembler = new MetadataMBeanInfoAssembler();
052 this.springAssembler.setAttributeSource(new AnnotationJmxAttributeSource());
053 }
054
055 public ModelMBean assemble(MBeanServer mBeanServer, Object obj, ObjectName name) throws JMException {
056 ModelMBeanInfo mbi = null;
057
058 // prefer to use the managed instance if it has been annotated with Spring JMX annotations
059 if (obj instanceof ManagedInstance) {
060 Object custom = ((ManagedInstance) obj).getInstance();
061 if (custom != null && ObjectHelper.hasAnnotation(custom.getClass().getAnnotations(), ManagedResource.class)) {
062 LOG.trace("Assembling MBeanInfo for: {} from custom @ManagedResource object: {}", name, custom);
063 // get the mbean info from the custom managed object
064 mbi = springAssembler.getMBeanInfo(custom, name.toString());
065 // and let the custom object be registered in JMX
066 obj = custom;
067 }
068 }
069
070 if (mbi == null) {
071 if (ObjectHelper.hasAnnotation(obj.getClass().getAnnotations(), ManagedResource.class)) {
072 // the object has a Spring ManagedResource annotations so assemble the MBeanInfo
073 LOG.trace("Assembling MBeanInfo for: {} from @ManagedResource object: {}", name, obj);
074 mbi = springAssembler.getMBeanInfo(obj, name.toString());
075 } else {
076 // fallback and let the default mbean assembler handle this instead
077 return super.assemble(mBeanServer, obj, name);
078 }
079 }
080
081 LOG.trace("Assembled MBeanInfo {}", mbi);
082
083 RequiredModelMBean mbean = (RequiredModelMBean) mBeanServer.instantiate(RequiredModelMBean.class.getName());
084 mbean.setModelMBeanInfo(mbi);
085
086 try {
087 mbean.setManagedResource(obj, "ObjectReference");
088 } catch (InvalidTargetObjectTypeException e) {
089 throw new JMException(e.getMessage());
090 }
091
092 // Allows the managed object to send notifications
093 if (obj instanceof NotificationSenderAware) {
094 ((NotificationSenderAware)obj).setNotificationSender(new NotificationSenderAdapter(mbean));
095 }
096
097 return mbean;
098 }
099
100 }