Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit dc8d3d6021f1b0c12feaf04322aa974228807412 0 parents
authored December 05, 2011
4  .gitignore
... ...
@@ -0,0 +1,4 @@
  1
+.classpath
  2
+.project
  3
+.settings
  4
+target
14  LICENSE
... ...
@@ -0,0 +1,14 @@
  1
+Copyright 2009 Peter Ledbrook
  2
+
  3
+Licensed under the Apache License, Version 2.0 (the "License");
  4
+you may not use this file except in compliance with the License.
  5
+You may obtain a copy of the License at
  6
+
  7
+  http://www.apache.org/licenses/LICENSE-2.0
  8
+
  9
+Unless required by applicable law or agreed to in writing, software
  10
+distributed under the License is distributed on an "AS IS" BASIS,
  11
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12
+See the License for the specific language governing permissions and
  13
+limitations under the License.
  14
+
45  MarshallersGrailsPlugin.groovy
... ...
@@ -0,0 +1,45 @@
  1
+import org.grails.plugins.marshallers.ExtendedConvertersConfigurationInitializer
  2
+import org.grails.plugins.marshallers.XmlMarshallerArtefactHandler
  3
+import org.grails.plugins.marshallers.JsonMarshallerArtefactHandler
  4
+
  5
+class MarshallersGrailsPlugin {
  6
+    // the plugin version
  7
+    def version = "0.1"
  8
+    // the version or versions of Grails the plugin is designed for
  9
+    def grailsVersion = "1.1 > *"
  10
+    
  11
+    def loadAfter = ['converters']
  12
+    
  13
+    
  14
+    // the other plugins this plugin depends on
  15
+    def dependsOn = [:]
  16
+    // resources that are excluded from plugin packaging
  17
+    def pluginExcludes = [
  18
+            "grails-app/views/error.gsp"
  19
+    ]
  20
+
  21
+    def artefacts = [ XmlMarshallerArtefactHandler, JsonMarshallerArtefactHandler ]
  22
+    
  23
+    def author = "Predrag Knezevic"
  24
+    def authorEmail = "pedjak@gmail.com"
  25
+    def title = "Easy Custom XML and JSON Marshalling for Grails Converters"
  26
+    def description = '''\\
  27
+Brief description of the plugin.
  28
+'''
  29
+
  30
+    // URL to the plugin's documentation
  31
+    def documentation = "http://grails.org/plugin/marshallers"
  32
+
  33
+    def doWithSpring = {
  34
+        // replace default convertersConfigurationInitializer from Converters plugin with ours
  35
+        convertersConfigurationInitializer(ExtendedConvertersConfigurationInitializer)
  36
+        ["xml", "json"].each { type ->
  37
+            application."${type}MarshallerClasses".each { marshallerClass ->
  38
+                "${marshallerClass.fullName}"(marshallerClass.clazz) { bean ->
  39
+                    bean.autowire = "byName"
  40
+                }
  41
+            }
  42
+        }
  43
+    }
  44
+        
  45
+}
6  application.properties
... ...
@@ -0,0 +1,6 @@
  1
+#Grails Metadata file
  2
+#Fri Dec 02 15:35:52 CET 2011
  3
+app.grails.version=1.3.7
  4
+app.name=marshallers
  5
+plugins.hibernate=1.3.7
  6
+plugins.tomcat=1.3.7
31  grails-app/conf/BuildConfig.groovy
... ...
@@ -0,0 +1,31 @@
  1
+grails.project.class.dir = "target/classes"
  2
+grails.project.test.class.dir = "target/test-classes"
  3
+grails.project.test.reports.dir = "target/test-reports"
  4
+//grails.project.war.file = "target/${appName}-${appVersion}.war"
  5
