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.spi;
018
019import java.io.Closeable;
020import java.util.List;
021import java.util.concurrent.CopyOnWriteArrayList;
022import java.util.function.Predicate;
023
024import org.apache.camel.CamelContext;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * A {@link CamelContext} creation and destruction tracker.
030 */
031public class CamelContextTracker implements Closeable {
032
033    private static final Logger LOG = LoggerFactory.getLogger(CamelContextTracker.class);
034
035    private static final List<CamelContextTracker> TRACKERS = new CopyOnWriteArrayList<>();
036
037    @FunctionalInterface
038    public interface Filter extends Predicate<CamelContext> {
039
040        boolean accept(CamelContext camelContext);
041
042        @Override
043        default boolean test(CamelContext camelContext) {
044            return accept(camelContext);
045        }
046    }
047
048    private final Filter filter;
049
050    public CamelContextTracker() {
051        filter = camelContext -> !camelContext.getClass().getName().contains("Proxy");
052    }
053
054    public CamelContextTracker(Filter filter) {
055        this.filter = filter;
056    }
057
058    /**
059     * Called to determine whether this tracker should accept the given context.
060     */
061    public boolean accept(CamelContext camelContext) {
062        return filter == null || filter.accept(camelContext);
063    }
064
065    /**
066     * Called when a context is created.
067     */
068    public void contextCreated(CamelContext camelContext) {
069        // do nothing
070    }
071
072    /**
073     * Called when a context has been shutdown.
074     */
075    public void contextDestroyed(CamelContext camelContext) {
076        // do nothing
077    }
078
079    /**
080     * Opens the tracker to start tracking when new {@link CamelContext} is created or destroyed.
081     */
082    public final void open() {
083        TRACKERS.add(this);
084    }
085
086    /**
087     * Closes the tracker so it not longer tracks.
088     */
089    @Override
090    public final void close() {
091        TRACKERS.remove(this);
092    }
093
094    public static synchronized void notifyContextCreated(CamelContext camelContext) {
095        for (CamelContextTracker tracker : TRACKERS) {
096            try {
097                if (tracker.accept(camelContext)) {
098                    tracker.contextCreated(camelContext);
099                }
100            } catch (Exception e) {
101                LOG.warn("Error calling CamelContext tracker. This exception is ignored.", e);
102            }
103        }
104    }
105
106    public static synchronized void notifyContextDestroyed(CamelContext camelContext) {
107        for (CamelContextTracker tracker : TRACKERS) {
108            try {
109                if (tracker.accept(camelContext)) {
110                    tracker.contextDestroyed(camelContext);
111                }
112            } catch (Exception e) {
113                LOG.warn("Error calling CamelContext tracker. This exception is ignored.", e);
114            }
115        }
116    }
117}