|
| 1 | +package org.sqlite.osgi; |
| 2 | + |
| 3 | +import java.sql.Driver; |
| 4 | +import java.sql.SQLException; |
| 5 | +import java.util.Properties; |
| 6 | +import java.util.function.Consumer; |
| 7 | + |
| 8 | +import javax.sql.ConnectionPoolDataSource; |
| 9 | +import javax.sql.DataSource; |
| 10 | +import javax.sql.XADataSource; |
| 11 | + |
| 12 | +import org.osgi.service.jdbc.DataSourceFactory; |
| 13 | +import org.sqlite.JDBC; |
| 14 | +import org.sqlite.SQLiteConfig; |
| 15 | +import org.sqlite.SQLiteDataSource; |
| 16 | +import org.sqlite.javax.SQLiteConnectionPoolDataSource; |
| 17 | + |
| 18 | +public class SQLiteDataSourceFactory implements DataSourceFactory { |
| 19 | + |
| 20 | + @Override |
| 21 | + public DataSource createDataSource(Properties props) throws SQLException { |
| 22 | + SQLiteDataSource dataSource = new SQLiteDataSource(getConfig(props)); |
| 23 | + setBasicDataSourceProperties(props, dataSource); |
| 24 | + return dataSource; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public ConnectionPoolDataSource createConnectionPoolDataSource(Properties props) throws SQLException { |
| 29 | + |
| 30 | + SQLiteConnectionPoolDataSource poolDataSource = new SQLiteConnectionPoolDataSource(getConfig(props)); |
| 31 | + setBasicDataSourceProperties(props, poolDataSource); |
| 32 | + return poolDataSource; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public XADataSource createXADataSource(Properties props) throws SQLException { |
| 37 | + throw new SQLException("XADataSource is not supported by SQLite"); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Driver createDriver(Properties props) throws SQLException { |
| 42 | + return new JDBC(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Method to transfer a property to a setter method |
| 47 | + * |
| 48 | + * @param props |
| 49 | + * @param key |
| 50 | + * @param consumer |
| 51 | + */ |
| 52 | + private static void setStandardProperty(Properties props, String key, Consumer<String> consumer) { |
| 53 | + String value = props.getProperty(key); |
| 54 | + if (value != null) { |
| 55 | + consumer.accept(value); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Set basic properties common to {@link SQLiteDataSource}s |
| 61 | + * |
| 62 | + * @param props |
| 63 | + * @param dataSource |
| 64 | + */ |
| 65 | + private static void setBasicDataSourceProperties(Properties props, SQLiteDataSource dataSource) { |
| 66 | + if (props != null) { |
| 67 | + setStandardProperty(props, DataSourceFactory.JDBC_DATABASE_NAME, dataSource::setDatabaseName); |
| 68 | + setStandardProperty(props, DataSourceFactory.JDBC_URL, dataSource::setUrl); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * converts user supplied properties into an internal {@link SQLiteConfig} |
| 74 | + * object |
| 75 | + * |
| 76 | + * @param userProperties the user properties, might be <code>null</code> |
| 77 | + * @return a {@link SQLiteConfig} config object reflecting the given user |
| 78 | + * properties |
| 79 | + */ |
| 80 | + private static SQLiteConfig getConfig(Properties userProperties) { |
| 81 | + SQLiteConfig config; |
| 82 | + if (userProperties == null) { |
| 83 | + config = new SQLiteConfig(); |
| 84 | + } else { |
| 85 | + Properties properties = new Properties(userProperties); |
| 86 | + setStandardProperty(userProperties, DataSourceFactory.JDBC_USER, v -> properties.setProperty("user", v)); |
| 87 | + setStandardProperty(userProperties, DataSourceFactory.JDBC_PASSWORD, |
| 88 | + v -> properties.setProperty("pass", v)); |
| 89 | + config = new SQLiteConfig(properties); |
| 90 | + } |
| 91 | + return config; |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments