View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.data.platform;
17  
18  import org.springframework.dao.DataAccessException;
19  import org.springframework.jdbc.core.ConnectionCallback;
20  import org.springframework.jdbc.core.JdbcTemplate;
21  
22  import javax.sql.DataSource;
23  import java.sql.Connection;
24  import java.sql.DatabaseMetaData;
25  import java.sql.SQLException;
26  import java.util.Collections;
27  import java.util.IdentityHashMap;
28  import java.util.Map;
29  import java.util.concurrent.ConcurrentHashMap;
30  import java.util.concurrent.ConcurrentMap;
31  
32  /**
33   * Contains constants and utilities related to the supported database platforms.
34   *
35   * <p>
36   * We use a String to represent the platform name as opposed to an Enum because this allows for the potential to
37   * configure and use custom platforms at runtime without requiring internal code modification to support a new platform.
38   * </p>
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public final class DatabasePlatforms {
43  
44      /**
45       * The name of the Oracle platform.
46       */
47      public static final String ORACLE = "Oracle";
48  
49      /**
50       * The name of the MySQL platform.
51       */
52      public static final String MYSQL = "MySQL";
53  
54      private static final Map<DataSource, DatabasePlatformInfo> platformCache
55              = Collections.synchronizedMap(new IdentityHashMap<DataSource, DatabasePlatformInfo>(8));
56  
57      /**
58       * Gets the platform information from the {@link DataSource}.
59       *
60       * @param dataSource the {@link DataSource} to consult.
61       * @return the platform information from the {@link DataSource}.
62       */
63      public static DatabasePlatformInfo detectPlatform(DataSource dataSource) {
64          if (dataSource == null) {
65              throw new IllegalArgumentException("DataSource must not be null.");
66          }
67          DatabasePlatformInfo platformInfo = platformCache.get(dataSource);
68          if (platformInfo == null) {
69              JdbcTemplate template = new JdbcTemplate(dataSource);
70                  platformCache.put(dataSource, template.execute(new ConnectionCallback<DatabasePlatformInfo>() {
71                              @Override
72                              public DatabasePlatformInfo doInConnection(
73                                      Connection connection) throws SQLException, DataAccessException {
74                                  DatabaseMetaData metadata = connection.getMetaData();
75                                  String vendorName = metadata.getDatabaseProductName();
76                                  int version = metadata.getDatabaseMajorVersion();
77                                  return new DatabasePlatformInfo(vendorName, version);
78                              }
79                      }));
80              if (platformInfo == null) {
81                  platformInfo = platformCache.get(dataSource);
82              }
83          }
84          return platformInfo;
85      }
86  
87      /**
88       * No-op constructor for final class.
89       */
90      private DatabasePlatforms() {}
91  
92  }