View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.jumpmind.symmetric.db;
22  
23  import javax.sql.DataSource;
24  
25  import org.springframework.beans.factory.BeanFactory;
26  import org.springframework.beans.factory.BeanFactoryAware;
27  import org.springframework.beans.factory.FactoryBean;
28  
29  /***
30   * Factory that will create either a jndiDataSource or a basicDataSource based
31   * on whether a JNDI name is provided.
32   */
33  public class DataSourceFactoryBean implements FactoryBean, BeanFactoryAware {
34  
35      private String jndiName;
36  
37      private String beanName;
38  
39      private BeanFactory beanFactory;
40  
41      public Object getObject() throws Exception {
42          if (jndiName == null || jndiName.trim().length() == 0) {
43              return beanFactory.getBean(beanName);
44          } else {
45              return beanFactory.getBean("jndiDataSource");
46          }
47      }
48  
49      public Class<DataSource> getObjectType() {
50          return DataSource.class;
51      }
52  
53      public boolean isSingleton() {
54          return true;
55      }
56  
57      public void setBeanFactory(BeanFactory beanFactory) {
58          this.beanFactory = beanFactory;
59      }
60  
61      public void setJndiName(String jndiName) {
62          this.jndiName = jndiName;
63      }
64  
65      public void setBeanName(String beanName) {
66          this.beanName = beanName;
67      }
68  
69  }