(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
26  * once. For test code, make sure to call
27  * {@link #setInjectorFactory(InjectorFactory)} before each test case to force
28  * the retrieval of a new factory and injector. This is important because if the
29  * simple injector is not created again it will use cached {@link Injector}
30  * instances from other tests.
31  * 
32  * @author Erik Brakkee
33  */
34 public class InjectorBuilder {
35
36     private static InjectorFactory FACTORY;
37
38     private static SimpleInjector INJECTOR;
39
40     /**
41      * Sets the injector factory. This is useful for testing.
42      * 
43      * @param aFactory
44      *            Factory to use.
45      */
46     public static void setInjectorFactory(InjectorFactory aFactory) {
47         FACTORY = aFactory;
48         INJECTOR = new SimpleInjector(aFactory);
49     }
50
51     /**
52      * Gets the injector factory by using the first one found using
53      * {@link ServiceLoader}.
54      * 
55      * @return InjectorFactory.
56      */
57     public static InjectorFactory getInjectorFactory() {
58         if (FACTORY == null) {
59             FACTORY = findInjectorFactory();
60             INJECTOR = new SimpleInjector(FACTORY);
61         }
62         return FACTORY;
63     }
64
65     /**
66      * Gets an injector that support injection into any type of object and
67      * performs caching of the injector obtained from the
68      * {@link InjectorFactory}.
69      * 
70      * @return Injector.
71      */
72     public static Injector getInjector() {
73         getInjectorFactory();
74         return INJECTOR;
75     }
76
77     /**
78      * Finds the injector factory musing <code>ServiceLoader</code>
79      * 
80      * @return InjectorFactory.
81      */
82     private static InjectorFactory findInjectorFactory() {
83         ServiceLoader<InjectorFactory> factories = ServiceLoader
84             .load(InjectorFactory.class);
85         try {
86             return (InjectorFactory) factories.iterator().next();
87         } catch (NoSuchElementException e) {
88             throw new RuntimeException("Can not find InjectorFactory to use");
89         }
90     }
91 }