(no commit message)
[utils] / support / inject / src / main / java / org / wamblee / inject / InjectorBuilder.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 InjectorBuilder {
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     /**
63      * Gets an injector that support injection into any type of object and 
64      * performs caching of the injector obtained from the {@link InjectorFactory}.
65      * @return Injector.
66      */
67     public static Injector getInjector() { 
68         getInjectorFactory();
69         return INJECTOR;
70     }
71
72     /**
73      * Finds the injector factory musing <code>ServiceLoader</code>
74      * 
75      * @return InjectorFactory.
76      */
77     private static InjectorFactory findInjectorFactory() {
78         ServiceLoader<InjectorFactory> factories = ServiceLoader
79             .load(InjectorFactory.class);
80         try {
81             return (InjectorFactory) factories.iterator().next();
82         } catch (NoSuchElementException e) {
83             throw new RuntimeException("Can not find InjectorFactory to use");
84         }
85     }
86 }