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.List;
021    
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlAttribute;
025    import javax.xml.bind.annotation.XmlElement;
026    import javax.xml.bind.annotation.XmlElements;
027    import javax.xml.bind.annotation.XmlRootElement;
028    import javax.xml.bind.annotation.XmlTransient;
029    
030    import org.apache.camel.RoutesBuilder;
031    import org.apache.camel.ShutdownRoute;
032    import org.apache.camel.ShutdownRunningTask;
033    import org.apache.camel.builder.RouteBuilder;
034    import org.apache.camel.core.xml.AbstractCamelContextFactoryBean;
035    import org.apache.camel.core.xml.CamelJMXAgentDefinition;
036    import org.apache.camel.core.xml.CamelPropertyPlaceholderDefinition;
037    import org.apache.camel.core.xml.CamelProxyFactoryDefinition;
038    import org.apache.camel.core.xml.CamelServiceExporterDefinition;
039    import org.apache.camel.model.ContextScanDefinition;
040    import org.apache.camel.model.InterceptDefinition;
041    import org.apache.camel.model.InterceptFromDefinition;
042    import org.apache.camel.model.InterceptSendToEndpointDefinition;
043    import org.apache.camel.model.OnCompletionDefinition;
044    import org.apache.camel.model.OnExceptionDefinition;
045    import org.apache.camel.model.PackageScanDefinition;
046    import org.apache.camel.model.RouteBuilderDefinition;
047    import org.apache.camel.model.RouteContextRefDefinition;
048    import org.apache.camel.model.RouteDefinition;
049    import org.apache.camel.model.ThreadPoolProfileDefinition;
050    import org.apache.camel.model.config.PropertiesDefinition;
051    import org.apache.camel.model.dataformat.DataFormatsDefinition;
052    import org.apache.camel.spi.PackageScanFilter;
053    import org.osgi.framework.BundleContext;
054    import org.osgi.service.blueprint.container.BlueprintContainer;
055    
056    /**
057     * A bean to create and initialize a {@link BlueprintCamelContext}
058     * and install routes either explicitly configured in
059     * Blueprint XML or found by searching the classpath for Java classes which extend
060     * {@link RouteBuilder} using the nested {@link #setPackages(String[])}.
061     *
062     * @version $Revision: 1024542 $
063     */
064    @XmlRootElement(name = "camelContext")
065    @XmlAccessorType(XmlAccessType.FIELD)
066    public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<BlueprintCamelContext> {
067    
068        @XmlAttribute(name = "depends-on", required = false)
069        private String dependsOn;
070        @XmlAttribute(required = false)
071        private String trace;
072        @XmlAttribute(required = false)
073        private String streamCache = "false";
074        @XmlAttribute(required = false)
075        private String delayer;
076        @XmlAttribute(required = false)
077        private String handleFault;
078        @XmlAttribute(required = false)
079        private String errorHandlerRef;
080        @XmlAttribute(required = false)
081        private String autoStartup = "true";
082        @XmlAttribute(required = false)
083        private ShutdownRoute shutdownRoute;
084        @XmlAttribute(required = false)
085        private ShutdownRunningTask shutdownRunningTask;
086        @XmlAttribute(required = false)
087        private Boolean lazyLoadTypeConverters = Boolean.FALSE;
088        @XmlElement(name = "properties", required = false)
089        private PropertiesDefinition properties;
090        @XmlElement(name = "propertyPlaceholder", type = CamelPropertyPlaceholderDefinition.class, required = false)
091        private CamelPropertyPlaceholderDefinition camelPropertyPlaceholder;
092        @XmlElement(name = "package", required = false)
093        private String[] packages = {};
094        @XmlElement(name = "packageScan", type = PackageScanDefinition.class, required = false)
095        private PackageScanDefinition packageScan;
096        @XmlElement(name = "contextScan", type = ContextScanDefinition.class, required = false)
097        private ContextScanDefinition contextScan;
098        @XmlElement(name = "jmxAgent", type = CamelJMXAgentDefinition.class, required = false)
099        private CamelJMXAgentDefinition camelJMXAgent;
100        @XmlElements({
101    //        @XmlElement(name = "beanPostProcessor", type = CamelBeanPostProcessor.class, required = false),
102            @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class, required = false),
103            @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class, required = false),
104            @XmlElement(name = "proxy", type = CamelProxyFactoryDefinition.class, required = false),
105            @XmlElement(name = "export", type = CamelServiceExporterDefinition.class, required = false),
106            @XmlElement(name = "errorHandler", type = ErrorHandlerDefinition.class, required = false)
107        })
108        private List beans;
109        @XmlElement(name = "routeBuilder", required = false)
110        private List<RouteBuilderDefinition> builderRefs = new ArrayList<RouteBuilderDefinition>();
111        @XmlElement(name = "routeContextRef", required = false)
112        private List<RouteContextRefDefinition> routeRefs = new ArrayList<RouteContextRefDefinition>();
113        @XmlElement(name = "threadPoolProfile", required = false)
114        private List<ThreadPoolProfileDefinition> threadPoolProfiles;
115        @XmlElement(name = "threadPool", required = false)
116        private List<CamelThreadPoolFactoryBean> threadPools;
117        @XmlElement(name = "endpoint", required = false)
118        private List<CamelEndpointFactoryBean> endpoints;
119        @XmlElement(name = "dataFormats", required = false)
120        private DataFormatsDefinition dataFormats;
121        @XmlElement(name = "onException", required = false)
122        private List<OnExceptionDefinition> onExceptions = new ArrayList<OnExceptionDefinition>();
123        @XmlElement(name = "onCompletion", required = false)
124        private List<OnCompletionDefinition> onCompletions = new ArrayList<OnCompletionDefinition>();
125        @XmlElement(name = "intercept", required = false)
126        private List<InterceptDefinition> intercepts = new ArrayList<InterceptDefinition>();
127        @XmlElement(name = "interceptFrom", required = false)
128        private List<InterceptFromDefinition> interceptFroms = new ArrayList<InterceptFromDefinition>();
129        @XmlElement(name = "interceptSendToEndpoint", required = false)
130        private List<InterceptSendToEndpointDefinition> interceptSendToEndpoints = new ArrayList<InterceptSendToEndpointDefinition>();
131        @XmlElement(name = "route", required = false)
132        private List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
133        @XmlTransient
134        private BlueprintCamelContext context;
135        @XmlTransient
136        private ClassLoader contextClassLoaderOnStart;
137    //    @XmlTransient
138    //    private ApplicationContext applicationContext;
139    //    @XmlTransient
140    //    private BeanPostProcessor beanPostProcessor;
141        @XmlTransient
142        private BlueprintContainer blueprintContainer;
143        @XmlTransient
144        private BundleContext bundleContext;
145        @XmlTransient
146        private boolean implicitId;
147    
148    
149        public Class getObjectType() {
150            return BlueprintCamelContext.class;
151        }
152    
153        @Override
154        public BlueprintCamelContext getContext(boolean create) {
155            if (context == null && create) {
156                context = createContext();
157                if (!isImplicitId()) {
158                    context.setName(getId());
159                }
160            }
161            return context;
162        }
163    
164        public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
165            this.blueprintContainer = blueprintContainer;
166        }
167    
168        public void setBundleContext(BundleContext bundleContext) {
169            this.bundleContext = bundleContext;
170        }
171    
172        protected BlueprintCamelContext createContext() {
173            return new BlueprintCamelContext(bundleContext, blueprintContainer);
174        }
175    
176        @Override
177        protected void initCustomRegistry(BlueprintCamelContext context) {
178        }
179    
180        @Override
181        protected <S> S getBeanForType(Class<S> clazz) {
182            return null;
183        }
184    
185        @Override
186        protected void initBeanPostProcessor(BlueprintCamelContext context) {
187        }
188    
189        @Override
190        protected void postProcessBeforeInit(RouteBuilder builder) {
191        }
192    
193        @Override
194        protected void findRouteBuildersByPackageScan(String[] packages, PackageScanFilter filter, List<RoutesBuilder> builders) throws Exception {
195        }
196    
197        @Override
198        protected void findRouteBuildersByContextScan(PackageScanFilter filter, List<RoutesBuilder> builders) throws Exception {
199        }
200    
201        public String getDependsOn() {
202            return dependsOn;
203        }
204    
205        public void setDependsOn(String dependsOn) {
206            this.dependsOn = dependsOn;
207        }
208    
209        public String getAutoStartup() {
210            return autoStartup;
211        }
212    
213        public void setAutoStartup(String autoStartup) {
214            this.autoStartup = autoStartup;
215        }
216    
217        public Boolean getLazyLoadTypeConverters() {
218            return lazyLoadTypeConverters;
219        }
220    
221        public void setLazyLoadTypeConverters(Boolean lazyLoadTypeConverters) {
222            this.lazyLoadTypeConverters = lazyLoadTypeConverters;
223        }
224    
225        public ShutdownRoute getShutdownRoute() {
226            return shutdownRoute;
227        }
228    
229        public void setShutdownRoute(ShutdownRoute shutdownRoute) {
230            this.shutdownRoute = shutdownRoute;
231        }
232    
233        public ShutdownRunningTask getShutdownRunningTask() {
234            return shutdownRunningTask;
235        }
236    
237        public void setShutdownRunningTask(ShutdownRunningTask shutdownRunningTask) {
238            this.shutdownRunningTask = shutdownRunningTask;
239        }
240    
241        public CamelPropertyPlaceholderDefinition getCamelPropertyPlaceholder() {
242            return camelPropertyPlaceholder;
243        }
244    
245        public void setCamelPropertyPlaceholder(CamelPropertyPlaceholderDefinition camelPropertyPlaceholder) {
246            this.camelPropertyPlaceholder = camelPropertyPlaceholder;
247        }
248    
249        public List<RouteContextRefDefinition> getRouteRefs() {
250            return routeRefs;
251        }
252    
253        public void setRouteRefs(List<RouteContextRefDefinition> routeRefs) {
254            this.routeRefs = routeRefs;
255        }
256    
257        public List<ThreadPoolProfileDefinition> getThreadPoolProfiles() {
258            return threadPoolProfiles;
259        }
260    
261        public void setThreadPoolProfiles(List<ThreadPoolProfileDefinition> threadPoolProfiles) {
262            this.threadPoolProfiles = threadPoolProfiles;
263        }
264    
265        public List<CamelThreadPoolFactoryBean> getThreadPools() {
266            return threadPools;
267        }
268    
269        public void setThreadPools(List<CamelThreadPoolFactoryBean> threadPools) {
270            this.threadPools = threadPools;
271        }
272    
273        public String getTrace() {
274            return trace;
275        }
276    
277        public void setTrace(String trace) {
278            this.trace = trace;
279        }
280    
281        public String getStreamCache() {
282            return streamCache;
283        }
284    
285        public void setStreamCache(String streamCache) {
286            this.streamCache = streamCache;
287        }
288    
289        public String getDelayer() {
290            return delayer;
291        }
292    
293        public void setDelayer(String delayer) {
294            this.delayer = delayer;
295        }
296    
297        public String getHandleFault() {
298            return handleFault;
299        }
300    
301        public void setHandleFault(String handleFault) {
302            this.handleFault = handleFault;
303        }
304    
305        public String getErrorHandlerRef() {
306            return errorHandlerRef;
307        }
308    
309        public void setErrorHandlerRef(String errorHandlerRef) {
310            this.errorHandlerRef = errorHandlerRef;
311        }
312    
313        public PropertiesDefinition getProperties() {
314            return properties;
315        }
316    
317        public void setProperties(PropertiesDefinition properties) {
318            this.properties = properties;
319        }
320    
321        public String[] getPackages() {
322            return packages;
323        }
324    
325        public void setPackages(String[] packages) {
326            this.packages = packages;
327        }
328    
329        public PackageScanDefinition getPackageScan() {
330            return packageScan;
331        }
332    
333        public void setPackageScan(PackageScanDefinition packageScan) {
334            this.packageScan = packageScan;
335        }
336    
337        public ContextScanDefinition getContextScan() {
338            return contextScan;
339        }
340    
341        public void setContextScan(ContextScanDefinition contextScan) {
342            this.contextScan = contextScan;
343        }
344    
345        public CamelJMXAgentDefinition getCamelJMXAgent() {
346            return camelJMXAgent;
347        }
348    
349        public void setCamelJMXAgent(CamelJMXAgentDefinition camelJMXAgent) {
350            this.camelJMXAgent = camelJMXAgent;
351        }
352    
353        public List getBeans() {
354            return beans;
355        }
356    
357        public void setBeans(List beans) {
358            this.beans = beans;
359        }
360    
361        public List<RouteBuilderDefinition> getBuilderRefs() {
362            return builderRefs;
363        }
364    
365        public void setBuilderRefs(List<RouteBuilderDefinition> builderRefs) {
366            this.builderRefs = builderRefs;
367        }
368    
369        public List<CamelEndpointFactoryBean> getEndpoints() {
370            return endpoints;
371        }
372    
373        public void setEndpoints(List<CamelEndpointFactoryBean> endpoints) {
374            this.endpoints = endpoints;
375        }
376    
377        public DataFormatsDefinition getDataFormats() {
378            return dataFormats;
379        }
380    
381        public void setDataFormats(DataFormatsDefinition dataFormats) {
382            this.dataFormats = dataFormats;
383        }
384    
385        public List<OnExceptionDefinition> getOnExceptions() {
386            return onExceptions;
387        }
388    
389        public void setOnExceptions(List<OnExceptionDefinition> onExceptions) {
390            this.onExceptions = onExceptions;
391        }
392    
393        public List<OnCompletionDefinition> getOnCompletions() {
394            return onCompletions;
395        }
396    
397        public void setOnCompletions(List<OnCompletionDefinition> onCompletions) {
398            this.onCompletions = onCompletions;
399        }
400    
401        public List<InterceptDefinition> getIntercepts() {
402            return intercepts;
403        }
404    
405        public void setIntercepts(List<InterceptDefinition> intercepts) {
406            this.intercepts = intercepts;
407        }
408    
409        public List<InterceptFromDefinition> getInterceptFroms() {
410            return interceptFroms;
411        }
412    
413        public void setInterceptFroms(List<InterceptFromDefinition> interceptFroms) {
414            this.interceptFroms = interceptFroms;
415        }
416    
417        public List<InterceptSendToEndpointDefinition> getInterceptSendToEndpoints() {
418            return interceptSendToEndpoints;
419        }
420    
421        public void setInterceptSendToEndpoints(List<InterceptSendToEndpointDefinition> interceptSendToEndpoints) {
422            this.interceptSendToEndpoints = interceptSendToEndpoints;
423        }
424    
425        public List<RouteDefinition> getRoutes() {
426            return routes;
427        }
428    
429        public void setRoutes(List<RouteDefinition> routes) {
430            this.routes = routes;
431        }
432    
433        public ClassLoader getContextClassLoaderOnStart() {
434            return contextClassLoaderOnStart;
435        }
436        
437        public boolean isImplicitId() {
438            return implicitId;
439        }
440        
441        public void setImplicitId(boolean flag) {
442            implicitId = flag;
443        }
444    
445    }