+grails.project.dependency.resolution = {
  6
+    // inherit Grails' default dependencies
  7
+    inherits("global") {
  8
+        // uncomment to disable ehcache
  9
+        // excludes 'ehcache'
  10
+    }
  11
+    log "warn" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
  12
+    repositories {
  13
+        grailsPlugins()
  14
+        grailsHome()
  15
+        grailsCentral()
  16
+
  17
+        // uncomment the below to enable remote dependency resolution
  18
+        // from public Maven repositories
  19
+        //mavenLocal()
  20
+        //mavenCentral()
  21
+        //mavenRepo "http://snapshots.repository.codehaus.org"
  22
+        //mavenRepo "http://repository.codehaus.org"
  23
+        //mavenRepo "http://download.java.net/maven/2/"
  24
+        //mavenRepo "http://repository.jboss.com/maven2/"
  25
+    }
  26
+    dependencies {
  27
+        // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
  28
+
  29
+        // runtime 'mysql:mysql-connector-java:5.1.13'
  30
+    }
  31
+}
24  grails-app/conf/Config.groovy
... ...
@@ -0,0 +1,24 @@
  1
+// configuration for plugin testing - will not be included in the plugin zip
  2
+ 
  3
+log4j = {
  4
+    // Example of changing the log pattern for the default console
  5
+    // appender:
  6
+    //
  7
+    //appenders {
  8
+    //    console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
  9
+    //}
  10
+
  11
+    error  'org.codehaus.groovy.grails.web.servlet',  //  controllers
  12
+           'org.codehaus.groovy.grails.web.pages', //  GSP
  13
+           'org.codehaus.groovy.grails.web.sitemesh', //  layouts
  14
+           'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
  15
+           'org.codehaus.groovy.grails.web.mapping', // URL mapping
  16
+           'org.codehaus.groovy.grails.commons', // core / classloading
  17
+           'org.codehaus.groovy.grails.plugins', // plugins
  18
+           'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
  19
+           'org.springframework',
  20
+           'org.hibernate',
  21
+           'net.sf.ehcache.hibernate'
  22
+
  23
+    warn   'org.mortbay.log'
  24
+}
32  grails-app/conf/DataSource.groovy
... ...
@@ -0,0 +1,32 @@
  1
+dataSource {
  2
+    pooled = true
  3
+    driverClassName = "org.hsqldb.jdbcDriver"
  4
+    username = "sa"
  5
+    password = ""
  6
+}
  7
+hibernate {
  8
+    cache.use_second_level_cache = true
  9
+    cache.use_query_cache = true
  10
+    cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
  11
+}
  12
+// environment specific settings
  13
+environments {
  14
+    development {
  15
+        dataSource {
  16
+            dbCreate = "create-drop" // one of 'create', 'create-drop','update'
  17
+            url = "jdbc:hsqldb:mem:devDB"
  18
+        }
  19
+    }
  20
+    test {
  21
+        dataSource {
  22
+            dbCreate = "update"
  23
+            url = "jdbc:hsqldb:mem:testDb"
  24
+        }
  25
+    }
  26
+    production {
  27
+        dataSource {
  28
+            dbCreate = "update"
  29
+            url = "jdbc:hsqldb:file:prodDb;shutdown=true"
  30
+        }
  31
+    }
  32
+}
13  grails-app/conf/UrlMappings.groovy
... ...
@@ -0,0 +1,13 @@
  1
+class UrlMappings {
  2
+
  3
+	static mappings = {
  4
+		"/$controller/$action?/$id?"{
  5
+			constraints {
  6
+				// apply constraints here
  7
+			}
  8
+		}
  9
+
  10
+		"/"(view:"/index")
  11
+		"500"(view:'/error')
  12
+	}
  13
+}
10  scripts/_Install.groovy
... ...
@@ -0,0 +1,10 @@
  1
+//
  2
+// This script is executed by Grails after plugin was installed to project.
  3
+// This script is a Gant script so you can use all special variables provided
  4
+// by Gant (such as 'baseDir' which points on project base dir). You can
  5
+// use 'ant' to access a global instance of AntBuilder
  6
+//
  7
+// For example you can create directory under project tree:
  8
+//
  9
+//    ant.mkdir(dir:"${basedir}/grails-app/jobs")
  10
+//
5  scripts/_Uninstall.groovy
... ...
@@ -0,0 +1,5 @@
  1
+//
  2
+// This script is executed by Grails when the plugin is uninstalled from project.
  3
+// Use this script if you intend to do any additional clean-up on uninstall, but
  4
+// beware of messing up SVN directories!
  5
+//
10  scripts/_Upgrade.groovy
... ...
@@ -0,0 +1,10 @@
  1
+//
  2
+// This script is executed by Grails during application upgrade ('grails upgrade'
  3
