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.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
029 public class BlueprintContainerRegistry implements Registry {
030
031 private final BlueprintContainer blueprintContainer;
032
033 public BlueprintContainerRegistry(BlueprintContainer blueprintContainer) {
034 this.blueprintContainer = blueprintContainer;
035 }
036
037 public Object lookup(String name) {
038 return blueprintContainer.getComponentInstance(name);
039 }
040
041 public <T> T lookup(String name, Class<T> type) {
042 try {
043 return type.cast(blueprintContainer.getComponentInstance(name));
044 } catch (NoSuchComponentException e) {
045 return null;
046 }
047 }
048
049 public <T> Map<String, T> lookupByType(Class<T> type) {
050 return lookupByType(blueprintContainer, type);
051 }
052
053 public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer, Class<T> type) {
054 Map<String, T> objects = new LinkedHashMap<String, T>();
055 for (ExtendedBeanMetadata metadata : blueprintContainer.getMetadata(ExtendedBeanMetadata.class)) {
056 try {
057 Class cl = metadata.getRuntimeClass();
058 if (cl == null && metadata.getClassName() != null) {
059 Bundle bundle = (Bundle) blueprintContainer.getComponentInstance("blueprintBundle");
060 cl = bundle.loadClass(metadata.getClassName());
061 }
062 if (cl == null || type.isAssignableFrom(cl)) {
063 Object o = blueprintContainer.getComponentInstance(metadata.getId());
064 objects.put(metadata.getId(), type.cast(o));
065 }
066 } catch (Throwable t) {
067 // ignore
068 }
069 }
070 for (MutableReferenceMetadata metadata : blueprintContainer.getMetadata(MutableReferenceMetadata.class)) {
071 try {
072 Class cl = metadata.getRuntimeInterface();
073 if (cl == null && metadata.getInterface() != null) {
074 Bundle bundle = (Bundle) blueprintContainer.getComponentInstance("blueprintBundle");
075 cl = bundle.loadClass(metadata.getInterface());
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 return objects;
086 }
087
088 }