On 07/07/2012 10:03 AM, seth redmond wrote:
I seem to be running into an error I can't find my way around when passing the 
xwiki context to an eventListener. According to the error message itself 
(below) I appear to be passing the right objects, but I'm still getting an 
exception thrown.

I should point out that I'm new to both xwiki and groovy, so I may have missed 
something obvious, is the context not instantiated by the time it's been sent  
to the event handler? or should I be grabbing it anew?

thanks

-s


Error and groovy code below...

2012-07-05 21:19:59,471 
[http://localhost:8080/xwiki/bin/preview/Task/look+into+CMH+%2F+TDT] ERROR 
.o.i.DefaultObservationManager - Failed to send event 
[org.xwiki.observation.event.DocumentUpdateEvent@6b193509] to listener 
[MailingEventListener@171aa1cb]
groovy.lang.MissingMethodException: No signature of method: static 
com.xpn.xwiki.plugin.mailsender.MailSenderPlugin.sendMail() is applicable for 
argument types: (com.xpn.xwiki.plugin.mailsender.Mail, 
com.xpn.xwiki.XWikiContext) values: [From [[email protected]], To [], 
Subject [xwiki done some stuff, yo], Text [document TaskClass has been 
changed], ...]
Possible solutions: sendMail(com.xpn.xwiki.plugin.mailsender.Mail, 
com.xpn.xwiki.XWikiContext), sendMail(com.xpn.xwiki.plugin.mailsender.Mail, 
com.xpn.xwiki.plugin.mailsender.MailConfiguration, com.xpn.xwiki.XWikiContext), 
sendMails(java.util.Collection, com.xpn.xwiki.XWikiContext), 
sendMails(java.util.Collection, 
com.xpn.xwiki.plugin.mailsender.MailConfiguration, com.xpn.xwiki.XWikiContext), 
findAll()


{{groovy}}

import org.xwiki.observation.*
import org.xwiki.observation.event.*
import com.xpn.xwiki.plugin.mailsender.*
import com.xpn.xwiki.web.*
import com.xpn.xwiki.*

class MailingEventListener implements EventListener
{
     def xwiki
     def xwcontext
     def context
     def xdoc

     MailingEventListener(xwiki, context)
     {

In general you should not cache the xwiki and context objects that you received here, since they're probably going to be wrong at the time when events occur. Always get a fresh reference to the context in onEvent, from the Execution instance. I know that you got this code from the notification tutorial, but that one is a bit deprecated.

         this.xwiki = xwiki
         this.context = context
         this.xwcontext = context.getContext()
         this.xdoc = context.doc
     }

     String getName()
     {
         // The unique name of this event listener
         return "mailing"
     }


     List<Event> getEvents()
     {
         // The list of events this listener listens to
         return Arrays.asList(new DocumentUpdateEvent())

This event is deprecated, you should use org.xwiki.bridge.event.DocumentUpdatedEvent instead

     }

     // Called by the Observation Manager when an event matches the list of 
events returned
     // by getEvents()
     void onEvent(Event event, Object source, Object data)
     {
         def mailfrom = '[email protected]'
         def mailto = xdoc.getStringValue('assignee')
         def subject = 'xwiki done some stuff, yo'
         def message = 'document '+xdoc.name+' has been changed'
         def mailItem = new Mail(mailfrom, mailto, null, null, subject, 
message, null)
         MailSenderPlugin.sendMail(mailItem, xwcontext);

The problem is that you're trying to call a static method, but sendMail is an instance method. You should get the right instance of the plugin using:
this.xwiki.getXWiki().getPlugin("mailsender", xwcontext)

     }
}

// Register against the Observation Manager
def observation = Utils.getComponent(ObservationManager.class)
observation.removeListener("mailing")
def listener = new MailingEventListener(xwiki, xcontext)
observation.addListener(listener)

println "{{info}}Listener $listener.name is now registered on 
$listener.events{{/info}}"
println "{{info}}Context = $listener.context{{/info}}"
println "{{info}}$listener.context.doc.name{{/info}}"

{{/groovy}}


--
Sergiu Dumitriu
http://purl.org/net/sergiu/


_______________________________________________
devs mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/devs

Reply via email to