It’s purpose is to make the database access available, it can also exposes Dao’s as services. It’s composed of two modules: a database one and a model one.

Database module

Here you can specify your database access configuration and expose the connections as OSGI services that will be consumed by the model module. By default Derby databases are supported, but you can tweak it to support Oracle, Mysql, PostGres, HSQL… In order to declare a database, you just have to edit your [parent]/[features]/src/main/resources/[database.cfg] and tweak the properties:
1
2
3
4
5
6
7
8
9
10
    osgi.jdbc.driver.name=osgiliathderbystandalone-pool-xa #driver to use, remove the '-xa' suffix if you don't need transactions
    databaseName=${project.parent.artifactId}.model.database #name of the database
    user=APP #db user
    password=APP #db password
    dataSourceName=${project.parent.artifactId}.model.database #name with which you'll be able to reference the created datasource in your persistence.xml
    portNumber=${database.port} #Port of the DB (change it in the [parent] pom.xml
    serverName=localhost #Server host
    serverAutoStart=true #Autostarts the database server on the host, set to false if it is already started
    createDatabase=create #autocreates the database (it'll reuse it if it already exists)

Then, you just have to reference this config file in your feature.xml prefixing the deployed config file by etc/org.ops4j.datasource-
1
2
3
4
    <feature name="my.model" version="${project.version}">
        <configfile finalname="/etc/org.ops4j.datasource-[nameofyourdb].cfg">mvn:com.foo/my/${project.version}/cfg/database</configfile>
    </feature>

Defining an other kind of datasource: You must define a new feature on the parent project features list (see features sub-module), containing the OSGI compliant database driver as well as a corresponding Pax-jdbc compliant JDBC driver (you can import the corresponding features). Then, modify the osgi.bnd file of the entity module removing the org.apache.derby.jdbc package import and adding those of your driver. Finally change the dataSource bean declaration in the blueprint file. Also, if you changed something on the datasource definition, you’ll also have to modify the datasource linkage in the model module pom.xml (the parent of the daos and entities one) as well as the database credentials in the parent pom.

Entities module

Datasource connection definition


Edit the src/main/resources/META-INF/persistence.xml file of your module and change the <jta-data-source>osgi:service/jdbc/${project.parent.artifactId}.database.xa</jta-data-source> and the <non-jta-data-source> Sections (referencing the non xa exported one). If you want to disable the transactional access, you’ve also to change the ‘transaction-type’ value to ‘RESOURCE_LOCAL’

Model object definition


In order to define a new model object, you’ve got to extends the ‘AbstractEntity’ abstract class of the src/main/java folder. All Java files that are located on this folder will be ‘delombokized’ e.g they will interpret the lombok annotations and generate the according Java code. Here’s an example of a model entity:
Then reference this class on the <class> section of the src/main/resources/META-INF/persistence.xml file

DAO module

The dao module declares method for accessing or persisting entities.

Declaring DAOs


In order to declare a Data Access Object you’ve to expose its API to others via an interface:
Then, with the help of spring data jpa that make all the basic CRUD operations available, you’ve to code a very small implementation:
Note that the OsgiService annotation exposes this implementation to other modules (via blueprint annotation compilation processing, which exposes it via the Osgi Service registry).

Adding JMS, REST or integration Tests to the JPA module

In order to add REST or JMS support to the JPA module, remove all net.osgiliath features on the pom.xml and add this one:
1
2
3
4
5
6
7
<dependency>
	<groupId>net.osgiliath.framework</groupId>
	<version>${osgiliath.framework.maven.version}</version>
	<artifactId>net.osgiliath.feature.webservices or net.osgiliath.feature.routes</artifactId>
	<type>xml</type>
	<classifier>features</classifier>
</dependency>
And the same for your projects feature (in the features module of your parent pom). Then, add these lines on your osgi.bnd Import-Package section:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Require-Bundle: org.hibernate.validator
Import-Package: \
 javax.annotation;version=0,\
 !javax.transaction,\
 javax.transaction;version='[1.1,2)',\
 javax.servlet;version='[3,4)',\
 org.springframework.data.domain;resolution:=optional,\
 org.apache.camel.builder.xml,\
 org.apache.camel.model.rest,\
 org.apache.camel.support,\
 org.apache.cxf.transport.servlet,\
 org.apache.cxf.service.factory,\
 io.swagger.jaxrs.listing,\
 *

Finally, follow the REST, JMS or Itests sections of the business module documentation

Comments