ca.infoway.messagebuilder.service
Class ServiceFactory<T>

Package class diagram package ServiceFactory
java.lang.Object
  extended by ca.infoway.messagebuilder.service.ServiceFactory<T>
Type Parameters:
T - - the interface type of the service
Direct Known Subclasses:
MessageDefinitionServiceFactory

public abstract class ServiceFactory<T>
extends Object

A common Java practice for allowing configurable implementations of a service interface is to look for a file in the META-INF directory of a jar. An example of this problem is XML parsing. There's an abstract factory type called javax.xml.parsers.DocumentBuilderFactory, and the expectation is that some component will provide a concrete implementation of this type.

The Java way of handling that is to look for a file in the META-INF directory called /META-INF/javax.xml.parsers.DocumentBuilderFactory

The file is a simple text file that contains the fully-qualified name of an implementing class, and Java can decide to pick one (or many) of those implementations.

This class implements behaviour like that -- it looks for a set of resources on the classpath, named after a specific type, and finds the corresponding classes.

Author:
Intelliware Development

Constructor Summary
ServiceFactory(Class<T> c)
          Standard constructor.
 
Method Summary
protected abstract  T chooseImplementationOfService(List<T> services)
          Choose an implementation of a service, based on several options.
 T create()
          Create an instance of the service.
protected abstract  T createTrivialService()
          Create a trivial service if no other option exists.
protected  List<T> getServices(Enumeration<URL> resources)
          Find all implementations of the message definition service.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServiceFactory

public ServiceFactory(Class<T> c)

Standard constructor.

Parameters:
c - - the type of service that the factory finds
Method Detail

create

public T create()

Create an instance of the service.

Returns:
- the instance of the service.

chooseImplementationOfService

protected abstract T chooseImplementationOfService(List<T> services)

Choose an implementation of a service, based on several options.

Returns:
- the instance of the service.

createTrivialService

protected abstract T createTrivialService()

Create a trivial service if no other option exists.

Returns:
- the trivial service.

getServices

protected List<T> getServices(Enumeration<URL> resources)
                       throws IOException

Find all implementations of the message definition service.

Parameters:
resources - - an enumeration of all the URLs to service files
Returns:
- a list of instances that implement the service interface.
Throws:
IOException - - if any resource cannot be accessed.


Copyright © 2013. All Rights Reserved.