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.Consumer; 024import org.apache.camel.Processor; 025import org.apache.camel.Route; 026import org.apache.camel.RuntimeCamelException; 027import org.apache.camel.api.management.ManagedCamelContext; 028import org.apache.camel.api.management.mbean.ManagedCamelContextMBean; 029import org.apache.camel.api.management.mbean.ManagedConsumerMBean; 030import org.apache.camel.api.management.mbean.ManagedProcessorMBean; 031import org.apache.camel.api.management.mbean.ManagedRouteMBean; 032import org.apache.camel.api.management.mbean.ManagedStepMBean; 033import org.apache.camel.model.Model; 034import org.apache.camel.model.ProcessorDefinition; 035import org.apache.camel.spi.ManagementStrategy; 036 037/** 038 * JMX capable {@link CamelContext}. 039 */ 040public class ManagedCamelContextImpl implements ManagedCamelContext { 041 042 private final CamelContext camelContext; 043 044 public ManagedCamelContextImpl(CamelContext camelContext) { 045 this.camelContext = camelContext; 046 } 047 048 private ManagementStrategy getManagementStrategy() { 049 return camelContext.getManagementStrategy(); 050 } 051 052 @Override 053 public <T extends ManagedProcessorMBean> T getManagedProcessor(String id, Class<T> type) { 054 // jmx must be enabled 055 if (getManagementStrategy().getManagementAgent() == null) { 056 return null; 057 } 058 059 Processor processor = camelContext.getProcessor(id); 060 ProcessorDefinition<?> def 061 = camelContext.getCamelContextExtension().getContextPlugin(Model.class).getProcessorDefinition(id); 062 063 // processor may be null if its anonymous inner class or as lambda 064 if (def != null) { 065 try { 066 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy() 067 .getObjectNameForProcessor(camelContext, processor, def); 068 return getManagementStrategy().getManagementAgent().newProxyClient(on, type); 069 } catch (MalformedObjectNameException e) { 070 throw RuntimeCamelException.wrapRuntimeCamelException(e); 071 } 072 } 073 074 return null; 075 } 076 077 @Override 078 public ManagedStepMBean getManagedStep(String id) { 079 // jmx must be enabled 080 if (getManagementStrategy().getManagementAgent() == null) { 081 return null; 082 } 083 084 Processor processor = camelContext.getProcessor(id); 085 ProcessorDefinition<?> def 086 = camelContext.getCamelContextExtension().getContextPlugin(Model.class).getProcessorDefinition(id); 087 088 // processor may be null if its anonymous inner class or as lambda 089 if (def != null) { 090 try { 091 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy() 092 .getObjectNameForStep(camelContext, processor, def); 093 return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedStepMBean.class); 094 } catch (MalformedObjectNameException e) { 095 throw RuntimeCamelException.wrapRuntimeCamelException(e); 096 } 097 } 098 099 return null; 100 } 101 102 @Override 103 public <T extends ManagedRouteMBean> T getManagedRoute(String routeId, Class<T> type) { 104 // jmx must be enabled 105 if (getManagementStrategy().getManagementAgent() == null) { 106 return null; 107 } 108 109 Route route = camelContext.getRoute(routeId); 110 111 if (route != null) { 112 try { 113 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy().getObjectNameForRoute(route); 114 return getManagementStrategy().getManagementAgent().newProxyClient(on, type); 115 } catch (MalformedObjectNameException e) { 116 throw RuntimeCamelException.wrapRuntimeCamelException(e); 117 } 118 } 119 120 return null; 121 } 122 123 @Override 124 public <T extends ManagedConsumerMBean> T getManagedConsumer(String id, Class<T> type) { 125 // jmx must be enabled 126 if (getManagementStrategy().getManagementAgent() == null) { 127 return null; 128 } 129 130 Route route = camelContext.getRoute(id); 131 if (route != null) { 132 try { 133 Consumer consumer = route.getConsumer(); 134 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy().getObjectNameForConsumer(camelContext, 135 consumer); 136 return getManagementStrategy().getManagementAgent().newProxyClient(on, type); 137 } catch (MalformedObjectNameException e) { 138 throw RuntimeCamelException.wrapRuntimeCamelException(e); 139 } 140 } 141 142 return null; 143 } 144 145 @Override 146 public ManagedCamelContextMBean getManagedCamelContext() { 147 // jmx must be enabled 148 if (getManagementStrategy().getManagementAgent() == null) { 149 return null; 150 } 151 // jmx must be started 152 if (getManagementStrategy().getManagementObjectNameStrategy() == null) { 153 return null; 154 } 155 156 try { 157 ObjectName on = getManagementStrategy().getManagementObjectNameStrategy() 158 .getObjectNameForCamelContext(camelContext); 159 return getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedCamelContextMBean.class); 160 } catch (MalformedObjectNameException e) { 161 throw RuntimeCamelException.wrapRuntimeCamelException(e); 162 } 163 } 164 165}