Skip to content

Commit

Permalink
Move metaenhancments out of NewInstance event
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Mar 9, 2012
1 parent 2090c42 commit 6453ef3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
16 changes: 10 additions & 6 deletions MybatisGriffonAddon.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import griffon.core.GriffonClass
import griffon.core.GriffonApplication
import griffon.plugins.mybatis.MybatisConnector
import griffon.plugins.mybatis.MybatisEnhancer
Expand All @@ -26,15 +27,18 @@ class MybatisGriffonAddon {
MybatisConnector.instance.connect(app)
}

void addonPostInit(GriffonApplication app) {
def types = app.config.griffon?.mybatis?.injectInto ?: ['controller']
for(String type : types) {
for(GriffonClass gc : app.artifactManager.getClassesOfType(type)) {
MybatisEnhancer.enhance(gc.metaClass)
}
}
}

def events = [
ShutdownStart: { app ->
MybatisConnector.instance.disconnect(app)
},
NewInstance: { klass, type, instance ->
def types = app.config.griffon?.mybatis?.injectInto ?: ['controller']
if(!types.contains(type)) return
MetaClass mc = app.artifactManager.findGriffonClass(klass).metaClass
MybatisEnhancer.enhance(mc)
}
]
}
8 changes: 4 additions & 4 deletions MybatisGriffonPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ Testing
-------
The `withSqlSession()` 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 `MybatisEnhancer.enhance(metaClassInstance, mybatisProviderInstance)` where
`mybatisProviderInstance` is of type `griffon.plugins.mybatis.MybatisProvider`. The contract for this interface looks like this
`mybatisProviderInstance` is of type `griffon.plugins.mybatis.SqlSessionProvider`. The contract for this interface looks like this
public interface MybatisProvider {
public interface SqlSessionProvider {
Object withSqlSession(Closure closure);
Object withSqlSession(String sessionFactoryName, Closure closure);
<T> T withSqlSession(CallableWithArgs<T> callable);
Expand All @@ -140,7 +140,7 @@ for this kind of tests. However you can use `MybatisEnhancer.enhance(metaClassIn
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 MyMybatisProvider implements MybatisProvider {
class MySqlSessionProvider implements SqlSessionProvider {
Object withSqlSession(String sessionFactoryName = 'default', Closure closure) { null }
public <T> T withSqlSession(String sessionFactoryName = 'default', CallableWithArgs<T> callable) { null }
}
Expand All @@ -150,7 +150,7 @@ This implementation may be used in the following way
class MyServiceTests extends GriffonUnitTestCase {
void testSmokeAndMirrors() {
MyService service = new MyService()
MybatisEnhancer.enhance(service.metaClass, new MyMybatisProvider())
MybatisEnhancer.enhance(service.metaClass, new MySqlSessionProvider())
// exercise service methods
}
}
Expand Down
2 changes: 0 additions & 2 deletions application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#Griffon Metadata file
#Sat Feb 18 22:32:31 CET 2012
app.griffon.version=0.9.5-rc2
app.name=mybatis
plugins.datasource=0.3
4 changes: 1 addition & 3 deletions scripts/CreateMybatisClass.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ includeTargets << griffonScript('_GriffonCreateArtifacts')

target(name: 'createMybatisClass', description: 'Creates a new Mybatis class',
prehook: null, posthook: null) {
depends(checkVersion, parseArguments)

ant.mkdir(dir: "${basedir}/src/mybatis")

def type = 'MybatisClass'
Expand Down Expand Up @@ -59,4 +57,4 @@ target(name: 'createMybatisClass', description: 'Creates a new Mybatis class',
replacefilter(token: "@table.name@", value: myClassName.toLowerCase())
}
}
setDefaultTarget('createMybatisClass')
setDefaultTarget('createMybatisClass')
5 changes: 5 additions & 0 deletions src/main/griffon/plugins/mybatis/MybatisEnhancer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
package griffon.plugins.mybatis

import griffon.util.CallableWithArgs
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
* @author Andres Almiray
*/
final class MybatisEnhancer {
private static final Logger LOG = LoggerFactory.getLogger(MybatisEnhancer)

private MybatisEnhancer() {}

static void enhance(MetaClass mc, SqlSessionProvider provider = SqlSessionFactoryHolder.instance) {
if(LOG.debugEnabled) LOG.debug("Enhancing $mc with $provider")
mc.withSqlSession = {Closure closure ->
provider.withSqlSession('default', closure)
}
Expand Down

0 comments on commit 6453ef3

Please sign in to comment.