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     */
017    package org.apache.camel.blueprint;
018    
019    import java.util.LinkedHashMap;
020    import java.util.Map;
021    import java.util.Set;
022    
023    import org.apache.camel.NoSuchBeanException;
024    import org.apache.camel.spi.Registry;
025    import org.osgi.framework.Bundle;
026    import org.osgi.service.blueprint.container.BlueprintContainer;
027    import org.osgi.service.blueprint.container.NoSuchComponentException;
028    import org.osgi.service.blueprint.reflect.BeanMetadata;
029    import org.osgi.service.blueprint.reflect.ComponentMetadata;
030    import org.osgi.service.blueprint.reflect.ReferenceMetadata;
031    
032    public class BlueprintContainerRegistry implements Registry {
033    
034        private final BlueprintContainer blueprintContainer;
035    
036        public BlueprintContainerRegistry(BlueprintContainer blueprintContainer) {
037            this.blueprintContainer = blueprintContainer;
038        }
039    
040        public Object lookup(String name) {
041            return blueprintContainer.getComponentInstance(name);
042        }
043    
044        public <T> T lookup(String name, Class<T> type) {
045            Object answer;
046            try {
047                answer = blueprintContainer.getComponentInstance(name);
048            } catch (NoSuchComponentException e) {
049                return null;
050            }
051    
052            // just to be safe
053            if (answer == null) {
054                return null;
055            }
056    
057            try {
058                return type.cast(answer);
059            } catch (Throwable e) {
060                String msg = "Found bean: " + name + " in BlueprintContainer: " + blueprintContainer
061                        + " of type: " + answer.getClass().getName() + " expected type was: " + type;
062                throw new NoSuchBeanException(name, msg, e);
063            }
064        }
065    
066        public <T> Map<String, T> lookupByType(Class<T> type) {
067            return lookupByType(blueprintContainer, type);
068        }
069    
070        public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer, Class<T> type) {
071            Bundle bundle = (Bundle) blueprintContainer.getComponentInstance("blueprintBundle");
072            Map<String, T> objects = new LinkedHashMap<String, T>();
073            Set<String> ids = blueprintContainer.getComponentIds();
074            for (String id : ids) {
075                try {
076                    ComponentMetadata metadata = blueprintContainer.getComponentMetadata(id);
077                    Class cl = null;
078                    if (metadata instanceof BeanMetadata) {
079                        BeanMetadata beanMetadata = (BeanMetadata)metadata;
080                        cl = bundle.loadClass(beanMetadata.getClassName());
081                    } else if (metadata instanceof ReferenceMetadata) {
082                        ReferenceMetadata referenceMetadata = (ReferenceMetadata)metadata;
083                        cl = bundle.loadClass(referenceMetadata.getInterface());
084                    }
085                    if (cl != null && type.isAssignableFrom(cl)) {
086                        Object o = blueprintContainer.getComponentInstance(metadata.getId());
087                        objects.put(metadata.getId(), type.cast(o));
088                    }
089                } catch (Throwable t) {
090                    // ignore
091                }
092            }
093            return objects;
094        }
095    }