1. Introduction
The Carbonado plugin enables lightweight access datastores using the Carbonado persistence abstraction layer. 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 Carbonado
that holds the settings for creating instances of com.amazon.carbonado.Repository
.
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 repository taking into account that each environment may connect to a different repositories.
repository {
type = 'map'
jdbc {
//
}
bdb {
transactionWriteNoSync = true
}
map {
//
}
}
environments {
development {
repository {
bdb {
environmentHomeFile = new File('build/carbonado-@application.name@-dev')
}
}
}
test {
repository {
bdb {
environmentHomeFile = new File('build/carbonado-@application.name@-test')
}
}
}
production {
repository {
bdb {
environmentHomeFile = new File('build/carbonado-@application.name@-prod')
}
}
}
}
You may configure multiple named repositories (the default factory is aptly named default
) as the following snippet
shows
repositories {
internal {
type = 'map'
}
people {
type = 'jdbc'
}
}
The following properties are optional
Property | Type | Default | Description |
---|---|---|---|
type |
String |
map |
Defines the type of repository. Valid values are |
connect_on_startup |
boolean |
false |
Establishes a connection to the repository at the beginning of the |
The plugin’s module registers a CarbonadoHandler
helper class that defines the base contract
for accessing a repository and issue queries to it. This class has the following methods
@Nullable
<R> R withCarbonado(@Nonnull RepositoryCallback<R> callback);
@Nullable
<R> R withCarbonado(@Nonnull String repositoryName, @Nonnull RepositoryCallback<R> callback);
void closeCarbonado();
void closeCarbonado(@Nonnull String repositoryName);
These method are aware of multiple repositories. If no repositoryName is specified when calling them then the default
repository 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: RepositoryCallback
.
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.
public interface RepositoryCallback<R> {
@Nullable
R handle(@Nonnull String repositoryName, @Nonnull Repository repository);
}
2.1.1. Bootstrap
You may execute arbitrary repository calls during connection and disconnection from a com.amazon.carbonado.Repository
. Simply
create a class that implements the CarbonadoBootstrap
interface and register it within a module, for example
package com.acme;
import griffon.plugins.carbonado.CarbonadoBootstrap;
import com.amazon.carbonado.Repository;
import javax.annotation.Nonnull;
import javax.inject.Named;
@Named("sample")
public class SampleCarbonadoBootstrap implements CarbonadoBootstrap {
@Override
public void init(@Nonnull String repositoryName, @Nonnull Repository repository) {
// operations after first connection to the repository
}
@Override
public void destroy(@Nonnull String repositoryName, @Nonnull Repository repository) {
// operations before disconnecting from the repository
}
}
package com.acme;
import griffon.plugins.carbonado.CarbonadoBootstrap;
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(CarbonadoBootstrap.class)
.to(SampleCarbonadoBootstrap.class)
.asSingleton();
}
}
2.2. Example
The following is a trivial usage of the CarbonadoHandler
inside a Java service
package com.acme;
import griffon.core.artifact.GriffonService;
import griffon.metadata.ArtifactProviderFor;
import org.codehaus.griffon.runtime.core.artifact.AbstractGriffonService;
import griffon.plugins.carbonado.CarbonadoHandler;
import griffon.plugins.carbonado.CarbonadoCallback;
import com.amazon.carbonado.Repository;
import javax.annotation.Nonnull;
import javax.inject.Inject;
@ArtifactProviderFor(GriffonService.class)
public class SampleService extends AbstractGriffonService {
@Inject
private CarbonadoHandler carbonadoHandler;
public String getPersonName(final int id) {
return carbonadoHandler.withCarbonado(new CarbonadoCallback<String>() {
public String handle(@Nonnull String repositoryName, @Nonnull Repository repository) {
...
});
}
}
Here’s the Groovy version of it
package com.acme
import griffon.core.artifact.GriffonService
import griffon.metadata.ArtifactProviderFor
import griffon.plugins.carbonado.CarbonadoHandler
import com.amazon.carbonado.Repository
import javax.inject.Inject
@ArtifactProviderFor(GriffonService)
class SampleService {
@Inject
private CarbonadoHandler carbonadoHandler
String getPersonName(int id) {
carbonadoHandler.withCarbonado { String repositoryName, Repository repository ->
...
}
}
}
2.3. Events
The following events will be triggered by CarbonadoHandler
- CarbonadoConnectStart(String repositoryName, Map<String, Object> config)
-
Triggered before connecting to the repository.
- CarbonadoConnectEnd(String repositoryName, Map<String, Object> config, Carbonado carbonado)
-
Triggered after connecting to the repository.
- CarbonadoDisconnectStart(String repositoryName, Map<String, Object> config, Carbonado carbonado)
-
Triggered before disconnecting from the repository.
- CarbonadoDisconnectEnd(String repositoryName, Map<String, Object> config)
-
Triggered after disconnecting from the repository.
Repository events may be triggered during connection and disconnection from a com.amazon.carbonado.Repository .
|
2.4. AST Transformation
You can apply the @CarbonadoAware
AST transformation on any class. This injects the behavior of CarbonadoHandler
into said class. The previous Groovy service example can be rewritten as follows
package com.acme
import griffon.core.artifact.GriffonService
import griffon.metadata.ArtifactProviderFor
import griffon.transform.CarbonadoAware
import com.amazon.carbonado.Repository
@CarbonadoAware
@ArtifactProviderFor(GriffonService)
class SampleService {
String getPersonName(int id) {
withCarbonado { String repositoryName, Repository repository ->
...
}
}
}
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-carbonado-plugin:2.1.0'
}
The griffon
plugin will take care of the rest given its configuration.
3.2. Maven
First configure the griffon-carbonado-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-carbonado-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-carbonado-core</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.griffon.plugins</groupId>
<artifactId>griffon-carbonado-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-carbonado-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. Carbonado
Module name: carbonado
bind(ResourceBundle.class)
.withClassifier(named("carbonado"))
.toProvider(new ResourceBundleProvider("Carbonado"))
.asSingleton();
bind(Configuration.class)
.withClassifier(named("carbonado"))
.to(DefaultCarbonadoConfiguration.class)
.asSingleton();
bind(RepositoryStorage.class)
.to(DefaultRepositoryStorage.class)
.asSingleton();
bind(RepositoryFactory.class)
.to(DefaultRepositoryFactory.class)
.asSingleton();
bind(CarbonadoHandler.class)
.to(DefaultCarbonadoHandler.class)
.asSingleton();
bind(GriffonAddon.class)
.to(CarbonadoAddon.class)
.asSingleton();