Skip to content

Commit be352fb

Browse files
author
Christoph Läubrich
committed
Support for the Data Service Specification
Add a first basic implementation of the DataSourceFactory
1 parent c7b54a2 commit be352fb

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@
149149
<instructions>
150150
<Bundle-SymbolicName>org.xerial.sqlite-jdbc;singleton:=true</Bundle-SymbolicName>
151151
<Import-Package>
152-
*;resolution:=optional
152+
*
153153
</Import-Package>
154+
<Bundle-Activator>org.sqlite.osgi.SQLiteActivator</Bundle-Activator>
154155
</instructions>
155156
</configuration>
156157
</plugin>
@@ -375,5 +376,18 @@
375376
<version>3.12.4</version>
376377
<scope>test</scope>
377378
</dependency>
379+
<!-- dependencies related to OSGi support -->
380+
<dependency>
381+
<groupId>org.osgi</groupId>
382+
<artifactId>org.osgi.service.jdbc</artifactId>
383+
<version>1.0.1</version>
384+
<scope>provided</scope>
385+
</dependency>
386+
<dependency>
387+
<groupId>org.osgi</groupId>
388+
<artifactId>org.osgi.framework</artifactId>
389+
<version>1.10.0</version>
390+
<scope>provided</scope>
391+
</dependency>
378392
</dependencies>
379393
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.sqlite.osgi;
2+
3+
import java.util.Hashtable;
4+
import org.osgi.framework.BundleActivator;
5+
import org.osgi.framework.BundleContext;
6+
import org.osgi.service.jdbc.DataSourceFactory;
7+
import org.sqlite.JDBC;
8+
import org.sqlite.SQLiteJDBCLoader;
9+
10+
public class SQLiteActivator implements BundleActivator {
11+
12+
@Override
13+
public void start(BundleContext context) throws Exception {
14+
Hashtable<String, Object> properties = new Hashtable<>();
15+
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, JDBC.class.getName());
16+
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, "SQLite JDBC driver");
17+
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION, SQLiteJDBCLoader.getVersion());
18+
context.registerService(DataSourceFactory.class, new SQLiteDataSourceFactory(), properties);
19+
}
20+
21+
@Override
22+
public void stop(BundleContext context) throws Exception {}
23+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
import javax.sql.ConnectionPoolDataSource;
8+
import javax.sql.DataSource;
9+
import javax.sql.XADataSource;
10+
import org.osgi.service.jdbc.DataSourceFactory;
11+
import org.sqlite.JDBC;
12+
import org.sqlite.SQLiteConfig;
13+
import org.sqlite.SQLiteDataSource;
14+
import org.sqlite.javax.SQLiteConnectionPoolDataSource;
15+
16+
public class SQLiteDataSourceFactory implements DataSourceFactory {
17+
18+
@Override
19+
public DataSource createDataSource(Properties props) throws SQLException {
20+
SQLiteDataSource dataSource = new SQLiteDataSource(getConfig(props));
21+
setBasicDataSourceProperties(props, dataSource);
22+
return dataSource;
23+
}
24+
25+
@Override
26+
public ConnectionPoolDataSource createConnectionPoolDataSource(Properties props)
27+
throws SQLException {
28+
29+
SQLiteConnectionPoolDataSource poolDataSource =
30+
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(
53+
Properties props, String key, Consumer<String> consumer) {
54+
String value = props.getProperty(key);
55+
if (value != null) {
56+
consumer.accept(value);
57+
}
58+
}
59+
60+
/**
61+
* Set basic properties common to {@link SQLiteDataSource}s
62+
*
63+
* @param props
64+
* @param dataSource
65+
*/
66+
private static void setBasicDataSourceProperties(
67+
Properties props, SQLiteDataSource dataSource) {
68+
if (props != null) {
69+
setStandardProperty(
70+
props, DataSourceFactory.JDBC_DATABASE_NAME, dataSource::setDatabaseName);
71+
setStandardProperty(props, DataSourceFactory.JDBC_URL, dataSource::setUrl);
72+
}
73+
}
74+
75+
/**
76+
* converts user supplied properties into an internal {@link SQLiteConfig} object
77+
*
78+
* @param userProperties the user properties, might be <code>null</code>
79+
* @return a {@link SQLiteConfig} config object reflecting the given user properties
80+
*/
81+
private static SQLiteConfig getConfig(Properties userProperties) {
82+
SQLiteConfig config;
83+
if (userProperties == null) {
84+
config = new SQLiteConfig();
85+
} else {
86+
Properties properties = new Properties(userProperties);
87+
setStandardProperty(
88+
userProperties,
89+
DataSourceFactory.JDBC_USER,
90+
v -> properties.setProperty("user", v));
91+
setStandardProperty(
92+
userProperties,
93+
DataSourceFactory.JDBC_PASSWORD,
94+
v -> properties.setProperty("pass", v));
95+
config = new SQLiteConfig(properties);
96+
}
97+
return config;
98+
}
99+
}

0 commit comments

Comments
 (0)