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 try { 042 return blueprintContainer.getComponentInstance(name); 043 } catch (NoSuchComponentException e) { 044 return null; 045 } 046 } 047 048 public <T> T lookup(String name, Class<T> type) { 049 Object answer; 050 try { 051 answer = blueprintContainer.getComponentInstance(name); 052 } catch (NoSuchComponentException e) { 053 return null; 054 } 055 056 // just to be safe 057 if (answer == null) { 058 return null; 059 } 060 061 try { 062 return type.cast(answer); 063 } catch (Throwable e) { 064 String msg = "Found bean: " + name + " in BlueprintContainer: " + blueprintContainer 065 + " of type: " + answer.getClass().getName() + " expected type was: " + type; 066 throw new NoSuchBeanException(name, msg, e); 067 } 068 } 069 070 public <T> Map<String, T> lookupByType(Class<T> type) { 071 return lookupByType(blueprintContainer, type); 072 } 073 074 @SuppressWarnings("unchecked") 075 public static <T> Map<String, T> lookupByType(BlueprintContainer blueprintContainer, Class<T> type) { 076 Bundle bundle = (Bundle) blueprintContainer.getComponentInstance("blueprintBundle"); 077 Map<String, T> objects = new LinkedHashMap<String, T>(); 078 Set<String> ids = blueprintContainer.getComponentIds(); 079 for (String id : ids) { 080 try { 081 ComponentMetadata metadata = blueprintContainer.getComponentMetadata(id); 082 Class<?> cl = null; 083 if (metadata instanceof BeanMetadata) { 084 BeanMetadata beanMetadata = (BeanMetadata)metadata; 085 cl = bundle.loadClass(beanMetadata.getClassName()); 086 } else if (metadata instanceof ReferenceMetadata) { 087 ReferenceMetadata referenceMetadata = (ReferenceMetadata)metadata; 088 cl = bundle.loadClass(referenceMetadata.getInterface()); 089 } 090 if (cl != null && type.isAssignableFrom(cl)) { 091 Object o = blueprintContainer.getComponentInstance(metadata.getId()); 092 objects.put(metadata.getId(), type.cast(o)); 093 } 094 } catch (Throwable t) { 095 // ignore 096 } 097 } 098 return objects; 099 } 100 }