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.util.ArrayList;
020 import java.util.List;
021 import java.util.Map;
022 import javax.xml.bind.annotation.XmlAccessType;
023 import javax.xml.bind.annotation.XmlAccessorType;
024 import javax.xml.bind.annotation.XmlAttribute;
025 import javax.xml.bind.annotation.XmlElement;
026 import javax.xml.bind.annotation.XmlElements;
027 import javax.xml.bind.annotation.XmlRootElement;
028 import javax.xml.bind.annotation.XmlTransient;
029
030 import org.apache.camel.CamelContext;
031 import org.apache.camel.RoutesBuilder;
032 import org.apache.camel.ShutdownRoute;
033 import org.apache.camel.ShutdownRunningTask;
034 import org.apache.camel.builder.RouteBuilder;
035 import org.apache.camel.component.properties.PropertiesComponent;
036 import org.apache.camel.core.xml.AbstractCamelContextFactoryBean;
037 import org.apache.camel.core.xml.CamelJMXAgentDefinition;
038 import org.apache.camel.core.xml.CamelPropertyPlaceholderDefinition;
039 import org.apache.camel.core.xml.CamelProxyFactoryDefinition;
040 import org.apache.camel.core.xml.CamelServiceExporterDefinition;
041 import org.apache.camel.model.ContextScanDefinition;
042 import org.apache.camel.model.InterceptDefinition;
043 import org.apache.camel.model.InterceptFromDefinition;
044 import org.apache.camel.model.InterceptSendToEndpointDefinition;
045 import org.apache.camel.model.OnCompletionDefinition;
046 import org.apache.camel.model.OnExceptionDefinition;
047 import org.apache.camel.model.PackageScanDefinition;
048 import org.apache.camel.model.RouteBuilderDefinition;
049 import org.apache.camel.model.RouteContextRefDefinition;
050 import org.apache.camel.model.RouteDefinition;
051 import org.apache.camel.model.ThreadPoolProfileDefinition;
052 import org.apache.camel.model.config.PropertiesDefinition;
053 import org.apache.camel.model.dataformat.DataFormatsDefinition;
054 import org.apache.camel.spi.PackageScanFilter;
055 import org.apache.camel.spi.Registry;
056 import org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer;
057 import org.slf4j.Logger;
058 import org.slf4j.LoggerFactory;
059 import org.springframework.beans.factory.DisposableBean;
060 import org.springframework.beans.factory.FactoryBean;
061 import org.springframework.beans.factory.InitializingBean;
062 import org.springframework.beans.factory.config.BeanPostProcessor;
063 import org.springframework.context.ApplicationContext;
064 import org.springframework.context.ApplicationContextAware;
065 import org.springframework.context.ApplicationEvent;
066 import org.springframework.context.ApplicationListener;
067 import org.springframework.context.event.ContextRefreshedEvent;
068
069 import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
070
071 /**
072 * A Spring {@link FactoryBean} to create and initialize a
073 * {@link SpringCamelContext} and install routes either explicitly configured in
074 * Spring XML or found by searching the classpath for Java classes which extend
075 * {@link RouteBuilder} using the nested {@link #setPackages(String[])}.
076 *
077 * @version
078 */
079 @XmlRootElement(name = "camelContext")
080 @XmlAccessorType(XmlAccessType.FIELD)
081 @SuppressWarnings("unused")
082 public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<SpringCamelContext>
083 implements FactoryBean<SpringCamelContext>, InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener<ApplicationEvent> {
084 private static final Logger LOG = LoggerFactory.getLogger(CamelContextFactoryBean.class);
085
086 @XmlAttribute(name = "depends-on", required = false)
087 private String dependsOn;
088 @XmlAttribute(required = false)
089 private String trace;
090 @XmlAttribute(required = false)
091 private String streamCache;
092 @XmlAttribute(required = false)
093 private String delayer;
094 @XmlAttribute(required = false)
095 private String handleFault;
096 @XmlAttribute(required = false)
097 private String errorHandlerRef;
098 @XmlAttribute(required = false)
099 private String autoStartup;
100 @XmlAttribute(required = false)
101 private String useMDCLogging;
102 @XmlAttribute(required = false)
103 private String useBreadcrumb;
104 @XmlAttribute(required = false)
105 private String managementNamePattern;
106 @XmlAttribute(required = false)
107 private ShutdownRoute shutdownRoute;
108 @XmlAttribute(required = false)
109 private ShutdownRunningTask shutdownRunningTask;
110 @XmlAttribute(required = false)
111 @Deprecated
112 private Boolean lazyLoadTypeConverters;
113 @XmlElement(name = "properties", required = false)
114 private PropertiesDefinition properties;
115 @XmlElement(name = "propertyPlaceholder", type = CamelPropertyPlaceholderDefinition.class, required = false)
116 private CamelPropertyPlaceholderDefinition camelPropertyPlaceholder;
117 @XmlElement(name = "package", required = false)
118 private String[] packages = {};
119 @XmlElement(name = "packageScan", type = PackageScanDefinition.class, required = false)
120 private PackageScanDefinition packageScan;
121 @XmlElement(name = "contextScan", type = ContextScanDefinition.class, required = false)
122 private ContextScanDefinition contextScan;
123 @XmlElement(name = "jmxAgent", type = CamelJMXAgentDefinition.class, required = false)
124 private CamelJMXAgentDefinition camelJMXAgent;
125 @XmlElements({
126 @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class, required = false),
127 @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class, required = false),
128 @XmlElement(name = "proxy", type = CamelProxyFactoryDefinition.class, required = false),
129 @XmlElement(name = "export", type = CamelServiceExporterDefinition.class, required = false),
130 @XmlElement(name = "errorHandler", type = ErrorHandlerDefinition.class, required = false)})
131 private List<?> beans;
132 @XmlElement(name = "routeBuilder", required = false)
133 private List<RouteBuilderDefinition> builderRefs = new ArrayList<RouteBuilderDefinition>();
134 @XmlElement(name = "routeContextRef", required = false)
135 private List<RouteContextRefDefinition> routeRefs = new ArrayList<RouteContextRefDefinition>();
136 @XmlElement(name = "threadPoolProfile", required = false)
137 private List<ThreadPoolProfileDefinition> threadPoolProfiles;
138 @XmlElement(name = "threadPool", required = false)
139 private List<CamelThreadPoolFactoryBean> threadPools;
140 @XmlElement(name = "endpoint", required = false)
141 private List<CamelEndpointFactoryBean> endpoints;
142 @XmlElement(name = "dataFormats", required = false)
143 private DataFormatsDefinition dataFormats;
144 @XmlElement(name = "redeliveryPolicyProfile", required = false)
145 private List<CamelRedeliveryPolicyFactoryBean> redeliveryPolicies;
146 @XmlElement(name = "onException", required = false)
147 private List<OnExceptionDefinition> onExceptions = new ArrayList<OnExceptionDefinition>();
148 @XmlElement(name = "onCompletion", required = false)
149 private List<OnCompletionDefinition> onCompletions = new ArrayList<OnCompletionDefinition>();
150 @XmlElement(name = "intercept", required = false)
151 private List<InterceptDefinition> intercepts = new ArrayList<InterceptDefinition>();
152 @XmlElement(name = "interceptFrom", required = false)
153 private List<InterceptFromDefinition> interceptFroms = new ArrayList<InterceptFromDefinition>();
154 @XmlElement(name = "interceptSendToEndpoint", required = false)
155 private List<InterceptSendToEndpointDefinition> interceptSendToEndpoints = new ArrayList<InterceptSendToEndpointDefinition>();
156 @XmlElement(name = "route", required = false)
157 private List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
158 @XmlTransient
159 private SpringCamelContext context;
160 @XmlTransient
161 private ClassLoader contextClassLoaderOnStart;
162 @XmlTransient
163 private ApplicationContext applicationContext;
164 @XmlTransient
165 private BeanPostProcessor beanPostProcessor;
166 @XmlTransient
167 private boolean implicitId;
168
169
170 @Override
171 public Class<SpringCamelContext> getObjectType() {
172 return SpringCamelContext.class;
173 }
174
175 protected <S> S getBeanForType(Class<S> clazz) {
176 S bean = null;
177 String[] names = getApplicationContext().getBeanNamesForType(clazz, true, true);
178 if (names.length == 1) {
179 bean = getApplicationContext().getBean(names[0], clazz);
180 }
181 if (bean == null) {
182 ApplicationContext parentContext = getApplicationContext().getParent();
183 if (parentContext != null) {
184 names = parentContext.getBeanNamesForType(clazz, true, true);
185 if (names.length == 1) {
186 bean = parentContext.getBean(names[0], clazz);
187 }
188 }
189 }
190 return bean;
191 }
192
193 @Override
194 protected void findRouteBuildersByPackageScan(String[] packages, PackageScanFilter filter, List<RoutesBuilder> builders) throws Exception {
195 // add filter to class resolver which then will filter
196 getContext().getPackageScanClassResolver().addFilter(filter);
197
198 PackageScanRouteBuilderFinder finder = new PackageScanRouteBuilderFinder(getContext(), packages, getContextClassLoaderOnStart(),
199 getBeanPostProcessor(), getContext().getPackageScanClassResolver());
200 finder.appendBuilders(builders);
201
202 // and remove the filter
203 getContext().getPackageScanClassResolver().removeFilter(filter);
204 }
205
206 @Override
207 protected void findRouteBuildersByContextScan(PackageScanFilter filter, List<RoutesBuilder> builders) throws Exception {
208 ContextScanRouteBuilderFinder finder = new ContextScanRouteBuilderFinder(getContext(), filter);
209 finder.appendBuilders(builders);
210 }
211
212 protected void initBeanPostProcessor(SpringCamelContext context) {
213 if (beanPostProcessor != null) {
214 if (beanPostProcessor instanceof ApplicationContextAware) {
215 ((ApplicationContextAware) beanPostProcessor).setApplicationContext(applicationContext);
216 }
217 if (beanPostProcessor instanceof CamelBeanPostProcessor) {
218 ((CamelBeanPostProcessor) beanPostProcessor).setCamelContext(getContext());
219 }
220 }
221 }
222
223 protected void postProcessBeforeInit(RouteBuilder builder) {
224 if (beanPostProcessor != null) {
225 // Inject the annotated resource
226 beanPostProcessor.postProcessBeforeInitialization(builder, builder.toString());
227 }
228 }
229
230 protected void initCustomRegistry(SpringCamelContext context) {
231 Registry registry = getBeanForType(Registry.class);
232 if (registry != null) {
233 LOG.info("Using custom Registry: " + registry);
234 context.setRegistry(registry);
235 }
236 }
237
238 @Override
239 protected void initPropertyPlaceholder() throws Exception {
240 super.initPropertyPlaceholder();
241
242 Map<String, BridgePropertyPlaceholderConfigurer> beans = applicationContext.getBeansOfType(BridgePropertyPlaceholderConfigurer.class);
243 if (beans.size() == 1) {
244 // setup properties component that uses this beans
245 BridgePropertyPlaceholderConfigurer configurer = beans.values().iterator().next();
246 String id = beans.keySet().iterator().next();
247 LOG.info("Bridging Camel and Spring property placeholder configurer with id: " + id);
248
249 // get properties component
250 PropertiesComponent pc = getContext().getComponent("properties", PropertiesComponent.class);
251 // replace existing resolver with us
252 configurer.setResolver(pc.getPropertiesResolver());
253 String ref = "ref:" + id;
254 pc.setPropertiesResolver(configurer);
255 // and update locations to have our as ref first
256 String[] locations = pc.getLocations();
257 String[] updatedLocations;
258 if (locations != null && locations.length > 0) {
259 updatedLocations = new String[locations.length + 1];
260 updatedLocations[0] = ref;
261 System.arraycopy(locations, 0, updatedLocations, 1, locations.length);
262 } else {
263 updatedLocations = new String[]{ref};
264 }
265 pc.setLocations(updatedLocations);
266 } else if (beans.size() > 1) {
267 LOG.warn("Cannot bridge Camel and Spring property placeholders, as exact only 1 bean of type BridgePropertyPlaceholderConfigurer"
268 + " must be defined, was {} beans defined.", beans.size());
269 }
270 }
271
272 public void onApplicationEvent(ApplicationEvent event) {
273 // From Spring 3.0.1, The BeanFactory applicationEventListener
274 // and Bean's applicationEventListener will be called,
275 // So we just delegate the onApplicationEvent call here.
276
277 SpringCamelContext context = getContext(false);
278 if (context != null) {
279 // let the spring camel context handle the events
280 context.onApplicationEvent(event);
281 } else {
282 LOG.debug("Publishing spring-event: {}", event);
283
284 if (event instanceof ContextRefreshedEvent) {
285 // now lets start the CamelContext so that all its possible
286 // dependencies are initialized
287 try {
288 LOG.trace("Starting the context now");
289 getContext().start();
290 } catch (Exception e) {
291 throw wrapRuntimeCamelException(e);
292 }
293 }
294 }
295 }
296
297 // Properties
298 // -------------------------------------------------------------------------
299
300 public ApplicationContext getApplicationContext() {
301 if (applicationContext == null) {
302 throw new IllegalArgumentException("No applicationContext has been injected!");
303 }
304 return applicationContext;
305 }
306
307 public void setApplicationContext(ApplicationContext applicationContext) {
308 this.applicationContext = applicationContext;
309 }
310
311 public void setBeanPostProcessor(BeanPostProcessor postProcessor) {
312 this.beanPostProcessor = postProcessor;
313 }
314
315 public BeanPostProcessor getBeanPostProcessor() {
316 return beanPostProcessor;
317 }
318
319 // Implementation methods
320 // -------------------------------------------------------------------------
321
322 /**
323 * Create the context
324 */
325 protected SpringCamelContext createContext() {
326 SpringCamelContext ctx = newCamelContext();
327 ctx.setName(getId());
328 return ctx;
329 }
330
331 protected SpringCamelContext newCamelContext() {
332 return new SpringCamelContext(getApplicationContext());
333 }
334
335 public SpringCamelContext getContext(boolean create) {
336 if (context == null && create) {
337 context = createContext();
338 }
339 return context;
340 }
341
342 public void setContext(SpringCamelContext context) {
343 this.context = context;
344 }
345
346 public List<RouteDefinition> getRoutes() {
347 return routes;
348 }
349
350 public void setRoutes(List<RouteDefinition> routes) {
351 this.routes = routes;
352 }
353
354 public List<CamelEndpointFactoryBean> getEndpoints() {
355 return endpoints;
356 }
357
358 public List<CamelRedeliveryPolicyFactoryBean> getRedeliveryPolicies() {
359 return redeliveryPolicies;
360 }
361
362 public List<InterceptDefinition> getIntercepts() {
363 return intercepts;
364 }
365
366 public void setIntercepts(List<InterceptDefinition> intercepts) {
367 this.intercepts = intercepts;
368 }
369
370 public List<InterceptFromDefinition> getInterceptFroms() {
371 return interceptFroms;
372 }
373
374 public void setInterceptFroms(List<InterceptFromDefinition> interceptFroms) {
375 this.interceptFroms = interceptFroms;
376 }
377
378 public List<InterceptSendToEndpointDefinition> getInterceptSendToEndpoints() {
379 return interceptSendToEndpoints;
380 }
381
382 public void setInterceptSendToEndpoints(List<InterceptSendToEndpointDefinition> interceptSendToEndpoints) {
383 this.interceptSendToEndpoints = interceptSendToEndpoints;
384 }
385
386 public PropertiesDefinition getProperties() {
387 return properties;
388 }
389
390 public void setProperties(PropertiesDefinition properties) {
391 this.properties = properties;
392 }
393
394 public String[] getPackages() {
395 return packages;
396 }
397
398 /**
399 * Sets the package names to be recursively searched for Java classes which
400 * extend {@link org.apache.camel.builder.RouteBuilder} to be auto-wired up to the
401 * {@link CamelContext} as a route. Note that classes are excluded if
402 * they are specifically configured in the spring.xml
403 * <p/>
404 * A more advanced configuration can be done using {@link #setPackageScan(org.apache.camel.model.PackageScanDefinition)}
405 *
406 * @param packages the package names which are recursively searched
407 * @see #setPackageScan(org.apache.camel.model.PackageScanDefinition)
408 */
409 public void setPackages(String[] packages) {
410 this.packages = packages;
411 }
412
413 public PackageScanDefinition getPackageScan() {
414 return packageScan;
415 }
416
417 /**
418 * Sets the package scanning information. Package scanning allows for the
419 * automatic discovery of certain camel classes at runtime for inclusion
420 * e.g. {@link org.apache.camel.builder.RouteBuilder} implementations
421 *
422 * @param packageScan the package scan
423 */
424 public void setPackageScan(PackageScanDefinition packageScan) {
425 this.packageScan = packageScan;
426 }
427
428 public ContextScanDefinition getContextScan() {
429 return contextScan;
430 }
431
432 /**
433 * Sets the context scanning (eg Spring's ApplicationContext) information.
434 * Context scanning allows for the automatic discovery of Camel routes runtime for inclusion
435 * e.g. {@link org.apache.camel.builder.RouteBuilder} implementations
436 *
437 * @param contextScan the context scan
438 */
439 public void setContextScan(ContextScanDefinition contextScan) {
440 this.contextScan = contextScan;
441 }
442
443 public CamelPropertyPlaceholderDefinition getCamelPropertyPlaceholder() {
444 return camelPropertyPlaceholder;
445 }
446
447 public void setCamelPropertyPlaceholder(CamelPropertyPlaceholderDefinition camelPropertyPlaceholder) {
448 this.camelPropertyPlaceholder = camelPropertyPlaceholder;
449 }
450
451 public void setCamelJMXAgent(CamelJMXAgentDefinition agent) {
452 camelJMXAgent = agent;
453 }
454
455 public String getTrace() {
456 return trace;
457 }
458
459 public void setTrace(String trace) {
460 this.trace = trace;
461 }
462
463 public String getStreamCache() {
464 return streamCache;
465 }
466
467 public void setStreamCache(String streamCache) {
468 this.streamCache = streamCache;
469 }
470
471 public String getDelayer() {
472 return delayer;
473 }
474
475 public void setDelayer(String delayer) {
476 this.delayer = delayer;
477 }
478
479 public String getHandleFault() {
480 return handleFault;
481 }
482
483 public void setHandleFault(String handleFault) {
484 this.handleFault = handleFault;
485 }
486
487 public String getAutoStartup() {
488 return autoStartup;
489 }
490
491 public void setAutoStartup(String autoStartup) {
492 this.autoStartup = autoStartup;
493 }
494
495 public String getUseMDCLogging() {
496 return useMDCLogging;
497 }
498
499 public void setUseMDCLogging(String useMDCLogging) {
500 this.useMDCLogging = useMDCLogging;
501 }
502
503 public String getUseBreadcrumb() {
504 return useBreadcrumb;
505 }
506
507 public void setUseBreadcrumb(String useBreadcrumb) {
508 this.useBreadcrumb = useBreadcrumb;
509 }
510
511 public String getManagementNamePattern() {
512 return managementNamePattern;
513 }
514
515 public void setManagementNamePattern(String managementNamePattern) {
516 this.managementNamePattern = managementNamePattern;
517 }
518
519 @Deprecated
520 public Boolean getLazyLoadTypeConverters() {
521 return lazyLoadTypeConverters;
522 }
523
524 @Deprecated
525 public void setLazyLoadTypeConverters(Boolean lazyLoadTypeConverters) {
526 this.lazyLoadTypeConverters = lazyLoadTypeConverters;
527 }
528
529 public CamelJMXAgentDefinition getCamelJMXAgent() {
530 return camelJMXAgent;
531 }
532
533 public List<RouteBuilderDefinition> getBuilderRefs() {
534 return builderRefs;
535 }
536
537 public void setBuilderRefs(List<RouteBuilderDefinition> builderRefs) {
538 this.builderRefs = builderRefs;
539 }
540
541 public List<RouteContextRefDefinition> getRouteRefs() {
542 return routeRefs;
543 }
544
545 public void setRouteRefs(List<RouteContextRefDefinition> routeRefs) {
546 this.routeRefs = routeRefs;
547 }
548
549 public String getErrorHandlerRef() {
550 return errorHandlerRef;
551 }
552
553 /**
554 * Sets the name of the error handler object used to default the error handling strategy
555 *
556 * @param errorHandlerRef the Spring bean ref of the error handler
557 */
558 public void setErrorHandlerRef(String errorHandlerRef) {
559 this.errorHandlerRef = errorHandlerRef;
560 }
561
562 public void setDataFormats(DataFormatsDefinition dataFormats) {
563 this.dataFormats = dataFormats;
564 }
565
566 public DataFormatsDefinition getDataFormats() {
567 return dataFormats;
568 }
569
570 public void setOnExceptions(List<OnExceptionDefinition> onExceptions) {
571 this.onExceptions = onExceptions;
572 }
573
574 public List<OnExceptionDefinition> getOnExceptions() {
575 return onExceptions;
576 }
577
578 public List<OnCompletionDefinition> getOnCompletions() {
579 return onCompletions;
580 }
581
582 public void setOnCompletions(List<OnCompletionDefinition> onCompletions) {
583 this.onCompletions = onCompletions;
584 }
585
586 public ShutdownRoute getShutdownRoute() {
587 return shutdownRoute;
588 }
589
590 public void setShutdownRoute(ShutdownRoute shutdownRoute) {
591 this.shutdownRoute = shutdownRoute;
592 }
593
594 public ShutdownRunningTask getShutdownRunningTask() {
595 return shutdownRunningTask;
596 }
597
598 public void setShutdownRunningTask(ShutdownRunningTask shutdownRunningTask) {
599 this.shutdownRunningTask = shutdownRunningTask;
600 }
601
602 public List<ThreadPoolProfileDefinition> getThreadPoolProfiles() {
603 return threadPoolProfiles;
604 }
605
606 public void setThreadPoolProfiles(List<ThreadPoolProfileDefinition> threadPoolProfiles) {
607 this.threadPoolProfiles = threadPoolProfiles;
608 }
609
610 public String getDependsOn() {
611 return dependsOn;
612 }
613
614 public void setDependsOn(String dependsOn) {
615 this.dependsOn = dependsOn;
616 }
617
618 public boolean isImplicitId() {
619 return implicitId;
620 }
621
622 public void setImplicitId(boolean flag) {
623 implicitId = flag;
624 }
625
626 }