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.MalformedObjectNameException;
020import javax.management.ObjectName;
021
022import org.apache.camel.CamelContext;
023import org.apache.camel.Processor;
024import org.apache.camel.Route;
025import org.apache.camel.RuntimeCamelException;
026import org.apache.camel.api.management.ManagedCamelContext;
027import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
028import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
029import org.apache.camel.api.management.mbean.ManagedRouteMBean;
030import org.apache.camel.api.management.mbean.ManagedStepMBean;
031import org.apache.camel.model.ModelCamelContext;
032import org.apache.camel.model.ProcessorDefinition;
033import org.apache.camel.spi.ManagementStrategy;
034
035/**
036 * JMX capable {@link CamelContext}.
037 */
038public class ManagedCamelContextImpl implements ManagedCamelContext {
039
040    private final CamelContext camelContext;
041
042    public ManagedCamelContextImpl(CamelContext camelContext) {
043        this.camelContext = camelContext;
044    }
045
046    private ManagementStrategy getManagementStrategy() {
047        return camelContext.getManagementStrategy();
048    }
049
050    public <T extends ManagedProcessorMBean> T getManagedProcessor(String id, Class<T> type) {
051        // jmx must be enabled
052        if (getManagementStrategy().getManagementAgent() == null) {
053            return null;
054        }
055
056        Processor processor = camelContext.getProcessor(id);
057        ProcessorDefinition def = camelContext.adapt(ModelCamelContext.class).getProcessorDefinition(id);
058
059        // processor may be null if its anonymous inner class or as lambda
060        if (def != null) {
061            try {
062                ObjectName on = getManagementStrategy().getManagementObjectNameStrategy()
063                        .getObjectNameForProcessor(camelContext, processor, def);
064                return getManagementStrategy().getManagementAgent().newProxyClient(on, type);
065            } catch (MalformedObjectNameException e) {
066                throw RuntimeCamelException.wrapRuntimeCamelException(e);
067            }
068        }
069
070        return null;
071    }
072
073    public ManagedStepMBean getManagedStep(String id) {
074        // jmx must be enabled
075        if (getManagementStrategy().getManagementAgent() == null) {
076            return null;
077        }
078
079        Processor processor = camelContext.getProcessor(id);
080        ProcessorDefinition def = camelContext.adapt(ModelCamelContext.class).getProcessorDefinition(id);
081
082        // processor may be null if its anonymous inner class or as lambda
083        if (def != null) {
084            try {
085                ObjectName on = getManagementStrategy().getManagementObjectNameStrategy()
086                    .getObjectNameForStep(camelContext, processor, def);
087                return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedStepMBean.class);
088            } catch (MalformedObjectNameException e) {
089                throw RuntimeCamelException.wrapRuntimeCamelException(e);
090            }
091        }
092
093        return null;
094    }
095
096    public <T extends ManagedRouteMBean> T getManagedRoute(String routeId, Class<T> type) {
097        // jmx must be enabled
098        if (getManagementStrategy().getManagementAgent() == null) {
099            return null;
100        }
101
102        Route route = camelContext.getRoute(routeId);
103
104        if (route != null) {
105            try {
106                ObjectName on = getManagementStrategy().getManagementObjectNameStrategy().getObjectNameForRoute(route);
107                return getManagementStrategy().getManagementAgent().newProxyClient(on, type);
108            } catch (MalformedObjectNameException e) {
109                throw RuntimeCamelException.wrapRuntimeCamelException(e);
110            }
111        }
112
113        return null;
114    }
115
116    public ManagedCamelContextMBean getManagedCamelContext() {
117        // jmx must be enabled
118        if (getManagementStrategy().getManagementAgent() == null) {
119            return null;
120        }
121
122        try {
123            ObjectName on = getManagementStrategy().getManagementObjectNameStrategy()
124                    .getObjectNameForCamelContext(camelContext);
125            return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedCamelContextMBean.class);
126        } catch (MalformedObjectNameException e) {
127            throw RuntimeCamelException.wrapRuntimeCamelException(e);
128        }
129    }
130
131}