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