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.lang.reflect.Method;
020
021 import javax.xml.bind.annotation.XmlTransient;
022
023 import org.apache.camel.CamelContext;
024 import org.apache.camel.CamelContextAware;
025 import org.apache.camel.Consume;
026 import org.apache.camel.Consumer;
027 import org.apache.camel.Endpoint;
028 import org.apache.camel.PollingConsumer;
029 import org.apache.camel.Processor;
030 import org.apache.camel.Producer;
031 import org.apache.camel.ProducerTemplate;
032 import org.apache.camel.Service;
033 import org.apache.camel.component.bean.BeanProcessor;
034 import org.apache.camel.component.bean.ProxyHelper;
035 import org.apache.camel.util.CamelContextHelper;
036 import org.apache.camel.util.ObjectHelper;
037 import org.apache.commons.logging.Log;
038 import org.apache.commons.logging.LogFactory;
039
040 /**
041 * A helper class for Camel based injector or post processing hooks which can be reused by
042 * both the <a href="http://camel.apache.org/spring.html">Spring</a>
043 * and <a href="http://camel.apache.org/guice.html">Guice</a> support.
044 *
045 * @version $Revision: 749562 $
046 */
047 public class CamelPostProcessorHelper implements CamelContextAware {
048 private static final transient Log LOG = LogFactory.getLog(CamelPostProcessorHelper.class);
049
050 @XmlTransient
051 private CamelContext camelContext;
052
053 public CamelPostProcessorHelper() {
054 }
055
056 public CamelPostProcessorHelper(CamelContext camelContext) {
057 this.setCamelContext(camelContext);
058 }
059
060 public CamelContext getCamelContext() {
061 return camelContext;
062 }
063
064 public void setCamelContext(CamelContext camelContext) {
065 this.camelContext = camelContext;
066 }
067
068 public void consumerInjection(Method method, Object bean) {
069 Consume consume = method.getAnnotation(Consume.class);
070 if (consume != null) {
071 LOG.info("Creating a consumer for: " + consume);
072 subscribeMethod(method, bean, consume.uri(), consume.ref());
073 }
074 }
075
076 public void subscribeMethod(Method method, Object bean, String endpointUri, String endpointName) {
077 // lets bind this method to a listener
078 String injectionPointName = method.getName();
079 Endpoint endpoint = getEndpointInjection(endpointUri, endpointName, injectionPointName, true);
080 if (endpoint != null) {
081 try {
082 Processor processor = createConsumerProcessor(bean, method, endpoint);
083 Consumer consumer = endpoint.createConsumer(processor);
084 if (LOG.isDebugEnabled()) {
085 LOG.debug("Created processor: " + processor + " for consumer: " + consumer);
086 }
087 startService(consumer);
088 } catch (Exception e) {
089 throw ObjectHelper.wrapRuntimeCamelException(e);
090 }
091 }
092 }
093
094 public void startService(Service service) throws Exception {
095 CamelContext camelContext = getCamelContext();
096 if (camelContext instanceof DefaultCamelContext) {
097 DefaultCamelContext defaultCamelContext = (DefaultCamelContext) camelContext;
098 defaultCamelContext.addService(service);
099 } else {
100 service.start();
101 }
102 }
103
104 /**
105 * Create a processor which invokes the given method when an incoming
106 * message exchange is received
107 */
108 protected Processor createConsumerProcessor(final Object pojo, final Method method, final Endpoint endpoint) {
109 BeanProcessor answer = new BeanProcessor(pojo, getCamelContext());
110 answer.setMethodObject(method);
111 return answer;
112 }
113
114 protected Endpoint getEndpointInjection(String uri, String name, String injectionPointName, boolean mandatory) {
115 return CamelContextHelper.getEndpointInjection(getCamelContext(), uri, name, injectionPointName, mandatory);
116 }
117
118 /**
119 * Creates the object to be injected for an {@link org.apache.camel.EndpointInject} or {@link org.apache.camel.Produce} injection point
120 */
121 public Object getInjectionValue(Class<?> type, String endpointUri, String endpointRef, String injectionPointName) {
122 if (type.isAssignableFrom(ProducerTemplate.class)) {
123 // endpoint is optional for this injection point
124 Endpoint endpoint = getEndpointInjection(endpointUri, endpointRef, injectionPointName, false);
125 return new DefaultProducerTemplate(getCamelContext(), endpoint);
126 } else {
127 Endpoint endpoint = getEndpointInjection(endpointUri, endpointRef, injectionPointName, true);
128 if (endpoint != null) {
129 if (type.isInstance(endpoint)) {
130 return endpoint;
131 } else if (type.isAssignableFrom(Producer.class)) {
132 return createInjectionProducer(endpoint);
133 } else if (type.isAssignableFrom(PollingConsumer.class)) {
134 return createInjectionPollingConsumer(endpoint);
135 } else if (type.isInterface()) {
136 // lets create a proxy
137 try {
138 return ProxyHelper.createProxy(endpoint, type);
139 } catch (Exception e) {
140 throw createProxyInstantiationRuntimeException(type, endpoint, e);
141 }
142 } else {
143 throw new IllegalArgumentException("Invalid type: " + type.getName()
144 + " which cannot be injected via @EndpointInject/@Produce for: " + endpoint);
145 }
146 }
147 return null;
148 }
149 }
150
151 protected RuntimeException createProxyInstantiationRuntimeException(Class<?> type, Endpoint endpoint, Exception e) {
152 return new ProxyInstantiationException(type, endpoint, e);
153 }
154
155 /**
156 * Factory method to create a started {@link org.apache.camel.PollingConsumer} to be injected into a POJO
157 */
158 protected PollingConsumer createInjectionPollingConsumer(Endpoint endpoint) {
159 try {
160 PollingConsumer pollingConsumer = endpoint.createPollingConsumer();
161 startService(pollingConsumer);
162 return pollingConsumer;
163 } catch (Exception e) {
164 throw ObjectHelper.wrapRuntimeCamelException(e);
165 }
166 }
167
168 /**
169 * A Factory method to create a started {@link org.apache.camel.Producer} to be injected into a POJO
170 */
171 protected Producer createInjectionProducer(Endpoint endpoint) {
172 try {
173 Producer producer = endpoint.createProducer();
174 startService(producer);
175 return producer;
176 } catch (Exception e) {
177 throw ObjectHelper.wrapRuntimeCamelException(e);
178 }
179 }
180 }