View Javadoc

1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo.jdbc;
22  
23  import org.synchronoss.cpo.*;
24  
25  import javax.sql.DataSource;
26  import java.util.*;
27  
28  /**
29   *
30   * @author dberry
31   */
32  public abstract class AbstractDataSourceInfo implements DataSourceInfo {
33    private DataSource dataSource = null;
34    private String dataSourceName = null;
35    // Make sure DataSource creation is thread safe.
36    private final Object LOCK = new Object();
37    
38    // common password strings
39    private final String PASSWORD = "password";
40    private final String PASSWD = "passwd";
41    private final String PWD = "pwd";
42  
43    public AbstractDataSourceInfo(String dataSourceName) {
44      this.dataSourceName=dataSourceName;
45    }
46    
47    public AbstractDataSourceInfo(String className, SortedMap<String, String> properties) {
48      this.dataSourceName=BuildDataSourceName(className, properties);
49    }
50  
51    public AbstractDataSourceInfo(String className, Properties properties) {
52      this.dataSourceName=BuildDataSourceName(className, properties);
53    }
54  
55    protected abstract DataSource createDataSource() throws CpoException ;
56    
57    @Override
58    public String getDataSourceName() {
59      return dataSourceName;
60    }
61  
62    @Override
63    public DataSource getDataSource() throws CpoException {
64      if (dataSource == null) {
65        synchronized (LOCK) {
66          try {
67            dataSource = createDataSource();
68          } catch (Exception e) {
69            throw new CpoException("Error instantiating DataSource", e);
70          }
71        }
72      }
73  
74      return dataSource;
75    }
76    
77    private String BuildDataSourceName(String s, Properties properties) {
78      // Use a tree map so that the properties are sorted. This way if we have
79      // the same datasource with the same properties but in different order,
80      // we will generate the same key.
81      SortedMap<String, String> map = new TreeMap<String, String>();
82      for (Object key : properties.keySet()){
83        map.put((String)key, properties.getProperty((String)key));
84      }
85      return BuildDataSourceName(s, map);
86    }
87    
88    private String BuildDataSourceName(String s, SortedMap<String, String> map) {
89      StringBuilder dsName = new StringBuilder(s);
90  
91      for (Object obj : map.keySet()) {
92        String key = (String)obj;
93        // Don't store the password in the datasource generated name.
94        if (!PASSWORD.equalsIgnoreCase(key) && !PASSWD.equalsIgnoreCase(key) && !PWD.equalsIgnoreCase(key) ) {
95          dsName.append(key);
96          dsName.append("=");
97          dsName.append(map.get(key));
98        }
99      }
100 
101     return dsName.toString();
102   }
103 
104 }