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 = Boolean.FALSE;
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                    // location supports multiple separated by comma
223                    pc.setLocation(sb.toString());
224                }
225    
226                if (pc.getLocations() != null) {
227                    // bridge camel properties with blueprint
228                    pc.setPropertiesParser(parser);
229                    pc.setPropertiesResolver(resolver);
230                }
231            }
232        }
233    
234        @Override
235        protected void initBeanPostProcessor(BlueprintCamelContext context) {
236        }
237    
238        @Override
239        protected void postProcessBeforeInit(RouteBuilder builder) {
240        }
241    
242        @Override
243        protected void findRouteBuildersByPackageScan(String[] packages, PackageScanFilter filter, List<RoutesBuilder> builders) throws Exception {
244            // add filter to class resolver which then will filter
245            getContext().getPackageScanClassResolver().addFilter(filter);
246            ClassLoader classLoader = new BundleDelegatingClassLoader(((ExtendedBlueprintContainer) blueprintContainer).getBundleContext().getBundle());
247            PackageScanRouteBuilderFinder finder = new PackageScanRouteBuilderFinder(getContext(), packages, classLoader,
248                                                                                     /*getBeanPostProcessor(),*/ getContext().getPackageScanClassResolver());
249            finder.appendBuilders(builders);
250    
251            // and remove the filter
252            getContext().getPackageScanClassResolver().removeFilter(filter);
253        }
254    
255        @Override
256        protected void findRouteBuildersByContextScan(PackageScanFilter filter, List<RoutesBuilder> builders) throws Exception {
257            ContextScanRouteBuilderFinder finder = new ContextScanRouteBuilderFinder(getContext(), filter);
258            finder.appendBuilders(builders);
259        }
260    
261        @Override
262        public void afterPropertiesSet() throws Exception {
263            super.afterPropertiesSet();
264            // setup the application context classloader with the bundle delegating classloader
265            ClassLoader cl = new BundleDelegatingClassLoader(((ExtendedBlueprintContainer) blueprintContainer).getBundleContext().getBundle());
266            LOG.debug("Set the application context classloader to: {}", cl);
267            getContext().setApplicationContextClassLoader(cl);
268            getContext().getManagementStrategy().addEventNotifier(new OsgiCamelContextPublisher(bundleContext));
269            try {
270                getClass().getClassLoader().loadClass("org.osgi.service.event.EventAdmin");
271                getContext().getManagementStrategy().addEventNotifier(new OsgiEventAdminNotifier(bundleContext));
272            } catch (Throwable t) {
273                // Ignore, if the EventAdmin package is not available, just don't use it
274                LOG.debug("EventAdmin package is not available, just don't use it");
275            }
276        }
277    
278        public String getDependsOn() {
279            return dependsOn;
280        }
281    
282        public void setDependsOn(String dependsOn) {
283            this.dependsOn = dependsOn;
284        }
285    
286        public String getAutoStartup() {
287            return autoStartup;
288        }
289    
290        public void setAutoStartup(String autoStartup) {
291            this.autoStartup = autoStartup;
292        }
293    
294        public String getUseMDCLogging() {
295            return useMDCLogging;
296        }
297    
298        public void setUseMDCLogging(String useMDCLogging) {
299            this.useMDCLogging = useMDCLogging;
300        }
301    
302        public String getUseBreadcrumb() {
303            return useBreadcrumb;
304        }
305    
306        public void setUseBreadcrumb(String useBreadcrumb) {
307            this.useBreadcrumb = useBreadcrumb;
308        }
309    
310        public Boolean getLazyLoadTypeConverters() {
311            return lazyLoadTypeConverters;
312        }
313    
314        public void setLazyLoadTypeConverters(Boolean lazyLoadTypeConverters) {
315            this.lazyLoadTypeConverters = lazyLoadTypeConverters;
316        }
317    
318        public ShutdownRoute getShutdownRoute() {
319            return shutdownRoute;
320        }
321    
322        public void setShutdownRoute(ShutdownRoute shutdownRoute) {
323            this.shutdownRoute = shutdownRoute;
324        }
325    
326        public ShutdownRunningTask getShutdownRunningTask() {
327            return shutdownRunningTask;
328        }
329    
330        public void setShutdownRunningTask(ShutdownRunningTask shutdownRunningTask) {
331            this.shutdownRunningTask = shutdownRunningTask;
332        }
333    
334        public CamelPropertyPlaceholderDefinition getCamelPropertyPlaceholder() {
335            return camelPropertyPlaceholder;
336        }
337    
338        public void setCamelPropertyPlaceholder(CamelPropertyPlaceholderDefinition camelPropertyPlaceholder) {
339            this.camelPropertyPlaceholder = camelPropertyPlaceholder;
340        }
341    
342        public List<RouteContextRefDefinition> getRouteRefs() {
343            return routeRefs;
344        }
345    
346        public void setRouteRefs(List<RouteContextRefDefinition> routeRefs) {
347            this.routeRefs = routeRefs;
348        }
349    
350        public List<CamelRedeliveryPolicyFactoryBean> getRedeliveryPolicies() {
351            return redeliveryPolicies;
352        }
353    
354        public void setRedeliveryPolicies(List<CamelRedeliveryPolicyFactoryBean> redeliveryPolicies) {
355            this.redeliveryPolicies = redeliveryPolicies;
356        }
357    
358        public List<ThreadPoolProfileDefinition> getThreadPoolProfiles() {
359            return threadPoolProfiles;
360        }
361    
362        public void setThreadPoolProfiles(List<ThreadPoolProfileDefinition> threadPoolProfiles) {
363            this.threadPoolProfiles = threadPoolProfiles;
364        }
365    
366        public List<CamelThreadPoolFactoryBean> getThreadPools() {
367            return threadPools;
368        }
369    
370        public void setThreadPools(List<CamelThreadPoolFactoryBean> threadPools) {
371            this.threadPools = threadPools;
372        }
373    
374        public String getTrace() {
375            return trace;
376        }
377    
378        public void setTrace(String trace) {
379            this.trace = trace;
380        }
381    
382        public String getStreamCache() {
383            return streamCache;
384        }
385    
386        public void setStreamCache(String streamCache) {
387            this.streamCache = streamCache;
388        }
389    
390        public String getDelayer() {
391            return delayer;
392        }
393    
394        public void setDelayer(String delayer) {
395            this.delayer = delayer;
396        }
397    
398        public String getHandleFault() {
399            return handleFault;
400        }
401    
402        public void setHandleFault(String handleFault) {
403            this.handleFault = handleFault;
404        }
405    
406        public String getErrorHandlerRef() {
407            return errorHandlerRef;
408        }
409    
410        public void setErrorHandlerRef(String errorHandlerRef) {
411            this.errorHandlerRef = errorHandlerRef;
412        }
413    
414        public PropertiesDefinition getProperties() {
415            return properties;
416        }
417    
418        public void setProperties(PropertiesDefinition properties) {
419            this.properties = properties;
420        }
421    
422        public String[] getPackages() {
423            return packages;
424        }
425    
426        public void setPackages(String[] packages) {
427            this.packages = packages;
428        }
429    
430        public PackageScanDefinition getPackageScan() {
431            return packageScan;
432        }
433    
434        public void setPackageScan(PackageScanDefinition packageScan) {
435            this.packageScan = packageScan;
436        }
437    
438        public ContextScanDefinition getContextScan() {
439            return contextScan;
440        }
441    
442        public void setContextScan(ContextScanDefinition contextScan) {
443            this.contextScan = contextScan;
444        }
445    
446        public CamelJMXAgentDefinition getCamelJMXAgent() {
447            return camelJMXAgent;
448        }
449    
450        public void setCamelJMXAgent(CamelJMXAgentDefinition camelJMXAgent) {
451            this.camelJMXAgent = camelJMXAgent;
452        }
453    
454        public List getBeans() {
455            return beans;
456        }
457    
458        public void setBeans(List beans) {
459            this.beans = beans;
460        }
461    
462        public List<RouteBuilderDefinition> getBuilderRefs() {
463            return builderRefs;
464        }
465    
466        public void setBuilderRefs(List<RouteBuilderDefinition> builderRefs) {
467            this.builderRefs = builderRefs;
468        }
469    
470        public List<CamelEndpointFactoryBean> getEndpoints() {
471            return endpoints;
472        }
473    
474        public void setEndpoints(List<CamelEndpointFactoryBean> endpoints) {
475            this.endpoints = endpoints;
476        }
477    
478        public DataFormatsDefinition getDataFormats() {
479            return dataFormats;
480        }
481    
482        public void setDataFormats(DataFormatsDefinition dataFormats) {
483            this.dataFormats = dataFormats;
484        }
485    
486        public List<OnExceptionDefinition> getOnExceptions() {
487            return onExceptions;
488        }
489    
490        public void setOnExceptions(List<OnExceptionDefinition> onExceptions) {
491            this.onExceptions = onExceptions;
492        }
493    
494        public List<OnCompletionDefinition> getOnCompletions() {
495            return onCompletions;
496        }
497    
498        public void setOnCompletions(List<OnCompletionDefinition> onCompletions) {
499            this.onCompletions = onCompletions;
500        }
501    
502        public List<InterceptDefinition> getIntercepts() {
503            return intercepts;
504        }
505    
506        public void setIntercepts(List<InterceptDefinition> intercepts) {
507            this.intercepts = intercepts;
508        }
509    
510        public List<InterceptFromDefinition> getInterceptFroms() {
511            return interceptFroms;
512        }
513    
514        public void setInterceptFroms(List<InterceptFromDefinition> interceptFroms) {
515            this.interceptFroms = interceptFroms;
516        }
517    
518        public List<InterceptSendToEndpointDefinition> getInterceptSendToEndpoints() {
519            return interceptSendToEndpoints;
520        }
521    
522        public void setInterceptSendToEndpoints(List<InterceptSendToEndpointDefinition> interceptSendToEndpoints) {
523            this.interceptSendToEndpoints = interceptSendToEndpoints;
524        }
525    
526        public List<RouteDefinition> getRoutes() {
527            return routes;
528        }
529    
530        public void setRoutes(List<RouteDefinition> routes) {
531            this.routes = routes;
532        }
533    
534        public boolean isImplicitId() {
535            return implicitId;
536        }
537        
538        public void setImplicitId(boolean flag) {
539            implicitId = flag;
540        }
541    
542        public Boolean getUseBlueprintPropertyResolver() {
543            return useBlueprintPropertyResolver;
544        }
545    
546        public void setUseBlueprintPropertyResolver(Boolean useBlueprintPropertyResolver) {
547            this.useBlueprintPropertyResolver = useBlueprintPropertyResolver;
548        }
549    
550        public boolean isUseBlueprintPropertyResolver() {
551            // enable by default
552            return useBlueprintPropertyResolver == null || useBlueprintPropertyResolver.booleanValue();
553        }
554    
555    }