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: 954861 $
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        @XmlElement(name = "properties", required = false)
087        private PropertiesDefinition properties;
088        @XmlElement(name = "propertyPlaceholder", type = CamelPropertyPlaceholderDefinition.class, required = false)
089        private CamelPropertyPlaceholderDefinition camelPropertyPlaceholder;
090        @XmlElement(name = "package", required = false)
091        private String[] packages = {};
092        @XmlElement(name = "packageScan", type = PackageScanDefinition.class, required = false)
093        private PackageScanDefinition packageScan;
094        @XmlElement(name = "contextScan", type = ContextScanDefinition.class, required = false)
095        private ContextScanDefinition contextScan;
096        @XmlElement(name = "jmxAgent", type = CamelJMXAgentDefinition.class, required = false)
097        private CamelJMXAgentDefinition camelJMXAgent;
098        @XmlElements({
099    //        @XmlElement(name = "beanPostProcessor", type = CamelBeanPostProcessor.class, required = false),
100            @XmlElement(name = "template", type = CamelProducerTemplateFactoryBean.class, required = false),
101            @XmlElement(name = "consumerTemplate", type = CamelConsumerTemplateFactoryBean.class, required = false),
102            @XmlElement(name = "proxy", type = CamelProxyFactoryDefinition.class, required = false),
103            @XmlElement(name = "export", type = CamelServiceExporterDefinition.class, required = false) //,
104    //        @XmlElement(name = "errorHandler", type = ErrorHandlerDefinition.class, required = false)
105        })
106        private List beans;
107        @XmlElement(name = "routeBuilder", required = false)
108        private List<RouteBuilderDefinition> builderRefs = new ArrayList<RouteBuilderDefinition>();
109        @XmlElement(name = "routeContextRef", required = false)
110        private List<RouteContextRefDefinition> routeRefs = new ArrayList<RouteContextRefDefinition>();
111        @XmlElement(name = "threadPoolProfile", required = false)
112        private List<ThreadPoolProfileDefinition> threadPoolProfiles;
113        @XmlElement(name = "threadPool", required = false)
114        private List<CamelThreadPoolFactoryBean> threadPools;
115        @XmlElement(name = "endpoint", required = false)
116        private List<CamelEndpointFactoryBean> endpoints;
117        @XmlElement(name = "dataFormats", required = false)
118        private DataFormatsDefinition dataFormats;
119        @XmlElement(name = "onException", required = false)
120        private List<OnExceptionDefinition> onExceptions = new ArrayList<OnExceptionDefinition>();
121        @XmlElement(name = "onCompletion", required = false)
122        private List<OnCompletionDefinition> onCompletions = new ArrayList<OnCompletionDefinition>();
123        @XmlElement(name = "intercept", required = false)
124        private List<InterceptDefinition> intercepts = new ArrayList<InterceptDefinition>();
125        @XmlElement(name = "interceptFrom", required = false)
126        private List<InterceptFromDefinition> interceptFroms = new ArrayList<InterceptFromDefinition>();
127        @XmlElement(name = "interceptSendToEndpoint", required = false)
128        private List<InterceptSendToEndpointDefinition> interceptSendToEndpoints = new ArrayList<InterceptSendToEndpointDefinition>();
129        @XmlElement(name = "route", required = false)
130        private List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
131        @XmlTransient
132        private BlueprintCamelContext context;
133        @XmlTransient
134        private ClassLoader contextClassLoaderOnStart;
135    //    @XmlTransient
136    //    private ApplicationContext applicationContext;
137    //    @XmlTransient
138    //    private BeanPostProcessor beanPostProcessor;
139        @XmlTransient
140        private BlueprintContainer blueprintContainer;
141        @XmlTransient
142        private BundleContext bundleContext;
143    
144    
145        public Class getObjectType() {
146            return BlueprintCamelContext.class;
147        }
148    
149        @Override
150        public BlueprintCamelContext getContext(boolean create) {
151            if (context == null && create) {
152                context = createContext();
153            }
154            return context;
155        }
156    
157        public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
158            this.blueprintContainer = blueprintContainer;
159        }
160    
161        public void setBundleContext(BundleContext bundleContext) {
162            this.bundleContext = bundleContext;
163        }
164    
165        protected BlueprintCamelContext createContext() {
166            return new BlueprintCamelContext(bundleContext, blueprintContainer);
167        }
168    
169        @Override
170        protected void initCustomRegistry(BlueprintCamelContext context) {
171        }
172    
173        @Override
174        protected <S> S getBeanForType(Class<S> clazz) {
175            return null;
176        }
177    
178        @Override
179        protected void initBeanPostProcessor(BlueprintCamelContext context) {
180        }
181    
182        @Override
183        protected void postProcessBeforeInit(RouteBuilder builder) {
184        }
185    
186        @Override
187        protected void findRouteBuildersByPackageScan(String[] packages, PackageScanFilter filter, List<RoutesBuilder> builders) throws Exception {
188        }
189    
190        @Override
191        protected void findRouteBuildersByContextScan(PackageScanFilter filter, List<RoutesBuilder> builders) throws Exception {
192        }
193    
194        public String getDependsOn() {
195            return dependsOn;
196        }
197    
198        public void setDependsOn(String dependsOn) {
199            this.dependsOn = dependsOn;
200        }
201    
202        public String getAutoStartup() {
203            return autoStartup;
204        }
205    
206        public void setAutoStartup(String autoStartup) {
207            this.autoStartup = autoStartup;
208        }
209    
210        public ShutdownRoute getShutdownRoute() {
211            return shutdownRoute;
212        }
213    
214        public void setShutdownRoute(ShutdownRoute shutdownRoute) {
215            this.shutdownRoute = shutdownRoute;
216        }
217    
218        public ShutdownRunningTask getShutdownRunningTask() {
219            return shutdownRunningTask;
220        }
221    
222        public void setShutdownRunningTask(ShutdownRunningTask shutdownRunningTask) {
223            this.shutdownRunningTask = shutdownRunningTask;
224        }
225    
226        public CamelPropertyPlaceholderDefinition getCamelPropertyPlaceholder() {
227            return camelPropertyPlaceholder;
228        }
229    
230        public void setCamelPropertyPlaceholder(CamelPropertyPlaceholderDefinition camelPropertyPlaceholder) {
231            this.camelPropertyPlaceholder = camelPropertyPlaceholder;
232        }
233    
234        public List<RouteContextRefDefinition> getRouteRefs() {
235            return routeRefs;
236        }
237    
238        public void setRouteRefs(List<RouteContextRefDefinition> routeRefs) {
239            this.routeRefs = routeRefs;
240        }
241    
242        public List<ThreadPoolProfileDefinition> getThreadPoolProfiles() {
243            return threadPoolProfiles;
244        }
245    
246        public void setThreadPoolProfiles(List<ThreadPoolProfileDefinition> threadPoolProfiles) {
247            this.threadPoolProfiles = threadPoolProfiles;
248        }
249    
250        public List<CamelThreadPoolFactoryBean> getThreadPools() {
251            return threadPools;
252        }
253    
254        public void setThreadPools(List<CamelThreadPoolFactoryBean> threadPools) {
255            this.threadPools = threadPools;
256        }
257    
258        public String getTrace() {
259            return trace;
260        }
261    
262        public void setTrace(String trace) {
263            this.trace = trace;
264        }
265    
266        public String getStreamCache() {
267            return streamCache;
268        }
269    
270        public void setStreamCache(String streamCache) {
271            this.streamCache = streamCache;
272        }
273    
274        public String getDelayer() {
275            return delayer;
276        }
277    
278        public void setDelayer(String delayer) {
279            this.delayer = delayer;
280        }
281    
282        public String getHandleFault() {
283            return handleFault;
284        }
285    
286        public void setHandleFault(String handleFault) {
287            this.handleFault = handleFault;
288        }
289    
290        public String getErrorHandlerRef() {
291            return errorHandlerRef;
292        }
293    
294        public void setErrorHandlerRef(String errorHandlerRef) {
295            this.errorHandlerRef = errorHandlerRef;
296        }
297    
298        public PropertiesDefinition getProperties() {
299            return properties;
300        }
301    
302        public void setProperties(PropertiesDefinition properties) {
303            this.properties = properties;
304        }
305    
306        public String[] getPackages() {
307            return packages;
308        }
309    
310        public void setPackages(String[] packages) {
311            this.packages = packages;
312        }
313    
314        public PackageScanDefinition getPackageScan() {
315            return packageScan;
316        }
317    
318        public void setPackageScan(PackageScanDefinition packageScan) {
319            this.packageScan = packageScan;
320        }
321    
322        public ContextScanDefinition getContextScan() {
323            return contextScan;
324        }
325    
326        public void setContextScan(ContextScanDefinition contextScan) {
327            this.contextScan = contextScan;
328        }
329    
330        public CamelJMXAgentDefinition getCamelJMXAgent() {
331            return camelJMXAgent;
332        }
333    
334        public void setCamelJMXAgent(CamelJMXAgentDefinition camelJMXAgent) {
335            this.camelJMXAgent = camelJMXAgent;
336        }
337    
338        public List getBeans() {
339            return beans;
340        }
341    
342        public void setBeans(List beans) {
343            this.beans = beans;
344        }
345    
346        public List<RouteBuilderDefinition> getBuilderRefs() {
347            return builderRefs;
348        }
349    
350        public void setBuilderRefs(List<RouteBuilderDefinition> builderRefs) {
351            this.builderRefs = builderRefs;
352        }
353    
354        public List<CamelEndpointFactoryBean> getEndpoints() {
355            return endpoints;
356        }
357    
358        public void setEndpoints(List<CamelEndpointFactoryBean> endpoints) {
359            this.endpoints = endpoints;
360        }
361    
362        public DataFormatsDefinition getDataFormats() {
363            return dataFormats;
364        }
365    
366        public void setDataFormats(DataFormatsDefinition dataFormats) {
367            this.dataFormats = dataFormats;
368        }
369    
370        public List<OnExceptionDefinition> getOnExceptions() {
371            return onExceptions;
372        }
373    
374        public void setOnExceptions(List<OnExceptionDefinition> onExceptions) {
375            this.onExceptions = onExceptions;
376        }
377    
378        public List<OnCompletionDefinition> getOnCompletions() {
379            return onCompletions;
380        }
381    
382        public void setOnCompletions(List<OnCompletionDefinition> onCompletions) {
383            this.onCompletions = onCompletions;
384        }
385    
386        public List<InterceptDefinition> getIntercepts() {
387            return intercepts;
388        }
389    
390        public void setIntercepts(List<InterceptDefinition> intercepts) {
391            this.intercepts = intercepts;
392        }
393    
394        public List<InterceptFromDefinition> getInterceptFroms() {
395            return interceptFroms;
396        }
397    
398        public void setInterceptFroms(List<InterceptFromDefinition> interceptFroms) {
399            this.interceptFroms = interceptFroms;
400        }
401    
402        public List<InterceptSendToEndpointDefinition> getInterceptSendToEndpoints() {
403            return interceptSendToEndpoints;
404        }
405    
406        public void setInterceptSendToEndpoints(List<InterceptSendToEndpointDefinition> interceptSendToEndpoints) {
407            this.interceptSendToEndpoints = interceptSendToEndpoints;
408        }
409    
410        public List<RouteDefinition> getRoutes() {
411            return routes;
412        }
413    
414        public void setRoutes(List<RouteDefinition> routes) {
415            this.routes = routes;
416        }
417    
418        public ClassLoader getContextClassLoaderOnStart() {
419            return contextClassLoaderOnStart;
420        }
421    
422    }