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