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