Currently Being Moderated

Events in Clearspace

Posted by Dawn Foster on Aug 7, 2007 2:27:00 PM

Learn about event handling within Clearspace from Bruce Ritchie.

 

 

Or download the Quicktime movie



Aug 10, 2007 9:47 PM timcolson timcolson  

Hey Bruce -- I've searched around a bit, but have not found documentation on how to register an object to receive events that are dispatched. Is there a comprehensive list of the events that are available?

 

Can you point to a simple example and/or docs?

 

Thanks,

Timo

Aug 13, 2007 10:15 AM Bruce Ritchie Bruce Ritchie  

Timo,

 

Try the following:

 


ThreadListener listener = new YourListener();
ThreadEventDispatcher.getInstance().addListener(listener);

 

If you want a listener registration that persists across restarts you can add a jive property such as "eventListeners.ThreadListener.YourListener" -> "com.acme.your.Listener"

 

Javadoc on the event classes can be found here.

 

Regards,

 

Bruce Ritchie

Oct 14, 2007 3:27 PM timcolson timcolson  

Hey Bruce -- it's been a while, but thanks for the info!

 

When you say, "add a jive property", do you mean in the jiveHome/jive_startup.xml file? So the process would be to write my ThreadListener class, bundle it into a plugin jar, install the plugin, add a hook to it in the jive_start.xml file, and then restart clearspace to pick up the registration?

 

And the XML would literally be this?

<jive>

... other stuff...

<eventListeners>

<ThreadListener>

   <TimListener>com.domain.clearspace.TimListener</TimListener>

</ThreadListener>

</eventListeners>

</jive>

 

 

Thanks,

Tim

Oct 14, 2007 3:32 PM timcolson timcolson  

Sorry, hit enter before digging around the API ref... found an example in the API doc for ThreadListener:

http://www.jivesoftware.com/builds/docs/clearspace/1.3.0/javadoc/api/com/jivesoftware/community/event/ThreadEventDispatcher.html

 

 


For example, to register the com.acme.CustomThreadListener class as a persistent listener the following code could be used: 

 

// add persistent key to have the listener loaded and registered automatically at startup JiveGlobals.setJiveProperty("eventListeners.ThreadListener.customThreadListener", "com.acme.CustomThreadListener.class");

 

// now add it for this runtime instance ThreadEventDispatcher.addListener(new com.acme.CustomerThreadListener());


 

So that seems to suggest that the plugin can add properties via JiveGlobals, i.e. no need to edit XML manually?

 

I'll search around, but I'm assuming there is probably an method called on the plugin during "installation" and that's where the JiveGlobals should be set?

 

-Tim

Oct 14, 2007 3:57 PM Bruce Ritchie Bruce Ritchie  

Tim,

 

Yes, use the JiveGlobals.setJiveProperty() method to set the property. When you set something in the jive_startup.xml it's only valid for that node (if you're running in a cluster) and is only accessible via the api with the JiveGlobals.get/setLocalProperty() methods.

 

Bruce

Oct 14, 2007 11:43 PM timcolson timcolson  

Thanks again, Bruce.

 

It looks like the method initializePlugin(PluginManager manager, java.io.File pluginDirectory) will be called on once server-startup...or for 1.6 this seems to have changed from the online 1.3 javadoc to  initializePlugin(PluginManager pluginManager, PluginMetaData pluginMetaData). Is that correct?

 

I setup a plugin.xml that points to my Plugin and added the following code to initialize (?) my eventListener. I see in catalina.out the "init", but nothing when I add/view a document given the code below. I don't see any other errors... so I'm not sure what I'm missing. Any ideas?

 

Thanks,

Tim

 

 

public class TimPlugin implements Plugin {

    public void initializePlugin(PluginManager pluginManager, PluginMetaData pluginMetaData) {

        System.out.println("Init TimPlugin!");

         // Setup my listener to do fun stuff when events happen! JiveGlobals.setJiveProperty("eventListeners.DocumentListener.TimEventListener", "com.domain.clearspace.TimDocumentListener.class");

 

    }

 

TimDocumentListener.java


package com.domain.clearspace;

 

import com.jivesoftware.community.event.DocumentEvent;

import com.jivesoftware.community.event.DocumentListener;

 

import static java.text.MessageFormat.format;

public class TimDocumentListener implements DocumentListener {

 

    public TimDocumentListener() { }

 

    public void documentAdded(DocumentEvent event) {

            System.out.println(format("TimEvent: documentAdded= {0}", event.getDocument().getPlainSubject()));

 

        }

        public void documentViewed(DocumentEvent event) {

            System.out.println(format("TimEvent: documentViewed= {0}", event.getDocument().getPlainSubject()));

 

        }

 

    public void documentDeleted(DocumentEvent event) {

    }

 

    public void documentMoved(DocumentEvent event) {

    }

 

    public void documentModified(DocumentEvent event) {

    }

 

    public void documentExpireWarning(DocumentEvent event) {

    }

 

    public void documentExpired(DocumentEvent event) {

    }

 

    public void documentRelatedObjectAdded(DocumentEvent event) {

    }

 

    public void documentRelatedObjectDeleted(DocumentEvent event) {

    }

 

    public void documentRated(DocumentEvent event) {

    }

 

    public void versionAdded(DocumentEvent event) {

    }

 

    public void versionModified(DocumentEvent event) {

    }

 

    public void versionDeleting(DocumentEvent event) {

    }

 

    public void binaryBodyDownloaded(DocumentEvent event) {

    }

 

 

}

Oct 15, 2007 12:05 AM timcolson timcolson  

Aha...figured it out, but still have some questions.

 

Based on the javadoc for DocumentEventDispatcher, it says to both register the listener via JiveGlobals, and also to add it to the runtime instance.

 

The example is this:

// now add it for this runtime instance

DocumentEventDispatcher.addListener(new com.acme.CustomerDocumentListener());

 

That does not work because addListener is not static, so I simply got an instance first.

// Setup listener

DocumentEventDispatcher ded = DocumentEventDispatcher.getInstance();

ded.addListener(new TimDocumentListener());

 

This works!

 

However, I'm confused... if initializePlugin is called when the server starts up, then why bother with JiveGlobals if you have to attach the listener manually as well? Maybe I just don't fully understand when initializePlugin is called during server lifecycle.

 

Could you explain, please?

Thanks!

-Tim

Oct 15, 2007 1:08 AM timcolson timcolson  

And how do you get the current user from an event (ex. DocumentEvent) handed to the listener class?

 

-T