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        @SuppressWarnings("unchecked")
071        public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer, Class<T> type) {
072            Bundle bundle = (Bundle) blueprintContainer.getComponentInstance("blueprintBundle");
073            Map<String, T> objects = new LinkedHashMap<String, T>();
074            Set<String> ids = blueprintContainer.getComponentIds();
075            for (String id : ids) {
076                try {
077                    ComponentMetadata metadata = blueprintContainer.getComponentMetadata(id);
078                    Class<?> cl = null;
079                    if (metadata instanceof BeanMetadata) {
080                        BeanMetadata beanMetadata = (BeanMetadata)metadata;
081                        cl = bundle.loadClass(beanMetadata.getClassName());
082                    } else if (metadata instanceof ReferenceMetadata) {
083                        ReferenceMetadata referenceMetadata = (ReferenceMetadata)metadata;
084                        cl = bundle.loadClass(referenceMetadata.getInterface());
085                    }
086                    if (cl != null && type.isAssignableFrom(cl)) {
087                        Object o = blueprintContainer.getComponentInstance(metadata.getId());
088                        objects.put(metadata.getId(), type.cast(o));
089                    }
090                } catch (Throwable t) {
091                    // ignore
092                }
093            }
094            return objects;
095        }
096    }