| 1 | package gov.hhs.fha.nhinc.subscription.repository.service;
|
|---|
| 2 |
|
|---|
| 3 | import gov.hhs.fha.nhinc.properties.PropertyAccessor;
|
|---|
| 4 | import gov.hhs.fha.nhinc.subscription.repository.SubscriptionRepositoryException;
|
|---|
| 5 | import org.apache.commons.logging.Log;
|
|---|
| 6 | import org.apache.commons.logging.LogFactory;
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * Factory for getting an instance of the subscription repository service
|
|---|
| 10 | *
|
|---|
| 11 | * @author Neil Webb
|
|---|
| 12 | */
|
|---|
| 13 | public class SubscriptionRepositoryFactory
|
|---|
| 14 | {
|
|---|
| 15 | private static Log log = LogFactory.getLog(SubscriptionRepositoryFactory.class);
|
|---|
| 16 | private static final String PROPERTIES_FILE_NAME = "gateway";
|
|---|
| 17 | private static final String IMPL_CLASS_NAME_KEY = "subscription.repository.implementation.class";
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Create a subscription repository service object. The implementation class
|
|---|
| 21 | * for the subscription repository service is defined in the
|
|---|
| 22 | * gateway.properties file using the key:
|
|---|
| 23 | * "subscription.repository.implementation.class"
|
|---|
| 24 | *
|
|---|
| 25 | * @return Subscription repository service instance.
|
|---|
| 26 | * @throws gov.hhs.fha.nhinc.subscription.repository.SubscriptionRepositoryException
|
|---|
| 27 | */
|
|---|
| 28 | public SubscriptionRepositoryService getSubscriptionRepositoryService() throws SubscriptionRepositoryException
|
|---|
| 29 | {
|
|---|
| 30 | SubscriptionRepositoryService repositoryService = null;
|
|---|
| 31 | String errorMessage = "Unknown error creating subscription repository service";
|
|---|
| 32 |
|
|---|
| 33 | String implClassName = null;
|
|---|
| 34 | try
|
|---|
| 35 | {
|
|---|
| 36 | log.debug("Retrieving the subscription repository class name");
|
|---|
| 37 | implClassName = PropertyAccessor.getProperty(PROPERTIES_FILE_NAME, IMPL_CLASS_NAME_KEY);
|
|---|
| 38 | log.debug("Retrieved the subscription repository class name: " + implClassName);
|
|---|
| 39 | }
|
|---|
| 40 | catch (Throwable t)
|
|---|
| 41 | {
|
|---|
| 42 | errorMessage = "An error occured locating the implementation class " +
|
|---|
| 43 | "for the subscription repository service. Please ensure " +
|
|---|
| 44 | "that the implementation class is defined in the " +
|
|---|
| 45 | "gateway.properties file using the key: " +
|
|---|
| 46 | "\"subscription.repository.implementation.class\". The " +
|
|---|
| 47 | "error message was: " + t.getMessage();
|
|---|
| 48 | log.error("Error retrieving the subscription implementaion class name: " + t.getMessage(), t);
|
|---|
| 49 | }
|
|---|
| 50 | if ((implClassName == null) || ("".equals(implClassName.trim())))
|
|---|
| 51 | {
|
|---|
| 52 | if (errorMessage == null)
|
|---|
| 53 | {
|
|---|
| 54 | errorMessage = "Unable to locate the implementation class " +
|
|---|
| 55 | "for the subscription repository service. Please ensure " +
|
|---|
| 56 | "that the implementation class is defined in the " +
|
|---|
| 57 | "gateway.properties file using the key: " +
|
|---|
| 58 | "\"subscription.repository.implementation.class\".";
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 | else
|
|---|
| 62 | {
|
|---|
| 63 | try
|
|---|
| 64 | {
|
|---|
| 65 | log.debug("Instantiating the subscription repository service using the class name: " + implClassName);
|
|---|
| 66 | repositoryService = (SubscriptionRepositoryService) Class.forName(implClassName).newInstance();
|
|---|
| 67 | }
|
|---|
| 68 | catch (Throwable t)
|
|---|
| 69 | {
|
|---|
| 70 | errorMessage = "Unable to instantiate the implementation class " +
|
|---|
| 71 | "for the subscription repository service. The class " +
|
|---|
| 72 | "name provided was: " + implClassName +
|
|---|
| 73 | ". Please ensure that this class exists and is " +
|
|---|
| 74 | "accessable. The exception was: " + t.getMessage();
|
|---|
| 75 |
|
|---|
| 76 | log.error("Error instantiating the subscription implementaion class: " + t.getMessage(), t);
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | if (repositoryService == null)
|
|---|
| 81 | {
|
|---|
| 82 | log.error(errorMessage);
|
|---|
| 83 | throw new SubscriptionRepositoryException(errorMessage);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | return repositoryService;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|