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.blueprint;
018
019import java.util.LinkedHashSet;
020import java.util.Set;
021
022import org.apache.aries.blueprint.ExtendedBeanMetadata;
023import org.osgi.service.blueprint.container.BlueprintContainer;
024import org.osgi.service.blueprint.reflect.ComponentMetadata;
025
026/**
027 * A helper class to find the ids of the {@link BlueprintCamelContext} defined
028 * in the {@link org.osgi.service.blueprint.container.BlueprintContainer}
029 */
030public final class BlueprintCamelContextLookupHelper {
031
032    private BlueprintCamelContextLookupHelper() {
033    }
034
035    /**
036     * Lookup all the {@link BlueprintCamelContext} in the {@link BlueprintContainer}.
037     *
038     * @param container the blueprint container, must be provided
039     * @return a set with the ids of the {@link BlueprintCamelContext}, never <tt>null</tt>, but can be empty set.
040     */
041    public static Set<String> lookupBlueprintCamelContext(BlueprintContainer container) {
042        Set<String> ids = new LinkedHashSet<>();
043        for (Object id : container.getComponentIds()) {
044            ComponentMetadata meta = container.getComponentMetadata(id.toString());
045
046            // must be extended meta, to see if its the blueprint camel context
047            if (meta instanceof ExtendedBeanMetadata) {
048                Class<?> clazz = ((ExtendedBeanMetadata) meta).getRuntimeClass();
049                if (clazz != null && BlueprintCamelContext.class.isAssignableFrom(clazz)) {
050                    // okay we found a BlueprintCamelContext
051                    ids.add(meta.getId());
052                }
053            }
054        }
055        return ids;
056    }
057}