(no commit message)
[utils] / support / inject / src / main / java / org / wamblee / inject / InjectorFactoryBuilder.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.inject;
17
18 import java.util.NoSuchElementException;
19 import java.util.ServiceLoader;
20
21 /**
22  * Utility for obtaining an implementation of the {@link InjectorFactory} using
23  * {@link ServiceLoader} and for obtaining a {@link SimpleInjector}. 
24  * 
25  * The builder takes care that the factory and simple injector are built only once. 
26  * For test code, make sure to call {@link #setInjectorFactory(InjectorFactory)}
27  * before each test case to force the retrieval of a new factory and injector. This 
28  * is important because if the simple injector is not created again it will use 
29  * cached {@link Injector} instances from other tests. 
30  * 
31  * @author Erik Brakkee
32  */
33 public class InjectorFactoryBuilder {
34
35     private static InjectorFactory FACTORY;
36     
37     private static SimpleInjector INJECTOR; 
38
39     /**
40      * Sets the injector factory. This is useful for testing. 
41      * @param aFactory Factory to use. 
42      */
43     public static void setInjectorFactory(InjectorFactory aFactory) {
44         FACTORY = aFactory;
45         INJECTOR = new SimpleInjector(aFactory);
46     }
47
48     /**
49      * Gets the injector factory by using the first one found using
50      * {@link ServiceLoader}.
51      * 
52      * @return InjectorFactory.
53      */
54     public static InjectorFactory getInjectorFactory() {
55         if (FACTORY == null) {
56             FACTORY = findInjectorFactory(); 
57             INJECTOR = new SimpleInjector(FACTORY);
58         }
59         return FACTORY;
60     }
61     
62     public static SimpleInjector getInjector() { 
63         getInjectorFactory();
64         return INJECTOR;
65     }
66
67     /**
68      * Finds the injector factory musing <code>ServiceLoader</code>
69      * 
70      * @return InjectorFactory.
71      */
72     private static InjectorFactory findInjectorFactory() {
73         ServiceLoader<InjectorFactory> factories = ServiceLoader
74             .load(InjectorFactory.class);
75         try {
76             return (InjectorFactory) factories.iterator().next();
77         } catch (NoSuchElementException e) {
78             throw new RuntimeException("Can not find InjectorFactory to use");
79         }
80     }
81 }