1. Introduction

The Jcouchdb plugin enables lightweight access to Couchdb. 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 Jcouchdb that holds the settings for creating instances of org.jcouchdb.db.Database. 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 database taking into account that each environment may connect to a different database.

src/main/resources/Jcouchdb.groovy
database {

}

environments {
    development {
        database {
            datastore = '@application.name@-dev'
        }
    }
    test {
        database {
            datastore = '@application.name@-test'
        }
    }
    production {
        database {
            datastore = '@application.name@-prod'
        }
    }
}

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

src/main/resources/Jcouchdb.groovy
databases {
    internal {
        datastore = '@application.name@-internal'
    }
    people {
        datastore = '@application.name@-people'
    }
}

The following properties are optional

Property Type Default Description

connect_on_startup

boolean

false

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

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

griffon.plugins.jcouchdb.DatabaseHandler.java
@Nullable
<R> R withJcouchdb(@Nonnull DatabaseCallback<R> callback);

@Nullable
<R> R withJcouchdb(@Nonnull String databaseName, @Nonnull DatabaseCallback<R> callback);

void closeJcouchdb();

void closeJcouchdb(@Nonnull String databaseName);

These method are aware of multiple databases. If no databaseName is specified when calling them then the default database 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: DatabaseCallback.

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.jcouchdb.DatabaseCallback.java
public interface DatabaseCallback<R> {
    @Nullable
    R handle(@Nonnull String databaseName, @Nonnull Database database);
}

2.1.1. Bootstrap

You may execute arbitrary database calls during connection and disconnection from a org.jcouchdb.db.Database. Simply create a class that implements the JcouchdbBootstrap interface and register it within a module, for example

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

import griffon.plugins.jcouchdb.JcouchdbBootstrap;
import org.jcouchdb.db.Database;

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

@Named("sample")
public class SampleJcouchdbBootstrap implements JcouchdbBootstrap {
    @Override
    public void init(@Nonnull String databaseName, @Nonnull Database database) {
        // operations after first connection to database
    }

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

import griffon.plugins.jcouchdb.JcouchdbBootstrap;
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(JcouchdbBootstrap.class)
            .to(SampleJcouchdbBootstrap.class)
            .asSingleton();
    }
}

2.2. Example

The following is a trivial usage of the DatabaseHandler 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.jcouchdb.DatabaseHandler;
import griffon.plugins.jcouchdb.DatabaseCallback;
import org.jcouchdb.db.Database;

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

@ArtifactProviderFor(GriffonService.class)
public class SampleService extends AbstractGriffonService {
    @Inject
    private DatabaseHandler databaseHandler;

    public String getPersonName(final int id) {
         return databaseHandler.withDatabase(new DatabaseCallback<String>() {
             public String handle(@Nonnull String databaseName, @Nonnull Database database) {
                 ...
         });
    }
}

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.jcouchdb.DatabaseHandler

import javax.inject.Inject

@ArtifactProviderFor(GriffonService)
class SampleService {
    @Inject
    private DatabaseHandler databaseHandler

    String getPersonName(int id) {
         databaseHandler.withDatabase { String databaseName, Database database ->
             ...
         }
    }
}

2.3. Events

The following events will be triggered by DatabaseHandler

JcouchdbConnectStart(String databaseName, Map<String, Object> config)

Triggered before connecting to the database.

ConfigureJcouchdbJSONParser(String databaseName, Map<String, Object> config, JSONParser parser)

Triggered when configurating the JSON parser

JcouchdbConnectEnd(String databaseName, Map<String, Object> config, Database database)

Triggered after connecting to the database.

JcouchdbDisconnectStart(String databaseName, Map<String, Object> config, Database database)

Triggered before disconnecting from the database.

JcouchdbDisconnectEnd(String databaseName, Map<String, Object> config)

Triggered after disconnecting from the database.

DataSource events may be triggered during connection and disconnection from a org.jcouchdb.db.Database.

2.4. AST Transformation

You can apply the {api_database_aware} AST transformation on any class. This injects the behavior of DatabaseHandler 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.JcouchdbAware

import org.jcouchdb.db.Database

@JcouchdbAware
@ArtifactProviderFor(GriffonService)
class SampleService {
    String getPersonName(int id) {
         withDatabase { String databaseName, Database database ->
             ...
         }
    }
}

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-jcouchdb-groovy-compile-2.1.0.jar, with locations

  • dsdl/griffon_jcouchdb.dsld

  • gdsl/griffon_jcouchdb.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-jcouchdb-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-jcouchdb-core:2.1.0'
}
Compile Only
dependencies {
    compileOnly 'org.codehaus.griffon.plugins:griffon-jcouchdb-groovy-compile:2.1.0'
}

3.2. Maven

First configure the griffon-jcouchdb-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-jcouchdb-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-jcouchdb-core</artifactId>
</dependency>
Provided scope
<dependency>
    <groupId>org.codehaus.griffon.plugins</groupId>
    <artifactId>griffon-jcouchdb-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-jcouchdb-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. Jcouchdb

Module name: jcouchdb

bind(ResourceBundle.class)
    .withClassifier(named("jcouchdb"))
    .toProvider(new ResourceBundleProvider("Jcouchdb"))
    .asSingleton();

bind(Configuration.class)
    .withClassifier(named("jcouchdb"))
    .to(DefaultJcouchdbConfiguration.class)
    .asSingleton();

bind(DatabaseStorage.class)
    .to(DefaultDatabaseStorage.class)
    .asSingleton();

bind(DatabaseFactory.class)
    .to(DefaultDatabaseFactory.class)
    .asSingleton();

bind(DatabaseHandler.class)
    .to(DefaultDatabaseHandler.class)
    .asSingleton();

bind(CouchDBUpdater.class)
    .to(DefaultCouchDBUpdater.class)
    .asSingleton();

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