[prev in list] [next in list] [prev in thread] [next in thread] 

List:       groovy-user
Subject:    [groovy-user] Groovy DSL and evaluation
From:       "daniel.mfreitas" <daniel.mfreitas () gmail ! com>
Date:       2009-10-31 16:53:26
Message-ID: 26144047.post () talk ! nabble ! com
[Download RAW message or body]


Hi folks,

I'm trying to create a DSL for managing a WebSphere clustered environment
programatically. I want my DSL to look something like this <operation>
<objectType>, [criteria], [commands]:

select Cell, [name : 'localhostCell01'], {
    //Commands are then executed against the target cell. The keystore will
be created at the cell level.
    create Keystore, [
        name : "sample"
        ...
    ]

    // This will select all servers named server1, and for each one of them,
create a JVM property
    select Server, [name : 'server1'], {
        select JavaVirtualMachine, {
            create Property, [
            ...
            ]
    }
}

The idea is that every command is executed against a context. We begin with
the root context that defines the select method:

def select(Class<?> clazz, Map<String, ?> criteria, Closure commands) {
        String objectType = clazz.name
	//Then check what type of object we want to select and query the
application server for it.
        //Once we have a reference to this object, create a new context for
it and make it current
}

To handle commands against the context, I am using the following in my
ScriptExecutor:

def executeScript(String script, variables) {
	def binding = new Binding(variables)

	def rootContext = new RootContext()
	binding.setVariable "context", rootContext
	new GroovyShell(binding).evaluate("context.with {$script}")
}

I use the 'with' keyword to create the context for the script so methods not
defined in the script falls through to the context object (which defines
select, create etc).

This works fine as of now for the root context. But once I have a new
context how do I programatically change it so that the methods are routed to
the new context? For example:

//This will invoke RootContext.select() method
select Cell, [name : 'name'], {
    //This should invoke CellContext.select() method
    select Node, [name : name], {
        //This should invoke NodeContext.create() method
        create Keystore, [...]
    }
}

Another problem arises if I want the user to be able to pass a script file
as a parameter instead of a String:

def executeScript(URL url, variables) {
	def script = new GroovyCodeSource(url)
	def binding = new Binding(variables)
         //Groovy will complain that the select method does not exist on
Scprit1, since I could not define
         //RootContext as the context for the with keyword
	new GroovyShell(binding).evaluate(script)
}

Any ideas?
-- 
View this message in context: http://old.nabble.com/Groovy-DSL-and-evaluation-tp26144047p26144047.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic