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