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.spring.spi;
018
019import java.lang.reflect.Method;
020import java.lang.reflect.Modifier;
021
022import org.apache.camel.RuntimeCamelException;
023import org.apache.camel.spi.Injector;
024import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
025import org.springframework.context.ConfigurableApplicationContext;
026
027/**
028 * A Spring implementation of {@link Injector} allowing Spring to be used to dependency inject newly created POJOs
029 */
030public class SpringInjector implements Injector {
031    private final ConfigurableApplicationContext applicationContext;
032    private int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
033    private boolean dependencyCheck;
034
035    public SpringInjector(ConfigurableApplicationContext applicationContext) {
036        this.applicationContext = applicationContext;
037    }
038
039    @Override
040    public <T> T newInstance(Class<T> type) {
041        return newInstance(type, true);
042    }
043
044    @Override
045    public <T> T newInstance(Class<T> type, String factoryMethod) {
046        T answer = null;
047        try {
048            // lookup factory method
049            Method fm = type.getMethod(factoryMethod);
050            if (Modifier.isStatic(fm.getModifiers()) && Modifier.isPublic(fm.getModifiers()) && fm.getReturnType() == type) {
051                Object obj = fm.invoke(null);
052                answer = type.cast(obj);
053            }
054        } catch (Exception e) {
055            throw new RuntimeCamelException("Error invoking factory method: " + factoryMethod + " on class: " + type, e);
056        }
057        return answer;
058    }
059
060    @Override
061    public <T> T newInstance(Class<T> type, boolean postProcessBean) {
062        Object value;
063        if (postProcessBean) {
064            value = applicationContext.getBeanFactory().createBean(type, autowireMode, dependencyCheck);
065        } else {
066            value = applicationContext.getBeanFactory().createBean(type, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
067        }
068        return type.cast(value);
069    }
070
071    @Override
072    public boolean supportsAutoWiring() {
073        return true;
074    }
075
076    public int getAutowireMode() {
077        return autowireMode;
078    }
079
080    public void setAutowireMode(int autowireMode) {
081        this.autowireMode = autowireMode;
082    }
083
084    public boolean isDependencyCheck() {
085        return dependencyCheck;
086    }
087
088    public void setDependencyCheck(boolean dependencyCheck) {
089        this.dependencyCheck = dependencyCheck;
090    }
091
092    public ConfigurableApplicationContext getApplicationContext() {
093        return applicationContext;
094    }
095}