package gov.va.med.edp.rpc; import gov.va.med.vistalink.adapter.cci.VistaLinkConnectionFactory; import gov.va.med.vistalink.institution.InstitutionMapNotInitializedException; import gov.va.med.vistalink.institution.InstitutionMappingDelegate; import gov.va.med.vistalink.institution.InstitutionMappingNotFoundException; import org.springframework.dao.DataAccessResourceFailureException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.resource.cci.ConnectionFactory; import java.text.MessageFormat; public class InstitutionMappingConnectionFactoryLocator implements ConnectionFactoryLocator { private static final String NO_CONNECTION_FACTORY = "Unable to obtain connection factory for station ''{0}''"; private static final String NO_CONNECTION_FACTORY_JNDI = "Unable to obtain connection factory for station ''{0}'' at JNDI name ''{1}''"; public ConnectionFactory getConnectionFactory(String stationNumber) throws DataAccessResourceFailureException { try { String jndiConnectorName = InstitutionMappingDelegate.getJndiConnectorNameForInstitution( stationNumber); try { Context ic = new InitialContext(); VistaLinkConnectionFactory vistaLinkConnectionFactory = (VistaLinkConnectionFactory) ic.lookup(jndiConnectorName); if (vistaLinkConnectionFactory == null) throw new DataAccessResourceFailureException(MessageFormat.format(NO_CONNECTION_FACTORY_JNDI, new Object[]{stationNumber, jndiConnectorName})); return vistaLinkConnectionFactory; } catch (NamingException e) { throw new DataAccessResourceFailureException(MessageFormat.format(NO_CONNECTION_FACTORY_JNDI, new Object[]{stationNumber, jndiConnectorName}), e); } } catch (InstitutionMappingNotFoundException e) { throw new DataAccessResourceFailureException(MessageFormat.format(NO_CONNECTION_FACTORY, new Object[]{stationNumber}), e); } catch (InstitutionMapNotInitializedException e) { throw new DataAccessResourceFailureException(MessageFormat.format(NO_CONNECTION_FACTORY, new Object[]{stationNumber}), e); } } }