cdi project in initial revision
[utils] / support / cdi / src / main / java / org / wamblee / cdi / CdiInjectorFactory.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.cdi;
17
18 import java.util.logging.Logger;
19
20 import javax.enterprise.inject.spi.BeanManager;
21
22 /**
23  * Factory that creates CDI injectors. In case no beanmanager is found then
24  * injectors will do nothing. This class may be subclassed for testing to
25  * override the injectors that are returned.
26  * 
27  * @author Erik Brakkee
28  */
29 public class CdiInjectorFactory implements InjectorFactory {
30
31     private static final Logger LOGGER = Logger
32         .getLogger(CdiInjectorFactory.class.getName());
33
34     private BeanManager beanManager;
35
36     /**
37      * Constructs the factory using the default bean manager.
38      */
39     public CdiInjectorFactory() {
40         this(BeanManagerLookup.lookup());
41     }
42
43     /**
44      * Constructs the factory using an explicit bean manager.
45      * 
46      * @param aBeanManager
47      */
48     public CdiInjectorFactory(BeanManager aBeanManager) {
49         beanManager = aBeanManager;
50     }
51
52     @Override
53     public Injector create(Class aClass) {
54         if (beanManager == null) {
55             // Typically for unit test.
56             return new Injector() {
57                 @Override
58                 public void inject(Object aComponent) {
59                     // Empty.
60                 }
61             };
62         }
63         return new CdiInjector(beanManager, aClass);
64     }
65
66 }