+// command). This script is a Gant script so you can use all special variables
  4
+// provided by Gant (such as 'baseDir' which points on project base dir). You can
  5
+// use 'ant' to access a global instance of AntBuilder
  6
+//
  7
+// For example you can create directory under project tree:
  8
+//
  9
+//    ant.mkdir(dir:"${basedir}/grails-app/jobs")
  10
+//
120  src/groovy/org/grails/plugins/marshallers/ConfigurationBuilder.groovy
... ...
@@ -0,0 +1,120 @@
  1
+
  2
+
  3
+package org.grails.plugins.marshallers
  4
+
  5
+
  6
+import org.codehaus.groovy.grails.web.converters.configuration.ConvertersConfigurationHolder;
  7
+import org.codehaus.groovy.grails.web.converters.configuration.DefaultConverterConfiguration;
  8
+import org.codehaus.groovy.grails.web.converters.marshaller.NameAwareMarshaller;
  9
+import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller;
  10
+
  11
+import groovy.lang.Closure;
  12
+
  13
+import org.codehaus.groovy.grails.web.converters.Converter;
  14
+
  15
+import org.codehaus.groovy.grails.web.converters.marshaller.ClosureOjectMarshaller;
  16
+
  17
+import groovy.lang.Closure;
  18
+import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
  19
+
  20
+/**
  21
+ * @author Predrag Knezevic
  22
+ * @version $Date: $
  23
+ */
  24
+class ConfigurationBuilder {
  25
+    private static String CONFIGURATION_FIELD = "configuration"
  26
+    
  27
+    def log
  28
+    def applicationContext
  29
+    def cfg
  30
+    def converterClass
  31
+    def cfgName
  32
+    def type
  33
+    
  34
+    def ensureCfg() {
  35
+        if (! (cfg instanceof DefaultConverterConfiguration)) {
  36
+            cfg = new DefaultConverterConfiguration(cfg)
  37
+            ConvertersConfigurationHolder.setDefaultConfiguration(converterClass, cfg)
  38
+        }
  39
+    }
  40
+    def registerSpringMarshallers() {
  41
+        // find all marshallers with the given configuration
  42
+        applicationContext.grailsApplication."${type}MarshallerClasses"*.clazz.findAll { clazz ->
  43
+            def cfg = GCU.getStaticPropertyValue(clazz, CONFIGURATION_FIELD)
  44
+            def found = cfg == null && cfgName == "default" 
  45
+            if (!found) {
  46
+                found = cfg instanceof Collection ? cfg.contains(cfgName) : (cfg == cfgName)
  47
+            }
  48
+            found
  49
+        }.each { register(it) }
  50
+    }
  51
+    
  52
+    def register(Object marshaller) {
  53
+        ensureCfg()
  54
+        if (!cfg.orderedObjectMarshallers.contains(marshaller)) {
  55
+            cfg.registerObjectMarshaller(marshaller)
  56
+            log.debug "Registered marshaller ${marshaller.class.name}"
  57
+        }
  58
+    }
  59
+    def register(Class clazz) {
  60
+        def marshaller = applicationContext.containsBean(clazz.name) ? applicationContext.getBean(clazz.name) : clazz.newInstance()
  61
+        register(marshaller)
  62
+    }
  63
+    
  64
+    def register(String marshallerName) {
  65
+        def marshaller = applicationContext.containsBean(marshallerName) ? applicationContext.getBean(marshallerName) : null
  66
+        if (marshaller == null) {
  67
+            log.warn("Cannot find and register marshaller with the name ${marshallerName}")
  68
+        } else {
  69
+            register(marshaller)
  70
+        }
  71
+    }
  72
+    
  73
+    def register(Class clazz, Closure cl) {
  74
+        ensureCfg()
  75
+        cfg.registerObjectMarshaller(clazz, cl)
  76
+        log.debug "Registered closure marshaller for ${clazz.name}" 
  77
+    }
  78
+    
  79
+    def register(Class clazz, String elementName, Closure cl) {
  80
+        ensureCfg()
  81
+        cfg.registerObjectMarshaller(new NameAwareClosureObjectMarshaller(clazz, elementName, cl))
  82
+        log.debug "Registered closure marshaller for ${clazz.name}"
  83
+    }
  84
+    
  85
+    def invokeMethod(String name, args) {
  86
+        if (args.size() != 1) {
  87
+            log.error "the number of params can be only one for ${name}"
  88
+        } else if (! args[0] instanceof Closure) {
  89
+            log.error "${name} accepts only closure as param"
  90
+        } else {
  91
+            log.info "create config with name ${name} under ${cfgName}"
  92
+            def newCfg = ConvertersConfigurationHolder.getNamedConverterConfiguration(name, converterClass)
  93
+            if (newCfg == null) {
  94
+                newCfg = new DefaultConverterConfiguration(cfg)
  95
+                ConvertersConfigurationHolder.setNamedConverterConfiguration(converterClass, name, newCfg)                
  96
+            }
  97
+            def builder = new ConfigurationBuilder(type: type, applicationContext: applicationContext, cfg: newCfg, converterClass: converterClass, log: log, cfgName: name)
  98
+            builder.registerSpringMarshallers()
  99
+            def closure = args[0]
  100
+            closure.delegate = builder
  101
+            closure.resolveStrategy = Closure.DELEGATE_FIRST;
  102
+            closure.call()
  103
+        }
  104
+    }
  105
+}
  106
