В папке etc создать файл org.ops4j.datasource-sqlite.cfg:
osgi.jdbc.driver.name=sqlite
databaseName=test.db
dataSourceName=mysqlite
В karaf-е выполнить команды:
feature:repo-add pax-jdbc
feature:install pax-jdbc-config pax-jdbc-sqlite
Командой
service:list javax.sql.DataSource
проверить корректность создания датасорса:
Использовать датасорс можно в программе - в blueprint.xml:
<reference id="dataSource" interface="javax.sql.DataSource"
filter="(osgi.jndi.service.name=mysqlite)"/>
Создать Blueprint Hello World.
Добавить в pom.xml:
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.16.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.5.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
Изменить blueprint.xml:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<reference id="dataSource" interface="javax.sql.DataSource"
filter="(osgi.jndi.service.name=mysqlite)"/>
<bean id="hellobean" class="App"
init-method="init">
<property name="dataSource" ref="dataSource"/>
</bean>
</blueprint>
Изменить App.java:
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
public class App {
DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void init() throws Exception {
try ( Connection connection = dataSource.getConnection();
Statement command = connection.createStatement() ) {
command.execute("create table token (id integer(100), name varchar(100))");
command.execute("insert into token (id, name) values ('1', 'Hello World!')");
}
catch (Exception e) {
System.out.println(e.getMessage());
throw e;
}
}
}
Теперь:
В папке servisemix появится файл test.db.