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.spring.spi;
018
019 import java.util.Map;
020
021 import org.apache.camel.spi.Registry;
022 import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
023 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
024 import org.springframework.context.ApplicationContext;
025
026 /**
027 * A {@link Registry} implementation which looks up the objects in the Spring
028 * {@link ApplicationContext}
029 *
030 * @version $Revision: 763484 $
031 */
032 public class ApplicationContextRegistry implements Registry {
033 private ApplicationContext applicationContext;
034
035 public ApplicationContextRegistry(ApplicationContext applicationContext) {
036 this.applicationContext = applicationContext;
037 }
038
039 public <T> T lookup(String name, Class<T> type) {
040 try {
041 Object value = applicationContext.getBean(name, type);
042 return type.cast(value);
043 } catch (NoSuchBeanDefinitionException e) {
044 return null;
045 } catch (BeanNotOfRequiredTypeException e) {
046 return null;
047 }
048 }
049
050 public Object lookup(String name) {
051 try {
052 return applicationContext.getBean(name);
053 } catch (NoSuchBeanDefinitionException e) {
054 return null;
055 }
056 }
057
058 @SuppressWarnings("unchecked")
059 public <T> Map<String, T> lookupByType(Class<T> type) {
060 return applicationContext.getBeansOfType(type);
061 }
062 }