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.HashSet;
020 import java.util.LinkedHashMap;
021 import java.util.Map;
022 import java.util.Set;
023
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 import org.osgi.service.blueprint.reflect.BeanMetadata;
030 import org.osgi.service.blueprint.reflect.ComponentMetadata;
031 import org.osgi.service.blueprint.reflect.ReferenceMetadata;
032
033 public class BlueprintContainerRegistry implements Registry {
034
035 private final BlueprintContainer blueprintContainer;
036
037 public BlueprintContainerRegistry(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<T>(map.values());
082 }
083
084 @Override
085 public Object lookup(String name) {
086 return lookupByName(name);
087 }
088
089 @Override
090 public <T> T lookup(String name, Class<T> type) {
091 return lookupByNameAndType(name, type);
092 }
093
094 @Override
095 public <T> Map<String, T> lookupByType(Class<T> type) {
096 return findByTypeWithName(type);
097 }
098
099 public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer, Class<T> type) {
100 Bundle bundle = (Bundle) blueprintContainer.getComponentInstance("blueprintBundle");
101 Map<String, T> objects = new LinkedHashMap<String, T>();
102 Set<String> ids = blueprintContainer.getComponentIds();
103 for (String id : ids) {
104 try {
105 ComponentMetadata metadata = blueprintContainer.getComponentMetadata(id);
106 Class<?> cl = null;
107 if (metadata instanceof BeanMetadata) {
108 BeanMetadata beanMetadata = (BeanMetadata)metadata;
109 cl = bundle.loadClass(beanMetadata.getClassName());
110 } else if (metadata instanceof ReferenceMetadata) {
111 ReferenceMetadata referenceMetadata = (ReferenceMetadata)metadata;
112 cl = bundle.loadClass(referenceMetadata.getInterface());
113 }
114 if (cl != null && type.isAssignableFrom(cl)) {
115 Object o = blueprintContainer.getComponentInstance(metadata.getId());
116 objects.put(metadata.getId(), type.cast(o));
117 }
118 } catch (Throwable t) {
119 // ignore
120 }
121 }
122 return objects;
123 }
124 }