+
  107
+class NameAwareClosureObjectMarshaller<T extends Converter> extends ClosureOjectMarshaller<T> implements NameAwareMarshaller {
  108
+    
  109
+    def elementName
  110
+    
  111
+    def NameAwareClosureObjectMarshaller(Class clazz, String elementName, Closure closure) {
  112
+        super(clazz, closure)
  113
+        this.elementName = elementName        
  114
+    }
  115
+    
  116
+    String getElementName(Object o) {
  117
+        elementName
  118
+    }
  119
+    
  120
+}
42  src/groovy/org/grails/plugins/marshallers/ExtendedConvertersConfigurationInitializer.groovy
... ...
@@ -0,0 +1,42 @@
  1
+
  2
+package org.grails.plugins.marshallers
  3
+
  4
+import grails.converters.JSON;
  5
+import grails.converters.XML;
  6
+import grails.util.GrailsConfig;
  7
+
  8
+import org.codehaus.groovy.grails.web.converters.configuration.ConvertersConfigurationHolder;
  9
+import org.codehaus.groovy.grails.web.converters.configuration.ConvertersConfigurationInitializer;
  10
+import org.codehaus.groovy.grails.web.converters.configuration.DefaultConverterConfiguration;
  11
+
  12
+/**
  13
+ * @author Predrag Knezevic
  14
+ * @version $Date: $
  15
+ */
  16
+class ExtendedConvertersConfigurationInitializer extends ConvertersConfigurationInitializer {
  17
+
  18
+    @Override
  19
+    public void initialize() {
  20
+        super.initialize()
  21
+        processGrailsConfigurations()
  22
+    }
  23
+
  24
+    protected def processGrailsConfigurations() {
  25
+        [xml: XML, json: JSON].each { type, converterClass ->
  26
+            def marshallerCfg = GrailsConfig.get("grails.plugins.marshallers.${type}")
  27
+            if (marshallerCfg != null) {
  28
+                processConfig(marshallerCfg, converterClass, type)
  29
+            }
  30
+        }
  31
+    }
  32
+    
  33
+    public void processConfig(Closure cfg, Class converterClass, type) {
  34
+        def converterCfg = ConvertersConfigurationHolder.getConverterConfiguration(converterClass)
  35
+        def builder = new ConfigurationBuilder(type: type, applicationContext: applicationContext, cfg: converterCfg, log: LOG, converterClass: converterClass, cfgName: "default")
  36
+        builder.registerSpringMarshallers()
  37
+        cfg.delegate = builder
  38
+        cfg.resolveStrategy = Closure.DELEGATE_FIRST;
  39
+        cfg.call()
  40
+    }
  41
+}
  42
+
16  src/java/grails/plugins/marshallers/DefaultJsonMarshallerGrailsClass.java
... ...
@@ -0,0 +1,16 @@
  1
+package org.grails.plugins.marshallers;
  2
+
  3
+import org.codehaus.groovy.grails.commons.AbstractInjectableGrailsClass;
  4
+
  5
