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.blueprint;
018
019 import java.util.ArrayList;
020 import java.util.Collection;
021 import java.util.List;
022 import java.util.Properties;
023 import javax.xml.bind.annotation.XmlAccessType;
024 import javax.xml.bind.annotation.XmlAccessorType;
025 import javax.xml.bind.annotation.XmlAttribute;
026 import javax.xml.bind.annotation.XmlElement;
027 import javax.xml.bind.annotation.XmlElements;
028 import javax.xml.bind.annotation.XmlRootElement;
029 import javax.xml.bind.annotation.XmlTransient;
030
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.osgi.OsgiCamelContextPublisher;
037 import org.apache.camel.core.osgi.OsgiEventAdminNotifier;
038 import org.apache.camel.core.osgi.utils.BundleDelegatingClassLoader;
039 import org.apache.camel.core.xml.AbstractCamelContextFactoryBean;
040 import org.apache.camel.core.xml.CamelJMXAgentDefinition;
041 import org.apache.camel.core.xml.CamelPropertyPlaceholderDefinition;
042 import org.apache.camel.core.xml.CamelServiceExporterDefinition;
043 import org.apache.camel.core.xml.CamelStreamCachingStrategyDefinition;
044 import org.apache.camel.model.ContextScanDefinition;
045 import org.apache.camel.model.InterceptDefinition;
046 import org.apache.camel.model.InterceptFromDefinition;
047 import org.apache.camel.model.InterceptSendToEndpointDefinition;
048 import org.apache.camel.model.OnCompletionDefinition;
049 import org.apache.camel.model.OnExceptionDefinition;
050 import org.apache.camel.model.PackageScanDefinition;
051 import org.apache.camel.model.RouteBuilderDefinition;
052 import org.apache.camel.model.RouteContextRefDefinition;
053 import org.apache.camel.model.RouteDefinition;
054 import org.apache.camel.model.ThreadPoolProfileDefinition;
055 import org.apache.camel.model.config.PropertiesDefinition;
056 import org.apache.camel.model.dataformat.DataFormatsDefinition;
057 import org.apache.camel.spi.PackageScanFilter;
058 import org.apache.camel.spi.Registry;
059 import org.osgi.framework.BundleContext;
060 import org.osgi.framework.ServiceReference;
061 import org.osgi.service.blueprint.container.BlueprintContainer;
062 import org.slf4j.Logger;
063 import org.slf4j.LoggerFactory;
064
065 /**
066 * A bean to create and initialize a {@link BlueprintCamelContext}
067 * and install routes either explicitly configured in
068 * Blueprint XML or found by searching the classpath for Java classes which extend
069 * {@link RouteBuilder} using the nested {@link #setPackages(String[])}.
070 *
071 * @version
072 */
073 @XmlRootElement(name = "camelContext")
074 @XmlAccessorType(XmlAccessType.FIELD)
075 public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<BlueprintCamelContext> {
076 private static final Logger LOG = LoggerFactory.getLogger(CamelContextFactoryBean.class);
077
078 @XmlAttribute(name = "depends-on", required = false)
079 private String dependsOn;
080 @XmlAttribute(required = false)
081 private String trace;
082 @XmlAttribute(required = false)
083 private String messageHistory;
084 @XmlAttribute(required = false)
085 private String streamCache = "false";
086 @XmlAttribute(required = false)
087 private String delayer;
088 @XmlAttribute(required = false)
089 private String handleFault;
090 @XmlAttribute(required = false)
091 private String errorHandlerRef;
092 @XmlAttribute(required = false)
093 private String autoStartup = "true";
094 @XmlAttribute(required = false)
095 private String useMDCLogging;
096 @XmlAttribute(required = false)
097 private String useBreadcrumb;
098 @XmlAttribute(required = false)
099 private String allowUseOriginalMessage;
100 @XmlAttribute(required = false)
101 private String managementNamePattern;
102 @XmlAttribute(required = false)
103 private String threadNamePattern;
104 @XmlAttribute(required = false)
105 private Boolean useBlueprintPropertyResolver;
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 @XmlAttribute(required = false)
114 private Boolean typeConverterStatisticsEnabled;
115 @XmlElement(name = "properties", required = false)
116 private PropertiesDefinition properties;
117 @XmlElement(name = "propertyPlaceholder", type = CamelPropertyPlaceholderDefinition.class, required = false)
118 private CamelPropertyPlaceholderDefinition camelPropertyPlaceholder;
119 @XmlElement(name = "package", required = false)
120 private String[] packages = {};
121 @XmlElement(name = "packageScan", type = PackageScanDefinition.class, required = false)
122 private PackageScanDefinition packageScan;
123 @XmlElement(name = "contextScan", type = ContextScanDefinition.class, required = false)
124 private ContextScanDefinition contextScan;
125 @XmlElement(name = "jmxAgent", type = CamelJMXAgentDefinition.class, required = false)
126 private CamelJMXAgentDefinition camelJMXAgent;
127 @XmlElement(name = "streamCaching", type = CamelStreamCachingStrategyDefinition.class, required = false)
128 private CamelStreamCachingStrategyDefinition camelStreamCachingStrategy;
129 @XmlElements({
130 @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class, required = false),
131 @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class, required = false),
132 @XmlElement(name = "proxy", type = CamelProxyFactoryBean.class, required = false),
133 @XmlElement(name = "export", type = CamelServiceExporterDefinition.class, required = false),
134 @XmlElement(name = "errorHandler", type = CamelErrorHandlerFactoryBean.class, required = false)
135 })
136 private List<?> beans;
137 @XmlElement(name = "routeBuilder", required = false)
138 private List<RouteBuilderDefinition> builderRefs = new ArrayList<RouteBuilderDefinition>();
139 @XmlElement(name = "routeContextRef", required = false)
140 private List<RouteContextRefDefinition> routeRefs = new ArrayList<RouteContextRefDefinition>();
141 @XmlElement(name = "threadPoolProfile", required = false)
142 private List<ThreadPoolProfileDefinition> threadPoolProfiles;
143 @XmlElement(name = "threadPool", required = false)
144 private List<CamelThreadPoolFactoryBean> threadPools;
145 @XmlElement(name = "endpoint", required = false)
146 private List<CamelEndpointFactoryBean> endpoints;
147 @XmlElement(name = "dataFormats", required = false)
148 private DataFormatsDefinition dataFormats;
149 @XmlElement(name = "redeliveryPolicyProfile", required = false)
150 private List<CamelRedeliveryPolicyFactoryBean> redeliveryPolicies;
151 @XmlElement(name = "onException", required = false)
152 private List<OnExceptionDefinition> onExceptions = new ArrayList<OnExceptionDefinition>();
153 @XmlElement(name = "onCompletion", required = false)
154 private List<OnCompletionDefinition> onCompletions = new ArrayList<OnCompletionDefinition>();
155 @XmlElement(name = "intercept", required = false)
156 private List<InterceptDefinition> intercepts = new ArrayList<InterceptDefinition>();
157 @XmlElement(name = "interceptFrom", required = false)
158 private List<InterceptFromDefinition> interceptFroms = new ArrayList<InterceptFromDefinition>();
159 @XmlElement(name = "interceptSendToEndpoint", required = false)
160 private List<InterceptSendToEndpointDefinition> interceptSendToEndpoints = new ArrayList<InterceptSendToEndpointDefinition>();
161 @XmlElement(name = "route", required = false)
162 private List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
163 @XmlTransient
164 private BlueprintCamelContext context;
165 @XmlTransient
166 private BlueprintContainer blueprintContainer;
167 @XmlTransient
168 private BundleContext bundleContext;
169 @XmlTransient
170 private boolean implicitId;
171
172 public Class<BlueprintCamelContext> getObjectType() {
173 return BlueprintCamelContext.class;
174 }
175
176 @Override
177 public BlueprintCamelContext getContext(boolean create) {
178 if (context == null && create) {
179 context = createContext();
180 if (!isImplicitId()) {
181 context.setName(getId());
182 }
183 }
184 return context;
185 }
186
187 public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
188 this.blueprintContainer = blueprintContainer;
189 }
190
191 public void setBundleContext(BundleContext bundleContext) {
192 this.bundleContext = bundleContext;
193 }
194
195 protected BlueprintCamelContext createContext() {
196 return new BlueprintCamelContext(bundleContext, blueprintContainer);
197 }
198
199 @Override
200 protected void initCustomRegistry(BlueprintCamelContext context) {
201 Registry registry = getBeanForType(Registry.class);
202 if (registry != null) {
203 LOG.info("Using custom Registry: " + registry);
204 context.setRegistry(registry);
205 }
206 }
207
208 @Override
209 protected <S> S getBeanForType(Class<S> clazz) {
210 Collection<S> objects = BlueprintContainerRegistry.lookupByType(blueprintContainer, clazz).values();
211 if (objects.size() == 1) {
212 return objects.iterator().next();
213 }
214 return null;
215 }
216
217 @Override
218 protected void initPropertyPlaceholder() throws Exception {
219 super.initPropertyPlaceholder();
220
221 // if blueprint property resolver is enabled on CamelContext then bridge PropertiesComponent to blueprint
222 if (isUseBlueprintPropertyResolver()) {
223 // lookup existing configured properties component
224 PropertiesComponent pc = getContext().getComponent("properties", PropertiesComponent.class);
225
226 BlueprintPropertiesParser parser = new BlueprintPropertiesParser(pc, blueprintContainer, pc.getPropertiesParser());
227 BlueprintPropertiesResolver resolver = new BlueprintPropertiesResolver(pc.getPropertiesResolver(), parser);
228
229 // any extra properties
230 ServiceReference<?> ref = bundleContext.getServiceReference(PropertiesComponent.OVERRIDE_PROPERTIES);
231 if (ref != null) {
232 Properties extra = (Properties) bundleContext.getService(ref);
233 if (extra != null) {
234 pc.setOverrideProperties(extra);
235 }
236 }
237
238 // no locations has been set, so its a default component
239 if (pc.getLocations() == null) {
240 StringBuilder sb = new StringBuilder();
241 String[] ids = parser.lookupPropertyPlaceholderIds();
242 for (String id : ids) {
243 sb.append("blueprint:").append(id).append(",");
244 }
245 if (sb.length() > 0) {
246 // location supports multiple separated by comma
247 pc.setLocation(sb.toString());
248 }
249 }
250
251 if (pc.getLocations() != null) {
252 // bridge camel properties with blueprint
253 pc.setPropertiesParser(parser);
254 pc.setPropertiesResolver(resolver);
255 }
256 }
257 }
258
259 @Override
260 protected void initBeanPostProcessor(BlueprintCamelContext context) {
261 }
262
263 @Override
264 protected void postProcessBeforeInit(RouteBuilder builder) {
265 }
266
267 @Override
268 protected void findRouteBuildersByPackageScan(String[] packages, PackageScanFilter filter, List<RoutesBuilder> builders) throws Exception {
269 // add filter to class resolver which then will filter
270 getContext().getPackageScanClassResolver().addFilter(filter);
271 ClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle());
272 PackageScanRouteBuilderFinder finder = new PackageScanRouteBuilderFinder(getContext(), packages, classLoader,
273 getContext().getPackageScanClassResolver());
274 finder.appendBuilders(builders);
275
276 // and remove the filter
277 getContext().getPackageScanClassResolver().removeFilter(filter);
278 }
279
280 @Override
281 protected void findRouteBuildersByContextScan(PackageScanFilter filter, List<RoutesBuilder> builders) throws Exception {
282 ContextScanRouteBuilderFinder finder = new ContextScanRouteBuilderFinder(getContext(), filter);
283 finder.appendBuilders(builders);
284 }
285
286 @Override
287 public void afterPropertiesSet() throws Exception {
288 super.afterPropertiesSet();
289 // setup the application context classloader with the bundle delegating classloader
290 ClassLoader cl = new BundleDelegatingClassLoader(bundleContext.getBundle());
291 LOG.debug("Set the application context classloader to: {}", cl);
292 getContext().setApplicationContextClassLoader(cl);
293 getContext().getManagementStrategy().addEventNotifier(new OsgiCamelContextPublisher(bundleContext));
294 try {
295 getClass().getClassLoader().loadClass("org.osgi.service.event.EventAdmin");
296 getContext().getManagementStrategy().addEventNotifier(new OsgiEventAdminNotifier(bundleContext));
297 } catch (Throwable t) {
298 // Ignore, if the EventAdmin package is not available, just don't use it
299 LOG.debug("EventAdmin package is not available, just don't use it");
300 }
301 // ensure routes is setup
302 setupRoutes();
303 }
304
305 public String getDependsOn() {
306 return dependsOn;
307 }
308
309 public void setDependsOn(String dependsOn) {
310 this.dependsOn = dependsOn;
311 }
312
313 public String getAutoStartup() {
314 return autoStartup;
315 }
316
317 public void setAutoStartup(String autoStartup) {
318 this.autoStartup = autoStartup;
319 }
320
321 public String getUseMDCLogging() {
322 return useMDCLogging;
323 }
324
325 public void setUseMDCLogging(String useMDCLogging) {
326 this.useMDCLogging = useMDCLogging;
327 }
328
329 public String getUseBreadcrumb() {
330 return useBreadcrumb;
331 }
332
333 public void setUseBreadcrumb(String useBreadcrumb) {
334 this.useBreadcrumb = useBreadcrumb;
335 }
336
337 public String getAllowUseOriginalMessage() {
338 return allowUseOriginalMessage;
339 }
340
341 public void setAllowUseOriginalMessage(String allowUseOriginalMessage) {
342 this.allowUseOriginalMessage = allowUseOriginalMessage;
343 }
344
345 public String getManagementNamePattern() {
346 return managementNamePattern;
347 }
348
349 public void setManagementNamePattern(String managementNamePattern) {
350 this.managementNamePattern = managementNamePattern;
351 }
352
353 public String getThreadNamePattern() {
354 return threadNamePattern;
355 }
356
357 public void setThreadNamePattern(String threadNamePattern) {
358 this.threadNamePattern = threadNamePattern;
359 }
360
361 @Deprecated
362 public Boolean getLazyLoadTypeConverters() {
363 // use false by default
364 return lazyLoadTypeConverters != null ? lazyLoadTypeConverters : Boolean.FALSE;
365 }
366
367 @Deprecated
368 public void setLazyLoadTypeConverters(Boolean lazyLoadTypeConverters) {
369 this.lazyLoadTypeConverters = lazyLoadTypeConverters;
370 }
371
372 public Boolean getTypeConverterStatisticsEnabled() {
373 return typeConverterStatisticsEnabled;
374 }
375
376 public void setTypeConverterStatisticsEnabled(Boolean typeConverterStatisticsEnabled) {
377 this.typeConverterStatisticsEnabled = typeConverterStatisticsEnabled;
378 }
379
380 public ShutdownRoute getShutdownRoute() {
381 return shutdownRoute;
382 }
383
384 public void setShutdownRoute(ShutdownRoute shutdownRoute) {
385 this.shutdownRoute = shutdownRoute;
386 }
387
388 public ShutdownRunningTask getShutdownRunningTask() {
389 return shutdownRunningTask;
390 }
391
392 public void setShutdownRunningTask(ShutdownRunningTask shutdownRunningTask) {
393 this.shutdownRunningTask = shutdownRunningTask;
394 }
395
396 public CamelPropertyPlaceholderDefinition getCamelPropertyPlaceholder() {
397 return camelPropertyPlaceholder;
398 }
399
400 public void setCamelPropertyPlaceholder(CamelPropertyPlaceholderDefinition camelPropertyPlaceholder) {
401 this.camelPropertyPlaceholder = camelPropertyPlaceholder;
402 }
403
404 public List<RouteContextRefDefinition> getRouteRefs() {
405 return routeRefs;
406 }
407
408 public void setRouteRefs(List<RouteContextRefDefinition> routeRefs) {
409 this.routeRefs = routeRefs;
410 }
411
412 public List<CamelRedeliveryPolicyFactoryBean> getRedeliveryPolicies() {
413 return redeliveryPolicies;
414 }
415
416 public void setRedeliveryPolicies(List<CamelRedeliveryPolicyFactoryBean> redeliveryPolicies) {
417 this.redeliveryPolicies = redeliveryPolicies;
418 }
419
420 public List<ThreadPoolProfileDefinition> getThreadPoolProfiles() {
421 return threadPoolProfiles;
422 }
423
424 public void setThreadPoolProfiles(List<ThreadPoolProfileDefinition> threadPoolProfiles) {
425 this.threadPoolProfiles = threadPoolProfiles;
426 }
427
428 public List<CamelThreadPoolFactoryBean> getThreadPools() {
429 return threadPools;
430 }
431
432 public void setThreadPools(List<CamelThreadPoolFactoryBean> threadPools) {
433 this.threadPools = threadPools;
434 }
435
436 public String getTrace() {
437 return trace;
438 }
439
440 public void setTrace(String trace) {
441 this.trace = trace;
442 }
443
444 public String getMessageHistory() {
445 return messageHistory;
446 }
447
448 public void setMessageHistory(String messageHistory) {
449 this.messageHistory = messageHistory;
450 }
451
452 public String getStreamCache() {
453 return streamCache;
454 }
455
456 public void setStreamCache(String streamCache) {
457 this.streamCache = streamCache;
458 }
459
460 public String getDelayer() {
461 return delayer;
462 }
463
464 public void setDelayer(String delayer) {
465 this.delayer = delayer;
466 }
467
468 public String getHandleFault() {
469 return handleFault;
470 }
471
472 public void setHandleFault(String handleFault) {
473 this.handleFault = handleFault;
474 }
475
476 public String getErrorHandlerRef() {
477 return errorHandlerRef;
478 }
479
480 public void setErrorHandlerRef(String errorHandlerRef) {
481 this.errorHandlerRef = errorHandlerRef;
482 }
483
484 public PropertiesDefinition getProperties() {
485 return properties;
486 }
487
488 public void setProperties(PropertiesDefinition properties) {
489 this.properties = properties;
490 }
491
492 public String[] getPackages() {
493 return packages;
494 }
495
496 public void setPackages(String[] packages) {
497 this.packages = packages;
498 }
499
500 public PackageScanDefinition getPackageScan() {
501 return packageScan;
502 }
503
504 public void setPackageScan(PackageScanDefinition packageScan) {
505 this.packageScan = packageScan;
506 }
507
508 public ContextScanDefinition getContextScan() {
509 return contextScan;
510 }
511
512 public void setContextScan(ContextScanDefinition contextScan) {
513 this.contextScan = contextScan;
514 }
515
516 public CamelJMXAgentDefinition getCamelJMXAgent() {
517 return camelJMXAgent;
518 }
519
520 public void setCamelJMXAgent(CamelJMXAgentDefinition camelJMXAgent) {
521 this.camelJMXAgent = camelJMXAgent;
522 }
523
524 public CamelStreamCachingStrategyDefinition getCamelStreamCachingStrategy() {
525 return camelStreamCachingStrategy;
526 }
527
528 public void setCamelStreamCachingStrategy(CamelStreamCachingStrategyDefinition camelStreamCachingStrategy) {
529 this.camelStreamCachingStrategy = camelStreamCachingStrategy;
530 }
531
532 public List<?> getBeans() {
533 return beans;
534 }
535
536 public void setBeans(List<?> beans) {
537 this.beans = beans;
538 }
539
540 public List<RouteBuilderDefinition> getBuilderRefs() {
541 return builderRefs;
542 }
543
544 public void setBuilderRefs(List<RouteBuilderDefinition> builderRefs) {
545 this.builderRefs = builderRefs;
546 }
547
548 public List<CamelEndpointFactoryBean> getEndpoints() {
549 return endpoints;
550 }
551
552 public void setEndpoints(List<CamelEndpointFactoryBean> endpoints) {
553 this.endpoints = endpoints;
554 }
555
556 public DataFormatsDefinition getDataFormats() {
557 return dataFormats;
558 }
559
560 public void setDataFormats(DataFormatsDefinition dataFormats) {
561 this.dataFormats = dataFormats;
562 }
563
564 public List<OnExceptionDefinition> getOnExceptions() {
565 return onExceptions;
566 }
567
568 public void setOnExceptions(List<OnExceptionDefinition> onExceptions) {
569 this.onExceptions = onExceptions;
570 }
571
572 public List<OnCompletionDefinition> getOnCompletions() {
573 return onCompletions;
574 }
575
576 public void setOnCompletions(List<OnCompletionDefinition> onCompletions) {
577 this.onCompletions = onCompletions;
578 }
579
580 public List<InterceptDefinition> getIntercepts() {
581 return intercepts;
582 }
583
584 public void setIntercepts(List<InterceptDefinition> intercepts) {
585 this.intercepts = intercepts;
586 }
587
588 public List<InterceptFromDefinition> getInterceptFroms() {
589 return interceptFroms;
590 }
591
592 public void setInterceptFroms(List<InterceptFromDefinition> interceptFroms) {
593 this.interceptFroms = interceptFroms;
594 }
595
596 public List<InterceptSendToEndpointDefinition> getInterceptSendToEndpoints() {
597 return interceptSendToEndpoints;
598 }
599
600 public void setInterceptSendToEndpoints(List<InterceptSendToEndpointDefinition> interceptSendToEndpoints) {
601 this.interceptSendToEndpoints = interceptSendToEndpoints;
602 }
603
604 public List<RouteDefinition> getRoutes() {
605 return routes;
606 }
607
608 public void setRoutes(List<RouteDefinition> routes) {
609 this.routes = routes;
610 }
611
612 public boolean isImplicitId() {
613 return implicitId;
614 }
615
616 public void setImplicitId(boolean flag) {
617 implicitId = flag;
618 }
619
620 public Boolean getUseBlueprintPropertyResolver() {
621 return useBlueprintPropertyResolver;
622 }
623
624 public void setUseBlueprintPropertyResolver(Boolean useBlueprintPropertyResolver) {
625 this.useBlueprintPropertyResolver = useBlueprintPropertyResolver;
626 }
627
628 public boolean isUseBlueprintPropertyResolver() {
629 // enable by default
630 return useBlueprintPropertyResolver == null || useBlueprintPropertyResolver.booleanValue();
631 }
632
633 }