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 */ 017package org.apache.camel.impl; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 024 025import org.apache.camel.NoSuchBeanException; 026import org.apache.camel.RuntimeCamelException; 027import org.apache.camel.spi.Registry; 028 029/** 030 * This registry will look up the object with the sequence of the registry list until it finds the Object. 031 */ 032public class CompositeRegistry implements Registry { 033 private final List<Registry> registryList; 034 035 public CompositeRegistry() { 036 registryList = new ArrayList<Registry>(); 037 } 038 039 public CompositeRegistry(List<Registry> registries) { 040 registryList = registries; 041 } 042 043 public void addRegistry(Registry registry) { 044 registryList.add(registry); 045 } 046 047 List<Registry> getRegistryList() { 048 return registryList; 049 } 050 051 public <T> T lookupByNameAndType(String name, Class<T> type) { 052 T answer = null; 053 RuntimeCamelException ex = null; 054 for (Registry registry : registryList) { 055 try { 056 answer = registry.lookupByNameAndType(name, type); 057 } catch (Throwable e) { 058 // do not double wrap the exception 059 if (e instanceof NoSuchBeanException) { 060 ex = (NoSuchBeanException)e; 061 } else { 062 ex = new NoSuchBeanException(name, "Cannot lookup: " + name + " from registry: " + registry 063 + " with expected type: " + type + " due: " + e.getMessage(), e); 064 } 065 } 066 if (answer != null) { 067 return answer; 068 } 069 } 070 if (ex != null) { 071 throw ex; 072 } else { 073 return answer; 074 } 075 } 076 077 public Object lookupByName(String name) { 078 Object answer = null; 079 for (Registry registry : registryList) { 080 answer = registry.lookupByName(name); 081 if (answer != null) { 082 break; 083 } 084 } 085 return answer; 086 } 087 088 public <T> Map<String, T> findByTypeWithName(Class<T> type) { 089 Map<String, T> answer = Collections.emptyMap(); 090 for (Registry registry : registryList) { 091 answer = registry.findByTypeWithName(type); 092 if (!answer.isEmpty()) { 093 break; 094 } 095 } 096 return answer; 097 } 098 099 public <T> Set<T> findByType(Class<T> type) { 100 Set<T> answer = Collections.emptySet(); 101 for (Registry registry : registryList) { 102 answer = registry.findByType(type); 103 if (!answer.isEmpty()) { 104 break; 105 } 106 } 107 return answer; 108 } 109 110 public Object lookup(String name) { 111 return lookupByName(name); 112 } 113 114 public <T> T lookup(String name, Class<T> type) { 115 return lookupByNameAndType(name, type); 116 } 117 118 public <T> Map<String, T> lookupByType(Class<T> type) { 119 return findByTypeWithName(type); 120 } 121}