additions for testability.
[utils] / support / inject / src / main / java / org / wamblee / inject / InjectorFactoryBuilder.java
index d8190919db6a0ca2f2e0163b541521b3edf55a04..13db8412d886a54474b808aa329ff69792aa7327 100644 (file)
@@ -26,13 +26,35 @@ import java.util.ServiceLoader;
  */
 public class InjectorFactoryBuilder {
 
+    private static InjectorFactory FACTORY;
+
+    /**
+     * Sets the injector factory. This is useful for testing. 
+     * @param aFactory Factory to use. 
+     */
+    public static void setInjectorFactory(InjectorFactory aFactory) {
+        FACTORY = aFactory;
+    }
+
     /**
-     * Gets the injector factory by using the first one found using 
-     * {@link ServiceLoader}. 
+     * Gets the injector factory by using the first one found using
+     * {@link ServiceLoader}.
      * 
-     * @return InjectorFactory. 
+     * @return InjectorFactory.
      */
     public static InjectorFactory getInjectorFactory() {
+        if (FACTORY == null) {
+            FACTORY = findInjectorFactory(); 
+        }
+        return FACTORY;
+    }
+
+    /**
+     * Finds the injector factory musing <code>ServiceLoader</code>
+     * 
+     * @return InjectorFactory.
+     */
+    private static InjectorFactory findInjectorFactory() {
         ServiceLoader<InjectorFactory> factories = ServiceLoader
             .load(InjectorFactory.class);
         try {