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 java.util.List; 020import java.util.Map; 021import java.util.Optional; 022 023import org.apache.camel.CamelContext; 024import org.apache.camel.Service; 025import org.apache.camel.spi.LifecycleStrategy; 026import org.apache.camel.spi.ManagementStrategy; 027import org.apache.camel.spi.ManagementStrategyFactory; 028import org.apache.camel.support.PropertyBindingSupport; 029 030/** 031 * Factory for creating JMX {@link ManagementStrategy}. 032 */ 033public class JmxManagementStrategyFactory implements ManagementStrategyFactory { 034 035 @Override 036 public ManagementStrategy create(CamelContext context, Map<String, Object> options) throws Exception { 037 DefaultManagementAgent agent = new DefaultManagementAgent(context); 038 if (options != null) { 039 PropertyBindingSupport.build().bind(context, agent, options); 040 } 041 042 return new JmxManagementStrategy(context, agent); 043 } 044 045 @Override 046 public LifecycleStrategy createLifecycle(CamelContext context) throws Exception { 047 return new JmxManagementLifecycleStrategy(context); 048 } 049 050 @Override 051 public void setupManagement(CamelContext camelContext, ManagementStrategy strategy, LifecycleStrategy lifecycle) { 052 camelContext.setManagementStrategy(strategy); 053 // must add management lifecycle strategy as first choice 054 if (!camelContext.getLifecycleStrategies().isEmpty()) { 055 056 // a bit of ugly code to handover pre registered services that has been add to an eager/provisional JmxManagementLifecycleStrategy 057 // which is now re-placed with a new JmxManagementLifecycleStrategy that is based on the end user configured settings 058 // and therefore will be in use 059 List<JmxManagementLifecycleStrategy.PreRegisterService> preServices = null; 060 JmxManagementLifecycleStrategy jmx = camelContext.getLifecycleStrategies().stream() 061 .filter(s -> s instanceof JmxManagementLifecycleStrategy) 062 .map(JmxManagementLifecycleStrategy.class::cast) 063 .findFirst().orElse(null); 064 if (jmx != null) { 065 preServices = jmx.getPreServices(); 066 } 067 068 if (preServices != null && !preServices.isEmpty() && lifecycle instanceof JmxManagementLifecycleStrategy) { 069 JmxManagementLifecycleStrategy existing = (JmxManagementLifecycleStrategy) lifecycle; 070 for (JmxManagementLifecycleStrategy.PreRegisterService pre : preServices) { 071 existing.addPreService(pre); 072 } 073 } 074 075 // camel-spring/camel-blueprint may re-initialize JMX during startup, so remove any previous 076 camelContext.getLifecycleStrategies().removeIf(s -> s instanceof JmxManagementLifecycleStrategy); 077 } 078 camelContext.getLifecycleStrategies().add(0, lifecycle); 079 } 080 081}