Skip to content

Commit 7377e27

Browse files
Christoph Läubrichgotson
Christoph Läubrich
authored andcommitted
Support for the Data Service Specification
Add a first basic implementation of the DataSourceFactory
1 parent 062440a commit 7377e27

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-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,27 @@
1+
package org.sqlite.osgi;
2+
3+
import java.util.Hashtable;
4+
5+
import org.osgi.framework.BundleActivator;
6+
import org.osgi.framework.BundleContext;
7+
import org.osgi.service.jdbc.DataSourceFactory;
8+
import org.sqlite.JDBC;
9+
import org.sqlite.SQLiteJDBCLoader;
10+
11+
public class SQLiteActivator implements BundleActivator {
12+
13+
@Override
14+
public void start(BundleContext context) throws Exception {
15+
Hashtable<String, Object> properties = new Hashtable<>();
16+
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, JDBC.class.getName());
17+
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, "SQLite JDBC driver");
18+
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION, SQLiteJDBCLoader.getVersion());
19+
context.registerService(DataSourceFactory.class, new SQLiteDataSourceFactory(), properties);
20+
}
21+
22+
@Override
23+
public void stop(BundleContext context) throws Exception {
24+
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)