Skip to content

Commit

Permalink
Add AST xform
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed May 12, 2013
1 parent 9da6721 commit 83542ec
Show file tree
Hide file tree
Showing 28 changed files with 1,140 additions and 155 deletions.
19 changes: 14 additions & 5 deletions NeodatisGriffonAddon.groovy
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2012 the original author or authors.
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,9 @@ import griffon.core.GriffonClass
import griffon.core.GriffonApplication
import griffon.plugins.neodatis.NeodatisConnector
import griffon.plugins.neodatis.NeodatisEnhancer
import griffon.plugins.neodatis.NeodatisContributionHandler

import static griffon.util.ConfigUtils.getConfigValueAsBoolean

/**
* @author Andres Almiray
Expand All @@ -48,20 +51,26 @@ class NeodatisGriffonAddon {
remove: {-> throw new UnsupportedOperationException("${Objects.class.name} is immutable!")}] as Iterator
*/
}
ConfigObject config = NeodatisConnector.instance.createConfig(app)
NeodatisConnector.instance.connect(app, config)
}

void addonPostInit(GriffonApplication app) {
NeodatisConnector.instance.createConfig(app)
def types = app.config.griffon?.neodatis?.injectInto ?: ['controller']
for(String type : types) {
for(GriffonClass gc : app.artifactManager.getClassesOfType(type)) {
for (String type : types) {
for (GriffonClass gc : app.artifactManager.getClassesOfType(type)) {
if (NeodatisContributionHandler.isAssignableFrom(gc.clazz)) continue
NeodatisEnhancer.enhance(gc.metaClass)
}
}
}

Map events = [
LoadAddonsEnd: { app, addons ->
if (getConfigValueAsBoolean(app.config, 'griffon.neodatis.connect.onstartup', true)) {
ConfigObject config = NeodatisConnector.instance.createConfig(app)
NeodatisConnector.instance.connect(app, config)
}
},
ShutdownStart: { app ->
ConfigObject config = NeodatisConnector.instance.createConfig(app)
NeodatisConnector.instance.disconnect(app, config)
Expand Down
188 changes: 158 additions & 30 deletions NeodatisGriffonPlugin.groovy
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2012 the original author or authors.
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,11 @@
*/
class NeodatisGriffonPlugin {
// the plugin version
String version = '0.6'
String version = '1.0.0'
// the version or versions of Griffon the plugin is designed for
String griffonVersion = '1.1.0 > *'
String griffonVersion = '1.3.0 > *'
// the other plugins this plugin depends on
Map dependsOn = [:]
Map dependsOn = [lombok: '0.5.0']
// resources that are included in plugin packaging
List pluginIncludes = []
// the plugin license
Expand Down Expand Up @@ -53,21 +53,23 @@ This plugin does NOT provide domain classes nor dynamic finders like GORM does.
Usage
-----
Upon installation the plugin will generate the following artifacts in `$appdir/griffon-app/conf`:
Upon installation the plugin will generate the following artifacts in
`$appdir/griffon-app/conf`:
* NeodatisConfig.groovy - contains the database definitions.
* BootstrapNeodatis.groovy - defines init/destroy hooks for data to be manipulated during app startup/shutdown.
* BootstrapNeodatis.groovy - defines init/destroy hooks for data to be
manipulated during app startup/shutdown.
A new dynamic method named `withOdb` will be injected into all controllers,
giving you access to a `org.neodatis.odb.ODB` object, with which you'll be able
to make calls to the database. Remember to make all database calls off the EDT
otherwise your application may appear unresponsive when doing long computations
inside the EDT.
giving you access to a `org.neodatis.odb.ODB` object, with which you'll be
able to make calls to the database. Remember to make all database calls off
the UI thread otherwise your application may appear unresponsive when doing
long computations inside the UI thread.
This method is aware of multiple databases. If no databaseName is specified when calling
it then the default database will be selected. Here are two example usages, the first
queries against the default database while the second queries a database whose name has
been configured as 'internal'
This method is aware of multiple databases. If no databaseName is specified
when calling it then the default database will be selected. Here are two example
usages, the first queries against the default database while the second queries
a database whose name has been configured as 'internal'
package sample
class SampleController {
Expand All @@ -77,19 +79,37 @@ been configured as 'internal'
}
}
This method is also accessible to any component through the singleton `griffon.plugins.neodatis.NeodatisConnector`.
You can inject these methods to non-artifacts via metaclasses. Simply grab hold of a particular metaclass and call
`NeodatisEnhancer.enhance(metaClassInstance, neodatisProviderInstance)`.
The following list enumerates all the variants of the injected method
* `<R> R withOdb(Closure<R> stmts)`
* `<R> R withOdb(CallableWithArgs<R> stmts)`
* `<R> R withOdb(String databaseName, Closure<R> stmts)`
* `<R> R withOdb(String databaseName, CallableWithArgs<R> stmts)`
These methods are also accessible to any component through the singleton
`griffon.plugins.neodatis.NeodatisConnector`. You can inject these methods to
non-artifacts via metaclasses. Simply grab hold of a particular metaclass and
call `NeodatisEnhancer.enhance(metaClassInstance, neodatisProviderInstance)`.
Configuration
-------------
### NeodatisAware AST Transformation
The preferred way to mark a class for method injection is by annotating it with
`@griffon.plugins.neodatis.NeodatisAware`. This transformation injects the
`griffon.plugins.neodatis.NeodatisContributionHandler` interface and default
behavior that fulfills the contract.
### Dynamic method injection
The `withOdb()` dynamic method will be added to controllers by default. You can
Dynamic methods will be added to controllers by default. You can
change this setting by adding a configuration flag in `griffon-app/conf/Config.groovy`
griffon.neodatis.injectInto = ['controller', 'service']
Dynamic method injection will be skipped for classes implementing
`griffon.plugins.neodatis.NeodatisContributionHandler`.
### Events
The following events will be triggered by this addon
Expand All @@ -99,7 +119,7 @@ The following events will be triggered by this addon
* NeodatisDisconnectStart[config, databaseName, odb] - triggered before disconnecting from the database
* NeodatisDisconnectEnd[config, databaseName] - triggered after disconnecting from the database
### Multiple Stores
### Multiple Databases
The config file `NeodatisConfig.groovy` defines a default database block. As the name
implies this is the database used by default, however you can configure named databases
Expand All @@ -117,29 +137,51 @@ can be done in this way
This block can be used inside the `environments()` block in the same way as the
default database block is used.
### Configuration Storage
The plugin will load and store the contents of `NeodatisConfig.groovy` inside the
application's configuration, under the `pluginConfig` namespace. You may retrieve
and/or update values using
app.config.pluginConfig.neodatis
### Connect at Startup
The plugin will attempt a connection to the default database at startup. If this
behavior is not desired then specify the following configuration flag in
`Config.groovy`
griffon.neodatis.connect.onstartup = false
### Example
A trivial sample application can be found at [https://github.com/aalmiray/griffon_sample_apps/tree/master/persistence/neodatis][2]
Testing
-------
The `withOdb()` dynamic method will not be automatically injected during unit testing, because addons are simply not initialized
for this kind of tests. However you can use `NeodatisEnhancer.enhance(metaClassInstance, neodatisProviderInstance)` where
`neodatisProviderInstance` is of type `griffon.plugins.neodatis.NeodatisProvider`. The contract for this interface looks like this
Dynamic methods will not be automatically injected during unit testing, because
addons are simply not initialized for this kind of tests. However you can use
`NeodatisEnhancer.enhance(metaClassInstance, neodatisProviderInstance)` where
`neodatisProviderInstance` is of type `griffon.plugins.neodatis.NeodatisProvider`.
The contract for this interface looks like this
public interface NeodatisProvider {
Object withOdb(Closure closure);
Object withOdb(String serverName, Closure closure);
<T> T withOdb(CallableWithArgs<T> callable);
<T> T withOdb(String serverName, CallableWithArgs<T> callable);
<R> R withOdb(Closure<R> closure);
<R> R withOdb(CallableWithArgs<R> callable);
<R> R withOdb(String databaseName, Closure<R> closure);
<R> R withOdb(String databaseName, CallableWithArgs<R> callable);
}
It's up to you define how these methods need to be implemented for your tests. For example, here's an implementation that never
fails regardless of the arguments it receives
It's up to you define how these methods need to be implemented for your tests.
For example, here's an implementation that never fails regardless of the
arguments it receives
class MyNeodatisProvider implements NeodatisProvider {
Object withOdb(String serverName = 'default', Closure closure) { null }
public <T> T withOdb(String serverName = 'default', CallableWithArgs<T> callable) { null }
public <R> R withOdb(Closure<R> closure) { null }
public <R> R withOdb(CallableWithArgs<R> callable) { null }
public <R> R withOdb(String databaseName, Closure<R> closure) { null }
public <R> R withOdb(String databaseName, CallableWithArgs<R> callable) { null }
}
This implementation may be used in the following way
Expand All @@ -152,8 +194,94 @@ This implementation may be used in the following way
}
}
On the other hand, if the service is annotated with `@NeodatisAware` then usage
of `NeodatisEnhancer` should be avoided at all costs. Simply set `neodatisProviderInstance`
on the service instance directly, like so, first the service definition
@griffon.plugins.neodatis.NeodatisAware
class MyService {
def serviceMethod() { ... }
}
Next is the test
class MyServiceTests extends GriffonUnitTestCase {
void testSmokeAndMirrors() {
MyService service = new MyService()
service.neodatisProvider = new MyNeodatisProvider()
// exercise service methods
}
}
Tool Support
------------
### 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-neodatis-compile-x.y.z.jar`, with locations
* dsdl/neodatis.dsld
* gdsl/neodatis.gdsl
### Lombok Support
Rewriting Java AST in a similar fashion to Groovy AST transformations is
possible thanks to the [lombok][3] plugin.
#### JavaC
Support for this compiler is provided out-of-the-box by the command line tools.
There's no additional configuration required.
#### Eclipse
Follow the steps found in the [Lombok][3] plugin for setting up Eclipse up to
number 5.
6. Go to the path where the `lombok.jar` was copied. This path is either found
inside the Eclipse installation directory or in your local settings. Copy
the following file from the project's working directory
$ cp $USER_HOME/.griffon/<version>/projects/<project>/plugins/neodatis-<version>/dist/griffon-neodatis-compile-<version>.jar .
6. Edit the launch script for Eclipse and tweak the boothclasspath entry so
that includes the file you just copied
-Xbootclasspath/a:lombok.jar:lombok-pg-<version>.jar:\
griffon-lombok-compile-<version>.jar:griffon-neodatis-compile-<version>.jar
7. Launch Eclipse once more. Eclipse should be able to provide content assist
for Java classes annotated with `@NeodatisAware`.
#### NetBeans
Follow the instructions found in [Annotation Processors Support in the NetBeans
IDE, Part I: Using Project Lombok][4]. You may need to specify
`lombok.core.AnnotationProcessor` in the list of Annotation Processors.
NetBeans should be able to provide code suggestions on Java classes annotated
with `@NeodatisAware`.
#### Intellij IDEA
Follow the steps found in the [Lombok][3] plugin for setting up Intellij IDEA
up to number 5.
6. Copy `griffon-neodatis-compile-<version>.jar` to the `lib` directory
$ pwd
$USER_HOME/Library/Application Support/IntelliJIdea11/lombok-plugin
$ cp $USER_HOME/.griffon/<version>/projects/<project>/plugins/neodatis-<version>/dist/griffon-neodatis-compile-<version>.jar lib
7. Launch IntelliJ IDEA once more. Code completion should work now for Java
classes annotated with `@NeodatisAware`.
[1]: http://neodatis.org/
[2]: https://github.com/aalmiray/griffon_sample_apps/tree/master/persistence/neodatis
[3]: /plugin/lombok
[4]: http://netbeans.org/kb/docs/java/annotations-lombok.html
'''
}
10 changes: 10 additions & 0 deletions README.md
@@ -0,0 +1,10 @@
### Building

This project requires all of its dependencies be available from maven compatible repositories.
Some of these dependencies have not been pushed to the Maven Central Repository, however you
can obtain them from [lombok-dev-deps][1].

Follow the instructions found there to install the required dependencies into your local Maven
repository before attempting to build this plugin.

[1]: https://github.com/aalmiray/lombok-dev-deps
5 changes: 4 additions & 1 deletion application.properties
@@ -1,2 +1,5 @@
app.griffon.version=1.1.0
#Griffon Metadata file
#Sun May 12 13:42:48 CDT 2013
app.griffon.version=1.3.0
app.name=neodatis
plugins.lombok=0.5.0
42 changes: 34 additions & 8 deletions griffon-app/conf/BuildConfig.groovy
Expand Up @@ -2,19 +2,45 @@ griffon.project.dependency.resolution = {
inherits "global"
log "warn"
repositories {
flatDir name: 'neodatisPluginLib', dirs: 'lib'
mavenCentral()
mavenLocal()
String basePath = pluginDirPath? "${pluginDirPath}/" : ''
flatDir name: 'neodatisLibDir', dirs: ["${basePath}lib"]
}
dependencies {
compile 'org.neodatis.odb:neodatis-odb:2.2.beta1.252'
build('org.eclipse.jdt:org.eclipse.jdt.core:3.6.0.v_A58') {
export = false
}
String lombokIdea = '0.5'
build("de.plushnikov.lombok-intellij-plugin:processor-api:$lombokIdea",
"de.plushnikov.lombok-intellij-plugin:processor-core:$lombokIdea",
"de.plushnikov.lombok-intellij-plugin:intellij-facade-factory:$lombokIdea",
"de.plushnikov.lombok-intellij-plugin:intellij-facade-api:$lombokIdea",
"de.plushnikov.lombok-intellij-plugin:intellij-facade-9:$lombokIdea",
"de.plushnikov.lombok-intellij-plugin:intellij-facade-10:$lombokIdea",
"de.plushnikov.lombok-intellij-plugin:intellij-facade-11:$lombokIdea") {
export = false
transitive = false
}
String ideaVersion = '11.1.4'
build("org.jetbrains.idea:idea-openapi:$ideaVersion",
"org.jetbrains.idea:extensions:$ideaVersion",
"org.jetbrains.idea:util:$ideaVersion",
"org.jetbrains.idea:annotations:$ideaVersion") {
export = false
}
}
}

griffon {
doc {
logo = '<a href="http://griffon.codehaus.org" target="_blank"><img alt="The Griffon Framework" src="../img/griffon.png" border="0"/></a>'
sponsorLogo = "<br/>"
footer = "<br/><br/>Made with Griffon (@griffon.version@)"
log4j = {
appenders {
console name: 'stdout', layout: pattern(conversionPattern: '%d [%t] %-5p %c - %m%n')
}
}

griffon.jars.destDir='target/addon'
error 'org.codehaus.griffon',
'org.springframework',
'org.apache.karaf',
'groovyx.net'
warn 'griffon'
}
2 changes: 1 addition & 1 deletion scripts/_Install.groovy
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2012 the original author or authors.
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
@@ -0,0 +1 @@
lombok.intellij.processor.clazz.NeodatisAwareProcessor
@@ -0,0 +1 @@
lombok.intellij.processor.clazz.NeodatisAwareProcessor

0 comments on commit 83542ec

Please sign in to comment.