1 | package gov.va.med.edp.vistalink.locator;
|
---|
2 |
|
---|
3 | import org.springframework.beans.BeansException;
|
---|
4 | import org.springframework.beans.factory.BeanFactory;
|
---|
5 | import org.springframework.beans.factory.BeanFactoryAware;
|
---|
6 | import org.springframework.dao.DataAccessResourceFailureException;
|
---|
7 | import org.springframework.util.StringUtils;
|
---|
8 |
|
---|
9 | import javax.resource.cci.ConnectionFactory;
|
---|
10 | import java.text.MessageFormat;
|
---|
11 |
|
---|
12 | import gov.va.med.edp.vistalink.ConnectionFactoryLocator;
|
---|
13 |
|
---|
14 | public class BeanFactoryConnectionFactoryLocator implements ConnectionFactoryLocator, BeanFactoryAware {
|
---|
15 |
|
---|
16 | private static final String NO_CONNECTION_FACTORY = "Unable to obtain connection factory for station ''{0}''";
|
---|
17 |
|
---|
18 | private BeanFactory beanFactory;
|
---|
19 | private String prefix;
|
---|
20 | private String suffix;
|
---|
21 |
|
---|
22 | public ConnectionFactory getConnectionFactory(String stationNumber) throws DataAccessResourceFailureException {
|
---|
23 | try {
|
---|
24 | ConnectionFactory connectionFactory = (ConnectionFactory) beanFactory.getBean(getBeanName(stationNumber), ConnectionFactory.class);
|
---|
25 | return connectionFactory;
|
---|
26 | } catch (BeansException e) {
|
---|
27 |
|
---|
28 | throw new DataAccessResourceFailureException(MessageFormat.format(NO_CONNECTION_FACTORY, new Object[] {stationNumber}), e);
|
---|
29 | // throw new DataAccessResourceFailureException(MessageFormat.format(NO_CONNECTION_FACTORY, stationNumber), e); // TODO: use this one for java 5
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | private String getBeanName(String stationNumber) {
|
---|
34 | // StringBuilder builder = new StringBuilder(); // TODO: switch to StringBuilder in java 5
|
---|
35 | StringBuffer builder = new StringBuffer();
|
---|
36 | if (StringUtils.hasText(getPrefix())) {
|
---|
37 | builder.append(getPrefix());
|
---|
38 | }
|
---|
39 | builder.append(stationNumber);
|
---|
40 | if (StringUtils.hasText(getSuffix())) {
|
---|
41 | builder.append(getSuffix());
|
---|
42 | }
|
---|
43 | return builder.toString();
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
---|
47 | this.beanFactory = beanFactory;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public String getPrefix() {
|
---|
51 | return prefix;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void setPrefix(String prefix) {
|
---|
55 | this.prefix = prefix;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public String getSuffix() {
|
---|
59 | return suffix;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void setSuffix(String suffix) {
|
---|
63 | this.suffix = suffix;
|
---|
64 | }
|
---|
65 | }
|
---|