Skip to: Site menu | Main content

A Grails-like Rich Internet Framework

Xmlrpc Plugin Print

Description

The Xmlrpc plugin adds a remoting client capable of communicating via XML-RPC. It is compatible with Grails' Xmlrpc plugin 0.1.

Installation

The current version of griffon-xmlrpc-plugin is 0.1

To install just issue the following command

griffon install-plugin xmlrpc

Usage

The plugin will inject the following dynamic methods:

  • withXmlrpc(Map params, Closure stmts) - executes stmts issuing XML-RPC calls to a remote server.

Where params may contain

Property Type Required Notes
url String XML-RPC server endpoint

Examples

This example relies on Grails as the service provider. Follow these steps to configure the service on the Grails side:

  1. Download a copy of Grails and install it.
  2. Create a new Grails application. We'll pick 'exporter' as the application name.
    grails create-app exporter
    
  3. Change into the application's directory. Install the xfire plugin.
    grails install-plugin xmlrpc
    
  4. Create a MathService
    grails create-service MathService
    
    grails-app/services/MathService.groovy
    class MathService extends Xmlrpc {
        boolean transactional = false
    
        def add(params){
            println "add(${params[0]}, ${params[1]})" // good old println() for quick debugging
            return params[0] + params[1]
        }
    }
    
  5. Create a controller that exposes the service
    grails create-controller math
    
    grails-app/controllers/MathController.groovy
    class MathController {
       MathService mathService
    
       def xmlrpc = {
          mathService.service(request, response)
       }
    }
    
  6. Start the application
    grails run-app
    

Now we're ready to build the Griffon application

  1. Create a new Griffon application. We'll pick MathClient as the name
    griffon create-app mathClient
    
  2. Install the xmlrpc plugin
    griffon install-plugin xmlrpc
    
  3. Fix the view script to look like this
    griffon-app/views/MathClientView.groovy
    application(title: 'Xmlrpc Plugin Example',
      pack: true,
      locationByPlatform: true,
      iconImage: imageIcon('/griffon-icon-48x48.png').image,
      iconImages: [imageIcon('/griffon-icon-48x48.png').image,
                   imageIcon('/griffon-icon-32x32.png').image,
                   imageIcon('/griffon-icon-16x16.png').image]) {
        gridLayout(cols: 2, rows: 4)
        label("Num1:")
        textField(columns: 20, text: bind(target: model, targetProperty: "num1"))
        label("Num2:")
        textField(columns: 20, text: bind(target: model, targetProperty: "num2"))
        label("Result:")
        label(text: bind{model.result})
        button("Calculate", enabled: bind{model.enabled}, actionPerformed: controller.calculate)
    }
    
  4. Let's add required properties to the model
    griffon-app/models/MathClientModel.groovy
    import groovy.beans.Bindable
    
    class MathClientModel {
       @Bindable String num1
       @Bindable String num2
       @Bindable String result
       @Bindable boolean enabled = true
    }
    
  5. Now for the controller code. Notice that there is minimal error handling in place. If the user types something that is not a number the client will surely break, but the code is sufficient for now.
    griffon-app/controllers/MathClientController.groovy
    class MathClientController {
        def model
    
        def calculate = { evt = null ->
            double a = model.num1.toDouble()
            double b = model.num2.toDouble()
            model.enabled = false
            doOutside {
                try {
                    def result = withXmlrpc(url: "http://localhost:8080/exporter/math/xmlrpc") {
                         add(a, b)
                    }
                    doLater { model.result = result.toString() }
                } finally {
                    model.enabled = true
                }
            }
        }
    }
    
  6. Start the application
    griffon run-app
    

All dynamic methods will create a new client when invoked unless you define an id: attribute. When this attribute is supplied the client will be stored as a property on the instance's metaClass. You will be able to access it via regular property access or using the id: again.

Configuration

Dynamic method injection

Dynamic methods will be added to controllers by default. You can change this setting by adding a configuration flag in Application.groovy

griffon.xmlrpc.injectInto = ["controller", "service"]

History

Version Date Notes
0.1 10-26-09 Initial release