1. Introduction

The Ohmdb plugin enables lightweight access to Ohmdb datasources. This plugin does NOT provide domain classes nor dynamic finders like GORM does.

Griffon version: 2.12.0

2. Usage

The following sections describe how you may use this plugin in a project.

2.1. Configuration

You must create a configuration file named Ohmdb that holds the settings for creating instances of com.ohmdb.api.Db. This file follows the same standard configuration mechanism as the application’s Config file, which means you can define the configuration using

  • a properties file

  • a Java file

  • a Groovy script

The following example shows the default settings needed to connect the default datasource taking into account that each environment may connect to a different datasource.

src/main/resources/Ohmdb.groovy
dataSource {
    delete = true
}

environments {
    development {
        dataSource {
            name = '@application.name@-dev.bin'
        }
    }
    test {
        dataSource {
            name = '@application.name@-test.bin'
        }
    }
    production {
        dataSource {
            name = '@application.name@-prod.bin'
        }
    }
}

You may configure multiple named datasources (the default factory is aptly named default) as the following snippet shows

src/main/resources/Ohmdb.groovy
dataSources {
    internal {
        name = '@application.name@-internal.bin'
        delete = true
    }
    people {
        name = '@application.name@-people.bin'
        delete = true
    }
}

The following properties are optional

Property Type Default Description

delete

boolean

false

Deletes the datasource file when disconnectingfrom the datasource.

connect_on_startup

boolean

false

Establishes a connection to the datasource at the beginning of the Startup phase.

The plugin’s module registers a DbHandler helper class that defines the base contract for accessing a datasource and issue queries to it. This class has the following methods

griffon.plugins.ohmdb.DbHandler.java
@Nullable
<R> R withOhmdb(@Nonnull DbCallback<R> callback);

@Nullable
<R> R withOhmdb(@Nonnull String dataSourceName, @Nonnull DbCallback<R> callback);

void closeOhmdb();

void closeOhmdb(@Nonnull String dataSourceName);

These method are aware of multiple datasources. If no dataSourceName is specified when calling them then the default datasource will be selected. You can inject an instance of this class anywhere it’s needed using @Inject. There is one callback you may use with this method: DbCallback.

This callback is defined using a functional interface approach, which means you can apply lambda expressions if running with JDK8+ or closures if running Groovy.

griffon.plugins.ohmdb.DbCallback.java
public interface DbCallback<R> {
    @Nullable
    R handle(@Nonnull String dataSourceName, @Nonnull Db db);
}

2.1.1. Bootstrap

You may execute arbitrary datasource calls during connection and disconnection from a com.ohmdb.api.Db. Simply create a class that implements the OhmdbBootstrap interface and register it within a module, for example

src/main/java/com/acme/SampleOhmdbBootstrap.java
package com.acme;

import griffon.plugins.ohmdb.OhmdbBootstrap;
import com.ohmdb.api.Db;

import javax.annotation.Nonnull;
import javax.inject.Named;

@Named("sample")
public class SampleOhmdbBootstrap implements OhmdbBootstrap {
    @Override
    public void init(@Nonnull String dataSourceName, @Nonnull Db db) {
        // operations after first connection to db
    }

    @Override
    public void destroy(@Nonnull String dataSourceName, @Nonnull Db db) {
        // operations before disconnecting from the db
    }
}
src/main/java/com/acme/ApplicationModule.java
package com.acme;

import griffon.plugins.ohmdb.OhmdbBootstrap;
import griffon.core.injection.Module;
import org.codehaus.griffon.runtime.core.injection.AbstractModule;
import org.kordamp.jipsy.ServiceProviderFor;

@ServiceProviderFor(Module.class)
public class ApplicationModule extends AbstractModule {
    @Override
    protected void doConfigure() {
        bind(OhmdbBootstrap.class)
            .to(SampleOhmdbBootstrap.class)
            .asSingleton();
    }
}

2.2. Example

The following is a trivial usage of the DbHandler inside a Java service

com.acme.SampleService.java
package com.acme;

import griffon.core.artifact.GriffonService;
import griffon.metadata.ArtifactProviderFor;
import org.codehaus.griffon.runtime.core.artifact.AbstractGriffonService;

import griffon.plugins.ohmdb.DbHandler;
import griffon.plugins.ohmdb.DbCallback;
import com.ohmdb.api.Db;

import javax.annotation.Nonnull;
import javax.inject.Inject;

@ArtifactProviderFor(GriffonService.class)
public class SampleService extends AbstractGriffonService {
    @Inject
    private DbHandler dbHandler;

