Skip to content

Commit 5ac503f

Browse files
Christoph Läubrichlaeubi
Christoph Läubrich
authored andcommitted
Support for the Data Service Specification
Add a first basic implementation of the DataSourceFactory
1 parent 49facf5 commit 5ac503f

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

pom.xml

+15-1
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@
161161
<instructions>
162162
<Bundle-SymbolicName>org.xerial.sqlite-jdbc;singleton:=true</Bundle-SymbolicName>
163163
<Import-Package>
164-
*;resolution:=optional
164+
*
165165
</Import-Package>
166+
<Bundle-Activator>org.sqlite.osgi.SQLiteActivator</Bundle-Activator>
166167
</instructions>
167168
</configuration>
168169
</plugin>
@@ -339,5 +340,18 @@
339340
<version>3.12.4</version>
340341
<scope>test</scope>
341342
</dependency>
343+
<!-- dependencies related to OSGi support -->
344+
<dependency>
345+
<groupId>org.osgi</groupId>
346+
<artifactId>org.osgi.service.jdbc</artifactId>
347+
<version>1.0.1</version>
348+
<scope>provided</scope>
349+
</dependency>
350+
<dependency>
351+
<groupId>org.osgi</groupId>
352+
<artifactId>org.osgi.framework</artifactId>
353+
<version>1.10.0</version>
354+
<scope>provided</scope>
355+
</dependency>
342356
</dependencies>
343357
</project>
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+
}
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)