com.iparelan.util
Class FactoryLoader

java.lang.Object
  extended by com.iparelan.util.UtilObject
      extended by com.iparelan.util.FactoryLoader

@Copyright(value="Copyright © 2008, Iparelan Solutions, LLC. All rights reserved.")
public final class FactoryLoader
extends UtilObject

A Service Locator that loads an AbstractFactory.

Version:
June 2008
Author:
Greg Mattes
See Also:
"Inversion of Control Containers and the Dependency Injection Pattern," Martin Fowler

Field Summary
 
Fields inherited from class com.iparelan.util.UtilObject
LOGGER, MSGS, UTILITIES_LOGGER_NAME
 
Constructor Summary
private FactoryLoader()
          Not instantiable.
 
Method Summary
private static Logger getLogger()
          Produces a viable logger.
static
<T extends AbstractFactory>
T
load(Class<T> factoryService)
          Produces an AbstractFactory as configured in some file of the META-INF/services directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FactoryLoader

private FactoryLoader()
Not instantiable.

Method Detail

load

public static <T extends AbstractFactory> T load(Class<T> factoryService)
Produces an AbstractFactory as configured in some file of the META-INF/services directory. The file name is the ClassLoader.html binary name of factoryService.

Implementation Note: Due to the details of the ServiceLoader class, several errors may be raised in the computation of this method. The policy of this implementation is to log all of the errors to the Configuration logger, then to raise only the most recent error to callers.

Implementation Note: Several service providers may be registered for any particular service as described by ServiceLoader. The policy of this implementation is to produce the first viable service discovered.

Parameters:
factoryService - The type of factory service to load. May not be null.
Returns:
A AbstractFactory. Never null.
Throws:
ServiceConfigurationError - Iff a service provider cannot be loaded.
NullPointerException - Iff factoryService is null.
See Also:
ServiceLoader.iterator()
To Do:
  • Currently this implementation is constrained to loading AbstractFactory, but it really could be more general.
  • Security consideration: ServiceLoader.loadInstalled(Class) instead of ServiceLoader.load(Class). The issue is whether the "extension class loader" or the "system class loader" should be used to get the service provider. The extension class loader apparently avoids the application's classpath, which might be altered by a malicious user. Formalling install the service as an extension and locking down the JVM (as on a CD-ROM or other read-only storage) might be preferable.

getLogger

private static Logger getLogger()
Produces a viable logger. First attempts to acquire the "proper" logger for the factory loader. If the proper logger is not available, presumably because the logging service itself is being loaded, then a naive logger is acquired.

Returns:
A logger. Never null.
Throws:
AssertionError - Iff a viable logger cannot be acquired.


Copyright © 2008 Iparelan Solutions, LLC. All rights reserved.