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 org.apache.camel.CamelContext; 020import org.apache.camel.RuntimeCamelException; 021import org.apache.camel.spi.Injector; 022import org.apache.camel.util.ReflectionInjector; 023 024/** 025 * A default implementation of {@link Injector} which just uses reflection to 026 * instantiate new objects using their zero argument constructor, 027 * and then performing bean post processing using {@link DefaultCamelBeanPostProcessor}. 028 * <p/> 029 * For more complex implementations try the Spring or Guice implementations. 030 * 031 * @see ReflectionInjector 032 */ 033public class DefaultInjector implements Injector { 034 035 // use the reflection injector 036 private final Injector delegate = new ReflectionInjector(); 037 private final DefaultCamelBeanPostProcessor postProcessor; 038 039 public DefaultInjector(CamelContext context) { 040 postProcessor = new DefaultCamelBeanPostProcessor(context); 041 } 042 043 @Override 044 public <T> T newInstance(Class<T> type) { 045 T answer = delegate.newInstance(type); 046 if (answer != null) { 047 try { 048 postProcessor.postProcessBeforeInitialization(answer, answer.getClass().getName()); 049 postProcessor.postProcessAfterInitialization(answer, answer.getClass().getName()); 050 } catch (Exception e) { 051 throw new RuntimeCamelException("Error during post processing of bean " + answer, e); 052 } 053 } 054 return answer; 055 } 056 057 @Override 058 public <T> T newInstance(Class<T> type, Object instance) { 059 T answer = delegate.newInstance(type, instance); 060 if (answer != null) { 061 try { 062 postProcessor.postProcessBeforeInitialization(answer, answer.getClass().getName()); 063 postProcessor.postProcessAfterInitialization(answer, answer.getClass().getName()); 064 } catch (Exception e) { 065 throw new RuntimeCamelException("Error during post processing of bean " + answer, e); 066 } 067 } 068 return answer; 069 } 070 071 @Override 072 public boolean supportsAutoWiring() { 073 return delegate.supportsAutoWiring(); 074 } 075}