de13ef54df162641c6552350a1508e25cef209ce
[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 import org.wamblee.inject.Injector;
23 import org.wamblee.inject.InjectorFactory;
24
25 /**
26  * Factory that creates CDI injectors. This class may be subclassed for testing to
27  * override the injectors that are returned.
28  * 
29  * @author Erik Brakkee
30  */
31 public class CdiInjectorFactory implements InjectorFactory {
32
33     private static final Logger LOGGER = Logger
34         .getLogger(CdiInjectorFactory.class.getName());
35
36     private BeanManager beanManager;
37     
38     /**
39      * Constructs the factory using a default bean manager. 
40      * 
41      * @throws IllegalArgumentException If bean manager is null. 
42      */
43     public CdiInjectorFactory() {
44         this(BeanManagerLookup.lookup());
45     }
46
47     /**
48      * Constructs the factory using an explicit bean manager.
49      * 
50      * @param aBeanManager
51      * @throws IllegalArgumentException If bean manager is null. 
52      */
53     public CdiInjectorFactory(BeanManager aBeanManager) {
54         if ( aBeanManager == null ) { 
55             throw new IllegalArgumentException("Bean manager is null");
56         }
57         beanManager = aBeanManager;
58     }
59
60     @Override
61     public Injector create(Class aClass) {
62         return new CdiInjector(beanManager, aClass);
63     }
64
65 }