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.impl;
018    
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.camel.NoSuchBeanException;
025    import org.apache.camel.spi.Registry;
026    
027    /**
028     * This registry will look up the object with the sequence of the registry list until it finds the Object.
029     */
030    public class CompositeRegistry implements Registry {
031        private List<Registry> registryList;
032        
033        public CompositeRegistry() {
034            registryList = new ArrayList<Registry>();
035        }
036        
037        public CompositeRegistry(List<Registry> registries) {
038            registryList = registries;
039        }
040        
041        public void addRegistry(Registry registry) {
042            registryList.add(registry);
043        }
044    
045        public <T> T lookup(String name, Class<T> type) {
046            T answer = null;
047            for (Registry registry : registryList) {
048                try {
049                    answer = registry.lookup(name, type);
050                    if (answer != null) {
051                        break;
052                    }
053                } catch (Throwable e) {
054                    // do not double wrap the exception
055                    if (e instanceof NoSuchBeanException) {
056                        throw (NoSuchBeanException) e;
057                    }
058                    throw new NoSuchBeanException(name, "Cannot lookup: " + name + " from registry: " + registry
059                            + " with expected type: " + type + " due: " + e.getMessage(), e);
060                }
061            }
062            return answer;
063        }
064    
065        public Object lookup(String name) {
066            Object answer = null;
067            for (Registry registry : registryList) {
068                answer = registry.lookup(name);
069                if (answer != null) {
070                    break;
071                }
072            }
073            return answer;
074        }
075    
076        public <T> Map<String, T> lookupByType(Class<T> type) {
077            Map<String, T> answer = Collections.<String, T>emptyMap();
078            for (Registry registry : registryList) {
079                answer = registry.lookupByType(type);
080                if (!answer.isEmpty()) {
081                    break;
082                }
083            }
084            return answer;
085        }
086    
087    }