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.Model; 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 @Override 051 public <T extends ManagedProcessorMBean> T getManagedProcessor(String id, Class<T> type) { 052 // jmx must be enabled 053 if (getManagementStrategy().getManagementAgent() == null) { 054 return null; 055 } 056 057 Processor processor = camelContext.getProcessor(id); 058 ProcessorDefinition<?> def 059 = camelContext.getCamelContextExtension().getContextPlugin(Model.class).getProcessorDefinition(id); 060 061 // processor may be null if its anonymous inner class or as lambda 062 if (def != null) { 063 try { 064 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy() 065 .getObjectNameForProcessor(camelContext, processor, def); 066 return getManagementStrategy().getManagementAgent().newProxyClient(on, type); 067 } catch (MalformedObjectNameException e) { 068 throw RuntimeCamelException.wrapRuntimeCamelException(e); 069 } 070 } 071 072 return null; 073 } 074 075 @Override 076 public ManagedStepMBean getManagedStep(String id) { 077 // jmx must be enabled 078 if (getManagementStrategy().getManagementAgent() == null) { 079 return null; 080 } 081 082 Processor processor = camelContext.getProcessor(id); 083 ProcessorDefinition<?> def 084 = camelContext.getCamelContextExtension().getContextPlugin(Model.class).getProcessorDefinition(id); 085 086 // processor may be null if its anonymous inner class or as lambda 087 if (def != null) { 088 try { 089 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy() 090 .getObjectNameForStep(camelContext, processor, def); 091 return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedStepMBean.class); 092 } catch (MalformedObjectNameException e) { 093 throw RuntimeCamelException.wrapRuntimeCamelException(e); 094 } 095 } 096 097 return null; 098 } 099 100 @Override 101 public <T extends ManagedRouteMBean> T getManagedRoute(String routeId, Class<T> type) { 102 // jmx must be enabled 103 if (getManagementStrategy().getManagementAgent() == null) { 104 return null; 105 } 106 107 Route route = camelContext.getRoute(routeId); 108 109 if (route != null) { 110 try { 111 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy().getObjectNameForRoute(route); 112 return getManagementStrategy().getManagementAgent().newProxyClient(on, type); 113 } catch (MalformedObjectNameException e) { 114 throw RuntimeCamelException.wrapRuntimeCamelException(e); 115 } 116 } 117 118 return null; 119 } 120 121 @Override 122 public ManagedCamelContextMBean getManagedCamelContext() { 123 // jmx must be enabled 124 if (getManagementStrategy().getManagementAgent() == null) { 125 return null; 126 } 127 // jmx must be started 128 if (getManagementStrategy().getManagementObjectNameStrategy() == null) { 129 return null; 130 } 131 132 try { 133 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy() 134 .getObjectNameForCamelContext(camelContext); 135 return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedCamelContextMBean.class); 136 } catch (MalformedObjectNameException e) { 137 throw RuntimeCamelException.wrapRuntimeCamelException(e); 138 } 139 } 140 141}