+/**
  6
+ * @author Predrag Knezevic
  7
+ * @version $Date: $
  8
+ */
  9
+public class DefaultJsonMarshallerGrailsClass extends AbstractInjectableGrailsClass
  10
+        implements JsonMarshallerGrailsClass {
  11
+
  12
+    public DefaultJsonMarshallerGrailsClass(Class<?> clazz) {
  13
+        super(clazz, JsonMarshallerArtefactHandler.TYPE);
  14
+    }
  15
+
  16
+}
16  src/java/grails/plugins/marshallers/DefaultXmlMarshallerGrailsClass.java
... ...
@@ -0,0 +1,16 @@
  1
+package org.grails.plugins.marshallers;
  2
+
  3
+import org.codehaus.groovy.grails.commons.AbstractInjectableGrailsClass;
  4
+
  5
+/**
  6
+ * @author Predrag Knezevic
  7
+ * @version $Date: $
  8
+ */
  9
+public class DefaultXmlMarshallerGrailsClass extends AbstractInjectableGrailsClass
  10
+        implements XmlMarshallerGrailsClass {
  11
+
  12
+    public DefaultXmlMarshallerGrailsClass(Class<?> clazz) {
  13
+        super(clazz, XmlMarshallerArtefactHandler.TYPE);
  14
+    }
  15
+
  16
+}
16  src/java/grails/plugins/marshallers/JsonMarshallerArtefactHandler.java
... ...
@@ -0,0 +1,16 @@
  1
+package org.grails.plugins.marshallers;
  2
+
  3
+import org.codehaus.groovy.grails.commons.ArtefactHandlerAdapter;
  4
+
  5
+/**
  6
+ * @author Predrag Knezevic
  7
+ */
  8
+public class JsonMarshallerArtefactHandler extends ArtefactHandlerAdapter {
  9
+
  10
+    public static final String TYPE = "JsonMarshaller";
  11
+
  12
+    public JsonMarshallerArtefactHandler() {
  13
+        super(TYPE, JsonMarshallerGrailsClass.class, DefaultJsonMarshallerGrailsClass.class, TYPE);
  14
+    }
  15
+    
  16
+}
12  src/java/grails/plugins/marshallers/JsonMarshallerGrailsClass.java
... ...
@@ -0,0 +1,12 @@
  1
+package org.grails.plugins.marshallers;
  2
+
  3
+import org.codehaus.groovy.grails.commons.InjectableGrailsClass;
  4
+import org.springframework.beans.BeanWrapper;
  5
+
  6
+/**
  7
+ * @author Predrag Knezevic
  8
+ * @version $Date: $
  9
+ */
  10
+public interface JsonMarshallerGrailsClass extends InjectableGrailsClass {
  11
+
  12
+}
17  src/java/grails/plugins/marshallers/XmlMarshallerArtefactHandler.java
... ...
@@ -0,0 +1,17 @@
  1
+package org.grails.plugins.marshallers;
  2
+
  3
+import org.codehaus.groovy.grails.commons.ArtefactHandlerAdapter;
  4
+
  5
+/**
  6
+ * @author Predrag Knezevic
  7
+ */
  8
+public class XmlMarshallerArtefactHandler extends ArtefactHandlerAdapter {
  9
+
  10
+    public static final String TYPE = "XmlMarshaller";
  11
+
  12
+    public XmlMarshallerArtefactHandler() {
  13
+        super(TYPE, XmlMarshallerGrailsClass.class, DefaultXmlMarshallerGrailsClass.class, TYPE);
  14
+    }
  15
+
  16
+    
  17
+}
12  src/java/grails/plugins/marshallers/XmlMarshallerGrailsClass.java
... ...
@@ -0,0 +1,12 @@
  1
+package org.grails.plugins.marshallers;
  2
+
  3
+import org.codehaus.groovy.grails.commons.InjectableGrailsClass;
  4
+import org.springframework.beans.BeanWrapper;
  5
+
  6
+/**
  7
+ * @author Predrag Knezevic
  8
+ * @version $Date: $
  9
+ */
  10
+public interface XmlMarshallerGrailsClass extends InjectableGrailsClass {
  11
+
  12
+}

0 notes on commit dc8d3d6

Please sign in to comment.
Something went wrong with that request. Please try again.