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