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.HashSet;
020import java.util.LinkedHashMap;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.camel.NoSuchBeanException;
025import org.apache.camel.spi.BeanRepository;
026import org.apache.camel.spi.Registry;
027import org.osgi.framework.Bundle;
028import org.osgi.service.blueprint.container.BlueprintContainer;
029import org.osgi.service.blueprint.container.NoSuchComponentException;
030import org.osgi.service.blueprint.reflect.BeanMetadata;
031import org.osgi.service.blueprint.reflect.ComponentMetadata;
032import org.osgi.service.blueprint.reflect.ReferenceMetadata;
033
034public class BlueprintContainerBeanRepository implements BeanRepository {
035
036    private final BlueprintContainer blueprintContainer;
037
038    public BlueprintContainerBeanRepository(BlueprintContainer blueprintContainer) {
039        this.blueprintContainer = blueprintContainer;
040    }
041
042    @Override
043    public Object lookupByName(String name) {
044        try {
045            return blueprintContainer.getComponentInstance(name);
046        } catch (NoSuchComponentException e) {
047            return null;
048        }
049    }
050
051    @Override
052    public <T> T lookupByNameAndType(String name, Class<T> type) {
053        Object answer;
054        try {
055            answer = blueprintContainer.getComponentInstance(name);
056        } catch (NoSuchComponentException e) {
057            return null;
058        }
059
060        // just to be safe
061        if (answer == null) {
062            return null;
063        }
064
065        try {
066            return type.cast(answer);
067        } catch (Throwable e) {
068            String msg = "Found bean: " + name + " in BlueprintContainer: " + blueprintContainer
069                    + " of type: " + answer.getClass().getName() + " expected type was: " + type;
070            throw new NoSuchBeanException(name, msg, e);
071        }
072    }
073
074    @Override
075    public <T> Map<String, T> findByTypeWithName(Class<T> type) {
076        return lookupByType(blueprintContainer, type);
077    }
078
079    @Override
080    public <T> Set<T> findByType(Class<T> type) {
081        Map<String, T> map = lookupByType(blueprintContainer, type);
082        return new HashSet<>(map.values());
083    }
084
085    public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer, Class<T> type) {
086        return lookupByType(blueprintContainer, type, true);
087    }
088
089    public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer, Class<T> type, boolean includeNonSingletons) {
090        Bundle bundle = (Bundle) blueprintContainer.getComponentInstance("blueprintBundle");
091        Map<String, T> objects = new LinkedHashMap<>();
092        Set<String> ids = blueprintContainer.getComponentIds();
093        for (String id : ids) {
094            try {
095                ComponentMetadata metadata = blueprintContainer.getComponentMetadata(id);
096                Class<?> cl = null;
097                if (metadata instanceof BeanMetadata) {
098                    BeanMetadata beanMetadata = (BeanMetadata)metadata;
099                    // should we skip the bean if its prototype and we are only looking for singletons?
100                    if (!includeNonSingletons) {
101                        String scope = beanMetadata.getScope();
102                        if (BeanMetadata.SCOPE_PROTOTYPE.equals(scope)) {
103                            continue;
104                        }
105                    }
106                    String clazz = beanMetadata.getClassName();
107                    if (clazz != null) {
108                        cl = bundle.loadClass(clazz);
109                    }
110                } else if (metadata instanceof ReferenceMetadata) {
111                    ReferenceMetadata referenceMetadata = (ReferenceMetadata)metadata;
112                    cl = bundle.loadClass(referenceMetadata.getInterface());
113                }
114                if (cl != null && type.isAssignableFrom(cl)) {
115                    Object o = blueprintContainer.getComponentInstance(metadata.getId());
116                    objects.put(metadata.getId(), type.cast(o));
117                }
118            } catch (Throwable t) {
119                // ignore
120            }
121        }
122        return objects;
123    }
124}