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.impl;
018
019import java.util.LinkedHashSet;
020import java.util.Set;
021
022import org.apache.camel.CamelContext;
023import org.apache.camel.spi.CamelContextTracker;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * A registry for {@link CamelContextTracker}.
029 */
030public final class CamelContextTrackerRegistry {
031
032    /**
033     * The registry singleton
034     */
035    public static final CamelContextTrackerRegistry INSTANCE = new CamelContextTrackerRegistry();
036
037    private static final Logger LOG = LoggerFactory.getLogger(CamelContextTrackerRegistry.class);
038
039    private final Set<CamelContextTracker> trackers = new LinkedHashSet<>();
040
041    private CamelContextTrackerRegistry() {
042        // hide constructor
043    }
044
045    public synchronized void addTracker(CamelContextTracker tracker) {
046        trackers.add(tracker);
047    }
048
049    public synchronized void removeTracker(CamelContextTracker tracker) {
050        trackers.remove(tracker);
051    }
052
053    synchronized void contextCreated(CamelContext camelContext) {
054        for (CamelContextTracker tracker : trackers) {
055            try {
056                if (tracker.accept(camelContext)) {
057                    tracker.contextCreated(camelContext);
058                }
059            } catch (Exception e) {
060                LOG.warn("Error calling CamelContext tracker. This exception is ignored.", e);
061            }
062        }
063    }
064}