    public String getPersonName(final int id) {
         return dbHandler.withOhmdb(new DbCallback<String>() {
             public String handle(@Nonnull String dataSourceName, @Nonnull Db db) {
                 ...
         });
    }
}

Here’s the Groovy version of it

com.acme.SampleService.groovy
package com.acme

import griffon.core.artifact.GriffonService
import griffon.metadata.ArtifactProviderFor

import griffon.plugins.ohmdb.DbHandler

import javax.inject.Inject

@ArtifactProviderFor(GriffonService)
class SampleService {
    @Inject
    private DbHandler dbHandler

    String getPersonName(int id) {
         dbHandler.withOhmdb { String dataSourceName, Db db ->
             ...
         }
    }
}

2.3. Events

The following events will be triggered by DbHandler

OhmdbConnectStart(String dataSourceName, Map<String, Object> config)

Triggered before connecting to the datasource.

OhmdbConfigurationSetup(String dataSourceName, Map<String, Object> config, Db db)

Triggered when configuring the database

OhmdbConnectEnd(String dataSourceName, Map<String, Object> config, Db db)

Triggered after connecting to the datasource.

OhmdbDisconnectStart(String dataSourceName, Map<String, Object> config, Db db)

Triggered before disconnecting from the datasource.

OhmdbDisconnectEnd(String dataSourceName, Map<String, Object> config)

Triggered after disconnecting from the datasource.

DataSource events may be triggered during connection and disconnection from a com.ohmdb.api.Db.

2.4. AST Transformation

You can apply the @OhmdbAware AST transformation on any class. This injects the behavior of DbHandler into said class. The previous Groovy service example can be rewritten as follows

com.acme.SampleService.groovy
package com.acme

import griffon.core.artifact.GriffonService
import griffon.metadata.ArtifactProviderFor
import griffon.transform.OhmdbAware

import com.ohmdb.api.Db

@OhmdbAware
@ArtifactProviderFor(GriffonService)
class SampleService {
    String getPersonName(int id) {
         withOhmdb { String dataSourceName, Db db ->
             ...
         }
    }
}

2.5. DSL Descriptors

This plugin provides DSL descriptors for Intellij IDEA and Eclipse (provided you have the Groovy Eclipse plugin installed). These descriptors are found inside the griffon-ohmdb-groovy-compile-2.1.0.jar, with locations

  • dsdl/griffon_ohmdb.dsld

  • gdsl/griffon_ohmdb.gdsl

3. Build Configuration

3.1. Gradle

You have two options for configuring this plugin: automatic and manual.

3.1.1. Automatic

As long as the project has the org.codehaus.griffon.griffon plugin applied to it you may include the following snippet in build.gradle

dependencies {
    griffon 'org.codehaus.griffon.plugins:griffon-ohmdb-plugin:2.1.0'
}

The griffon plugin will take care of the rest given its configuration.

3.1.2. Manual

You will need to configure any of the following blocks depending on your setup

dependencies {
    compile 'org.codehaus.griffon.plugins:griffon-ohmdb-core:2.1.0'
}
Compile Only
dependencies {
    compileOnly 'org.codehaus.griffon.plugins:griffon-ohmdb-groovy-compile:2.1.0'
}

3.2. Maven

First configure the griffon-ohmdb-plugin BOM in your POM file, by placing the following snippet before the <build> element

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.griffon.plugins</groupId>
            <artifactId>griffon-ohmdb-plugin</artifactId>
            <version>2.1.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Next configure dependencies as required by your particular setup

<dependency>
    <groupId>org.codehaus.griffon.plugins</groupId>
    <artifactId>griffon-ohmdb-core</artifactId>
</dependency>
Provided scope
<dependency>
    <groupId>org.codehaus.griffon.plugins</groupId>
    <artifactId>griffon-ohmdb-groovy-compile</artifactId>
</dependency>

Don’t forget to configure all -compile dependencies with the maven-surefire-plugin, like so

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <classpathDependencyExcludes>
            <classpathDependencyExclude>
                org.codehaus.griffon:griffon-ohmdb-groovy-compile
            </classpathDependencyExclude>
        </classpathDependencyExcludes>
    </configuration>
</plugin>

4. Modules

The following sections display all bindings per module. Use this information to successfully override a binding on your own modules or to troubleshoot a module binding if the wrong type has been applied by the Griffon runtime.

4.1. Ohmdb

Module name: ohmdb

bind(ResourceBundle.class)
    .withClassifier(named("ohmdb"))
    .toProvider(new ResourceBundleProvider("Ohmdb"))
    .asSingleton();

bind(Configuration.class)
    .withClassifier(named("ohmdb"))
    .to(DefaultOhmdbConfiguration.class)
    .asSingleton();

bind(DbStorage.class)
    .to(DefaultDbStorage.class)
    .asSingleton();

bind(DbFactory.class)
    .to(DefaultDbFactory.class)
    .asSingleton();

bind(DbHandler.class)
    .to(DefaultDbHandler.class)
    .asSingleton();

bind(GriffonAddon.class)
    .to(OhmdbAddon.class)
    .asSingleton();