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