CAMEL-10567: Camel-Jackson: Add an option to allow the UnmarshallType header use
[camel.git] / components / camel-cdi / src / main / java / org / apache / camel / cdi / CdiCamelContext.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.camel.cdi;
18
19 import javax.annotation.PostConstruct;
20 import javax.annotation.PreDestroy;
21 import javax.enterprise.inject.Instance;
22 import javax.inject.Inject;
23
24 import org.apache.camel.impl.DefaultCamelContext;
25 import org.apache.camel.spi.Injector;
26 import org.apache.camel.spi.Registry;
27 import org.apache.camel.util.ObjectHelper;
28
29 /**
30 * CDI {@link org.apache.camel.CamelContext} class.
31 */
32 public class CdiCamelContext extends DefaultCamelContext {
33
34 public CdiCamelContext() {
35 super(new CdiBeanRegistry());
36 setInjector(new CdiInjector(getInjector()));
37 }
38
39 @Inject
40 public void setRegistry(Instance<Registry> instance) {
41 if (isSingular(instance)) {
42 setRegistry(instance.get());
43 }
44 }
45
46 @Inject
47 public void setInjector(Instance<Injector> instance) {
48 if (isSingular(instance)) {
49 setInjector(instance.get());
50 }
51 }
52
53 private <T> boolean isSingular(Instance<T> instance) {
54 return !instance.isUnsatisfied() && !instance.isAmbiguous();
55 }
56
57 @PostConstruct
58 @Override
59 public void start() {
60 try {
61 super.start();
62 } catch (Exception e) {
63 ObjectHelper.wrapRuntimeCamelException(e);
64 }
65 }
66
67 @PreDestroy
68 @Override
69 public void stop() {
70 try {
71 super.stop();
72 } catch (Exception e) {
73 ObjectHelper.wrapRuntimeCamelException(e);
74 }
75 }
76
77 }

Copyright 2016, The Apache Software Foundation.