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.AbstractDataSourceInfo;
24  
25  import java.util.*;
26  
27  /**
28   *
29   * @author dberry
30   */
31  public abstract class AbstractJdbcDataSourceInfo extends AbstractDataSourceInfo {
32    // common password strings
33    private static final String PASSWORD = "password";
34    private static final String PASSWD = "passwd";
35    private static final String PWD = "pwd";
36  
37    public AbstractJdbcDataSourceInfo(String dataSourceName) {
38      super(dataSourceName);
39    }
40  
41    public AbstractJdbcDataSourceInfo(String className, SortedMap<String, String> properties) {
42      super(BuildDataSourceName(className, properties));
43    }
44  
45    public AbstractJdbcDataSourceInfo(String className, Properties properties) {
46      super(BuildDataSourceName(className, properties));
47    }
48  
49    private static String BuildDataSourceName(String s, Properties properties) {
50      // Use a tree map so that the properties are sorted. This way if we have
51      // the same datasource with the same properties but in different order,
52      // we will generate the same key.
53      SortedMap<String, String> map = new TreeMap<>();
54      for (Object key : properties.keySet()){
55        map.put((String)key, properties.getProperty((String)key));
56      }
57      return BuildDataSourceName(s, map);
58    }
59  
60    private static String BuildDataSourceName(String s, SortedMap<String, String> map) {
61      StringBuilder dsName = new StringBuilder(s);
62  
63      for (Object obj : map.keySet()) {
64        String key = (String)obj;
65        // Don't store the password in the datasource generated name.
66        if (!PASSWORD.equalsIgnoreCase(key) && !PASSWD.equalsIgnoreCase(key) && !PWD.equalsIgnoreCase(key) ) {
67          dsName.append(key);
68          dsName.append("=");
69          dsName.append(map.get(key));
70        }
71      }
72  
73      return dsName.toString();
74    }
75  
76  }