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;
018    
019    import java.lang.reflect.Field;
020    import java.lang.reflect.Method;
021    
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlRootElement;
025    import javax.xml.bind.annotation.XmlTransient;
026    
027    import org.apache.camel.CamelContextAware;
028    import org.apache.camel.Endpoint;
029    import org.apache.camel.EndpointInject;
030    import org.apache.camel.Produce;
031    import org.apache.camel.impl.CamelPostProcessorHelper;
032    import org.apache.camel.spring.util.ReflectionUtils;
033    import org.apache.camel.util.ObjectHelper;
034    import org.apache.commons.logging.Log;
035    import org.apache.commons.logging.LogFactory;
036    import org.springframework.beans.BeanInstantiationException;
037    import org.springframework.beans.BeansException;
038    import org.springframework.beans.factory.config.BeanPostProcessor;
039    import org.springframework.context.ApplicationContext;
040    import org.springframework.context.ApplicationContextAware;
041    
042    /**
043     * A bean post processor which implements the <a href="http://activemq.apache.org/camel/bean-integration.html">Bean Integration</a>
044     * features in Camel such as the <a href="http://activemq.apache.org/camel/bean-injection.html">Bean Injection</a> of objects like
045     * {@link Endpoint} and
046     * {@link org.apache.camel.ProducerTemplate} together with support for
047     * <a href="http://activemq.apache.org/camel/pojo-consuming.html">POJO Consuming</a> via the 
048     * {@link org.apache.camel.Consume} and {@link org.apache.camel.MessageDriven} annotations along with
049     * <a href="http://activemq.apache.org/camel/pojo-producing.html">POJO Producing</a> via the
050     * {@link org.apache.camel.Produce} annotation along with other annotations such as
051     * {@link org.apache.camel.RecipientList} for creating <a href="http://activemq.apache.org/camel/recipientlist-annotation.html">a Recipient List router via annotations</a>.
052     * <p>
053     * If you use the &lt;camelContext&gt; element in your <a href="http://activemq.apache.org/camel/spring.html">Spring XML</a> 
054     * then one of these bean post processors is implicity installed and configured for you. So you should never have to
055     * explicitly create or configure one of these instances.
056     *
057     * @version $Revision: 707305 $
058     */
059    @XmlRootElement(name = "beanPostProcessor")
060    @XmlAccessorType(XmlAccessType.FIELD)
061    public class CamelBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
062        private static final transient Log LOG = LogFactory.getLog(CamelBeanPostProcessor.class);
063        @XmlTransient
064        private SpringCamelContext camelContext;
065        @XmlTransient
066        private ApplicationContext applicationContext;
067        @XmlTransient
068        private CamelPostProcessorHelper postProcessor;
069    
070        public CamelBeanPostProcessor() {
071        }
072    
073        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
074            injectFields(bean);
075            injectMethods(bean);
076            if (bean instanceof CamelContextAware) {
077                CamelContextAware contextAware = (CamelContextAware)bean;
078                if (camelContext == null) {
079                    LOG.warn("No CamelContext defined yet so cannot inject into: " + bean);
080                } else {
081                    contextAware.setCamelContext(camelContext);
082                }
083            }
084            return bean;
085        }
086    
087        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
088            return bean;
089        }
090    
091        // Properties
092        // -------------------------------------------------------------------------
093    
094        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
095            this.applicationContext = applicationContext;
096        }
097    
098        public SpringCamelContext getCamelContext() {
099            return camelContext;
100        }
101    
102        public void setCamelContext(SpringCamelContext camelContext) {
103            this.camelContext = camelContext;
104            postProcessor = new CamelPostProcessorHelper(camelContext) {
105                @Override
106                protected RuntimeException createProxyInstantiationRuntimeException(Class<?> type, Endpoint endpoint, Exception e) {
107                    return new BeanInstantiationException(type, "Could not instantiate proxy of type " + type.getName() + " on endpoint " + endpoint, e);
108                }
109            };
110        }
111    
112        // Implementation methods
113        // -------------------------------------------------------------------------
114    
115        /**
116         * A strategy method to allow implementations to perform some custom JBI
117         * based injection of the POJO
118         *
119         * @param bean the bean to be injected
120         */
121        protected void injectFields(final Object bean) {
122            ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
123                public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
124                    EndpointInject annotation = field.getAnnotation(EndpointInject.class);
125                    if (annotation != null) {
126                        injectField(field, annotation.uri(), annotation.name(), bean);
127                    }
128                    Produce produce = field.getAnnotation(Produce.class);
129                    if (produce != null) {
130                        injectField(field, produce.uri(), produce.ref(), bean);
131                    }
132                }
133            });
134        }
135    
136        protected void injectField(Field field, String endpointUri, String endpointRef, Object bean) {
137            ReflectionUtils.setField(field, bean, getPostProcessor().getInjectionValue(field.getType(), endpointUri, endpointRef, field.getName()));
138        }
139    
140        protected void injectMethods(final Object bean) {
141            ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
142                @SuppressWarnings("unchecked")
143                public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
144                    setterInjection(method, bean);
145                    getPostProcessor().consumerInjection(method, bean);
146                }
147            });
148        }
149    
150        protected void setterInjection(Method method, Object bean) {
151            EndpointInject annoation = method.getAnnotation(EndpointInject.class);
152            if (annoation != null) {
153                setterInjection(method, bean, annoation.uri(), annoation.name());
154            }
155            Produce produce = method.getAnnotation(Produce.class);
156            if (produce != null) {
157                setterInjection(method, bean, produce.uri(), produce.ref());
158            }
159        }
160    
161        protected void setterInjection(Method method, Object bean, String endpointUri, String endpointRef) {
162            Class<?>[] parameterTypes = method.getParameterTypes();
163            if (parameterTypes != null) {
164                if (parameterTypes.length != 1) {
165                    LOG.warn("Ignoring badly annotated method for injection due to incorrect number of parameters: " + method);
166                } else {
167                    String propertyName = ObjectHelper.getPropertyName(method);
168                    Object value = getPostProcessor().getInjectionValue(parameterTypes[0], endpointUri, endpointRef, propertyName);
169                    ObjectHelper.invokeMethod(method, bean, value);
170                }
171            }
172        }
173    
174    
175        protected void consumerInjection(final Object bean) {
176            org.springframework.util.ReflectionUtils.doWithMethods(bean.getClass(), new org.springframework.util.ReflectionUtils.MethodCallback() {
177                @SuppressWarnings("unchecked")
178                public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
179                    /*
180                     * TODO support callbacks? if
181                     * (method.getAnnotation(Callback.class) != null) { try {
182                     * Expression e = ExpressionFactory.createExpression(
183                     * method.getAnnotation(Callback.class).condition());
184                     * JexlContext jc = JexlHelper.createContext();
185                     * jc.getVars().put("this", obj); Object r = e.evaluate(jc); if
186                     * (!(r instanceof Boolean)) { throw new
187                     * RuntimeException("Expression did not returned a boolean value
188                     * but: " + r); } Boolean oldVal =
189                     * req.getCallbacks().get(method); Boolean newVal = (Boolean) r;
190                     * if ((oldVal == null || !oldVal) && newVal) {
191                     * req.getCallbacks().put(method, newVal); method.invoke(obj,
192                     * new Object[0]); // TODO: handle return value and sent it as
193                     * the answer } } catch (Exception e) { throw new
194                     * RuntimeException("Unable to invoke callback", e); } }
195                     */
196                }
197            });
198        }
199    
200        public CamelPostProcessorHelper getPostProcessor() {
201            ObjectHelper.notNull(postProcessor, "postProcessor");
202            return postProcessor;
203        }
204    }